comparison hg4j/src/test/java/org/tmatesoft/hg/test/TestStatus.java @ 213:6ec4af642ba8 gradle

Project uses Gradle for build - actual changes
author Alexander Kitaev <kitaev@gmail.com>
date Tue, 10 May 2011 10:52:53 +0200
parents
children
comparison
equal deleted inserted replaced
212:edb2e2829352 213:6ec4af642ba8
1 /*
2 * Copyright (c) 2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.test;
18
19 import static org.hamcrest.CoreMatchers.equalTo;
20 import static org.tmatesoft.hg.core.HgStatus.*;
21 import static org.tmatesoft.hg.core.HgStatus.Kind.*;
22 import static org.tmatesoft.hg.repo.HgRepository.TIP;
23
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.TreeMap;
31
32 import org.junit.Assume;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.tmatesoft.hg.core.HgStatus;
36 import org.tmatesoft.hg.core.HgStatusCommand;
37 import org.tmatesoft.hg.repo.HgLookup;
38 import org.tmatesoft.hg.repo.HgRepository;
39 import org.tmatesoft.hg.repo.HgStatusCollector;
40 import org.tmatesoft.hg.repo.HgWorkingCopyStatusCollector;
41 import org.tmatesoft.hg.util.Path;
42
43
44 /**
45 *
46 * @author Artem Tikhomirov
47 * @author TMate Software Ltd.
48 */
49 public class TestStatus {
50
51 @Rule
52 public ErrorCollectorExt errorCollector = new ErrorCollectorExt();
53
54 private final HgRepository repo;
55 private StatusOutputParser statusParser;
56 private ExecHelper eh;
57
58 public static void main(String[] args) throws Throwable {
59 TestStatus test = new TestStatus();
60 test.testLowLevel();
61 test.testStatusCommand();
62 test.testPerformance();
63 test.errorCollector.verify();
64 }
65
66 public TestStatus() throws Exception {
67 this(new HgLookup().detectFromWorkingDir());
68 }
69
70 private TestStatus(HgRepository hgRepo) {
71 repo = hgRepo;
72 Assume.assumeTrue(!repo.isInvalid());
73 statusParser = new StatusOutputParser();
74 eh = new ExecHelper(statusParser, null);
75 }
76
77 @Test
78 public void testLowLevel() throws Exception {
79 final HgWorkingCopyStatusCollector wcc = new HgWorkingCopyStatusCollector(repo);
80 statusParser.reset();
81 eh.run("hg", "status", "-A");
82 HgStatusCollector.Record r = wcc.status(HgRepository.TIP);
83 report("hg status -A", r, statusParser);
84 //
85 statusParser.reset();
86 int revision = 3;
87 eh.run("hg", "status", "-A", "--rev", String.valueOf(revision));
88 r = wcc.status(revision);
89 report("status -A --rev " + revision, r, statusParser);
90 //
91 statusParser.reset();
92 eh.run("hg", "status", "-A", "--change", String.valueOf(revision));
93 r = new HgStatusCollector.Record();
94 new HgStatusCollector(repo).change(revision, r);
95 report("status -A --change " + revision, r, statusParser);
96 //
97 statusParser.reset();
98 int rev2 = 80;
99 final String range = String.valueOf(revision) + ":" + String.valueOf(rev2);
100 eh.run("hg", "status", "-A", "--rev", range);
101 r = new HgStatusCollector(repo).status(revision, rev2);
102 report("Status -A -rev " + range, r, statusParser);
103 }
104
105 @Test
106 public void testStatusCommand() throws Exception {
107 final HgStatusCommand sc = new HgStatusCommand(repo).all();
108 StatusCollector r;
109 statusParser.reset();
110 eh.run("hg", "status", "-A");
111 sc.execute(r = new StatusCollector());
112 report("hg status -A", r);
113 //
114 statusParser.reset();
115 int revision = 3;
116 eh.run("hg", "status", "-A", "--rev", String.valueOf(revision));
117 sc.base(revision).execute(r = new StatusCollector());
118 report("status -A --rev " + revision, r);
119 //
120 statusParser.reset();
121 eh.run("hg", "status", "-A", "--change", String.valueOf(revision));
122 sc.base(TIP).revision(revision).execute(r = new StatusCollector());
123 report("status -A --change " + revision, r);
124
125 // TODO check not -A, but defaults()/custom set of modifications
126 }
127
128 private static class StatusCollector implements HgStatusCommand.Handler {
129 private final Map<HgStatus.Kind, List<Path>> map = new TreeMap<HgStatus.Kind, List<Path>>();
130
131 public void handleStatus(HgStatus s) {
132 List<Path> l = map.get(s.getKind());
133 if (l == null) {
134 l = new LinkedList<Path>();
135 map.put(s.getKind(), l);
136 }
137 l.add(s.getPath());
138 }
139
140 public List<Path> get(Kind k) {
141 List<Path> rv = map.get(k);
142 if (rv == null) {
143 return Collections.emptyList();
144 }
145 return rv;
146 }
147 }
148
149 public void testRemovedAgainstNonTip() {
150 /*
151 status --rev N when a file added past revision N was removed ((both physically and in dirstate), but not yet committed
152
153 Reports extra REMOVED file (the one added and removed in between). Shall not
154 */
155 }
156
157 /*
158 * With warm-up of previous tests, 10 runs, time in milliseconds
159 * 'hg status -A': Native client total 953 (95 per run), Java client 94 (9)
160 * 'hg status -A --rev 3:80': Native client total 1828 (182 per run), Java client 235 (23)
161 * 'hg log --debug', 10 runs: Native client total 1766 (176 per run), Java client 78 (7)
162 *
163 * 18.02.2011
164 * 'hg status -A --rev 3:80', 10 runs: Native client total 2000 (200 per run), Java client 250 (25)
165 * 'hg log --debug', 10 runs: Native client total 2297 (229 per run), Java client 125 (12)
166 *
167 * 9.3.2011 (DataAccess instead of byte[] in ReflogStream.Inspector
168 * 'hg status -A', 10 runs: Native client total 1516 (151 per run), Java client 219 (21)
169 * 'hg status -A --rev 3:80', 10 runs: Native client total 1875 (187 per run), Java client 3187 (318) (!!! ???)
170 * 'hg log --debug', 10 runs: Native client total 2484 (248 per run), Java client 344 (34)
171 */
172 public void testPerformance() throws Exception {
173 final int runs = 10;
174 final long start1 = System.currentTimeMillis();
175 for (int i = 0; i < runs; i++) {
176 statusParser.reset();
177 eh.run("hg", "status", "-A", "--rev", "3:80");
178 }
179 final long start2 = System.currentTimeMillis();
180 for (int i = 0; i < runs; i++) {
181 StatusCollector r = new StatusCollector();
182 new HgStatusCommand(repo).all().base(3).revision(80).execute(r);
183 }
184 final long end = System.currentTimeMillis();
185 System.out.printf("'hg status -A --rev 3:80', %d runs: Native client total %d (%d per run), Java client %d (%d)\n", runs, start2-start1, (start2-start1)/runs, end-start2, (end-start2)/runs);
186 }
187
188 private void report(String what, StatusCollector r) {
189 reportNotEqual(what + "#MODIFIED", r.get(Modified), statusParser.getModified());
190 reportNotEqual(what + "#ADDED", r.get(Added), statusParser.getAdded());
191 reportNotEqual(what + "#REMOVED", r.get(Removed), statusParser.getRemoved());
192 reportNotEqual(what + "#CLEAN", r.get(Clean), statusParser.getClean());
193 reportNotEqual(what + "#IGNORED", r.get(Ignored), statusParser.getIgnored());
194 reportNotEqual(what + "#MISSING", r.get(Missing), statusParser.getMissing());
195 reportNotEqual(what + "#UNKNOWN", r.get(Unknown), statusParser.getUnknown());
196 // FIXME test copies
197 }
198
199 private void report(String what, HgStatusCollector.Record r, StatusOutputParser statusParser) {
200 reportNotEqual(what + "#MODIFIED", r.getModified(), statusParser.getModified());
201 reportNotEqual(what + "#ADDED", r.getAdded(), statusParser.getAdded());
202 reportNotEqual(what + "#REMOVED", r.getRemoved(), statusParser.getRemoved());
203 reportNotEqual(what + "#CLEAN", r.getClean(), statusParser.getClean());
204 reportNotEqual(what + "#IGNORED", r.getIgnored(), statusParser.getIgnored());
205 reportNotEqual(what + "#MISSING", r.getMissing(), statusParser.getMissing());
206 reportNotEqual(what + "#UNKNOWN", r.getUnknown(), statusParser.getUnknown());
207 List<Path> copiedKeyDiff = difference(r.getCopied().keySet(), statusParser.getCopied().keySet());
208 HashMap<Path, String> copyDiff = new HashMap<Path,String>();
209 if (copiedKeyDiff.isEmpty()) {
210 for (Path jk : r.getCopied().keySet()) {
211 Path jv = r.getCopied().get(jk);
212 if (statusParser.getCopied().containsKey(jk)) {
213 Path cmdv = statusParser.getCopied().get(jk);
214 if (!jv.equals(cmdv)) {
215 copyDiff.put(jk, jv + " instead of " + cmdv);
216 }
217 } else {
218 copyDiff.put(jk, "ERRONEOUSLY REPORTED IN JAVA");
219 }
220 }
221 }
222 errorCollector.checkThat(what + "#Non-matching 'copied' keys: ", copiedKeyDiff, equalTo(Collections.<Path>emptyList()));
223 errorCollector.checkThat(what + "#COPIED", copyDiff, equalTo(Collections.<Path,String>emptyMap()));
224 }
225
226 private <T> void reportNotEqual(String what, Collection<T> l1, Collection<T> l2) {
227 List<T> diff = difference(l1, l2);
228 errorCollector.checkThat(what, diff, equalTo(Collections.<T>emptyList()));
229 }
230
231 private static <T> List<T> difference(Collection<T> l1, Collection<T> l2) {
232 LinkedList<T> result = new LinkedList<T>(l2);
233 for (T t : l1) {
234 if (l2.contains(t)) {
235 result.remove(t);
236 } else {
237 result.add(t);
238 }
239 }
240 return result;
241 }
242 }