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.StringUtils;
26  import org.codehaus.plexus.util.cli.CommandLineException;
27  import org.codehaus.plexus.util.cli.CommandLineUtils;
28  import org.codehaus.plexus.util.cli.Commandline;
29  
30  import java.io.File;
31  import java.io.FileInputStream;
32  import java.io.InputStream;
33  
34  /**
35   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
36   * @version $Id: SvnScmTestUtils.java 483105 2006-12-06 15:07:54Z evenisse $
37   */
38  public final class SvnScmTestUtils
39  {
40      private SvnScmTestUtils()
41      {
42      }
43  
44      public static void initializeRepository( File repositoryRoot )
45          throws Exception
46      {
47          if ( repositoryRoot.exists() )
48          {
49              FileUtils.deleteDirectory( repositoryRoot );
50          }
51  
52          Assert.assertTrue( "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
53                             repositoryRoot.mkdirs() );
54  
55          ScmTestCase.execute( repositoryRoot.getParentFile(), "svnadmin", "create " + repositoryRoot.getName() );
56  
57          loadSvnDump( repositoryRoot,
58                       new SvnScmTestUtils().getClass().getClassLoader().getResourceAsStream( "tck/tck.dump" ) );
59      }
60  
61      public static void initializeRepository( File repositoryRoot, File dump )
62          throws Exception
63      {
64          if ( repositoryRoot.exists() )
65          {
66              FileUtils.deleteDirectory( repositoryRoot );
67          }
68  
69          Assert.assertTrue( "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
70                             repositoryRoot.mkdirs() );
71  
72          ScmTestCase.execute( repositoryRoot.getParentFile(), "svnadmin", "create " + repositoryRoot.getName() );
73  
74          Assert.assertTrue( "The dump file doesn't exist: " + dump.getAbsolutePath(), dump.exists() );
75  
76          loadSvnDump( repositoryRoot, new FileInputStream( dump ) );
77      }
78  
79      private static void loadSvnDump( File repositoryRoot, InputStream dumpStream )
80          throws Exception
81      {
82          Commandline cl = new Commandline();
83  
84          cl.setExecutable( "svnadmin" );
85  
86          cl.setWorkingDirectory( repositoryRoot.getParentFile().getAbsolutePath() );
87  
88          cl.createArgument().setValue( "load" );
89  
90          cl.createArgument().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          {
100             System.err.println( "-----------------------------------------" );
101             System.err.println( "Command line: " + cl );
102             System.err.println( "Working directory: " + cl.getWorkingDirectory() );
103             System.err.println( "-----------------------------------------" );
104             System.err.println( "Standard output: " );
105             System.err.println( "-----------------------------------------" );
106             System.err.println( stdout.getOutput() );
107             System.err.println( "-----------------------------------------" );
108 
109             System.err.println( "Standard error: " );
110             System.err.println( "-----------------------------------------" );
111             System.err.println( stderr.getOutput() );
112             System.err.println( "-----------------------------------------" );
113         }
114 
115         if ( exitValue != 0 )
116         {
117             Assert.fail( "Exit value wasn't 0, was:" + exitValue );
118         }
119     }
120 
121     public static String getScmUrl( File repositoryRootFile )
122         throws CommandLineException
123     {
124         String repositoryRoot = repositoryRootFile.getAbsolutePath();
125 
126         // TODO: it'd be great to build this into CommandLineUtils somehow
127         // TODO: some way without a custom cygwin sys property?
128         if ( "true".equals( System.getProperty( "cygwin" ) ) )
129         {
130             Commandline cl = new Commandline();
131 
132             cl.setExecutable( "cygpath" );
133 
134             cl.createArgument().setValue( "--unix" );
135 
136             cl.createArgument().setValue( repositoryRoot );
137 
138             CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
139 
140             int exitValue = CommandLineUtils.executeCommandLine( cl, stdout, null );
141 
142             if ( exitValue != 0 )
143             {
144                 throw new CommandLineException( "Unable to convert cygwin path, exit code = " + exitValue );
145             }
146 
147             repositoryRoot = stdout.getOutput().trim();
148         }
149         else if ( System.getProperty( "os.name" ).startsWith( "Windows" ) )
150         {
151             repositoryRoot = "/" + StringUtils.replace( repositoryRoot, "\\", "/" );
152         }
153 
154         return "scm:svn:file://" + repositoryRoot;
155     }
156 }