001    package org.apache.maven.scm.tck.command.diff;
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    
022    import org.apache.maven.scm.ScmFile;
023    import org.apache.maven.scm.ScmFileSet;
024    import org.apache.maven.scm.ScmTckTestCase;
025    import org.apache.maven.scm.ScmTestCase;
026    import org.apache.maven.scm.ScmVersion;
027    import org.apache.maven.scm.command.diff.DiffScmResult;
028    import org.apache.maven.scm.provider.ScmProvider;
029    import org.apache.maven.scm.repository.ScmRepository;
030    
031    import java.io.File;
032    import java.util.Iterator;
033    import java.util.List;
034    import java.util.Map;
035    import java.util.TreeSet;
036    
037    /**
038     * This test tests the diff command.
039     *
040     * @author <a href="mailto:brett@apache.org">Brett Porter</a>
041     * @version $Id: DiffCommandTckTest.java 1241523 2012-02-07 17:08:26Z hboutemy $
042     */
043    public abstract class DiffCommandTckTest
044        extends ScmTckTestCase
045    {
046    
047        public void testDiffCommand()
048            throws Exception
049        {
050            ScmRepository repository = getScmRepository();
051    
052            // ----------------------------------------------------------------------
053            // Change the files
054            // ----------------------------------------------------------------------
055    
056            //
057            // readme.txt is changed (changed file in the root directory)
058            // project.xml is added (added file in the root directory)
059            // src/test/resources is untouched (a empty directory is left untouched)
060            // src/test/java is untouched (a non empty directory is left untouched)
061            // src/test/java/org (a empty directory is added)
062            // src/main/java/org/Foo.java (a non empty directory is added)
063            //
064    
065            // /readme.txt
066            ScmTestCase.makeFile( getWorkingCopy(), "/readme.txt", "changed readme.txt" );
067    
068            // /project.xml
069            ScmTestCase.makeFile( getWorkingCopy(), "/project.xml", "changed project.xml" );
070    
071            addToWorkingTree( getWorkingCopy(), new File( "project.xml" ), repository );
072    
073            // /src/test/java/org
074            ScmTestCase.makeDirectory( getWorkingCopy(), "/src/test/java/org" );
075    
076            addToWorkingTree( getWorkingCopy(), new File( "src/test/java/org" ), repository );
077    
078            // /src/main/java/org/Foo.java
079            ScmTestCase.makeFile( getWorkingCopy(), "/src/main/java/org/Foo.java" );
080    
081            addToWorkingTree( getWorkingCopy(), new File( "src/main/java/org" ), repository );
082    
083            // src/main/java/org/Foo.java
084            addToWorkingTree( getWorkingCopy(), new File( "src/main/java/org/Foo.java" ), repository );
085    
086            // ----------------------------------------------------------------------
087            // Diff the project
088            // ----------------------------------------------------------------------
089    
090            ScmProvider provider = getScmManager().getProviderByUrl( getScmUrl() );
091            ScmFileSet fileSet = new ScmFileSet( getWorkingCopy() );
092            DiffScmResult result = provider.diff( repository, fileSet, null, (ScmVersion) null );
093    
094            assertNotNull( "The command returned a null result.", result );
095    
096            assertResultIsSuccess( result );
097    
098            List<ScmFile> changedFiles = result.getChangedFiles();
099    
100            Map<String, CharSequence> differences = result.getDifferences();
101    
102            assertEquals( "Expected 3 files in the changed files list " + changedFiles, 3, changedFiles.size() );
103    
104            assertEquals( "Expected 3 files in the differences list " + differences, 3, differences.size() );
105    
106            // ----------------------------------------------------------------------
107            // Assert the files in the changed files list
108            // ----------------------------------------------------------------------
109    
110            Iterator<ScmFile> files = new TreeSet<ScmFile>( changedFiles ).iterator();
111    
112            //Check Foo.java
113            ScmFile file = files.next();
114    
115            assertPath( "/src/main/java/org/Foo.java", file.getPath() );
116    
117            assertTrue( file.getStatus().isDiff() );
118    
119            String postRangeStr = "+/src/main/java/org/Foo.java\n\\ No newline at end of file\n";
120            String actualStr = differences.get( file.getPath() ).toString();
121            assertTrue( actualStr.endsWith( postRangeStr ) );
122    
123            //Check readme.txt
124            file = files.next();
125    
126            assertPath( "/readme.txt", file.getPath() );
127    
128            assertTrue( file.getStatus().isDiff() );
129    
130            postRangeStr =
131                "-/readme.txt\n\\ No newline at end of file\n+changed readme.txt\n\\ No newline at end of file\n";
132            actualStr = differences.get( file.getPath() ).toString();
133            assertTrue( actualStr.endsWith( postRangeStr ) );
134    
135            //Check project.xml
136            file = files.next();
137    
138            assertPath( "/project.xml", file.getPath() );
139    
140            postRangeStr = "+changed project.xml\n\\ No newline at end of file\n";
141            actualStr = differences.get( file.getPath() ).toString();
142            assertTrue( actualStr.endsWith( postRangeStr ) );
143    
144            assertTrue( file.getStatus().isDiff() );
145        }
146    }