001package org.apache.maven.scm.provider.cvslib.command.update;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.scm.ScmFile;
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.ScmFileStatus;
025import org.apache.maven.scm.command.update.UpdateScmResult;
026import org.apache.maven.scm.manager.ScmManager;
027import org.apache.maven.scm.provider.cvslib.AbstractCvsScmTest;
028import org.apache.maven.scm.provider.cvslib.CvsScmTestUtils;
029import org.apache.maven.scm.repository.ScmRepository;
030import org.codehaus.plexus.util.FileUtils;
031import org.codehaus.plexus.util.IOUtil;
032
033import java.io.File;
034import java.io.FileWriter;
035
036/**
037 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
038 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
039 *
040 */
041public class CvsUpdateCommandTest
042    extends AbstractCvsScmTest
043{
044    private File repository;
045
046    private File workingDirectory;
047
048    private File assertionDirectory;
049
050    /**
051     * {@inheritDoc}
052     */
053    public void setUp()
054        throws Exception
055    {
056        super.setUp();
057
058        repository = getTestFile( "target/update-test/repository" );
059
060        workingDirectory = getTestFile( "target/update-test/working-directory" );
061
062        assertionDirectory = getTestFile( "target/update-test/assertion-directory" );
063
064        CvsScmTestUtils.initRepo( repository, workingDirectory, assertionDirectory );
065    }
066
067    /**
068     * {@inheritDoc}
069     */
070    protected String getModule()
071    {
072        return "test-repo/update";
073    }
074
075    /**
076     * @todo merge into tck
077     */
078    public void testCvsUpdate()
079        throws Exception
080    {
081
082        FileWriter writer = null;
083        try
084        {
085            if ( !isSystemCmd( CvsScmTestUtils.CVS_COMMAND_LINE ) )
086            {
087                System.err.println(
088                    "'" + CvsScmTestUtils.CVS_COMMAND_LINE + "' is not a system command. Ignored " + getName() + "." );
089                return;
090            }
091
092            ScmManager scmManager = getScmManager();
093
094            String scmUrl = CvsScmTestUtils.getScmUrl( repository, getModule() );
095
096            // Check out the repo to a working directory where files will be modified and committed
097            String arguments =
098                "-f -d " + repository.getAbsolutePath() + " " + "co -d " + workingDirectory.getName() + " "
099                    + getModule();
100
101            CvsScmTestUtils.executeCVS( workingDirectory.getParentFile(), arguments );
102
103            // Check out the repo to a assertion directory where the command will be used
104            arguments = "-f -d " + repository.getAbsolutePath() + " " + "co -d " + assertionDirectory.getName() + " "
105                + getModule();
106
107            CvsScmTestUtils.executeCVS( assertionDirectory.getParentFile(), arguments );
108
109            // A new check out should return 0 updated files.
110            ScmRepository scmRepository = scmManager.makeScmRepository( scmUrl );
111
112            UpdateScmResult result = scmManager.update( scmRepository, new ScmFileSet( assertionDirectory ) );
113
114            assertNotNull( result );
115
116            if ( !result.isSuccess() )
117            {
118                System.out.println( "result.providerMessage: " + result.getProviderMessage() );
119
120                System.out.println( "result.commandOutput: " + result.getCommandOutput() );
121
122                fail( "Command failed" );
123            }
124
125            assertNull( result.getProviderMessage() );
126
127            assertNull( result.getCommandOutput() );
128
129            assertNotNull( result.getUpdatedFiles() );
130
131            assertEquals( 0, result.getUpdatedFiles().size() );
132
133            // Modifing a file
134            File fooJava = new File( workingDirectory, "Foo.java" );
135
136            String content = FileUtils.fileRead( fooJava );
137
138            writer = new FileWriter( fooJava );
139
140            writer.write( content + System.getProperty( "line.separator" ) );
141            writer.write( "extra line" );
142
143            writer.close();
144
145            // Adding a new file
146            writer = new FileWriter( new File( workingDirectory, "New.txt" ) );
147
148            writer.write( "new file" );
149
150            writer.close();
151
152            arguments = "-f -d " + repository.getAbsolutePath() + " add New.txt";
153
154            CvsScmTestUtils.executeCVS( workingDirectory, arguments );
155
156            // Committing
157            arguments = "-f -d " + repository.getAbsolutePath() + " commit -m .";
158
159            CvsScmTestUtils.executeCVS( workingDirectory, arguments );
160
161            // Check the updated files
162            result = scmManager.update( scmRepository, new ScmFileSet( assertionDirectory ) );
163
164            assertNotNull( result );
165
166            if ( !result.isSuccess() )
167            {
168                System.out.println( "result.providerMessage: " + result.getProviderMessage() );
169
170                System.out.println( "result.commandOutput: " + result.getCommandOutput() );
171
172                fail( "Command failed" );
173            }
174
175            assertNull( result.getProviderMessage() );
176
177            assertNull( result.getCommandOutput() );
178
179            assertNotNull( result.getUpdatedFiles() );
180
181            assertEquals( 2, result.getUpdatedFiles().size() );
182
183            ScmFile file1 = result.getUpdatedFiles().get( 0 );
184
185            assertPath( "Foo.java", file1.getPath() );
186
187            assertEquals( ScmFileStatus.UPDATED, file1.getStatus() );
188
189            ScmFile file2 = result.getUpdatedFiles().get( 1 );
190
191            assertPath( "New.txt", file2.getPath() );
192
193            assertEquals( ScmFileStatus.UPDATED, file2.getStatus() );
194        }
195        finally
196        {
197            IOUtil.close( writer );
198        }
199    }
200}