View Javadoc

1   package org.apache.maven.scm.provider.accurev.command;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
5    * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the
6    * Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a
7    * copy of the License at
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
13   * governing permissions and limitations under the License.
14   */
15  
16  import static org.hamcrest.Matchers.containsString;
17  import static org.hamcrest.Matchers.is;
18  import static org.hamcrest.Matchers.lessThan;
19  import static org.hamcrest.Matchers.not;
20  import static org.hamcrest.Matchers.notNullValue;
21  import static org.junit.Assert.assertThat;
22  
23  import java.io.File;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.maven.scm.ScmTestCase;
28  import org.apache.maven.scm.log.DefaultLog;
29  import org.apache.maven.scm.log.ScmLogger;
30  import org.apache.maven.scm.provider.accurev.AccuRevInfo;
31  import org.apache.maven.scm.provider.accurev.AccuRevScmProvider;
32  import org.apache.maven.scm.provider.accurev.AccuRevScmProviderRepository;
33  import org.apache.maven.scm.provider.accurev.cli.AccuRevCommandLine;
34  import org.apache.maven.scm.provider.accurev.cli.AccuRevJUnitUtil;
35  import org.codehaus.plexus.PlexusContainer;
36  import org.codehaus.plexus.util.StringUtils;
37  
38  public class AccuRevTckUtil
39  {
40  
41      private String depotName = null;
42  
43      private AccuRevCommandLine accurevCL;
44  
45      private AccuRevInfo info;
46  
47      private String url;
48  
49      private ScmLogger logger;
50  
51      private String tckBaseDir;
52  
53      private String workingStream;
54  
55      public static String getSystemProperty( String name, String defaultValue )
56      {
57          String mavenProperty = "${" + name + "}";
58          String result = System.getProperty( name, mavenProperty );
59          if ( mavenProperty.equals( result ) )
60          {
61              result = defaultValue;
62          }
63          return result;
64      }
65  
66      public String getScmUrl()
67          throws Exception
68      {
69          if ( url == null )
70          {
71  
72              // Either tckUrlPrefix or tckAllowImpliedLogin must be set.
73              // This is to prevent accidentally running the tck tests against say your production accurev server
74  
75              String tckUrlPrefix = getSystemProperty( "tckUrlPrefix", "" );
76  
77              if ( StringUtils.isBlank( tckUrlPrefix ) )
78              {
79                  assertThat( "Property \"tckUrlPrefix\" is not set."
80                      + " To enable tck tests against an externally logged in accurev session,"
81                      + " please set property \"tckAllowImpliedLogin\" to \"true\"",
82                              getSystemProperty( "tckAllowImpliedLogin", "false" ), is( "true" ) );
83              }
84              else
85              {
86                  assertThat( "tckUrlPrefix must of the form [[user[/pass]]@host[:port]", tckUrlPrefix,
87                              containsString( "@" ) );
88              }
89  
90              url = "scm:accurev:" + tckUrlPrefix + ":" + getWorkingStream() + ":?tagFormat='" + getDepotName() + "_%s'";
91  
92              getLogger().debug( "Using scmURL=" + url );
93          }
94  
95          return url;
96  
97      }
98  
99      private void setLogger( PlexusContainer plexusContainer )
100         throws Exception
101     {
102         this.logger = AccuRevJUnitUtil.getLogger( plexusContainer );
103     }
104 
105     public void initRepo( PlexusContainer container )
106         throws Exception
107     {
108         setLogger( container );
109         initRepo();
110     }
111 
112     private void initRepo()
113         throws Exception
114     {
115 
116         assertLoggedInOK();
117 
118         assertThat( "Can't execute TckTests in an accurev workspace, please set 'tckBaseDir' property",
119                     getAccuRevInfo().isWorkSpace(), is( false ) );
120 
121         File initDir = ScmTestCase.getTestFile( getTckBaseDir(), "target/" + getDepotName() + "/init" );
122 
123         assertThat( "AccuRev workspace path limit of 127 characters execeeded, please set 'tckBaseDir' property",
124                     initDir.getAbsolutePath().length(), lessThan( 127 ) );
125 
126         getAccuRevCL().mkdepot( getDepotName() );
127 
128         String newStreamName = getWorkingStream();
129 
130         getAccuRevCL().mkstream( getDepotName(), newStreamName );
131 
132         /*
133          * Since scmFileNames is not populated before this is called... we get to duplicate some code here.
134          * ChrisGWarp: It is now! :-)
135          * TODO raise patch to fix this. (still)
136          */
137 
138         List<String> scmFileNames = new ArrayList<String>( 4 );
139         scmFileNames.add( "/pom.xml" );
140         scmFileNames.add( "/readme.txt" );
141         scmFileNames.add( "/src/main/java/Application.java" );
142         scmFileNames.add( "/src/test/java/Test.java" );
143 
144         for ( String filename : scmFileNames )
145         {
146             ScmTestCase.makeFile( initDir, filename, filename );
147         }
148 
149         String initWorkSpace = getDepotName() + "_initRepo";
150         getAccuRevCL().mkws( newStreamName, initWorkSpace, initDir );
151 
152         getAccuRevCL().add( initDir, null, "initial version" );
153         getAccuRevCL().promoteAll( initDir, "initial version" );
154 
155         getAccuRevCL().rmws( initWorkSpace + "_" + getAccuRevInfo().getUser() );
156     }
157 
158     public String getWorkingStream()
159     {
160         if ( workingStream == null )
161         {
162             workingStream = getDepotName() + "_tckTests";
163         }
164         return workingStream;
165     }
166 
167     private String getTckBaseDir()
168     {
169         if ( tckBaseDir == null )
170         {
171             tckBaseDir = getSystemProperty( "tckBaseDir", "" );
172             if ( StringUtils.isBlank( tckBaseDir ) )
173             {
174                 tckBaseDir = ScmTestCase.getBasedir();
175             }
176             getLogger().debug( "tckBaseDir=" + tckBaseDir );
177         }
178 
179         return tckBaseDir;
180     }
181 
182     private void assertLoggedInOK()
183         throws Exception
184     {
185 
186         assertThat( getAccuRevInfo().getUser(), notNullValue() );
187         assertThat( getAccuRevInfo().getUser(), is( not( "(not logged in)" ) ) );
188     }
189 
190     public void tearDown()
191         throws Exception
192     {
193         // nothing left...
194     }
195 
196     public String getDepotName()
197     {
198         if ( depotName == null )
199         {
200             depotName = "mvnscm_" + ( System.currentTimeMillis() / 1000 );
201         }
202         return depotName;
203     }
204 
205     public ScmLogger getLogger()
206     {
207         if ( logger == null )
208         {
209             logger = new DefaultLog();
210         }
211 
212         return logger;
213     }
214 
215     public AccuRevCommandLine getAccuRevCL()
216         throws Exception
217     {
218         if ( accurevCL == null )
219         {
220             AccuRevScmProvider provider = new AccuRevScmProvider();
221             provider.addListener( getLogger() );
222             AccuRevScmProviderRepository repo =
223                 (AccuRevScmProviderRepository) provider.makeProviderScmRepository( getScmUrl(), ':' );
224             getLogger().debug( repo.toString() );
225             accurevCL = (AccuRevCommandLine) repo.getAccuRev();
226 
227             if ( !StringUtils.isEmpty( repo.getUser() ) )
228             {
229                 accurevCL.login( repo.getUser(), repo.getPassword() );
230             }
231 
232         }
233 
234         return accurevCL;
235     }
236 
237     public void removeWorkSpace( File basedir )
238         throws Exception
239     {
240         try
241         {
242             assertLoggedInOK();
243         }
244         catch ( AssertionError e )
245         {
246             return;
247         }
248         if ( basedir.exists() )
249         {
250             AccuRevInfo bdInfo = accurevCL.info( basedir );
251             if ( bdInfo.isWorkSpaceTop() )
252             {
253                 accurevCL.promoteAll( basedir, "clear default group" );
254                 accurevCL.rmws( bdInfo.getWorkSpace() );
255             }
256         }
257     }
258 
259     public AccuRevInfo getAccuRevInfo()
260         throws Exception
261     {
262         if ( info == null )
263         {
264             File basedir = new File( getTckBaseDir() );
265             info = getAccuRevCL().info( basedir );
266         }
267 
268         return info;
269 
270     }
271 
272     /*
273      * Need to put this in a sub directory because you can't re-use workspace directories And for some stupid reason we
274      * only have 127 characters available for the path name
275      */
276     public File getWorkingCopy()
277     {
278         return ScmTestCase.getTestFile( getTckBaseDir(), "target/" + getDepotName() + "/co" );
279     }
280 
281     public File getAssertionCopy()
282     {
283         return ScmTestCase.getTestFile( getTckBaseDir(), "target/" + getDepotName() + "/as" );
284     }
285 
286     public File getUpdatingCopy()
287     {
288         return ScmTestCase.getTestFile( getTckBaseDir(), "target/" + getDepotName() + "/up" );
289     }
290 }