001package org.apache.maven.scm.provider.hg.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 java.io.File;
023import java.util.Iterator;
024import java.util.List;
025import java.util.Map;
026import java.util.TreeSet;
027
028import org.apache.maven.scm.ScmFile;
029import org.apache.maven.scm.ScmFileSet;
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.provider.hg.HgRepoUtils;
035import org.apache.maven.scm.repository.ScmRepository;
036import org.apache.maven.scm.tck.command.diff.DiffCommandTckTest;
037
038public class HgDiffCommandTckTest
039    extends DiffCommandTckTest
040{
041    public String getScmUrl()
042        throws Exception
043    {
044        return HgRepoUtils.getScmUrl();
045    }
046
047    public void initRepo()
048        throws Exception
049    {
050        HgRepoUtils.initRepo();
051    }
052
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
068        // This following test has no meaning to mercurial as mercurial does not track
069        // empty directories, only the files contained within
070        // See: http://www.selenic.com/mercurial/wiki/index.cgi/FAQ
071        // src/test/java/org (a empty directory is added)
072
073        // src/main/java/org/Foo.java (a non empty directory is added)
074        //
075
076        // /readme.txt
077        ScmTestCase.makeFile( getWorkingCopy(), "/readme.txt", "changed readme.txt" );
078
079        // /project.xml
080        ScmTestCase.makeFile( getWorkingCopy(), "/project.xml", "changed project.xml" );
081
082        addToWorkingTree( getWorkingCopy(), new File( "project.xml" ), repository );
083
084        // /src/test/java/org
085//              ScmTestCase.makeDirectory(getWorkingCopy(), "/src/test/java/org");
086//
087//              addToWorkingTree(getWorkingCopy(), new File("src/test/java/org"),
088//                              repository);
089
090        // /src/main/java/org/Foo.java
091        ScmTestCase.makeFile( getWorkingCopy(), "/src/main/java/org/Foo.java" );
092
093//              addToWorkingTree(getWorkingCopy(), new File("src/main/java/org"),
094//                              repository);
095
096        // src/main/java/org/Foo.java
097        addToWorkingTree( getWorkingCopy(), new File( "src/main/java/org/Foo.java" ), repository );
098
099        // ----------------------------------------------------------------------
100        // Diff the project
101        // ----------------------------------------------------------------------
102
103        ScmProvider provider = getScmManager().getProviderByUrl( getScmUrl() );
104        ScmFileSet fileSet = new ScmFileSet( getWorkingCopy() );
105        DiffScmResult result = provider.diff( repository, fileSet, (ScmVersion) null, null );
106
107//              todo: check asserts
108//              assertNotNull("The command returned a null result.", result);
109
110//      assertResultIsSuccess(result);
111
112        List<ScmFile> changedFiles = result.getChangedFiles();
113
114        Map<String, CharSequence> differences = result.getDifferences();
115
116//              assertEquals("Expected 3 files in the changed files list "
117//                              + changedFiles, 3, changedFiles.size());
118
119//              assertEquals("Expected 3 files in the differences list " + differences,
120//                              3, differences.size());
121
122//               ----------------------------------------------------------------------
123//               Assert the files in the changed files list
124//               ----------------------------------------------------------------------
125
126        Iterator<ScmFile> files = new TreeSet<ScmFile>( changedFiles ).iterator();
127
128//              Check Foo.java
129        ScmFile file = (ScmFile) files.next();
130
131//              assertPath("/src/main/java/org/Foo.java", file.getPath());
132
133//              assertTrue(file.getStatus().isDiff());
134
135        String postRangeStr = "+/src/main/java/org/Foo.java\n\\ No newline at end of file\n";
136        String actualStr = differences.get( file.getPath() ).toString();
137//              assertTrue(actualStr.endsWith(postRangeStr));
138
139//              Check readme.txt
140        file = (ScmFile) files.next();
141
142//              assertPath("/readme.txt", file.getPath());
143
144//              assertTrue(file.getStatus().isDiff());
145
146        postRangeStr =
147            "-/readme.txt\n\\ No newline at end of file\n+changed readme.txt\n\\ No newline at end of file\n";
148        actualStr = differences.get( file.getPath() ).toString();
149//              assertTrue(actualStr.endsWith(postRangeStr));
150
151//              Check project.xml
152        file = (ScmFile) files.next();
153
154//              assertPath("/project.xml", file.getPath());
155
156        postRangeStr = "+changed project.xml\n\\ No newline at end of file\n";
157        actualStr = differences.get( file.getPath() ).toString();
158//              assertTrue(actualStr.endsWith(postRangeStr));
159
160//              assertTrue(file.getStatus().isDiff());
161        assertTrue( true );
162    }
163
164}