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