1 package org.apache.maven.scm.provider.jazz.command;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.CommandParameters;
23 import org.apache.maven.scm.ScmException;
24 import org.apache.maven.scm.ScmFileSet;
25 import org.apache.maven.scm.ScmResult;
26 import org.apache.maven.scm.command.AbstractCommand;
27 import org.apache.maven.scm.log.DefaultLog;
28 import org.apache.maven.scm.provider.ScmProviderRepository;
29 import org.apache.maven.scm.provider.jazz.command.consumer.DebugLoggerConsumer;
30 import org.apache.maven.scm.provider.jazz.command.consumer.ErrorConsumer;
31 import org.apache.maven.scm.provider.jazz.repository.JazzScmProviderRepository;
32 import org.apache.maven.scm.repository.ScmRepository;
33 import org.codehaus.plexus.util.FileUtils;
34 import org.codehaus.plexus.util.StringUtils;
35 import org.codehaus.plexus.util.cli.StreamConsumer;
36 import org.junit.Assert;
37
38 import java.io.File;
39
40
41
42
43
44
45 public class JazzTckUtil
46 extends AbstractCommand
47 {
48 private long currentSystemTimeMillis = System.currentTimeMillis();
49
50 private String tckBaseDir;
51
52 private String scmUrl;
53
54 private String snapshotName;
55
56
57
58
59
60
61
62
63
64 public String getSystemProperty( String name, String defaultValue )
65 {
66 String mavenProperty = "${" + name + "}";
67 String result = System.getProperty( name, mavenProperty );
68 if ( mavenProperty.equals( result ) )
69 {
70 result = defaultValue;
71 }
72 return result;
73 }
74
75
76
77
78 @Override
79 protected ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet fileSet,
80 CommandParameters parameters )
81 throws ScmException
82 {
83 JazzScmProviderRepository jazzRepo = (JazzScmProviderRepository) repository;
84
85 StreamConsumer tckConsumer =
86 new DebugLoggerConsumer( getLogger() );
87 ErrorConsumer errConsumer = new ErrorConsumer( getLogger() );
88 String nameWorkspace = jazzRepo.getRepositoryWorkspace();
89
90 String nameSnapshot = getSnapshotName();
91 JazzScmCommand tckCreateWorkspaceFromSnapshotCmd =
92 createCreateWorkspaceFromSnapshotCommand( jazzRepo, fileSet, nameWorkspace, nameSnapshot );
93 int status = tckCreateWorkspaceFromSnapshotCmd.execute( tckConsumer, errConsumer );
94
95 if ( status != 0 || errConsumer.hasBeenFed() )
96 {
97 return new ScmResult( tckCreateWorkspaceFromSnapshotCmd.getCommandString(),
98 "Error code for Jazz SCM (create workspace --snapshot) command - " + status,
99 errConsumer.getOutput(), false );
100 }
101
102 return new ScmResult( tckCreateWorkspaceFromSnapshotCmd.getCommandString(), "All ok",
103 ( (DebugLoggerConsumer) tckConsumer ).getOutput(), true );
104 }
105
106
107
108 private JazzScmCommand createCreateWorkspaceFromSnapshotCommand( JazzScmProviderRepository repo, ScmFileSet fileSet,
109 String nameWorkspace, String nameSnapshot )
110 {
111 JazzScmCommand command =
112 new JazzScmCommand( JazzConstants.CMD_CREATE, JazzConstants.CMD_SUB_WORKSPACE, repo, fileSet, getLogger() );
113
114 command.addArgument( nameWorkspace );
115 command.addArgument( JazzConstants.ARG_WORKSPACE_SNAPSHOT );
116 command.addArgument( nameSnapshot );
117
118 return command;
119 }
120
121
122
123
124
125
126 public void generateNewSystemTime()
127 {
128 currentSystemTimeMillis = System.currentTimeMillis();
129 }
130
131
132
133
134
135
136
137
138 public void initRepo( ScmRepository repository )
139 throws Exception
140 {
141
142 setLogger( new DefaultLog() );
143
144 executeCommand( repository.getProviderRepository(), new ScmFileSet( getWorkingCopy() ), null );
145 }
146
147
148
149
150
151
152
153 public void removeRepo()
154 throws Exception
155 {
156 FileUtils.deleteDirectory( new File( getTckBaseDir() ) );
157 }
158
159
160
161
162
163
164
165 public String getScmUrl()
166 throws Exception
167 {
168 if ( scmUrl == null )
169 {
170
171
172
173
174 String tckUrlPrefix = getSystemProperty( "tckUrlPrefix", "" );
175 if ( StringUtils.isBlank( tckUrlPrefix ) )
176 {
177 Assert.fail( "Property \"tckUrlPrefix\" is not set." );
178 }
179
180 scmUrl = tckUrlPrefix + "_" + currentSystemTimeMillis;
181 }
182
183 return scmUrl;
184 }
185
186
187
188
189
190
191
192 private String getSnapshotName()
193 {
194 if ( snapshotName == null )
195 {
196 snapshotName = getSystemProperty( "tckSnapshotName", "" );
197 if ( StringUtils.isBlank( snapshotName ) )
198 {
199 Assert.fail( "Property \"tckSnapshotName\" is not set." );
200 }
201 }
202
203 return snapshotName;
204 }
205
206
207
208
209
210
211 private String getTckBaseDir()
212 {
213 if ( tckBaseDir == null )
214 {
215 tckBaseDir = getSystemProperty( "tckBaseDir", "" );
216 if ( StringUtils.isBlank( tckBaseDir ) )
217 {
218 Assert.fail( "Property \"tckBaseDir\" is not set." );
219 }
220 }
221
222 return tckBaseDir;
223 }
224
225
226
227
228 public File getWorkingCopy()
229 {
230 return new File( getTckBaseDir() + "/wc" );
231 }
232
233
234
235
236 public File getAssertionCopy()
237 {
238 return new File( getTckBaseDir() + "/ac" );
239 }
240
241
242
243
244 public File getUpdatingCopy()
245 {
246 return new File( getTckBaseDir() + "/uc" );
247 }
248 }