View Javadoc

1   package org.apache.maven.scm.provider.svn;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.Assert;
23  import org.apache.maven.scm.ScmTestCase;
24  import org.codehaus.plexus.util.FileUtils;
25  import org.codehaus.plexus.util.Os;
26  import org.codehaus.plexus.util.StringUtils;
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  
31  import java.io.File;
32  import java.io.FileInputStream;
33  import java.io.InputStream;
34  
35  /**
36   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
37   *
38   */
39  public final class SvnScmTestUtils
40  {
41      /** 'svn' command line */
42      public static final String SVN_COMMAND_LINE = "svn";
43  
44      /** 'svnadmin' command line */
45      public static final String SVNADMIN_COMMAND_LINE = "svnadmin";
46  
47      private SvnScmTestUtils()
48      {
49      }
50  
51      public static void initializeRepository( File repositoryRoot )
52          throws Exception
53      {
54          if ( repositoryRoot.exists() )
55          {
56              FileUtils.deleteDirectory( repositoryRoot );
57          }
58          
59          Assert.assertFalse( "repositoryRoot still exists", repositoryRoot.exists() );
60  
61          Assert.assertTrue( "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
62                             repositoryRoot.mkdirs() );
63  
64          ScmTestCase.execute( repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE, "create " + repositoryRoot.getName() );
65  
66          loadSvnDump( repositoryRoot,
67                       new SvnScmTestUtils().getClass().getClassLoader().getResourceAsStream( "tck/tck.dump" ) );
68      }
69  
70      public static void initializeRepository( File repositoryRoot, File dump )
71          throws Exception
72      {
73          if ( repositoryRoot.exists() )
74          {
75              FileUtils.deleteDirectory( repositoryRoot );
76          }
77  
78          Assert.assertTrue( "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
79                             repositoryRoot.mkdirs() );
80  
81          ScmTestCase.execute( repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE, "create " + repositoryRoot.getName() );
82  
83          Assert.assertTrue( "The dump file doesn't exist: " + dump.getAbsolutePath(), dump.exists() );
84  
85          loadSvnDump( repositoryRoot, new FileInputStream( dump ) );
86      }
87  
88      private static void loadSvnDump( File repositoryRoot, InputStream dumpStream )
89          throws Exception
90      {
91          Commandline cl = new Commandline();
92  
93          cl.setExecutable( SVNADMIN_COMMAND_LINE );
94  
95          cl.setWorkingDirectory( repositoryRoot.getParentFile().getAbsolutePath() );
96  
97          cl.createArg().setValue( "load" );
98  
99          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         String repositoryRoot = repositoryRootFile.getAbsolutePath();
134 
135         // TODO: it'd be great to build this into CommandLineUtils somehow
136         // TODO: some way without a custom cygwin sys property?
137         if ( "true".equals( System.getProperty( "cygwin" ) ) )
138         {
139             Commandline cl = new Commandline();
140 
141             cl.setExecutable( "cygpath" );
142 
143             cl.createArg().setValue( "--unix" );
144 
145             cl.createArg().setValue( repositoryRoot );
146 
147             CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
148 
149             int exitValue = CommandLineUtils.executeCommandLine( cl, stdout, null );
150 
151             if ( exitValue != 0 )
152             {
153                 throw new CommandLineException( "Unable to convert cygwin path, exit code = " + exitValue );
154             }
155 
156             repositoryRoot = stdout.getOutput().trim();
157         }
158         else if ( Os.isFamily( "windows" ) )
159         {
160             repositoryRoot = "/" + StringUtils.replace( repositoryRoot, "\\", "/" );
161         }
162 
163         return "scm:svn:file://" + repositoryRoot;
164     }
165 }