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