annotate test/org/tmatesoft/hg/test/TestPhases.java @ 474:09f2d38ecf26

Tests for phases support
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 12 Jul 2012 15:36:21 +0200
parents
children 9c9d09111aee
rev   line source
474
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2012 TMate Software Ltd
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.test;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import static org.junit.Assert.*;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.util.regex.Matcher;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.util.regex.Pattern;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import org.junit.Rule;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 import org.junit.Test;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 import org.tmatesoft.hg.internal.PhasesHelper;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import org.tmatesoft.hg.repo.HgChangelog;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 import org.tmatesoft.hg.repo.HgLookup;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 import org.tmatesoft.hg.repo.HgParentChildMap;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 import org.tmatesoft.hg.repo.HgPhase;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 import org.tmatesoft.hg.repo.HgRepository;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 /**
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 * {hg4j.tests.repos}/test-phases/
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 * @author Artem Tikhomirov
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 * @author TMate Software Ltd.
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 */
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 public class TestPhases {
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 @Rule
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 public ErrorCollectorExt errorCollector = new ErrorCollectorExt();
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 @Test
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 public void testHelperNoParentChildMap() throws Exception {
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 HgRepository repo = Configuration.get().find("test-phases");
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 HgPhase[] expected = readPhases(repo);
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 final long start = System.nanoTime();
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 PhasesHelper ph = new PhasesHelper(repo, null);
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 initAndCheck(ph, expected);
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 final long end = System.nanoTime();
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 System.out.printf("Without ParentWalker (simulates log command for single file): %d ms\n", (end - start)/1000);
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 }
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 @Test
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 public void testHelperWithParentChildMap() throws Exception {
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 HgRepository repo = Configuration.get().find("test-phases");
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 HgPhase[] expected = readPhases(repo);
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 final long start1 = System.nanoTime();
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 HgParentChildMap<HgChangelog> pw = new HgParentChildMap<HgChangelog>(repo.getChangelog());
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 pw.init();
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 final long start2 = System.nanoTime();
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 PhasesHelper ph = new PhasesHelper(repo, pw);
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 initAndCheck(ph, expected);
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 final long end = System.nanoTime();
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 System.out.printf("With ParentWalker(simulates log command for whole repo): %d ms (pw init: %,d ns)\n", (end - start1)/1000, start2 - start1);
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 }
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 private HgPhase[] initAndCheck(PhasesHelper ph, HgPhase[] expected) {
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 HgChangelog clog = ph.getRepo().getChangelog();
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 HgPhase[] result = new HgPhase[clog.getRevisionCount()];
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 for (int i = 0, l = clog.getLastRevision(); i <= l; i++) {
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 result[i] = ph.getPhase(i, null);
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 }
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 assertEquals(expected.length, result.length);
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 for (int i = 0; i < result.length; i++) {
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 errorCollector.assertTrue(result[i] == expected[i]);
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 }
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 return result;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 }
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 private static HgPhase[] readPhases(HgRepository repo) throws Exception {
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 HgPhase[] result = new HgPhase[repo.getChangelog().getRevisionCount()];
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 OutputParser.Stub output = new OutputParser.Stub();
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 ExecHelper eh = new ExecHelper(output, repo.getWorkingDir());
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 eh.run("hg", "phase", "-r", "0:-1");
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 Matcher m = Pattern.compile("(\\d+): (\\w+)$", Pattern.MULTILINE).matcher(output.result());
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 int i = 0;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 while (m.find()) {
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 int x = Integer.parseInt(m.group(1));
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 assert x == i;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 HgPhase v = HgPhase.parse(m.group(2));
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 result[x] = v;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 i++;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 }
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 return result;
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 }
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 public static void main(String[] args) throws Exception {
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 HgRepository repo = new HgLookup().detect(System.getProperty("user.home") + "/hg/test-phases/");
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 HgPhase[] v = readPhases(repo);
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 printPhases(v);
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 }
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104 private static void printPhases(HgPhase[] phase) {
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 for (int i = 0; i < phase.length; i++) {
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 System.out.printf("rev:%3d, phase:%s\n", i, phase[i]);
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 }
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
108 }
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109
09f2d38ecf26 Tests for phases support
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 }