View Javadoc
1   package org.apache.maven.scm.provider.cvslib.command.update;
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.ScmFile;
23  import org.apache.maven.scm.ScmFileSet;
24  import org.apache.maven.scm.ScmFileStatus;
25  import org.apache.maven.scm.ScmTestCase;
26  import org.apache.maven.scm.command.update.UpdateScmResult;
27  import org.apache.maven.scm.manager.ScmManager;
28  import org.apache.maven.scm.provider.cvslib.CvsScmTestUtils;
29  import org.apache.maven.scm.repository.ScmRepository;
30  import org.codehaus.plexus.util.FileUtils;
31  import org.codehaus.plexus.util.IOUtil;
32  
33  import java.io.File;
34  import java.io.FileWriter;
35  
36  /**
37   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
38   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
39   *
40   */
41  public class CvsUpdateCommandTest
42      extends ScmTestCase
43  {
44      private File repository;
45  
46      private File workingDirectory;
47  
48      private File assertionDirectory;
49  
50      /**
51       * {@inheritDoc}
52       */
53      public void setUp()
54          throws Exception
55      {
56          super.setUp();
57  
58          repository = getTestFile( "target/update-test/repository" );
59  
60          workingDirectory = getTestFile( "target/update-test/working-directory" );
61  
62          assertionDirectory = getTestFile( "target/update-test/assertion-directory" );
63  
64          CvsScmTestUtils.initRepo( repository, workingDirectory, assertionDirectory );
65      }
66  
67      private String getModule()
68      {
69          return "test-repo/update";
70      }
71  
72      /**
73       * @todo merge into tck
74       */
75      public void testCvsUpdate()
76          throws Exception
77      {
78  
79          FileWriter writer = null;
80          try
81          {
82              if ( !isSystemCmd( CvsScmTestUtils.CVS_COMMAND_LINE ) )
83              {
84                  ScmTestCase.printSystemCmdUnavail( CvsScmTestUtils.CVS_COMMAND_LINE, getName() );
85                  return;
86              }
87  
88              ScmManager scmManager = getScmManager();
89  
90              String scmUrl = CvsScmTestUtils.getScmUrl( repository, getModule() );
91  
92              // Check out the repo to a working directory where files will be modified and committed
93              String arguments =
94                  "-f -d " + repository.getAbsolutePath() + " " + "co -d " + workingDirectory.getName() + " "
95                      + getModule();
96  
97              CvsScmTestUtils.executeCVS( workingDirectory.getParentFile(), arguments );
98  
99              // Check out the repo to a assertion directory where the command will be used
100             arguments = "-f -d " + repository.getAbsolutePath() + " " + "co -d " + assertionDirectory.getName() + " "
101                 + getModule();
102 
103             CvsScmTestUtils.executeCVS( assertionDirectory.getParentFile(), arguments );
104 
105             // A new check out should return 0 updated files.
106             ScmRepository scmRepository = scmManager.makeScmRepository( scmUrl );
107 
108             UpdateScmResult result = scmManager.update( scmRepository, new ScmFileSet( assertionDirectory ) );
109 
110             assertNotNull( result );
111 
112             if ( !result.isSuccess() )
113             {
114                 System.out.println( "result.providerMessage: " + result.getProviderMessage() );
115 
116                 System.out.println( "result.commandOutput: " + result.getCommandOutput() );
117 
118                 fail( "Command failed" );
119             }
120 
121             assertNull( result.getProviderMessage() );
122 
123             assertNull( result.getCommandOutput() );
124 
125             assertNotNull( result.getUpdatedFiles() );
126 
127             assertEquals( 0, result.getUpdatedFiles().size() );
128 
129             // Modifing a file
130             File fooJava = new File( workingDirectory, "Foo.java" );
131 
132             String content = FileUtils.fileRead( fooJava );
133 
134             writer = new FileWriter( fooJava );
135 
136             writer.write( content + System.getProperty( "line.separator" ) );
137             writer.write( "extra line" );
138 
139             writer.close();
140 
141             // Adding a new file
142             writer = new FileWriter( new File( workingDirectory, "New.txt" ) );
143 
144             writer.write( "new file" );
145 
146             writer.close();
147 
148             arguments = "-f -d " + repository.getAbsolutePath() + " add New.txt";
149 
150             CvsScmTestUtils.executeCVS( workingDirectory, arguments );
151 
152             // Committing
153             arguments = "-f -d " + repository.getAbsolutePath() + " commit -m .";
154 
155             CvsScmTestUtils.executeCVS( workingDirectory, arguments );
156 
157             // Check the updated files
158             result = scmManager.update( scmRepository, new ScmFileSet( assertionDirectory ) );
159 
160             assertNotNull( result );
161 
162             if ( !result.isSuccess() )
163             {
164                 System.out.println( "result.providerMessage: " + result.getProviderMessage() );
165 
166                 System.out.println( "result.commandOutput: " + result.getCommandOutput() );
167 
168                 fail( "Command failed" );
169             }
170 
171             assertNull( result.getProviderMessage() );
172 
173             assertNull( result.getCommandOutput() );
174 
175             assertNotNull( result.getUpdatedFiles() );
176 
177             assertEquals( 2, result.getUpdatedFiles().size() );
178 
179             ScmFile file1 = result.getUpdatedFiles().get( 0 );
180 
181             assertPath( "Foo.java", file1.getPath() );
182 
183             assertEquals( ScmFileStatus.UPDATED, file1.getStatus() );
184 
185             ScmFile file2 = result.getUpdatedFiles().get( 1 );
186 
187             assertPath( "New.txt", file2.getPath() );
188 
189             assertEquals( ScmFileStatus.UPDATED, file2.getStatus() );
190         }
191         finally
192         {
193             IOUtil.close( writer );
194         }
195     }
196 }