View Javadoc
1   package org.apache.maven.scm.tck.command.checkin;
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.ScmTckTestCase;
26  import org.apache.maven.scm.command.add.AddScmResult;
27  import org.apache.maven.scm.command.checkin.CheckInScmResult;
28  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
29  import org.codehaus.plexus.util.FileUtils;
30  import org.codehaus.plexus.util.IOUtil;
31  
32  import java.io.File;
33  import java.io.FileWriter;
34  import java.io.PrintWriter;
35  import java.util.List;
36  import java.util.Map;
37  
38  /**
39   * This test tests the check out command.
40   *
41   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
42   *
43   */
44  public abstract class CheckInCommandTckTest
45      extends ScmTckTestCase
46  {
47      public void testCheckInCommandTest()
48          throws Exception
49      {
50          // Make sure that the correct files was checked out
51          File fooJava = new File( getWorkingCopy(), "src/main/java/Foo.java" );
52  
53          File barJava = new File( getWorkingCopy(), "src/main/java/Bar.java" );
54  
55          File readmeTxt = new File( getWorkingCopy(), "readme.txt" );
56  
57          assertFalse( "check Foo.java doesn't yet exist", fooJava.canRead() );
58  
59          assertFalse( "check Bar.java doesn't yet exist", barJava.canRead() );
60  
61          assertTrue( "check can read readme.txt", readmeTxt.canRead() );
62  
63          // Change the files
64          createFooJava( fooJava );
65  
66          createBarJava( barJava );
67  
68          changeReadmeTxt( readmeTxt );
69  
70          AddScmResult addResult = getScmManager().add( getScmRepository(),
71                                                        new ScmFileSet( getWorkingCopy(), "src/main/java/Foo.java",
72                                                                        null ) );
73  
74          assertResultIsSuccess( addResult );
75  
76          CheckInScmResult result =
77              getScmManager().checkIn( getScmRepository(), new ScmFileSet( getWorkingCopy() ), "Commit message" );
78  
79          assertResultIsSuccess( result );
80  
81          List<ScmFile> files = result.getCheckedInFiles();
82  
83          assertNotNull( files );
84  
85          assertEquals( 2, files.size() );
86  
87          Map<String, ScmFile> fileMap = mapFilesByPath( files );
88          ScmFile file1 = fileMap.get( "src/main/java/Foo.java" );
89          assertNotNull( file1 );
90          assertEquals( ScmFileStatus.CHECKED_IN, file1.getStatus() );
91  
92          ScmFile file2 = fileMap.get( "readme.txt" );
93          assertNotNull( file2 );
94          assertEquals( ScmFileStatus.CHECKED_IN, file2.getStatus() );
95  
96          CheckOutScmResult checkoutResult =
97              getScmManager().checkOut( getScmRepository(), new ScmFileSet( getAssertionCopy() ) );
98  
99          assertResultIsSuccess( checkoutResult );
100 
101         fooJava = new File( getAssertionCopy(), "src/main/java/Foo.java" );
102 
103         barJava = new File( getAssertionCopy(), "src/main/java/Bar.java" );
104 
105         readmeTxt = new File( getAssertionCopy(), "readme.txt" );
106 
107         assertTrue( "check can read Foo.java", fooJava.canRead() );
108 
109         assertFalse( "check Bar.java doesn't exist", barJava.canRead() );
110 
111         assertTrue( "check can read readme.txt", readmeTxt.canRead() );
112 
113         assertEquals( "check readme.txt contents", "changed file", FileUtils.fileRead( readmeTxt ) );
114     }
115 
116     public void testCheckInCommandPartialFileset()
117         throws Exception
118     {
119         // Make sure that the correct files was checked out
120         File fooJava = new File( getWorkingCopy(), "src/main/java/Foo.java" );
121 
122         File barJava = new File( getWorkingCopy(), "src/main/java/Bar.java" );
123 
124         File readmeTxt = new File( getWorkingCopy(), "readme.txt" );
125 
126         assertFalse( "check Foo.java doesn't yet exist", fooJava.canRead() );
127 
128         assertFalse( "check Bar.java doesn't yet exist", barJava.canRead() );
129 
130         assertTrue( "check can read readme.txt", readmeTxt.canRead() );
131 
132         // Change the files
133         createFooJava( fooJava );
134 
135         createBarJava( barJava );
136 
137         changeReadmeTxt( readmeTxt );
138 
139         AddScmResult addResult = getScmManager().getProviderByUrl( getScmUrl() ).add( getScmRepository(),
140                                                                                       new ScmFileSet( getWorkingCopy(),
141                                                                                                       "src/main/java/Foo.java",
142                                                                                                       null ) );
143 
144         assertResultIsSuccess( addResult );
145 
146         CheckInScmResult result =
147             getScmManager().checkIn( getScmRepository(), new ScmFileSet( getWorkingCopy(), "**/Foo.java", null ),
148                                      "Commit message" );
149 
150         assertResultIsSuccess( result );
151 
152         List<ScmFile> files = result.getCheckedInFiles();
153 
154         assertNotNull( files );
155 
156         assertEquals( 1, files.size() );
157 
158         ScmFile file1 = files.get( 0 );
159 
160         assertEquals( ScmFileStatus.CHECKED_IN, file1.getStatus() );
161 
162         assertPath( "/test-repo/check-in/Foo.java", file1.getPath() );
163 
164         CheckOutScmResult checkoutResult =
165             getScmManager().checkOut( getScmRepository(), new ScmFileSet( getAssertionCopy() ) );
166 
167         assertResultIsSuccess( checkoutResult );
168 
169         fooJava = new File( getAssertionCopy(), "src/main/java/Foo.java" );
170 
171         barJava = new File( getAssertionCopy(), "src/main/java/Bar.java" );
172 
173         readmeTxt = new File( getAssertionCopy(), "readme.txt" );
174 
175         assertTrue( "check can read Foo.java", fooJava.canRead() );
176 
177         assertFalse( "check Bar.java doesn't exist", barJava.canRead() );
178 
179         assertTrue( "check can read readme.txt", readmeTxt.canRead() );
180 
181         assertEquals( "check readme.txt contents", "/readme.txt", FileUtils.fileRead( readmeTxt ) );
182     }
183 
184     private void createFooJava( File fooJava )
185         throws Exception
186     {
187         FileWriter output = new FileWriter( fooJava );
188 
189         PrintWriter printer = new PrintWriter( output );
190         try
191         {
192             printer.println( "public class Foo" );
193             printer.println( "{" );
194 
195             printer.println( "    public void foo()" );
196             printer.println( "    {" );
197             printer.println( "        int i = 10;" );
198             printer.println( "    }" );
199 
200             printer.println( "}" );
201         }
202         finally
203         {
204             IOUtil.close( output );
205             IOUtil.close( printer );
206         }
207     }
208 
209     private void createBarJava( File barJava )
210         throws Exception
211     {
212         FileWriter output = new FileWriter( barJava );
213 
214         PrintWriter printer = new PrintWriter( output );
215 
216         printer.println( "public class Bar" );
217         printer.println( "{" );
218 
219         printer.println( "    public int bar()" );
220         printer.println( "    {" );
221         printer.println( "        return 20;" );
222         printer.println( "    }" );
223 
224         printer.println( "}" );
225 
226         printer.close();
227 
228         output.close();
229     }
230 
231     private void changeReadmeTxt( File readmeTxt )
232         throws Exception
233     {
234         FileWriter output = new FileWriter( readmeTxt );
235 
236         output.write( "changed file" );
237 
238         output.close();
239     }
240 }