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 org.junit.Assert;
023import org.apache.maven.scm.ScmTestCase;
024import org.codehaus.plexus.util.FileUtils;
025import org.codehaus.plexus.util.cli.CommandLineException;
026import org.codehaus.plexus.util.cli.CommandLineUtils;
027import org.codehaus.plexus.util.cli.Commandline;
028
029import java.io.File;
030import java.io.FileInputStream;
031import java.io.InputStream;
032
033/**
034 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
035 *
036 */
037public final class SvnScmTestUtils
038{
039    /** 'svn' command line */
040    public static final String SVN_COMMAND_LINE = "svn";
041
042    /** 'svnadmin' command line */
043    public static final String SVNADMIN_COMMAND_LINE = "svnadmin";
044
045    private SvnScmTestUtils()
046    {
047    }
048
049    public static void initializeRepository( File repositoryRoot )
050        throws Exception
051    {
052        if ( repositoryRoot.exists() )
053        {
054            FileUtils.deleteDirectory( repositoryRoot );
055        }
056
057        Assert.assertFalse( "repositoryRoot still exists", repositoryRoot.exists() );
058
059        Assert.assertTrue( "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
060                           repositoryRoot.mkdirs() );
061
062        ScmTestCase.execute( repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE,
063                             "create " + repositoryRoot.getName() );
064
065        loadSvnDump( repositoryRoot,
066                     new SvnScmTestUtils().getClass().getClassLoader().getResourceAsStream( "tck/tck.dump" ) );
067    }
068
069    public static void initializeRepository( File repositoryRoot, File dump )
070        throws Exception
071    {
072        if ( repositoryRoot.exists() )
073        {
074            FileUtils.deleteDirectory( repositoryRoot );
075        }
076
077        Assert.assertTrue( "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
078                           repositoryRoot.mkdirs() );
079
080        ScmTestCase.execute( repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE,
081                             "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        return "scm:svn:" + repositoryRootFile.toPath().toAbsolutePath().toUri().toASCIIString();
134    }
135}