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.commands;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.Map;
31  
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  import com.aragost.javahg.Repository;
36  import com.aragost.javahg.internals.CopyRenameHelper;
37  import com.aragost.javahg.test.AbstractTestCase;
38  
39  public class CopyCommandTest extends AbstractTestCase {
40  
41      @Test
42      public void testParseCopyLineFileToFile() throws IOException {
43          Assert.assertArrayEquals(osSeps("x", "y"), parseLine("x", "y", "copying x to y\n", false));
44      }
45  
46      @Test
47      public void testParseCopyLineFileToFileInDir() throws IOException {
48          Assert.assertArrayEquals(osSeps("x", "b/y"), parseLine("x", "b/y", "copying x to b/y\n", false));
49      }
50  
51      @Test
52      public void testParseCopyLineFileInDirToFileInDir() throws IOException {
53          Assert.assertArrayEquals(osSeps("a/x", "b/y"), parseLine("a/x", "b/y", "copying a/x to b/y\n", false));
54      }
55  
56      @Test
57      public void testParseCopyLineFileToDir() throws IOException {
58          String[] lines = parseLine("x", "b/", "copying x to b/x\n", true);
59          Assert.assertEquals("x", lines[0]);
60          Assert.assertEquals(osSep("b/x"), lines[1]);
61      }
62  
63      @Test
64      public void testParseCopyLineDirToDir() throws IOException {
65          Assert.assertArrayEquals(osSeps("a/x", "b/a/x"), parseLine("a/", "b/", "copying a/x to b/a/x\n", true));
66      }
67  
68      @Test
69      public void testParseCopyLineDirToDirInDir() throws IOException {
70          Assert.assertArrayEquals(osSeps("a/x", "b/c/a/x"), parseLine("a/", "b/c/", "copying a/x to b/c/a/x\n", true));
71      }
72  
73      @Test
74      public void testCopyFileToFile() throws IOException {
75          Repository repo = getTestRepository();
76          writeFile("x", "");
77          commit();
78  
79          Map<String, String> result = CopyCommand.on(repo).execute("x", "y");
80          Assert.assertEquals(1, result.size());
81          Assert.assertEquals("x", result.get("y"));
82      }
83  
84      @Test
85      public void testCopyFileToFileInDir() throws IOException {
86          Repository repo = getTestRepository();
87          writeFile("x", "");
88          commit();
89          new File(repo.getDirectory(), "b").mkdir();
90  
91          Map<String, String> result = CopyCommand.on(repo).execute("x", "b/y");
92          Assert.assertEquals(1, result.size());
93          Assert.assertEquals("x", result.get(osSep("b/y")));
94      }
95  
96      @Test
97      public void testCopyFileInDirToFileInDir() throws IOException {
98          Repository repo = getTestRepository();
99          new File(repo.getDirectory(), "a").mkdir();
100         new File(repo.getDirectory(), "b").mkdir();
101         writeFile("a/x", "");
102         commit();
103 
104         Map<String, String> result = CopyCommand.on(repo).execute("a/x", "b/y");
105         Assert.assertEquals(1, result.size());
106         Assert.assertEquals(osSep("a/x"), result.get(osSep("b/y")));
107     }
108 
109     @Test
110     public void testCopyFileToDir() throws IOException {
111         Repository repo = getTestRepository();
112         new File(repo.getDirectory(), "b").mkdir();
113         writeFile("x", "");
114         commit();
115 
116         Map<String, String> result = CopyCommand.on(repo).execute("x", "b");
117         Assert.assertEquals(1, result.size());
118         Assert.assertEquals("x", result.get(osSep("b/x")));
119     }
120 
121     @Test
122     public void testCopyDirToDir() throws IOException {
123         Repository repo = getTestRepository();
124         new File(repo.getDirectory(), "a").mkdir();
125         new File(repo.getDirectory(), "b").mkdir();
126         writeFile("a/x", "");
127         commit();
128 
129         Map<String, String> result = CopyCommand.on(repo).execute("a", "b");
130         Assert.assertEquals(1, result.size());
131         Assert.assertEquals(osSep("a/x"), result.get(osSep("b/a/x")));
132     }
133     
134     @Test
135     public void testCopyDirToDir2() throws IOException {
136         Repository repo = getTestRepository();
137         new File(repo.getDirectory(), "a/").mkdir();
138         new File(repo.getDirectory(), "a/bbb/").mkdir();
139         writeFile("a/bbb/x", "");
140         writeFile("a/bbb/x2", "");
141         commit();
142 
143         Map<String, String> result = CopyCommand.on(repo).execute("a/bbb", "a/bbb2");
144         Assert.assertEquals(2, result.size());
145         Assert.assertEquals(osSep("a/bbb/x"), result.get(osSep("a/bbb2/x")));
146     }
147     
148     @Test
149     public void testCopyDirToDirInDir() throws IOException {
150         Repository repo = getTestRepository();
151         new File(repo.getDirectory(), "a").mkdir();
152         new File(repo.getDirectory(), "b/c").mkdirs();
153         writeFile("a/x", "");
154         commit();
155 
156         Map<String, String> result = CopyCommand.on(repo).execute("a", "b/c");
157         Assert.assertEquals(1, result.size());
158         Assert.assertEquals(osSep("a/x"), result.get(osSep("b/c/a/x")));
159     }
160 
161     @Test
162     public void testCopyMultipleFiles() throws IOException {
163         Repository repo = getTestRepository();
164         new File(repo.getDirectory(), "a").mkdir();
165         writeFile("a/x", "");
166         writeFile("a/y", "");
167         writeFile("z", "");
168         commit();
169 
170         new File(repo.getDirectory(), "dst").mkdir();
171         Map<String, String> result = CopyCommand.on(repo).execute("z", "a", "dst");
172         Assert.assertEquals(3, result.size());
173         Assert.assertEquals(osSep("a/x"), result.get(osSep("dst/a/x")));
174         Assert.assertEquals(osSep("a/y"), result.get(osSep("dst/a/y")));
175         Assert.assertEquals("z", result.get(osSep("dst/z")));
176     }
177 
178     @Test(expected = IllegalArgumentException.class)
179     public void testOneFile() throws IOException {
180         Repository repo = getTestRepository();
181         CopyCommand.on(repo).execute("a");
182     }
183 
184     @Test(expected = NullPointerException.class)
185     public void testNullPointer() throws IOException {
186         Repository repo = getTestRepository();
187         CopyCommand.on(repo).execute((String[]) null);
188     }
189 
190     @Test
191     public void testCopyWithAfterFlag() throws IOException {
192         Repository repo = getTestRepository();
193         File dir = repo.getDirectory();
194         writeFile("x", "");
195         commit();
196         new File(dir, "x").renameTo(new File(dir, "y"));
197 
198         Map<String, String> result = CopyCommand.on(repo).after().execute("x", "y");
199         Assert.assertEquals(1, result.size());
200         Assert.assertEquals("x", result.get("y"));
201     }
202 
203     @Test
204     public void testCopyAddedFile() throws IOException {
205         Repository repo = getTestRepository();
206         writeFile("x", "");
207         AddCommand.on(repo).execute();
208 
209         // The 'x has not been committed yet, so no copy data will be
210         // stored for x.' message is sent on stderr and is notparsed
211         // by execute.
212         Map<String, String> result = CopyCommand.on(repo).execute("x", "y");
213         Assert.assertEquals(1, result.size());
214         Assert.assertEquals("x", result.get("y"));
215     }
216 
217     private String[] parseLine(String src, String dst, String line, boolean dstIsDirectory) {
218         return CopyRenameHelper.parseCopyLine(osSep(line), CopyCommand.PREFIX);
219     }
220 
221     /**
222      * Return / with the OS specific file seperator
223      * 
224      * @param s
225      * @return
226      */
227     private String osSep(String s) {
228         char osSep = File.separatorChar;
229         return s.replace('/', osSep);
230     }
231 
232     private String[] osSeps(String... ss) {
233         String[] result = new String[ss.length];
234         for (int i = 0; i < result.length; i++) {
235             result[i] = osSep(ss[i]);
236         }
237         return result;
238     }
239 
240 }