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   */
36  public final class SvnScmTestUtils {
37      /** 'svn' command line */
38      public static final String SVN_COMMAND_LINE = "svn";
39  
40      /** 'svnadmin' command line */
41      public static final String SVNADMIN_COMMAND_LINE = "svnadmin";
42  
43      private SvnScmTestUtils() {}
44  
45      public static void initializeRepository(File repositoryRoot) throws Exception {
46          if (repositoryRoot.exists()) {
47              FileUtils.deleteDirectory(repositoryRoot);
48          }
49  
50          Assert.assertFalse("repositoryRoot still exists", repositoryRoot.exists());
51  
52          Assert.assertTrue(
53                  "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
54                  repositoryRoot.mkdirs());
55  
56          ScmTestCase.execute(
57                  repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE, "create " + repositoryRoot.getName());
58  
59          loadSvnDump(
60                  repositoryRoot,
61                  new SvnScmTestUtils().getClass().getClassLoader().getResourceAsStream("tck/tck.dump"));
62      }
63  
64      public static void initializeRepository(File repositoryRoot, File dump) throws Exception {
65          if (repositoryRoot.exists()) {
66              FileUtils.deleteDirectory(repositoryRoot);
67          }
68  
69          Assert.assertTrue(
70                  "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
71                  repositoryRoot.mkdirs());
72  
73          ScmTestCase.execute(
74                  repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE, "create " + repositoryRoot.getName());
75  
76          Assert.assertTrue("The dump file doesn't exist: " + dump.getAbsolutePath(), dump.exists());
77  
78          loadSvnDump(repositoryRoot, new FileInputStream(dump));
79      }
80  
81      private static void loadSvnDump(File repositoryRoot, InputStream dumpStream) throws Exception {
82          Commandline cl = new Commandline();
83  
84          cl.setExecutable(SVNADMIN_COMMAND_LINE);
85  
86          cl.setWorkingDirectory(repositoryRoot.getParentFile().getAbsolutePath());
87  
88          cl.createArg().setValue("load");
89  
90          cl.createArg().setValue(repositoryRoot.getAbsolutePath());
91  
92          CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
93  
94          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
95  
96          int exitValue = CommandLineUtils.executeCommandLine(cl, dumpStream, stdout, stderr);
97  
98          if (exitValue != 0) {
99              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 }