001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.svn;
020
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.InputStream;
024
025import org.apache.maven.scm.ScmTestCase;
026import org.codehaus.plexus.util.FileUtils;
027import org.codehaus.plexus.util.cli.CommandLineException;
028import org.codehaus.plexus.util.cli.CommandLineUtils;
029import org.codehaus.plexus.util.cli.Commandline;
030import org.junit.Assert;
031
032/**
033 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
034 */
035public final class SvnScmTestUtils {
036    /** 'svn' command line. */
037    public static final String SVN_COMMAND_LINE = "svn";
038
039    /** 'svnadmin' command line. */
040    public static final String SVNADMIN_COMMAND_LINE = "svnadmin";
041
042    private SvnScmTestUtils() {}
043
044    public static void initializeRepository(File repositoryRoot) throws Exception {
045        if (repositoryRoot.exists()) {
046            FileUtils.deleteDirectory(repositoryRoot);
047        }
048
049        Assert.assertFalse("repositoryRoot still exists", repositoryRoot.exists());
050
051        Assert.assertTrue(
052                "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
053                repositoryRoot.mkdirs());
054
055        ScmTestCase.execute(
056                repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE, "create " + repositoryRoot.getName());
057
058        loadSvnDump(
059                repositoryRoot,
060                new SvnScmTestUtils().getClass().getClassLoader().getResourceAsStream("tck/tck.dump"));
061    }
062
063    public static void initializeRepository(File repositoryRoot, File dump) throws Exception {
064        if (repositoryRoot.exists()) {
065            FileUtils.deleteDirectory(repositoryRoot);
066        }
067
068        Assert.assertTrue(
069                "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
070                repositoryRoot.mkdirs());
071
072        ScmTestCase.execute(
073                repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE, "create " + repositoryRoot.getName());
074
075        Assert.assertTrue("The dump file doesn't exist: " + dump.getAbsolutePath(), dump.exists());
076
077        loadSvnDump(repositoryRoot, new FileInputStream(dump));
078    }
079
080    private static void loadSvnDump(File repositoryRoot, InputStream dumpStream) throws Exception {
081        Commandline cl = new Commandline();
082
083        cl.setExecutable(SVNADMIN_COMMAND_LINE);
084
085        cl.setWorkingDirectory(repositoryRoot.getParentFile().getAbsolutePath());
086
087        cl.createArg().setValue("load");
088
089        cl.createArg().setValue(repositoryRoot.getAbsolutePath());
090
091        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
092
093        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
094
095        int exitValue = CommandLineUtils.executeCommandLine(cl, dumpStream, stdout, stderr);
096
097        if (exitValue != 0) {
098            System.err.println("-----------------------------------------");
099            System.err.println("Command line: " + cl);
100            System.err.println("Working directory: " + cl.getWorkingDirectory());
101            System.err.println("-----------------------------------------");
102            System.err.println("Standard output: ");
103            System.err.println("-----------------------------------------");
104            System.err.println(stdout.getOutput());
105            System.err.println("-----------------------------------------");
106
107            System.err.println("Standard error: ");
108            System.err.println("-----------------------------------------");
109            System.err.println(stderr.getOutput());
110            System.err.println("-----------------------------------------");
111        }
112
113        if (exitValue != 0) {
114            Assert.fail("Exit value wasn't 0, was:" + exitValue);
115        }
116    }
117
118    public static String getScmUrl(File repositoryRootFile) throws CommandLineException {
119        return "scm:svn:" + repositoryRootFile.toPath().toAbsolutePath().toUri().toASCIIString();
120    }
121}