Mercurial > hg4j
comparison test/org/tmatesoft/hg/test/TestManifest.java @ 479:59b7c817bc4d
Methods from Main graduated as tests
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 12 Jul 2012 20:02:49 +0200 |
parents | d78cb5ca3053 |
children |
comparison
equal
deleted
inserted
replaced
478:e74580e24feb | 479:59b7c817bc4d |
---|---|
16 */ | 16 */ |
17 package org.tmatesoft.hg.test; | 17 package org.tmatesoft.hg.test; |
18 | 18 |
19 import static org.hamcrest.CoreMatchers.equalTo; | 19 import static org.hamcrest.CoreMatchers.equalTo; |
20 import static org.hamcrest.CoreMatchers.notNullValue; | 20 import static org.hamcrest.CoreMatchers.notNullValue; |
21 import static org.junit.Assert.assertTrue; | 21 import static org.junit.Assert.*; |
22 import static org.tmatesoft.hg.repo.HgRepository.TIP; | 22 import static org.tmatesoft.hg.repo.HgRepository.TIP; |
23 | 23 |
24 import java.util.Collections; | 24 import java.util.Collections; |
25 import java.util.LinkedHashMap; | 25 import java.util.LinkedHashMap; |
26 import java.util.LinkedList; | 26 import java.util.LinkedList; |
27 import java.util.Map; | 27 import java.util.Map; |
28 import java.util.regex.Pattern; | |
28 | 29 |
29 import org.junit.Rule; | 30 import org.junit.Rule; |
30 import org.junit.Test; | 31 import org.junit.Test; |
31 import org.tmatesoft.hg.core.HgManifestHandler; | 32 import org.tmatesoft.hg.core.HgManifestHandler; |
32 import org.tmatesoft.hg.core.HgFileRevision; | 33 import org.tmatesoft.hg.core.HgFileRevision; |
33 import org.tmatesoft.hg.core.HgManifestCommand; | 34 import org.tmatesoft.hg.core.HgManifestCommand; |
34 import org.tmatesoft.hg.core.Nodeid; | 35 import org.tmatesoft.hg.core.Nodeid; |
36 import org.tmatesoft.hg.internal.IntMap; | |
35 import org.tmatesoft.hg.repo.HgLookup; | 37 import org.tmatesoft.hg.repo.HgLookup; |
38 import org.tmatesoft.hg.repo.HgManifest; | |
36 import org.tmatesoft.hg.repo.HgRepository; | 39 import org.tmatesoft.hg.repo.HgRepository; |
40 import org.tmatesoft.hg.repo.HgManifest.Flags; | |
37 import org.tmatesoft.hg.util.Path; | 41 import org.tmatesoft.hg.util.Path; |
38 | 42 |
39 | 43 |
40 /** | 44 /** |
41 * | 45 * |
65 public static void main(String[] args) throws Throwable { | 69 public static void main(String[] args) throws Throwable { |
66 TestManifest tm = new TestManifest(); | 70 TestManifest tm = new TestManifest(); |
67 tm.testTip(); | 71 tm.testTip(); |
68 tm.testFirstRevision(); | 72 tm.testFirstRevision(); |
69 tm.testRevisionInTheMiddle(); | 73 tm.testRevisionInTheMiddle(); |
74 tm.testWalkFileRevisions(); | |
70 tm.errorCollector.verify(); | 75 tm.errorCollector.verify(); |
71 } | 76 } |
72 | 77 |
73 public TestManifest() throws Exception { | 78 public TestManifest() throws Exception { |
74 this(new HgLookup().detectFromWorkingDir()); | 79 this(new HgLookup().detectFromWorkingDir()); |
96 if (rev == 0) { | 101 if (rev == 0) { |
97 throw new IllegalStateException("Need manifest with few revisions"); | 102 throw new IllegalStateException("Need manifest with few revisions"); |
98 } | 103 } |
99 testRevision(rev); | 104 testRevision(rev); |
100 } | 105 } |
106 | |
107 @Test | |
108 public void testWalkFileRevisions() throws Exception { | |
109 // hg --debug manifest --rev 150 | grep cmdline/org/tmatesoft/hg/console/Main.java | |
110 String fname = "cmdline/org/tmatesoft/hg/console/Main.java"; | |
111 Pattern ptrn = Pattern.compile("(\\w+)\\s+\\d{3}\\s+\\Q" + fname + "\\E$", Pattern.MULTILINE); | |
112 OutputParser.Stub output = new OutputParser.Stub(); | |
113 ExecHelper eh = new ExecHelper(output, repo.getWorkingDir()); | |
114 int[] revisions = new int[] { 100, 150, 200, 210, 300 }; | |
115 final IntMap<Nodeid> expected = new IntMap<Nodeid>(10); | |
116 for (int r : revisions) { | |
117 eh.run("hg", "--debug", "manifest", "--rev", Integer.toString(r)); | |
118 for (String l : output.lines(ptrn, 1)) { | |
119 assertFalse(expected.containsKey(r)); | |
120 expected.put(r, Nodeid.fromAscii(l)); | |
121 } | |
122 if (!expected.containsKey(r)) { | |
123 expected.put(r, Nodeid.NULL); | |
124 } | |
125 } | |
126 assertEquals(revisions.length, expected.size()); | |
127 final Path path = Path.create(fname); | |
128 repo.getManifest().walkFileRevisions(path, new HgManifest.Inspector() { | |
129 | |
130 private Nodeid expectedNodeid; | |
131 private int clogRev; | |
132 | |
133 public boolean begin(int mainfestRevision, Nodeid nid, int changelogRevision) { | |
134 clogRev = changelogRevision; | |
135 assertTrue(expected.containsKey(changelogRevision)); | |
136 expectedNodeid = expected.get(changelogRevision); | |
137 // we walk revisions of specific file, hence can't get into manifest without | |
138 // that file, expectedNodeid should have been reported by `hg manifest`: | |
139 assertTrue(!expectedNodeid.isNull()); | |
140 return true; | |
141 } | |
142 | |
143 public boolean next(Nodeid nid, Path fname, Flags flags) { | |
144 assertEquals(path, fname); // only selected path shall pass | |
145 assertEquals(expectedNodeid, nid); | |
146 expected.remove(clogRev); | |
147 return true; | |
148 } | |
149 | |
150 public boolean end(int manifestRevision) { | |
151 return true; | |
152 } | |
153 | |
154 }, revisions); | |
155 for (int r : revisions) { | |
156 if (expected.containsKey(r)) { | |
157 // Each manifest entry reported by `hg manifest` shall be | |
158 // reported (and removed) by walkFileRevisions(), too. Only null nodeids | |
159 // (revisions where the file was missing) are left | |
160 assertTrue(expected.get(r).isNull()); | |
161 } | |
162 } | |
163 } | |
164 | |
101 | 165 |
102 private void testRevision(int rev) throws Exception { | 166 private void testRevision(int rev) throws Exception { |
103 manifestParser.reset(); | 167 manifestParser.reset(); |
104 eh.run("hg", "manifest", "--debug", "--rev", String.valueOf(rev == TIP ? -1 : rev)); | 168 eh.run("hg", "manifest", "--debug", "--rev", String.valueOf(rev == TIP ? -1 : rev)); |
105 revisions.clear(); | 169 revisions.clear(); |