View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.svn;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.InputStream;
24  
25  import org.apache.maven.scm.ScmTestCase;
26  import org.codehaus.plexus.util.FileUtils;
27  import org.codehaus.plexus.util.cli.CommandLineException;
28  import org.codehaus.plexus.util.cli.CommandLineUtils;
29  import org.codehaus.plexus.util.cli.Commandline;
30  import org.junit.Assert;
31  
32  /**
33   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
34   */
35  public final class SvnScmTestUtils {
36      /** 'svn' command line. */
37      public static final String SVN_COMMAND_LINE = "svn";
38  
39      /** 'svnadmin' command line. */
40      public static final String SVNADMIN_COMMAND_LINE = "svnadmin";
41  
42      private SvnScmTestUtils() {}
43  
44      public static void initializeRepository(File repositoryRoot) throws Exception {
45          if (repositoryRoot.exists()) {
46              FileUtils.deleteDirectory(repositoryRoot);
47          }
48  
49          Assert.assertFalse("repositoryRoot still exists", repositoryRoot.exists());
50  
51          Assert.assertTrue(
52                  "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
53                  repositoryRoot.mkdirs());
54  
55          ScmTestCase.execute(
56                  repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE, "create " + repositoryRoot.getName());
57  
58          loadSvnDump(
59                  repositoryRoot,
60                  new SvnScmTestUtils().getClass().getClassLoader().getResourceAsStream("tck/tck.dump"));
61      }
62  
63      public static void initializeRepository(File repositoryRoot, File dump) throws Exception {
64          if (repositoryRoot.exists()) {
65              FileUtils.deleteDirectory(repositoryRoot);
66          }
67  
68          Assert.assertTrue(
69                  "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
70                  repositoryRoot.mkdirs());
71  
72          ScmTestCase.execute(
73                  repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE, "create " + repositoryRoot.getName());
74  
75          Assert.assertTrue("The dump file doesn't exist: " + dump.getAbsolutePath(), dump.exists());
76  
77          loadSvnDump(repositoryRoot, new FileInputStream(dump));
78      }
79  
80      private static void loadSvnDump(File repositoryRoot, InputStream dumpStream) throws Exception {
81          Commandline cl = new Commandline();
82  
83          cl.setExecutable(SVNADMIN_COMMAND_LINE);
84  
85          cl.setWorkingDirectory(repositoryRoot.getParentFile().getAbsolutePath());
86  
87          cl.createArg().setValue("load");
88  
89          cl.createArg().setValue(repositoryRoot.getAbsolutePath());
90  
91          CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
92  
93          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
94  
95          int exitValue = CommandLineUtils.executeCommandLine(cl, dumpStream, stdout, stderr);
96  
97          if (exitValue != 0) {
98              System.err.println("-----------------------------------------");
99              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 }