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 */
043public abstract class CheckInCommandTckTest
044    extends ScmTckTestCase
045{
046    public void testCheckInCommandTest()
047        throws Exception
048    {
049        // Make sure that the correct files was checked out
050        File fooJava = new File( getWorkingCopy(), "src/main/java/Foo.java" );
051
052        File barJava = new File( getWorkingCopy(), "src/main/java/Bar.java" );
053
054        File readmeTxt = new File( getWorkingCopy(), "readme.txt" );
055
056        assertFalse( "check Foo.java doesn't yet exist", fooJava.canRead() );
057
058        assertFalse( "check Bar.java doesn't yet exist", barJava.canRead() );
059
060        assertTrue( "check can read readme.txt", readmeTxt.canRead() );
061
062        // Change the files
063        createFooJava( fooJava );
064
065        createBarJava( barJava );
066
067        changeReadmeTxt( readmeTxt );
068
069        AddScmResult addResult = getScmManager().add( getScmRepository(),
070                                                      new ScmFileSet( getWorkingCopy(), "src/main/java/Foo.java",
071                                                                      null ) );
072
073        assertResultIsSuccess( addResult );
074
075        CheckInScmResult result =
076            getScmManager().checkIn( getScmRepository(), new ScmFileSet( getWorkingCopy() ), "Commit message" );
077
078        assertResultIsSuccess( result );
079
080        List<ScmFile> files = result.getCheckedInFiles();
081
082        assertNotNull( files );
083
084        assertEquals( 2, files.size() );
085
086        Map<String, ScmFile> fileMap = mapFilesByPath( files );
087        ScmFile file1 = fileMap.get( "src/main/java/Foo.java" );
088        assertNotNull( file1 );
089        assertEquals( ScmFileStatus.CHECKED_IN, file1.getStatus() );
090
091        ScmFile file2 = fileMap.get( "readme.txt" );
092        assertNotNull( file2 );
093        assertEquals( ScmFileStatus.CHECKED_IN, file2.getStatus() );
094
095        CheckOutScmResult checkoutResult =
096            getScmManager().checkOut( getScmRepository(), new ScmFileSet( getAssertionCopy() ) );
097
098        assertResultIsSuccess( checkoutResult );
099
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}