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