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, "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
136
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 }