001package org.apache.maven.scm.provider.accurev.command;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the
006 * Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a
007 * copy of the License at
008 * 
009 * http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
012 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
013 * governing permissions and limitations under the License.
014 */
015
016import static org.hamcrest.Matchers.containsString;
017import static org.hamcrest.Matchers.is;
018import static org.hamcrest.Matchers.lessThan;
019import static org.hamcrest.Matchers.not;
020import static org.hamcrest.Matchers.notNullValue;
021import static org.junit.Assert.assertThat;
022
023import java.io.File;
024import java.util.ArrayList;
025import java.util.List;
026
027import org.apache.maven.scm.ScmTestCase;
028import org.apache.maven.scm.log.DefaultLog;
029import org.apache.maven.scm.log.ScmLogger;
030import org.apache.maven.scm.provider.accurev.AccuRevInfo;
031import org.apache.maven.scm.provider.accurev.AccuRevScmProvider;
032import org.apache.maven.scm.provider.accurev.AccuRevScmProviderRepository;
033import org.apache.maven.scm.provider.accurev.cli.AccuRevCommandLine;
034import org.apache.maven.scm.provider.accurev.cli.AccuRevJUnitUtil;
035import org.codehaus.plexus.PlexusContainer;
036import org.codehaus.plexus.util.StringUtils;
037
038public class AccuRevTckUtil
039{
040
041    private String depotName = null;
042
043    private AccuRevCommandLine accurevCL;
044
045    private AccuRevInfo info;
046
047    private String url;
048
049    private ScmLogger logger;
050
051    private String tckBaseDir;
052
053    private String workingStream;
054
055    public static String getSystemProperty( String name, String defaultValue )
056    {
057        String mavenProperty = "${" + name + "}";
058        String result = System.getProperty( name, mavenProperty );
059        if ( mavenProperty.equals( result ) )
060        {
061            result = defaultValue;
062        }
063        return result;
064    }
065
066    public String getScmUrl()
067        throws Exception
068    {
069        if ( url == null )
070        {
071
072            // Either tckUrlPrefix or tckAllowImpliedLogin must be set.
073            // This is to prevent accidentally running the tck tests against say your production accurev server
074
075            String tckUrlPrefix = getSystemProperty( "tckUrlPrefix", "" );
076
077            if ( StringUtils.isBlank( tckUrlPrefix ) )
078            {
079                assertThat( "Property \"tckUrlPrefix\" is not set."
080                    + " To enable tck tests against an externally logged in accurev session,"
081                    + " please set property \"tckAllowImpliedLogin\" to \"true\"",
082                            getSystemProperty( "tckAllowImpliedLogin", "false" ), is( "true" ) );
083            }
084            else
085            {
086                assertThat( "tckUrlPrefix must of the form [[user[/pass]]@host[:port]", tckUrlPrefix,
087                            containsString( "@" ) );
088            }
089
090            url = "scm:accurev:" + tckUrlPrefix + ":" + getWorkingStream() + ":?tagFormat='" + getDepotName() + "_%s'";
091
092            getLogger().debug( "Using scmURL=" + url );
093        }
094
095        return url;
096
097    }
098
099    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}