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.provider.local.command.update;
020
021import java.io.File;
022import java.io.FileReader;
023import java.io.Reader;
024import java.util.Date;
025import java.util.Iterator;
026import java.util.List;
027import java.util.TreeSet;
028
029import org.apache.maven.scm.ScmFile;
030import org.apache.maven.scm.ScmFileSet;
031import org.apache.maven.scm.ScmTestCase;
032import org.apache.maven.scm.command.update.UpdateScmResult;
033import org.apache.maven.scm.manager.ScmManager;
034import org.apache.maven.scm.provider.local.metadata.LocalScmMetadata;
035import org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader;
036import org.apache.maven.scm.repository.ScmRepository;
037import org.apache.maven.scm.tck.command.update.UpdateCommandTckTest;
038import org.codehaus.plexus.util.FileUtils;
039import org.codehaus.plexus.util.IOUtil;
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 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
049 *
050 */
051public class LocalUpdateCommandTckTest extends UpdateCommandTckTest {
052    private static final String moduleName = "update-tck";
053
054    public String getScmUrl() throws Exception {
055        return "scm:local|" + getRepositoryRoot() + "|" + moduleName;
056    }
057
058    public void initRepo() throws Exception {
059        makeRepo(getRepositoryRoot());
060    }
061
062    /**
063     * Tests that a file that has been deleted from repository after checkout will be removed by scm-local. Local
064     * additions must not be deleted.
065     */
066    @Test
067    public void testDeletion() throws Exception {
068        FileUtils.deleteDirectory(getUpdatingCopy());
069
070        ScmRepository repository = makeScmRepository(getScmUrl());
071
072        checkOut(getUpdatingCopy(), repository);
073
074        // Check preconditions
075        File readmeFileLocal = new File(getUpdatingCopy(), "readme.txt");
076        assertTrue(readmeFileLocal.exists());
077        File newFileLocal = new File(getUpdatingCopy(), "newfile.xml");
078        assertFalse(newFileLocal.exists());
079
080        // Delete readme.txt from repository
081        File readmeFileRepo = new File(getRepositoryRoot(), moduleName + "/readme.txt");
082        assertTrue(readmeFileRepo.exists());
083        assertTrue("Could not delete", readmeFileRepo.delete());
084        assertFalse(readmeFileRepo.exists());
085
086        // Make local addition to updating copy - this one must not be touched
087        ScmTestCase.makeFile(getUpdatingCopy(), "newfile.xml", "added newfile.xml locally");
088        assertTrue(newFileLocal.exists());
089
090        // ----------------------------------------------------------------------
091        // Update the project
092        // ----------------------------------------------------------------------
093
094        ScmManager scmManager = getScmManager();
095        Date lastUpdate = new Date(System.currentTimeMillis());
096        Thread.sleep(1000);
097        UpdateScmResult result = scmManager.update(repository, new ScmFileSet(getUpdatingCopy()), lastUpdate);
098
099        assertNotNull("The command returned a null result.", result);
100
101        assertResultIsSuccess(result);
102
103        List<ScmFile> updatedFiles = result.getUpdatedFiles();
104
105        assertEquals("Expected 1 files in the updated files list " + updatedFiles, 1, updatedFiles.size());
106
107        // ----------------------------------------------------------------------
108        // Assert the files in the updated files list
109        // ----------------------------------------------------------------------
110
111        Iterator<ScmFile> files = new TreeSet<ScmFile>(updatedFiles).iterator();
112
113        // readme.txt
114        ScmFile file = (ScmFile) files.next();
115        assertPath("/readme.txt", file.getPath());
116        assertTrue(file.getStatus().isUpdate());
117
118        // ----------------------------------------------------------------------
119        // Assert working directory contents
120        // ----------------------------------------------------------------------
121
122        // readme.txt
123        assertFalse("Expected local copy of readme.txt to be deleted", readmeFileLocal.exists());
124
125        // newfile.xml
126        assertTrue("Expected local copy of newfile.xml NOT to be deleted", newFileLocal.exists());
127
128        // ----------------------------------------------------------------------
129        // Assert metadata file
130        // ----------------------------------------------------------------------
131        File metadataFile = new File(getUpdatingCopy(), ".maven-scm-local");
132        assertTrue("Expected metadata file .maven-scm-local does not exist", metadataFile.exists());
133        Reader reader = new FileReader(metadataFile);
134        LocalScmMetadata metadata;
135        try {
136            metadata = new LocalScmMetadataXpp3Reader().read(reader);
137        } finally {
138            IOUtil.close(reader);
139        }
140        File root = new File(getRepositoryRoot() + "/" + moduleName);
141        @SuppressWarnings("unchecked")
142        List<String> fileNames = FileUtils.getFileNames(root, "**", null, false);
143        assertEquals(fileNames, metadata.getRepositoryFileNames());
144    }
145
146    private void makeRepo(File workingDirectory) throws Exception {
147        makeFile(workingDirectory, moduleName + "/pom.xml", "/pom.xml");
148
149        makeFile(workingDirectory, moduleName + "/readme.txt", "/readme.txt");
150
151        makeFile(workingDirectory, moduleName + "/src/main/java/Application.java", "/src/main/java/Application.java");
152
153        makeFile(workingDirectory, moduleName + "/src/test/java/Test.java", "/src/test/java/Test.java");
154
155        makeDirectory(workingDirectory, moduleName + "/src/test/resources");
156    }
157}