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