View Javadoc

1   /*
2    * #%L
3    * JavaHg
4    * %%
5    * Copyright (C) 2011 aragost Trifork ag
6    * %%
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   * #L%
25   */
26  package com.aragost.javahg;
27  
28  import java.io.IOException;
29  import java.util.List;
30  import java.util.Map;
31  
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  import com.aragost.javahg.Changeset.Extra;
36  import com.aragost.javahg.commands.AddCommand;
37  import com.aragost.javahg.commands.CommitCommand;
38  import com.aragost.javahg.commands.RemoveCommand;
39  import com.aragost.javahg.commands.TagCommand;
40  import com.aragost.javahg.test.AbstractTestCase;
41  
42  public class ChangesetTest extends AbstractTestCase {
43  
44      @Test
45      public void testNullNode() throws IOException {
46          Repository repo = getTestRepository();
47          Assert.assertNull(repo.changeset("0000000000000000000000000000000000000000"));
48      }
49  
50      @Test
51      public void testRevisionNodeEquality() throws IOException {
52          Repository repo = getTestRepository();
53          writeFile("x", "abc");
54          String node0 = commit().getNode();
55          writeFile("x", "");
56          String node1 = commit().getNode();
57  
58          Assert.assertFalse(repo.changeset(node0).equals(repo.changeset(node1)));
59          Assert.assertFalse(repo.changeset(node1).equals(repo.changeset(node0)));
60          Assert.assertEquals(repo.changeset(node0), repo.changeset(node0));
61          Assert.assertEquals(repo.changeset(node1), repo.changeset(node1));
62      }
63  
64      @Test
65      public void testParents() throws IOException {
66          DateTime d = DateTime.parse("0 0");
67          Repository repo = getTestRepository();
68  
69          writeFile("x", "abc");
70          AddCommand.on(repo).execute();
71          Changeset rev0 = CommitCommand.on(repo).message("added x").user("user").date(d).execute();
72  
73          writeFile("x", "def");
74          Changeset rev1 = CommitCommand.on(repo).message("changed x").user("user").date(d).execute();
75  
76          Assert.assertEquals(rev1.getParent1(), rev0);
77          Assert.assertNull(rev1.getParent2());
78      }
79  
80      @Test
81      public void testFields() throws IOException {
82          Repository repo = getTestRepository();
83          
84          // write a and b initially 
85          writeFile("a");
86          writeFile("b");
87          AddCommand.on(repo).execute();
88          CommitCommand.on(repo).user("test").message("add a and b").execute();
89          
90          // remove a, modify b, and add c
91          DateTime d = DateTime.parse("1000 0");
92          RemoveCommand.on(repo).execute("a");
93          appendFile("b");
94          writeFile("c");
95          AddCommand.on(repo).execute("c");
96          Changeset c = CommitCommand.on(repo).user("test").date(d).message("add, modify, and remove").execute();
97  
98          Assert.assertEquals("test", c.getUser());
99          Assert.assertEquals(d, c.getTimestamp());
100         Assert.assertEquals("add, modify, and remove", c.getMessage());
101         Assert.assertEquals("default", c.getBranch());
102         // test for files
103         Assert.assertEquals("a", c.getDeletedFiles().get(0));
104         Assert.assertEquals("b", c.getModifiedFiles().get(0));
105         Assert.assertEquals("c", c.getAddedFiles().get(0));
106     }
107     
108     @Test
109     public void testLazyFileLoading() throws IOException {
110         Repository repo = getTestRepository();
111         writeFile("a");
112         AddCommand.on(repo).execute();
113         Changeset c = CommitCommand.on(repo).user("test").message("added a").execute();
114         Assert.assertNull("file data should be null", c.getFileData());
115         Assert.assertEquals("a", c.getAddedFiles().get(0));
116         Assert.assertNotNull("file data should not be null", c.getFileData());
117     }
118 
119     @Test
120     public void testNotEqualDifferentTypes() throws IOException {
121         Repository repo = getTestRepository();
122         writeFile("a");
123         AddCommand.on(repo).execute();
124         Changeset c = CommitCommand.on(repo).user("test").message("added a").execute();
125         Assert.assertFalse(c.equals("not a changeset"));
126     }
127 
128     @Test
129     public void testNotEqualNodes() throws IOException {
130         writeFile("a");
131         Changeset node0 = commit();
132         writeFile("b");
133         Changeset node1 = commit();
134         Assert.assertFalse(node0.equals(node1));
135     }
136 
137     @Test
138     public void testExtra() throws IOException {
139         BaseRepository repo = getTestRepository();
140         createChangeset();
141         Changeset cs = CommitCommand.on(repo).message("a").user("user").closeBranch().execute();
142         Extra extra = cs.getExtra();
143         Map<String, String> map = extra.stringValuedMap();
144         Assert.assertEquals(2, map.size());
145         Assert.assertEquals("default", map.get("branch"));
146         Assert.assertEquals("1", map.get("close"));
147 
148         String branchName = "blåbærgrød\r:=\n\t\\";
149         repo.workingCopy().setBranchName(branchName);
150         cs = commit();
151         Assert.assertEquals(branchName, cs.getExtra().getString("branch"));
152     }
153 
154     @Test
155     public void testTags() throws IOException {
156         Repository repo = getTestRepository();
157         Changeset cs1 = createChangeset();
158         TagCommand.on(repo).rev(cs1.getNode()).user("a").execute("a", "blåbærgrød");
159         Changeset cs2 = createChangeset();
160         TagCommand.on(repo).rev(cs2.getNode()).user("a").execute("\t b\t b \t");
161 
162         List<String> tags = cs1.tags();
163         Assert.assertArrayEquals(new Object[] { "a", "blåbærgrød" }, tags.toArray());
164 
165         tags = cs2.tags();
166         Assert.assertArrayEquals(new Object[] { "b\t b" }, tags.toArray());
167 
168         tags = repo.tip().tags();
169         Assert.assertEquals(0, tags.size());
170 
171     }
172 
173 }