001package org.apache.maven.scm.tck.command.checkin;
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.ScmTckTestCase;
026import org.apache.maven.scm.command.add.AddScmResult;
027import org.apache.maven.scm.command.checkin.CheckInScmResult;
028import org.apache.maven.scm.command.checkout.CheckOutScmResult;
029import org.codehaus.plexus.util.FileUtils;
030import org.codehaus.plexus.util.IOUtil;
031
032import java.io.File;
033import java.io.FileWriter;
034import java.io.PrintWriter;
035import java.util.List;
036import java.util.Map;
037
038/**
039 * This test tests the check out command.
040 *
041 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
042 *
043 */
044public abstract class CheckInCommandTckTest
045    extends ScmTckTestCase
046{
047    public void testCheckInCommandTest()
048        throws Exception
049    {
050        // Make sure that the correct files was checked out
051        File fooJava = new File( getWorkingCopy(), "src/main/java/Foo.java" );
052
053        File barJava = new File( getWorkingCopy(), "src/main/java/Bar.java" );
054
055        File readmeTxt = new File( getWorkingCopy(), "readme.txt" );
056
057        assertFalse( "check Foo.java doesn't yet exist", fooJava.canRead() );
058
059        assertFalse( "check Bar.java doesn't yet exist", barJava.canRead() );
060
061        assertTrue( "check can read readme.txt", readmeTxt.canRead() );
062
063        // Change the files
064        createFooJava( fooJava );
065
066        createBarJava( barJava );
067
068        changeReadmeTxt( readmeTxt );
069
070        AddScmResult addResult = getScmManager().add( getScmRepository(),
071                                                      new ScmFileSet( getWorkingCopy(), "src/main/java/Foo.java",
072                                                                      null ) );
073
074        assertResultIsSuccess( addResult );
075
076        CheckInScmResult result =
077            getScmManager().checkIn( getScmRepository(), new ScmFileSet( getWorkingCopy() ), "Commit message" );
078
079        assertResultIsSuccess( result );
080
081        List<ScmFile> files = result.getCheckedInFiles();
082
083        assertNotNull( files );
084
085        assertEquals( 2, files.size() );
086
087        Map<String, ScmFile> fileMap = mapFilesByPath( files );
088        ScmFile file1 = fileMap.get( "src/main/java/Foo.java" );
089        assertNotNull( file1 );
090        assertEquals( ScmFileStatus.CHECKED_IN, file1.getStatus() );
091
092        ScmFile file2 = fileMap.get( "readme.txt" );
093        assertNotNull( file2 );
094        assertEquals( ScmFileStatus.CHECKED_IN, file2.getStatus() );
095
096        CheckOutScmResult checkoutResult =
097            getScmManager().checkOut( getScmRepository(), new ScmFileSet( getAssertionCopy() ) );
098
099        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}