1 package com.aragost.javahg.commands;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Map;
6
7 import org.junit.Assert;
8 import org.junit.Test;
9
10 import com.aragost.javahg.Repository;
11 import com.aragost.javahg.test.AbstractTestCase;
12
13 public class RenameCommandTest extends AbstractTestCase {
14
15 @Test
16 public void testCopyFileToFile() throws IOException {
17 Repository repo = getTestRepository();
18 writeFile("x", "");
19 commit();
20
21 Map<String, String> result = RenameCommand.on(repo).execute("x", "y");
22 Assert.assertEquals(1, result.size());
23 Assert.assertEquals("x", result.get("y"));
24 }
25
26 @Test
27 public void testCopyFileToFileInDir() throws IOException {
28 Repository repo = getTestRepository();
29 writeFile("x", "");
30 commit();
31 new File(repo.getDirectory(), "b").mkdir();
32
33 Map<String, String> result = RenameCommand.on(repo).execute("x", "b/y");
34 Assert.assertEquals(1, result.size());
35 Assert.assertEquals("x", result.get(osSep("b/y")));
36 }
37
38 @Test
39 public void testCopyFileInDirToFileInDir() throws IOException {
40 Repository repo = getTestRepository();
41 new File(repo.getDirectory(), "a").mkdir();
42 new File(repo.getDirectory(), "b").mkdir();
43 writeFile("a/x", "");
44 commit();
45
46 Map<String, String> result = RenameCommand.on(repo).execute("a/x", "b/y");
47 Assert.assertEquals(1, result.size());
48 Assert.assertEquals(osSep("a/x"), result.get(osSep("b/y")));
49 }
50
51 @Test
52 public void testCopyFileToDir() throws IOException {
53 Repository repo = getTestRepository();
54 new File(repo.getDirectory(), "b").mkdir();
55 writeFile("x", "");
56 commit();
57
58 Map<String, String> result = RenameCommand.on(repo).execute("x", "b");
59 Assert.assertEquals(1, result.size());
60 Assert.assertEquals("x", result.get(osSep("b/x")));
61 }
62
63 @Test
64 public void testCopyDirToDir() throws IOException {
65 Repository repo = getTestRepository();
66 new File(repo.getDirectory(), "a").mkdir();
67 new File(repo.getDirectory(), "b").mkdir();
68 writeFile("a/x", "");
69 commit();
70
71 Map<String, String> result = RenameCommand.on(repo).execute("a", "b");
72 Assert.assertEquals(1, result.size());
73 Assert.assertEquals(osSep("a/x"), result.get(osSep("b/a/x")));
74 }
75
76 @Test
77 public void testCopyDirToDirInDir() throws IOException {
78 Repository repo = getTestRepository();
79 new File(repo.getDirectory(), "a").mkdir();
80 new File(repo.getDirectory(), "b/c").mkdirs();
81 writeFile("a/x", "");
82 commit();
83
84 Map<String, String> result = RenameCommand.on(repo).execute("a", "b/c");
85 Assert.assertEquals(1, result.size());
86 Assert.assertEquals(osSep("a/x"), result.get(osSep("b/c/a/x")));
87 }
88
89 @Test
90 public void testCopyMultipleFiles() throws IOException {
91 Repository repo = getTestRepository();
92 new File(repo.getDirectory(), "a").mkdir();
93 writeFile("a/x", "");
94 writeFile("a/y", "");
95 writeFile("z", "");
96 commit();
97
98 new File(repo.getDirectory(), "dst").mkdir();
99 Map<String, String> result = RenameCommand.on(repo).execute("z", "a",
100 "dst");
101 Assert.assertEquals(3, result.size());
102 Assert.assertEquals(osSep("a/x"), result.get(osSep("dst/a/x")));
103 Assert.assertEquals(osSep("a/y"), result.get(osSep("dst/a/y")));
104 Assert.assertEquals("z", result.get(osSep("dst/z")));
105 }
106
107 @Test(expected = IllegalArgumentException.class)
108 public void testOneFile() throws IOException {
109 Repository repo = getTestRepository();
110 RenameCommand.on(repo).execute("a");
111 }
112
113 @Test(expected = NullPointerException.class)
114 public void testNullPointer() throws IOException {
115 Repository repo = getTestRepository();
116 RenameCommand.on(repo).execute((String[]) null);
117 }
118
119 @Test
120 public void testCopyWithAfterFlag() throws IOException {
121 Repository repo = getTestRepository();
122 File dir = repo.getDirectory();
123 writeFile("x", "");
124 commit();
125 new File(dir, "x").renameTo(new File(dir, "y"));
126
127 Map<String, String> result = RenameCommand.on(repo).after()
128 .execute("x", "y");
129 Assert.assertEquals(1, result.size());
130 Assert.assertEquals("x", result.get("y"));
131 }
132
133 @Test
134 public void testCopyAddedFile() throws IOException {
135 Repository repo = getTestRepository();
136 writeFile("x", "");
137 AddCommand.on(repo).execute();
138
139
140
141
142 Map<String, String> result = RenameCommand.on(repo).execute("x", "y");
143 Assert.assertEquals(1, result.size());
144 Assert.assertEquals("x", result.get("y"));
145 }
146
147
148
149
150
151
152
153 private String osSep(String s) {
154 char osSep = File.separatorChar;
155 return s.replace('/', osSep);
156 }
157 }