001package org.apache.maven.scm.provider.svn;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import junit.framework.Assert;
023import org.apache.maven.scm.ScmTestCase;
024import org.codehaus.plexus.util.FileUtils;
025import org.codehaus.plexus.util.Os;
026import org.codehaus.plexus.util.StringUtils;
027import org.codehaus.plexus.util.cli.CommandLineException;
028import org.codehaus.plexus.util.cli.CommandLineUtils;
029import org.codehaus.plexus.util.cli.Commandline;
030
031import java.io.File;
032import java.io.FileInputStream;
033import java.io.InputStream;
034
035/**
036 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
037 *
038 */
039public final class SvnScmTestUtils
040{
041    /** 'svn' command line */
042    public static final String SVN_COMMAND_LINE = "svn";
043
044    /** 'svnadmin' command line */
045    public static final String SVNADMIN_COMMAND_LINE = "svnadmin";
046
047    private SvnScmTestUtils()
048    {
049    }
050
051    public static void initializeRepository( File repositoryRoot )
052        throws Exception
053    {
054        if ( repositoryRoot.exists() )
055        {
056            FileUtils.deleteDirectory( repositoryRoot );
057        }
058        
059        Assert.assertFalse( "repositoryRoot still exists", repositoryRoot.exists() );
060
061        Assert.assertTrue( "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
062                           repositoryRoot.mkdirs() );
063
064        ScmTestCase.execute( repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE, "create " + repositoryRoot.getName() );
065
066        loadSvnDump( repositoryRoot,
067                     new SvnScmTestUtils().getClass().getClassLoader().getResourceAsStream( "tck/tck.dump" ) );
068    }
069
070    public static void initializeRepository( File repositoryRoot, File dump )
071        throws Exception
072    {
073        if ( repositoryRoot.exists() )
074        {
075            FileUtils.deleteDirectory( repositoryRoot );
076        }
077
078        Assert.assertTrue( "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
079                           repositoryRoot.mkdirs() );
080
081        ScmTestCase.execute( repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE, "create " + repositoryRoot.getName() );
082
083        Assert.assertTrue( "The dump file doesn't exist: " + dump.getAbsolutePath(), dump.exists() );
084
085        loadSvnDump( repositoryRoot, new FileInputStream( dump ) );
086    }
087
088    private static void loadSvnDump( File repositoryRoot, InputStream dumpStream )
089        throws Exception
090    {
091        Commandline cl = new Commandline();
092
093        cl.setExecutable( SVNADMIN_COMMAND_LINE );
094
095        cl.setWorkingDirectory( repositoryRoot.getParentFile().getAbsolutePath() );
096
097        cl.createArg().setValue( "load" );
098
099        cl.createArg().setValue( repositoryRoot.getAbsolutePath() );
100
101        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
102
103        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
104
105        int exitValue = CommandLineUtils.executeCommandLine( cl, dumpStream, stdout, stderr );
106
107        if ( exitValue != 0 )
108        {
109            System.err.println( "-----------------------------------------" );
110            System.err.println( "Command line: " + cl );
111            System.err.println( "Working directory: " + cl.getWorkingDirectory() );
112            System.err.println( "-----------------------------------------" );
113            System.err.println( "Standard output: " );
114            System.err.println( "-----------------------------------------" );
115            System.err.println( stdout.getOutput() );
116            System.err.println( "-----------------------------------------" );
117
118            System.err.println( "Standard error: " );
119            System.err.println( "-----------------------------------------" );
120            System.err.println( stderr.getOutput() );
121            System.err.println( "-----------------------------------------" );
122        }
123
124        if ( exitValue != 0 )
125        {
126            Assert.fail( "Exit value wasn't 0, was:" + exitValue );
127        }
128    }
129
130    public static String getScmUrl( File repositoryRootFile )
131        throws CommandLineException
132    {
133        String repositoryRoot = repositoryRootFile.getAbsolutePath();
134
135        // TODO: it'd be great to build this into CommandLineUtils somehow
136        // TODO: some way without a custom cygwin sys property?
137        if ( "true".equals( System.getProperty( "cygwin" ) ) )
138        {
139            Commandline cl = new Commandline();
140
141            cl.setExecutable( "cygpath" );
142
143            cl.createArg().setValue( "--unix" );
144
145            cl.createArg().setValue( repositoryRoot );
146
147            CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
148
149            int exitValue = CommandLineUtils.executeCommandLine( cl, stdout, null );
150
151            if ( exitValue != 0 )
152            {
153                throw new CommandLineException( "Unable to convert cygwin path, exit code = " + exitValue );
154            }
155
156            repositoryRoot = stdout.getOutput().trim();
157        }
158        else if ( Os.isFamily( "windows" ) )
159        {
160            repositoryRoot = "/" + StringUtils.replace( repositoryRoot, "\\", "/" );
161        }
162
163        return "scm:svn:file://" + repositoryRoot;
164    }
165}