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.CommandParameter;
028import org.apache.maven.scm.CommandParameters;
029import org.apache.maven.scm.ScmFile;
030import org.apache.maven.scm.ScmFileSet;
031import org.apache.maven.scm.ScmFileStatus;
032import org.apache.maven.scm.ScmTckTestCase;
033import org.apache.maven.scm.command.add.AddScmResult;
034import org.apache.maven.scm.command.checkin.CheckInScmResult;
035import org.apache.maven.scm.command.checkout.CheckOutScmResult;
036import org.apache.maven.scm.provider.ScmProvider;
037import org.apache.maven.scm.util.FilenameUtils;
038import org.codehaus.plexus.util.FileUtils;
039import org.junit.Assume;
040import org.junit.Test;
041
042import static org.junit.Assert.assertEquals;
043import static org.junit.Assert.assertFalse;
044import static org.junit.Assert.assertNotNull;
045import static org.junit.Assert.assertTrue;
046
047/**
048 * This test tests the check out command.
049 *
050 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
051 */
052public abstract class CheckInCommandTckTest extends ScmTckTestCase {
053    @Test
054    public void testCheckInCommandTest() throws Exception {
055        // Make sure that the correct files was checked out
056        File fooJava = new File(getWorkingCopy(), "src/main/java/Foo.java");
057
058        File barJava = new File(getWorkingCopy(), "src/main/java/Bar.java");
059
060        File readmeTxt = new File(getWorkingCopy(), "readme.txt");
061
062        assertFalse("check Foo.java doesn't yet exist", fooJava.canRead());
063
064        assertFalse("check Bar.java doesn't yet exist", barJava.canRead());
065
066        assertTrue("check can read readme.txt", readmeTxt.canRead());
067
068        // Change the files
069        createFooJava(fooJava);
070
071        createBarJava(barJava);
072
073        changeReadmeTxt(readmeTxt);
074
075        AddScmResult addResult = getScmManager()
076                .add(getScmRepository(), new ScmFileSet(getWorkingCopy(), "src/main/java/Foo.java", null));
077
078        assertResultIsSuccess(addResult);
079
080        List<ScmFile> files = addResult.getAddedFiles();
081        assertNotNull(files);
082        assertEquals(1, files.size());
083        // SCM-998: filename separators not yet harmonized
084        assertEquals(
085                "src/main/java/Foo.java",
086                FilenameUtils.normalizeFilename(files.get(0).getPath()));
087
088        CommandParameters commandParameters = new CommandParameters();
089        commandParameters.setString(CommandParameter.MESSAGE, "Commit message");
090        commandParameters.setString(CommandParameter.SCM_COMMIT_SIGN, "false");
091
092        CheckInScmResult result =
093                getScmManager().checkIn(getScmRepository(), new ScmFileSet(getWorkingCopy()), commandParameters);
094
095        assertResultIsSuccess(result);
096
097        files = result.getCheckedInFiles();
098        assertNotNull(files);
099        assertEquals(2, files.size());
100
101        Map<String, ScmFile> fileMap = mapFilesByPath(files);
102        ScmFile file1 = fileMap.get("src/main/java/Foo.java");
103        assertNotNull(file1);
104        assertEquals(ScmFileStatus.CHECKED_IN, file1.getStatus());
105
106        ScmFile file2 = fileMap.get("readme.txt");
107        assertNotNull(file2);
108        assertEquals(ScmFileStatus.CHECKED_IN, file2.getStatus());
109
110        CheckOutScmResult checkoutResult =
111                getScmManager().checkOut(getScmRepository(), new ScmFileSet(getAssertionCopy()));
112
113        assertResultIsSuccess(checkoutResult);
114
115        fooJava = new File(getAssertionCopy(), "src/main/java/Foo.java");
116
117        barJava = new File(getAssertionCopy(), "src/main/java/Bar.java");
118
119        readmeTxt = new File(getAssertionCopy(), "readme.txt");
120
121        assertTrue("check can read Foo.java", fooJava.canRead());
122
123        assertFalse("check Bar.java doesn't exist", barJava.canRead());
124
125        assertTrue("check can read readme.txt", readmeTxt.canRead());
126
127        assertEquals("check readme.txt contents", "changed file", FileUtils.fileRead(readmeTxt));
128    }
129
130    @Test
131    public void testCheckInCommandPartialFileset() throws Exception {
132        // Make sure that the correct files was checked out
133        File fooJava = new File(getWorkingCopy(), "src/main/java/Foo.java");
134
135        File barJava = new File(getWorkingCopy(), "src/main/java/Bar.java");
136
137        File readmeTxt = new File(getWorkingCopy(), "readme.txt");
138
139        assertFalse("check Foo.java doesn't yet exist", fooJava.canRead());
140
141        assertFalse("check Bar.java doesn't yet exist", barJava.canRead());
142
143        assertTrue("check can read readme.txt", readmeTxt.canRead());
144
145        // Change the files
146        createFooJava(fooJava);
147
148        createBarJava(barJava);
149
150        changeReadmeTxt(readmeTxt);
151
152        AddScmResult addResult = getScmManager()
153                .getProviderByUrl(getScmUrl())
154                .add(getScmRepository(), new ScmFileSet(getWorkingCopy(), "src/main/java/Foo.java", null));
155
156        assertResultIsSuccess(addResult);
157
158        CommandParameters commandParameters = new CommandParameters();
159        commandParameters.setString(CommandParameter.MESSAGE, "Commit message");
160        commandParameters.setString(CommandParameter.SCM_COMMIT_SIGN, "false");
161
162        CheckInScmResult result = getScmManager()
163                .checkIn(getScmRepository(), new ScmFileSet(getWorkingCopy(), "**/Foo.java", null), commandParameters);
164
165        assertResultIsSuccess(result);
166
167        List<ScmFile> files = result.getCheckedInFiles();
168        assertNotNull(files);
169        assertEquals(1, files.size());
170
171        ScmFile file1 = files.get(0);
172        assertEquals(ScmFileStatus.CHECKED_IN, file1.getStatus());
173        assertPath("src/main/java/Foo.java", file1.getPath());
174
175        CheckOutScmResult checkoutResult =
176                getScmManager().checkOut(getScmRepository(), new ScmFileSet(getAssertionCopy()));
177
178        assertResultIsSuccess(checkoutResult);
179
180        fooJava = new File(getAssertionCopy(), "src/main/java/Foo.java");
181
182        barJava = new File(getAssertionCopy(), "src/main/java/Bar.java");
183
184        readmeTxt = new File(getAssertionCopy(), "readme.txt");
185
186        assertTrue("check can read Foo.java", fooJava.canRead());
187
188        assertFalse("check Bar.java doesn't exist", barJava.canRead());
189
190        assertTrue("check can read readme.txt", readmeTxt.canRead());
191
192        assertEquals("check readme.txt contents", "/readme.txt", FileUtils.fileRead(readmeTxt));
193    }
194
195    @Test
196    public void testCheckInCommandFilesetWithBasedirOtherThanWorkingCopyRoot() throws Exception {
197        ScmProvider scmProvider = getScmManager().getProviderByUrl(getScmUrl());
198
199        Assume.assumeFalse(
200                "Local provider does not properly support basedir",
201                scmProvider.getScmType().equals("local"));
202        // Make sure that the correct files was checked out
203        File fooJava = new File(getWorkingCopy(), "src/main/java/Foo.java");
204
205        assertFalse("check Foo.java doesn't yet exist", fooJava.canRead());
206
207        // Change the files
208        createFooJava(fooJava);
209
210        AddScmResult addResult = scmProvider.add(
211                getScmRepository(), new ScmFileSet(new File(getWorkingCopy(), "src"), "main/java/Foo.java", null));
212
213        assertResultIsSuccess(addResult);
214
215        List<ScmFile> files = addResult.getAddedFiles();
216        assertNotNull(files);
217        assertEquals(1, files.size());
218        // SCM-998: filename separators not yet harmonized
219        assertEquals(
220                "main/java/Foo.java",
221                FilenameUtils.normalizeFilename(files.get(0).getPath()));
222
223        CommandParameters commandParameters = new CommandParameters();
224        commandParameters.setString(CommandParameter.MESSAGE, "Commit message");
225        commandParameters.setString(CommandParameter.SCM_COMMIT_SIGN, "false");
226
227        CheckInScmResult result = getScmManager()
228                .checkIn(
229                        getScmRepository(),
230                        new ScmFileSet(new File(getWorkingCopy(), "src"), "**/Foo.java", null),
231                        commandParameters);
232
233        assertResultIsSuccess(result);
234
235        files = result.getCheckedInFiles();
236        assertNotNull(files);
237        assertEquals(1, files.size());
238
239        ScmFile file1 = files.get(0);
240        assertEquals(ScmFileStatus.CHECKED_IN, file1.getStatus());
241        assertPath("main/java/Foo.java", file1.getPath());
242
243        CheckOutScmResult checkoutResult =
244                getScmManager().checkOut(getScmRepository(), new ScmFileSet(getAssertionCopy()));
245
246        assertResultIsSuccess(checkoutResult);
247
248        fooJava = new File(getAssertionCopy(), "src/main/java/Foo.java");
249
250        assertTrue("check can read Foo.java", fooJava.canRead());
251    }
252
253    private void createFooJava(File fooJava) throws Exception {
254        try (PrintWriter printer = new PrintWriter(new FileWriter(fooJava))) {
255            printer.println("public class Foo");
256            printer.println("{");
257
258            printer.println("    public void foo()");
259            printer.println("    {");
260            printer.println("        int i = 10;");
261            printer.println("    }");
262
263            printer.println("}");
264        }
265    }
266
267    private void createBarJava(File barJava) throws Exception {
268        try (PrintWriter printer = new PrintWriter(new FileWriter(barJava))) {
269            printer.println("public class Bar");
270            printer.println("{");
271
272            printer.println("    public int bar()");
273            printer.println("    {");
274            printer.println("        return 20;");
275            printer.println("    }");
276
277            printer.println("}");
278        }
279    }
280
281    private void changeReadmeTxt(File readmeTxt) throws Exception {
282        try (FileWriter output = new FileWriter(readmeTxt)) {
283            output.write("changed file");
284        }
285    }
286}