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.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
37
38
39 public final class SvnScmTestUtils
40 {
41
42 public static final String SVN_COMMAND_LINE = "svn";
43
44
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,
65 "create " + repositoryRoot.getName() );
66
67 loadSvnDump( repositoryRoot,
68 new SvnScmTestUtils().getClass().getClassLoader().getResourceAsStream( "tck/tck.dump" ) );
69 }
70
71 public static void initializeRepository( File repositoryRoot, File dump )
72 throws Exception
73 {
74 if ( repositoryRoot.exists() )
75 {
76 FileUtils.deleteDirectory( repositoryRoot );
77 }
78
79 Assert.assertTrue( "Could not make repository root directory: " + repositoryRoot.getAbsolutePath(),
80 repositoryRoot.mkdirs() );
81
82 ScmTestCase.execute( repositoryRoot.getParentFile(), SVNADMIN_COMMAND_LINE,
83 "create " + repositoryRoot.getName() );
84
85 Assert.assertTrue( "The dump file doesn't exist: " + dump.getAbsolutePath(), dump.exists() );
86
87 loadSvnDump( repositoryRoot, new FileInputStream( dump ) );
88 }
89
90 private static void loadSvnDump( File repositoryRoot, InputStream dumpStream )
91 throws Exception
92 {
93 Commandline cl = new Commandline();
94
95 cl.setExecutable( SVNADMIN_COMMAND_LINE );
96
97 cl.setWorkingDirectory( repositoryRoot.getParentFile().getAbsolutePath() );
98
99 cl.createArg().setValue( "load" );
100
101 cl.createArg().setValue( repositoryRoot.getAbsolutePath() );
102
103 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
104
105 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
106
107 int exitValue = CommandLineUtils.executeCommandLine( cl, dumpStream, stdout, stderr );
108
109 if ( exitValue != 0 )
110 {
111 System.err.println( "-----------------------------------------" );
112 System.err.println( "Command line: " + cl );
113 System.err.println( "Working directory: " + cl.getWorkingDirectory() );
114 System.err.println( "-----------------------------------------" );
115 System.err.println( "Standard output: " );
116 System.err.println( "-----------------------------------------" );
117 System.err.println( stdout.getOutput() );
118 System.err.println( "-----------------------------------------" );
119
120 System.err.println( "Standard error: " );
121 System.err.println( "-----------------------------------------" );
122 System.err.println( stderr.getOutput() );
123 System.err.println( "-----------------------------------------" );
124 }
125
126 if ( exitValue != 0 )
127 {
128 Assert.fail( "Exit value wasn't 0, was:" + exitValue );
129 }
130 }
131
132 public static String getScmUrl( File repositoryRootFile )
133 throws CommandLineException
134 {
135 String repositoryRoot = repositoryRootFile.getAbsolutePath();
136
137
138
139 if ( "true".equals( System.getProperty( "cygwin" ) ) )
140 {
141 Commandline cl = new Commandline();
142
143 cl.setExecutable( "cygpath" );
144
145 cl.createArg().setValue( "--unix" );
146
147 cl.createArg().setValue( repositoryRoot );
148
149 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
150
151 int exitValue = CommandLineUtils.executeCommandLine( cl, stdout, null );
152
153 if ( exitValue != 0 )
154 {
155 throw new CommandLineException( "Unable to convert cygwin path, exit code = " + exitValue );
156 }
157
158 repositoryRoot = stdout.getOutput().trim();
159 }
160 else if ( Os.isFamily( "windows" ) )
161 {
162 repositoryRoot = "/" + StringUtils.replace( repositoryRoot, "\\", "/" );
163 }
164
165 return "scm:svn:file://" + repositoryRoot;
166 }
167 }