View Javadoc
1   package org.apache.maven.scm.provider.jazz.command;
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.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   * Common utilities for Jazz TCK tests.
42   *
43   * @author <a href="mailto:ChrisGWarp@gmail.com">Chris Graham</a>
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       * Get the specified system property. Borrowed from AccuRevTckUtil.
58       * TODO: Refactor to a common usage.
59       *
60       * @param name         The name of the property to get.
61       * @param defaultValue A default value if not found.
62       * @return
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      /* (non-Javadoc)
76      * @see org.apache.maven.scm.command.AbstractCommand#executeCommand(org.apache.maven.scm.provider.ScmProviderRepository, org.apache.maven.scm.ScmFileSet, org.apache.maven.scm.CommandParameters)
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() );      // No need for a dedicated consumer for this
87          ErrorConsumer errConsumer = new ErrorConsumer( getLogger() );
88          String nameWorkspace = jazzRepo.getRepositoryWorkspace();
89          //String nameSnapshot = "MavenSCMTestSnapshot";
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     // Create the JazzScmCommand to execute the "scm create workspace ..." command
107     // This will create a workspace of the same name as the tag.
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      * If a TCK test case has more than one test case, it will need
123      * to generate a new workspace for each test. Use this method
124      * to provide uniqueness again.
125      */
126     public void generateNewSystemTime()
127     {
128         currentSystemTimeMillis = System.currentTimeMillis();
129     }
130 
131     /**
132      * Create a unique repository workspace using the system time, based
133      * upon a supplied snapshot. The creation of this initial snapshot
134      * currently can not be scripted, so it needs to be done manually first.
135      *
136      * @see org.apache.maven.scm.ScmTckTestCase#initRepo()
137      */
138     public void initRepo( ScmRepository repository )
139         throws Exception
140     {
141         // Set a default logger. because I cann't get to the ones later on...
142         setLogger( new DefaultLog() );
143         // Create the unique workspace based upon a snapshot
144         executeCommand( repository.getProviderRepository(), new ScmFileSet( getWorkingCopy() ), null );
145     }
146 
147     /**
148      * This method is available to those SCM clients that need to perform
149      * a cleanup at the end of the tests. It is needed when server side
150      * operations are performed, or the check out dirs are outside
151      * of the normal target directory.
152      */
153     public void removeRepo()
154         throws Exception
155     {
156         FileUtils.deleteDirectory( new File( getTckBaseDir() ) );
157     }
158 
159     /**
160      * Return the URL used for this specific TCK test execution.
161      * It generates a unique workspace name, based on the system time.
162      *
163      * @see org.apache.maven.scm.ScmTckTestCase#getScmUrl()
164      */
165     public String getScmUrl()
166         throws Exception
167     {
168         if ( scmUrl == null )
169         {
170             // tckUrlPrefix is the system property that is used to seed the SCM URL.
171             // EG:
172             // "scm:jazz:Deb;Deb@https://rtc:9444/jazz:MavenSCMTestWorkspace"
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      * Get the snapshot name, getting it from the system properties if necessary.
188      *
189      * @return The name of the snapshot used to create a repository workspace,
190      *         which is then loaded into the tckBaseDir.
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      * Get the base directory used for the tck tests.
208      *
209      * @return The base directory used for the tck tests, the sandbox.
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      * @see org.apache.maven.scm.ScmTestCase#getWorkingCopy()
227      */
228     public File getWorkingCopy()
229     {
230         return new File( getTckBaseDir() + "/wc" );
231     }
232 
233     /**
234      * @see org.apache.maven.scm.ScmTestCase#getAssertionCopy()
235      */
236     public File getAssertionCopy()
237     {
238         return new File( getTckBaseDir() + "/ac" );
239     }
240 
241     /**
242      * @see org.apache.maven.scm.ScmTestCase#getUpdatingCopy()
243      */
244     public File getUpdatingCopy()
245     {
246         return new File( getTckBaseDir() + "/uc" );
247     }
248 }