View Javadoc
1   package org.apache.maven.scm.provider.synergy.util;
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 org.apache.maven.scm.ScmException;
23  import org.apache.maven.scm.ScmTag;
24  import org.apache.maven.scm.ScmTestCase;
25  import org.codehaus.plexus.util.cli.Commandline;
26  
27  import java.io.File;
28  import java.util.LinkedList;
29  import java.util.List;
30  
31  /**
32   * @author <a href="mailto:julien.henry@capgemini.com">Julien Henry</a>
33   */
34  public class SynergyCCMTest
35      extends ScmTestCase
36  {
37  
38      public void testShowTaskObjects()
39          throws Exception
40      {
41          Commandline cl = SynergyCCM.showTaskObjects( 45, "my format", "CCM_ADDR" );
42          assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
43          assertCommandLine( "ccm task -show objects -f \"my format\" 45", null, cl );
44          cl = SynergyCCM.showTaskObjects( 45, null, "CCM_ADDR" );
45          assertCommandLine( "ccm task -show objects 45", null, cl );
46      }
47  
48      public void testQuery()
49          throws Exception
50      {
51          Commandline cl = SynergyCCM.query( "my query", "my format", "CCM_ADDR" );
52          assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
53          assertCommandLine( "ccm query -u -f \"my format\" \"my query\"", null, cl );
54          cl = SynergyCCM.query( "my query", null, "CCM_ADDR" );
55          assertCommandLine( "ccm query -u \"my query\"", null, cl );
56      }
57  
58      public void testCreateBaseline()
59          throws Exception
60      {
61          Commandline cl =
62              SynergyCCM.createBaseline( "myProject~1", "theBaseline", "my_release", "my_purpose", "CCM_ADDR" );
63          assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
64          assertCommandLine( "ccm baseline -create theBaseline -p myProject~1 -release my_release -purpose my_purpose",
65                             null, cl );
66      }
67  
68      public void testCreate()
69          throws Exception
70      {
71          File f = File.createTempFile( "test", null );
72          f.deleteOnExit();
73          List<File> list = new LinkedList<File>();
74          list.add( f );
75          Commandline cl = SynergyCCM.create( list, "test creation", "CCM_ADDR" );
76          assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
77          if ( f.getCanonicalPath().indexOf( ' ' ) > -1 )
78          {
79              assertCommandLine( "ccm create -c \"test creation\" \"" + f.getCanonicalPath() + "\"", null, cl );
80          }
81          else
82          {
83              assertCommandLine( "ccm create -c \"test creation\" " + f.getCanonicalPath(), null, cl );
84          }
85          File f2 = File.createTempFile( "test", null );
86          f2.deleteOnExit();
87          list.add( f2 );
88          cl = SynergyCCM.create( list, "test creation", "CCM_ADDR" );
89          if ( f.getCanonicalPath().indexOf( ' ' ) > -1 )
90          {
91              if ( f2.getCanonicalPath().indexOf( ' ' ) > -1 )
92              {
93                  assertCommandLine( "ccm create -c \"test creation\" \"" + f.getCanonicalPath() + "\" \"" +
94                      f2.getCanonicalPath() + "\"", null, cl );
95              }
96              else
97              {
98                  assertCommandLine(
99                      "ccm create -c \"test creation\" \"" + f.getCanonicalPath() + "\" " + f2.getCanonicalPath() + "",
100                     null, cl );
101             }
102         }
103         else
104         {
105             if ( f2.getCanonicalPath().indexOf( ' ' ) > -1 )
106             {
107                 assertCommandLine(
108                     "ccm create -c \"test creation\" " + f.getCanonicalPath() + " \"" + f2.getCanonicalPath() + "\"",
109                     null, cl );
110             }
111             else
112             {
113                 assertCommandLine(
114                     "ccm create -c \"test creation\" " + f.getCanonicalPath() + " " + f2.getCanonicalPath(), null, cl );
115             }
116         }
117     }
118 
119     public void testCreateTask()
120         throws Exception
121     {
122         /*
123          * NOTE: Quoting of arguments can differ for Windows/Unix, hence we normalize to single quotes for the purpose
124          * of testing.
125          */
126 
127         Commandline cl = SynergyCCM.createTask( "the synopsis", "release", true, "CCM_ADDR" );
128         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
129         String actual = cl.toString().replace( '\"', '\'' );
130         String expected = "ccm task -create -synopsis 'the synopsis' -release release";
131         assertTrue( "[" + actual + "] does not contain [" + expected + "]",
132                     actual.indexOf( expected ) > -1 );
133 
134         cl = SynergyCCM.createTask( "the synopsis", null, true, "CCM_ADDR" );
135         actual = cl.toString().replace( '\"', '\'' );
136         expected = "ccm task -create -synopsis 'the synopsis'";
137         assertTrue( "[" + actual + "] does not contain [" + expected + "]",
138                     actual.indexOf( expected ) > -1 );
139     }
140 
141     public void testCheckinTask()
142         throws Exception
143     {
144         Commandline cl = SynergyCCM.checkinTask( "truc", "a comment", "CCM_ADDR" );
145         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
146         assertCommandLine( "ccm task -checkin truc -comment \"a comment\"", null, cl );
147     }
148 
149     public void testDelete()
150         throws Exception
151     {
152         File f = File.createTempFile( "test", null );
153         f.deleteOnExit();
154         List<File> list = new LinkedList<File>();
155         list.add( f );
156         Commandline cl = SynergyCCM.delete( list, "CCM_ADDR", true );
157         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
158         if ( f.getCanonicalPath().indexOf( ' ' ) > -1 )
159         {
160             assertCommandLine( "ccm delete -replace \"" + f.getCanonicalPath() + "\"", null, cl );
161         }
162         else
163         {
164             assertCommandLine( "ccm delete -replace " + f.getCanonicalPath(), null, cl );
165         }
166         File f2 = File.createTempFile( "test", null );
167         f2.deleteOnExit();
168         list.add( f2 );
169         cl = SynergyCCM.delete( list, "CCM_ADDR", false );
170         if ( f.getCanonicalPath().indexOf( ' ' ) > -1 )
171         {
172             if ( f2.getCanonicalPath().indexOf( ' ' ) > -1 )
173             {
174                 assertCommandLine( "ccm delete \"" + f.getCanonicalPath() + "\" \"" + f2.getCanonicalPath() + "\"",
175                                    null, cl );
176             }
177             else
178             {
179                 assertCommandLine( "ccm delete \"" + f.getCanonicalPath() + "\" " + f2.getCanonicalPath() + "", null,
180                                    cl );
181             }
182         }
183         else
184         {
185             if ( f2.getCanonicalPath().indexOf( ' ' ) > -1 )
186             {
187                 assertCommandLine( "ccm delete " + f.getCanonicalPath() + " \"" + f2.getCanonicalPath() + "\"", null,
188                                    cl );
189             }
190             else
191             {
192                 assertCommandLine( "ccm delete " + f.getCanonicalPath() + " " + f2.getCanonicalPath(), null, cl );
193             }
194         }
195     }
196 
197     public void testReconfigure()
198         throws Exception
199     {
200         Commandline cl = SynergyCCM.reconfigure( "project~1", "CCM_ADDR" );
201         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
202         assertCommandLine( "ccm reconfigure -recurse -p project~1", null, cl );
203     }
204 
205     public void testReconfigureProperties()
206         throws Exception
207     {
208         Commandline cl = SynergyCCM.reconfigureProperties( "project~1", "CCM_ADDR" );
209         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
210         assertCommandLine( "ccm reconfigure_properties -refresh project~1", null, cl );
211     }
212 
213     public void testReconcileUwa()
214         throws Exception
215     {
216         Commandline cl = SynergyCCM.reconcileUwa( "project~1", "CCM_ADDR" );
217         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
218         assertCommandLine( "ccm rwa -r -uwa -p project~1", null, cl );
219         cl = SynergyCCM.reconcileUwa( null, "CCM_ADDR" );
220         assertCommandLine( "ccm rwa -r -uwa", null, cl );
221     }
222 
223     public void testReconcileUdb()
224         throws Exception
225     {
226         Commandline cl = SynergyCCM.reconcileUdb( "project~1", "CCM_ADDR" );
227         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
228         assertCommandLine( "ccm rwa -r -udb -p project~1", null, cl );
229         cl = SynergyCCM.reconcileUdb( null, "CCM_ADDR" );
230         assertCommandLine( "ccm rwa -r -udb", null, cl );
231     }
232 
233     public void testDir()
234         throws Exception
235     {
236         File f = File.createTempFile( "foo", null );
237         f.deleteOnExit();
238         Commandline cl = SynergyCCM.dir( f.getParentFile(), "format", "CCM_ADDR" );
239         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
240         assertEquals( f.getParentFile().getCanonicalFile(), cl.getWorkingDirectory().getCanonicalFile() );
241         assertCommandLine( "ccm dir -m -f format", f.getParentFile().getCanonicalFile(), cl );
242     }
243 
244     public void testCheckoutFiles()
245         throws Exception
246     {
247         File f = File.createTempFile( "test", null );
248         f.deleteOnExit();
249         List<File> list = new LinkedList<File>();
250         list.add( f );
251         Commandline cl = SynergyCCM.checkoutFiles( list, "CCM_ADDR" );
252         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
253         if ( f.getCanonicalPath().indexOf( ' ' ) > -1 )
254         {
255             assertCommandLine( "ccm co \"" + f.getCanonicalPath() + "\"", null, cl );
256         }
257         else
258         {
259             assertCommandLine( "ccm co " + f.getCanonicalPath(), null, cl );
260         }
261         File f2 = File.createTempFile( "test", null );
262         f2.deleteOnExit();
263         list.add( f2 );
264         cl = SynergyCCM.checkoutFiles( list, "CCM_ADDR" );
265         if ( f.getCanonicalPath().indexOf( ' ' ) > -1 )
266         {
267             if ( f2.getCanonicalPath().indexOf( ' ' ) > -1 )
268             {
269                 assertCommandLine( "ccm co \"" + f.getCanonicalPath() + "\" \"" + f2.getCanonicalPath() + "\"", null,
270                                    cl );
271             }
272             else
273             {
274                 assertCommandLine( "ccm co \"" + f.getCanonicalPath() + "\" " + f2.getCanonicalPath() + "", null, cl );
275             }
276         }
277         else
278         {
279             if ( f2.getCanonicalPath().indexOf( ' ' ) > -1 )
280             {
281                 assertCommandLine( "ccm co " + f.getCanonicalPath() + " \"" + f2.getCanonicalPath() + "\"", null, cl );
282             }
283             else
284             {
285                 assertCommandLine( "ccm co " + f.getCanonicalPath() + " " + f2.getCanonicalPath(), null, cl );
286             }
287         }
288     }
289 
290     public void testCheckoutProject()
291         throws Exception
292     {
293         Commandline cl = SynergyCCM.checkoutProject( null, "MyProject", new ScmTag( "MyVersion" ), "MyPurpose",
294                                                      "MyRelease", "CCM_ADDR" );
295         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
296         assertCommandLine( "ccm co -subprojects -rel -t MyVersion -purpose MyPurpose -release MyRelease -p MyProject",
297                            null, cl );
298         File f = File.createTempFile( "test", null );
299         f.deleteOnExit();
300         cl = SynergyCCM.checkoutProject( f.getParentFile(), "MyProject", new ScmTag( "MyVersion" ), "MyPurpose",
301                                          "MyRelease", "CCM_ADDR" );
302         if ( f.getCanonicalPath().indexOf( ' ' ) > -1 )
303         {
304             assertCommandLine( "ccm co -subprojects -rel -t MyVersion -purpose MyPurpose -release MyRelease -path \"" +
305                 f.getParentFile().getCanonicalPath() + "\" -p MyProject", null, cl );
306         }
307         else
308         {
309             assertCommandLine( "ccm co -subprojects -rel -t MyVersion -purpose MyPurpose -release MyRelease -path " +
310                 f.getParentFile().getCanonicalPath() + " -p MyProject", null, cl );
311         }
312     }
313 
314     public void testCheckinProject()
315         throws Exception
316     {
317         Commandline cl = SynergyCCM.checkinProject( "MyProject", "a comment", "CCM_ADDR" );
318         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
319         assertCommandLine( "ccm ci -c \"a comment\" -p MyProject", null, cl );
320     }
321 
322     public void testCheckinFiles()
323         throws Exception
324     {
325         File f = File.createTempFile( "test", null );
326         f.deleteOnExit();
327         List<File> list = new LinkedList<File>();
328         list.add( f );
329         Commandline cl = SynergyCCM.checkinFiles( list, "a comment", "CCM_ADDR" );
330         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
331         if ( f.getCanonicalPath().indexOf( ' ' ) > -1 )
332         {
333             assertCommandLine( "ccm ci -c \"a comment\" \"" + f.getCanonicalPath() + "\"", null, cl );
334         }
335         else
336         {
337             assertCommandLine( "ccm ci -c \"a comment\" " + f.getCanonicalPath(), null, cl );
338         }
339         File f2 = File.createTempFile( "test", null );
340         f2.deleteOnExit();
341         list.add( f2 );
342         cl = SynergyCCM.checkinFiles( list, "a comment", "CCM_ADDR" );
343         if ( f.getCanonicalPath().indexOf( ' ' ) > -1 )
344         {
345             if ( f2.getCanonicalPath().indexOf( ' ' ) > -1 )
346             {
347                 assertCommandLine(
348                     "ccm ci -c \"a comment\" \"" + f.getCanonicalPath() + "\" \"" + f2.getCanonicalPath() + "\"", null,
349                     cl );
350             }
351             else
352             {
353                 assertCommandLine(
354                     "ccm ci -c \"a comment\" \"" + f.getCanonicalPath() + "\" " + f2.getCanonicalPath() + "", null,
355                     cl );
356             }
357         }
358         else
359         {
360             if ( f2.getCanonicalPath().indexOf( ' ' ) > -1 )
361             {
362                 assertCommandLine(
363                     "ccm ci -c \"a comment\" " + f.getCanonicalPath() + " \"" + f2.getCanonicalPath() + "\"", null,
364                     cl );
365             }
366             else
367             {
368                 assertCommandLine( "ccm ci -c \"a comment\" " + f.getCanonicalPath() + " " + f2.getCanonicalPath(),
369                                    null, cl );
370             }
371         }
372     }
373 
374     public void testSync()
375         throws Exception
376     {
377         Commandline cl = SynergyCCM.synchronize( "myProject", "CCM_ADDR" );
378         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
379         assertCommandLine( "ccm sync -r -p myProject", null, cl );
380     }
381 
382     public void testShowWorkArea()
383         throws Exception
384     {
385         Commandline cl = SynergyCCM.showWorkArea( "MyProject~1", "CCM_ADDR" );
386         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
387         assertCommandLine( "ccm wa -show MyProject~1", null, cl );
388     }
389 
390     public void testStart()
391         throws Exception
392     {
393         Commandline cl = SynergyCCM.start( "user", "pass", SynergyRole.BUILD_MGR );
394         assertCommandLine( "ccm start -nogui -m -q -n user -pw pass -r build_mgr", null, cl );
395     }
396 	
397     public void testStartRemote()
398         throws Exception
399     {
400         Commandline cl = SynergyCCM.startRemote( "user", "pass", SynergyRole.BUILD_MGR );
401         assertCommandLine( "ccm start -nogui -m -q -rc -n user -pw pass -r build_mgr", null, cl );
402     }
403 
404     public void testStop()
405         throws Exception
406     {
407         Commandline cl = SynergyCCM.stop( "CCM_ADDR" );
408         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
409         assertCommandLine( "ccm stop", null, cl );
410     }
411 
412     public void testDelimiter()
413         throws Exception
414     {
415         Commandline cl = SynergyCCM.delimiter( "CCM_ADDR" );
416         assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
417         assertCommandLine( "ccm delimiter", null, cl );
418     }
419     
420     public void testShowDefaultTask()
421     	throws Exception
422     {
423     	Commandline cl = SynergyCCM.showDefaultTask( "CCM_ADDR" );
424     	assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
425     	assertCommandLine( "ccm task -default", null, cl );
426     }
427     
428     public void testSetDefaultTask()
429 		throws Exception
430 	{
431     	Commandline cl = SynergyCCM.setDefaultTask( 4711, "CCM_ADDR" );
432     	assertTrue( "CCM_ADDR is not set.", assertContains( cl.getEnvironmentVariables(), "CCM_ADDR=CCM_ADDR" ) );
433     	assertCommandLine( "ccm task -default 4711", null, cl );
434 	}
435     
436 
437     public boolean assertContains( String[] array, String value )
438     {
439         for ( int i = 0; i < array.length; i++ )
440         {
441             if ( array[i].equals( value ) )
442             {
443                 return true;
444             }
445         }
446         return false;
447     }
448 }