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 *
035 */
036public final class SvnScmTestUtils {
037    /** 'svn' command line */
038    public static final String SVN_COMMAND_LINE = "svn";
039
040    /** 'svnadmin' command line */
041    public static final String SVNADMIN_COMMAND_LINE = "svnadmin";
042
043    private SvnScmTestUtils() {}
044
045    public static void initializeRepository(File repositoryRoot) throws Exception {
046        if (repositoryRoot.exists()) {
047            FileUtils.deleteDirectory(repositoryRoot);
048        }
049
050        Assert.assertFalse("repositoryRoot still exists", repositoryRoot.exists());
051
052        Assert.assertTrue(
053                "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
054                repositoryRoot.mkdirs());
055
056        ScmTestCase.execute(
057                repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE, "create " + repositoryRoot.getName());
058
059        loadSvnDump(
060                repositoryRoot,
061                new SvnScmTestUtils().getClass().getClassLoader().getResourceAsStream("tck/tck.dump"));
062    }
063
064    public static void initializeRepository(File repositoryRoot, File dump) throws Exception {
065        if (repositoryRoot.exists()) {
066            FileUtils.deleteDirectory(repositoryRoot);
067        }
068
069        Assert.assertTrue(
070                "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
071                repositoryRoot.mkdirs());
072
073        ScmTestCase.execute(
074                repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE, "create " + repositoryRoot.getName());
075
076        Assert.assertTrue("The dump file doesn't exist: " + dump.getAbsolutePath(), dump.exists());
077
078        loadSvnDump(repositoryRoot, new FileInputStream(dump));
079    }
080
081    private static void loadSvnDump(File repositoryRoot, InputStream dumpStream) throws Exception {
082        Commandline cl = new Commandline();
083
084        cl.setExecutable(SVNADMIN_COMMAND_LINE);
085
086        cl.setWorkingDirectory(repositoryRoot.getParentFile().getAbsolutePath());
087
088        cl.createArg().setValue("load");
089
090        cl.createArg().setValue(repositoryRoot.getAbsolutePath());
091
092        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
093
094        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
095
096        int exitValue = CommandLineUtils.executeCommandLine(cl, dumpStream, stdout, stderr);
097
098        if (exitValue != 0) {
099            System.err.println("-----------------------------------------");
100            System.err.println("Command line: " + cl);
101            System.err.println("Working directory: " + cl.getWorkingDirectory());
102            System.err.println("-----------------------------------------");
103            System.err.println("Standard output: ");
104            System.err.println("-----------------------------------------");
105            System.err.println(stdout.getOutput());
106            System.err.println("-----------------------------------------");
107
108            System.err.println("Standard error: ");
109            System.err.println("-----------------------------------------");
110            System.err.println(stderr.getOutput());
111            System.err.println("-----------------------------------------");
112        }
113
114        if (exitValue != 0) {
115            Assert.fail("Exit value wasn't 0, was:" + exitValue);
116        }
117    }
118
119    public static String getScmUrl(File repositoryRootFile) throws CommandLineException {
120        return "scm:svn:" + repositoryRootFile.toPath().toAbsolutePath().toUri().toASCIIString();
121    }
122}