1 package org.apache.maven.scm.provider.svn;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
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
127
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 }