1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  package com.aragost.javahg.commands;
27  
28  import static org.junit.Assume.assumeTrue;
29  
30  import java.io.File;
31  import java.io.IOException;
32  import java.nio.charset.CodingErrorAction;
33  import java.nio.charset.MalformedInputException;
34  import java.util.Collection;
35  import java.util.List;
36  
37  import org.junit.Assert;
38  import org.junit.Test;
39  
40  import com.aragost.javahg.HgVersion;
41  import com.aragost.javahg.Repository;
42  import com.aragost.javahg.internals.RuntimeIOException;
43  import com.aragost.javahg.test.AbstractTestCase;
44  import com.google.common.collect.Sets;
45  import com.google.common.io.Files;
46  
47  public class StatusCommandTest extends AbstractTestCase {
48  
49      @Test
50      public void testSimple() throws IOException {
51          setup();
52  
53          List<StatusLine> status = StatusCommand.on(getTestRepository()).lines();
54          Assert.assertEquals(2, status.size());
55      }
56  
57      @Test
58      public void testAllFlag() throws IOException {
59          Repository repo = getTestRepository();
60          writeFile(".hgignore", "");
61          writeFile("modified");
62          writeFile("removed");
63          writeFile("clean");
64          writeFile("missing");
65          commit();
66          writeFile("modified", "new content");
67          RemoveCommand.on(repo).execute("removed");
68          CopyCommand.on(repo).execute("clean", "copied");
69          new File(repo.getDirectory(), "missing").delete();
70          writeFile("added");
71          AddCommand.on(repo).execute("added");
72  
73          writeFile(".hgignore", "syntax: glob\nignored\n");
74          CommitCommand.on(repo).message("message").user("user").execute(".hgignore");
75          writeFile("ignored");
76          writeFile("unknown");
77  
78          
79          
80          boolean notFixed = repo.getHgVersion().isBefore(HgVersion.fromString("2.1.1"));
81          Repository repo2 = repo;
82          if (notFixed) {
83              repo2 = Repository.open(repo.getDirectory());
84          }
85  
86          StatusCommand statusCmd = StatusCommand.on(repo2).all();
87          StatusResult result = statusCmd.execute();
88  
89          Assert.assertArrayEquals(new String[] { "modified" }, result.getModified().toArray());
90          Assert.assertArrayEquals(new String[] { "added" }, result.getAdded().toArray());
91          Assert.assertArrayEquals(new String[] { "removed" }, result.getRemoved().toArray());
92          Assert.assertArrayEquals(new String[] { ".hgignore", "clean" }, result.getClean().toArray());
93          Assert.assertArrayEquals(new String[] { "missing" }, result.getMissing().toArray());
94          Assert.assertArrayEquals(new String[] { "unknown" }, result.getUnknown().toArray());
95          Assert.assertArrayEquals(new String[] { "ignored" }, result.getIgnored().toArray());
96  
97          if (notFixed) {
98              repo2.close();
99          }
100     }
101 
102     @Test
103     public void testCopy() throws IOException {
104         Repository repo = getTestRepository();
105         writeFile("a");
106         commit();
107         CopyCommand.on(repo).execute("a", "c");
108         StatusResult result = StatusCommand.on(repo).copies().execute();
109         Assert.assertEquals("a", result.getCopied().get("c"));
110         Assert.assertEquals(1, result.getCopied().size());
111     }
112 
113     @Test
114     public void testIterator() throws IOException {
115         setup();
116 
117         int count = 0;
118         Collection<String> names = Sets.newHashSet("a.txt", "b.txt");
119         for (StatusLine line : StatusCommand.on(getTestRepository())) {
120             Assert.assertTrue(names.contains(line.getFileName()));
121             count++;
122         }
123         Assert.assertEquals(2, count);
124 
125         
126         VersionCommand.on(getTestRepository()).execute();
127     }
128 
129     @Test
130     public void testErrorActionReport() throws IOException {
131         assumeTrue(System.getProperty("os.name").equals("Linux"));
132         assumeTrue(System.getProperty("file.encoding").equals("ISO-8859-1"));
133 
134         Repository repo = getTestRepository();
135         File dir = repo.getDirectory();
136 
137         
138         
139 
140         
141         File apples = new File(dir, "\u00C6bler.txt");
142         Files.write("\u00C6bler", apples, utf8());
143 
144         StatusCommand cmd = StatusCommand.on(repo);
145 
146         try {
147             cmd.execute();
148             assertFailedExecution(cmd);
149         } catch (Exception ex) {
150             Assert.assertEquals(RuntimeIOException.class, ex.getClass());
151             Assert.assertEquals(MalformedInputException.class, ex.getCause().getClass());
152         }
153     }
154 
155     @Test
156     public void testErrorActionReplace() throws IOException {
157         assumeTrue(System.getProperty("os.name").equals("Linux"));
158         assumeTrue(System.getProperty("file.encoding").equals("ISO-8859-1"));
159 
160         Repository repo = getTestRepository();
161         File dir = repo.getDirectory();
162 
163         
164         
165 
166         
167         File apples = new File(dir, "\u00C6bler.txt");
168         Files.write("\u00C6bler", apples, utf8());
169 
170         getFirstServer(repo).setErrorAction(CodingErrorAction.REPLACE);
171         StatusResult result = StatusCommand.on(repo).execute();
172         Assert.assertEquals(1, result.getUnknown().size());
173         
174         Assert.assertEquals("\uFFFDbler.txt", result.getUnknown().get(0));
175     }
176 
177     @Test
178     public void testErrorActionIgnore() throws IOException {
179         assumeTrue(System.getProperty("os.name").equals("Linux"));
180         assumeTrue(System.getProperty("file.encoding").equals("ISO-8859-1"));
181 
182         Repository repo = getTestRepository();
183         File dir = repo.getDirectory();
184 
185         
186         
187 
188         
189         File apples = new File(dir, "\u00C6bler.txt");
190         Files.write("\u00C6bler", apples, utf8());
191 
192         getFirstServer(repo).setErrorAction(CodingErrorAction.IGNORE);
193         StatusResult result = StatusCommand.on(repo).execute();
194         Assert.assertEquals(1, result.getUnknown().size());
195         Assert.assertEquals("bler.txt", result.getUnknown().get(0));
196     }
197 
198     private StatusCommand setup() throws IOException {
199         Repository repo = getTestRepository();
200         File dir = repo.getDirectory();
201         StatusCommand cmd = StatusCommand.on(repo);
202         StatusResult result = cmd.execute();
203         Assert.assertTrue(result.getAdded().isEmpty());
204         Assert.assertTrue(result.getClean().isEmpty());
205         Assert.assertTrue(result.getCopied().isEmpty());
206         Assert.assertTrue(result.getIgnored().isEmpty());
207         Assert.assertTrue(result.getMissing().isEmpty());
208         Assert.assertTrue(result.getModified().isEmpty());
209         Assert.assertTrue(result.getRemoved().isEmpty());
210         Assert.assertTrue(result.getUnknown().isEmpty());
211 
212         AddCommand addCmd = AddCommand.on(repo);
213         File a = new File(dir, "a.txt");
214         File b = new File(dir, "b.txt");
215         Files.write("BOO", a, utf8());
216         Files.write("BOO", b, utf8());
217 
218         addCmd.execute(a.getAbsolutePath());
219         return cmd;
220     }
221 }