001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.tck.command.checkin;
020
021import java.io.File;
022import java.io.FileWriter;
023import java.io.PrintWriter;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.maven.scm.ScmFile;
028import org.apache.maven.scm.ScmFileSet;
029import org.apache.maven.scm.ScmFileStatus;
030import org.apache.maven.scm.ScmTckTestCase;
031import org.apache.maven.scm.command.add.AddScmResult;
032import org.apache.maven.scm.command.checkin.CheckInScmResult;
033import org.apache.maven.scm.command.checkout.CheckOutScmResult;
034import org.apache.maven.scm.provider.ScmProvider;
035import org.apache.maven.scm.util.FilenameUtils;
036import org.codehaus.plexus.util.FileUtils;
037import org.codehaus.plexus.util.IOUtil;
038import org.junit.Assume;
039import org.junit.Test;
040
041import static org.junit.Assert.assertEquals;
042import static org.junit.Assert.assertFalse;
043import static org.junit.Assert.assertNotNull;
044import static org.junit.Assert.assertTrue;
045
046/**
047 * This test tests the check out command.
048 *
049 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
050 */
051public abstract class CheckInCommandTckTest extends ScmTckTestCase {
052    @Test
053    public void testCheckInCommandTest() throws Exception {
054        // Make sure that the correct files was checked out
055        File fooJava = new File(getWorkingCopy(), "src/main/java/Foo.java");
056
057        File barJava = new File(getWorkingCopy(), "src/main/java/Bar.java");
058
059        File readmeTxt = new File(getWorkingCopy(), "readme.txt");
060
061        assertFalse("check Foo.java doesn't yet exist", fooJava.canRead());
062
063        assertFalse("check Bar.java doesn't yet exist", barJava.canRead());
064
065        assertTrue("check can read readme.txt", readmeTxt.canRead());
066
067        // Change the files
068        createFooJava(fooJava);
069
070        createBarJava(barJava);
071
072        changeReadmeTxt(readmeTxt);
073
074        AddScmResult addResult = getScmManager()
075                .add(getScmRepository(), new ScmFileSet(getWorkingCopy(), "src/main/java/Foo.java", null));
076
077        assertResultIsSuccess(addResult);
078
079        List<ScmFile> files = addResult.getAddedFiles();
080        assertNotNull(files);
081        assertEquals(1, files.size());
082        // SCM-998: filename separators not yet harmonized
083        assertEquals(
084                "src/main/java/Foo.java",
085                FilenameUtils.normalizeFilename(files.get(0).getPath()));
086
087        CheckInScmResult result =
088                getScmManager().checkIn(getScmRepository(), new ScmFileSet(getWorkingCopy()), "Commit message");
089
090        assertResultIsSuccess(result);
091
092        files = result.getCheckedInFiles();
093        assertNotNull(files);
094        assertEquals(2, files.size());
095
096        Map<String, ScmFile> fileMap = mapFilesByPath(files);
097        ScmFile file1 = fileMap.get("src/main/java/Foo.java");
098        assertNotNull(file1);
099        assertEquals(ScmFileStatus.CHECKED_IN, file1.getStatus());
100
101        ScmFile file2 = fileMap.get("readme.txt");
102        assertNotNull(file2);
103        assertEquals(ScmFileStatus.CHECKED_IN, file2.getStatus());
104
105        CheckOutScmResult checkoutResult =
106                getScmManager().checkOut(getScmRepository(), new ScmFileSet(getAssertionCopy()));
107
108        assertResultIsSuccess(checkoutResult);
109
110        fooJava = new File(getAssertionCopy(), "src/main/java/Foo.java");
111
112        barJava = new File(getAssertionCopy(), "src/main/java/Bar.java");
113
114        readmeTxt = new File(getAssertionCopy(), "readme.txt");
115
116        assertTrue("check can read Foo.java", fooJava.canRead());
117
118        assertFalse("check Bar.java doesn't exist", barJava.canRead());
119
120        assertTrue("check can read readme.txt", readmeTxt.canRead());
121
122        assertEquals("check readme.txt contents", "changed file", FileUtils.fileRead(readmeTxt));
123    }
124
125    @Test
126    public void testCheckInCommandPartialFileset() throws Exception {
127        // Make sure that the correct files was checked out
128        File fooJava = new File(getWorkingCopy(), "src/main/java/Foo.java");
129
130        File barJava = new File(getWorkingCopy(), "src/main/java/Bar.java");
131
132        File readmeTxt = new File(getWorkingCopy(), "readme.txt");
133
134        assertFalse("check Foo.java doesn't yet exist", fooJava.canRead());
135
136        assertFalse("check Bar.java doesn't yet exist", barJava.canRead());
137
138        assertTrue("check can read readme.txt", readmeTxt.canRead());
139
140        // Change the files
141        createFooJava(fooJava);
142
143        createBarJava(barJava);
144
145        changeReadmeTxt(readmeTxt);
146
147        AddScmResult addResult = getScmManager()
148                .getProviderByUrl(getScmUrl())
149                .add(getScmRepository(), new ScmFileSet(getWorkingCopy(), "src/main/java/Foo.java", null));
150
151        assertResultIsSuccess(addResult);
152
153        CheckInScmResult result = getScmManager()
154                .checkIn(getScmRepository(), new ScmFileSet(getWorkingCopy(), "**/Foo.java", null), "Commit message");
155
156        assertResultIsSuccess(result);
157
158        List<ScmFile> files = result.getCheckedInFiles();
159        assertNotNull(files);
160        assertEquals(1, files.size());
161
162        ScmFile file1 = files.get(0);
163        assertEquals(ScmFileStatus.CHECKED_IN, file1.getStatus());
164        assertPath("src/main/java/Foo.java", file1.getPath());
165
166        CheckOutScmResult checkoutResult =
167                getScmManager().checkOut(getScmRepository(), new ScmFileSet(getAssertionCopy()));
168
169        assertResultIsSuccess(checkoutResult);
170
171        fooJava = new File(getAssertionCopy(), "src/main/java/Foo.java");
172
173        barJava = new File(getAssertionCopy(), "src/main/java/Bar.java");
174
175        readmeTxt = new File(getAssertionCopy(), "readme.txt");
176
177        assertTrue("check can read Foo.java", fooJava.canRead());
178
179        assertFalse("check Bar.java doesn't exist", barJava.canRead());
180
181        assertTrue("check can read readme.txt", readmeTxt.canRead());
182
183        assertEquals("check readme.txt contents", "/readme.txt", FileUtils.fileRead(readmeTxt));
184    }
185
186    @Test
187    public void testCheckInCommandFilesetWithBasedirOtherThanWorkingCopyRoot() throws Exception {
188        ScmProvider scmProvider = getScmManager().getProviderByUrl(getScmUrl());
189
190        Assume.assumeFalse(
191                "Local provider does not properly support basedir",
192                scmProvider.getScmType().equals("local"));
193        // Make sure that the correct files was checked out
194        File fooJava = new File(getWorkingCopy(), "src/main/java/Foo.java");
195
196        assertFalse("check Foo.java doesn't yet exist", fooJava.canRead());
197
198        // Change the files
199        createFooJava(fooJava);
200
201        AddScmResult addResult = scmProvider.add(
202                getScmRepository(), new ScmFileSet(new File(getWorkingCopy(), "src"), "main/java/Foo.java", null));
203
204        assertResultIsSuccess(addResult);
205
206        List<ScmFile> files = addResult.getAddedFiles();
207        assertNotNull(files);
208        assertEquals(1, files.size());
209        // SCM-998: filename separators not yet harmonized
210        assertEquals(
211                "main/java/Foo.java",
212                FilenameUtils.normalizeFilename(files.get(0).getPath()));
213
214        CheckInScmResult result = getScmManager()
215                .checkIn(
216                        getScmRepository(),
217                        new ScmFileSet(new File(getWorkingCopy(), "src"), "**/Foo.java", null),
218                        "Commit message");
219
220        assertResultIsSuccess(result);
221
222        files = result.getCheckedInFiles();
223        assertNotNull(files);
224        assertEquals(1, files.size());
225
226        ScmFile file1 = files.get(0);
227        assertEquals(ScmFileStatus.CHECKED_IN, file1.getStatus());
228        assertPath("main/java/Foo.java", file1.getPath());
229
230        CheckOutScmResult checkoutResult =
231                getScmManager().checkOut(getScmRepository(), new ScmFileSet(getAssertionCopy()));
232
233        assertResultIsSuccess(checkoutResult);
234
235        fooJava = new File(getAssertionCopy(), "src/main/java/Foo.java");
236
237        assertTrue("check can read Foo.java", fooJava.canRead());
238    }
239
240    private void createFooJava(File fooJava) throws Exception {
241        FileWriter output = new FileWriter(fooJava);
242
243        PrintWriter printer = new PrintWriter(output);
244        try {
245            printer.println("public class Foo");
246            printer.println("{");
247
248            printer.println("    public void foo()");
249            printer.println("    {");
250            printer.println("        int i = 10;");
251            printer.println("    }");
252
253            printer.println("}");
254        } finally {
255            IOUtil.close(output);
256            IOUtil.close(printer);
257        }
258    }
259
260    private void createBarJava(File barJava) throws Exception {
261        FileWriter output = new FileWriter(barJava);
262
263        PrintWriter printer = new PrintWriter(output);
264
265        printer.println("public class Bar");
266        printer.println("{");
267
268        printer.println("    public int bar()");
269        printer.println("    {");
270        printer.println("        return 20;");
271        printer.println("    }");
272
273        printer.println("}");
274
275        printer.close();
276
277        output.close();
278    }
279
280    private void changeReadmeTxt(File readmeTxt) throws Exception {
281        FileWriter output = null;
282
283        try {
284            output = new FileWriter(readmeTxt);
285
286            output.write("changed file");
287        } finally {
288            IOUtil.close(output);
289        }
290    }
291}