comparison 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
comparison
equal deleted inserted replaced
473:5c09a9f2e073 474:09f2d38ecf26
1 /*
2 * Copyright (c) 2012 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.junit.Assert.*;
20
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.tmatesoft.hg.internal.PhasesHelper;
27 import org.tmatesoft.hg.repo.HgChangelog;
28 import org.tmatesoft.hg.repo.HgLookup;
29 import org.tmatesoft.hg.repo.HgParentChildMap;
30 import org.tmatesoft.hg.repo.HgPhase;
31 import org.tmatesoft.hg.repo.HgRepository;
32
33 /**
34 * {hg4j.tests.repos}/test-phases/
35 * @author Artem Tikhomirov
36 * @author TMate Software Ltd.
37 */
38 public class TestPhases {
39
40 @Rule
41 public ErrorCollectorExt errorCollector = new ErrorCollectorExt();
42
43 @Test
44 public void testHelperNoParentChildMap() throws Exception {
45 HgRepository repo = Configuration.get().find("test-phases");
46 HgPhase[] expected = readPhases(repo);
47 final long start = System.nanoTime();
48 PhasesHelper ph = new PhasesHelper(repo, null);
49 initAndCheck(ph, expected);
50 final long end = System.nanoTime();
51 System.out.printf("Without ParentWalker (simulates log command for single file): %d ms\n", (end - start)/1000);
52 }
53
54 @Test
55 public void testHelperWithParentChildMap() throws Exception {
56 HgRepository repo = Configuration.get().find("test-phases");
57 HgPhase[] expected = readPhases(repo);
58 final long start1 = System.nanoTime();
59 HgParentChildMap<HgChangelog> pw = new HgParentChildMap<HgChangelog>(repo.getChangelog());
60 pw.init();
61 final long start2 = System.nanoTime();
62 PhasesHelper ph = new PhasesHelper(repo, pw);
63 initAndCheck(ph, expected);
64 final long end = System.nanoTime();
65 System.out.printf("With ParentWalker(simulates log command for whole repo): %d ms (pw init: %,d ns)\n", (end - start1)/1000, start2 - start1);
66 }
67
68 private HgPhase[] initAndCheck(PhasesHelper ph, HgPhase[] expected) {
69 HgChangelog clog = ph.getRepo().getChangelog();
70 HgPhase[] result = new HgPhase[clog.getRevisionCount()];
71 for (int i = 0, l = clog.getLastRevision(); i <= l; i++) {
72 result[i] = ph.getPhase(i, null);
73 }
74 assertEquals(expected.length, result.length);
75 for (int i = 0; i < result.length; i++) {
76 errorCollector.assertTrue(result[i] == expected[i]);
77 }
78 return result;
79 }
80
81 private static HgPhase[] readPhases(HgRepository repo) throws Exception {
82 HgPhase[] result = new HgPhase[repo.getChangelog().getRevisionCount()];
83 OutputParser.Stub output = new OutputParser.Stub();
84 ExecHelper eh = new ExecHelper(output, repo.getWorkingDir());
85 eh.run("hg", "phase", "-r", "0:-1");
86 Matcher m = Pattern.compile("(\\d+): (\\w+)$", Pattern.MULTILINE).matcher(output.result());
87 int i = 0;
88 while (m.find()) {
89 int x = Integer.parseInt(m.group(1));
90 assert x == i;
91 HgPhase v = HgPhase.parse(m.group(2));
92 result[x] = v;
93 i++;
94 }
95 return result;
96 }
97
98 public static void main(String[] args) throws Exception {
99 HgRepository repo = new HgLookup().detect(System.getProperty("user.home") + "/hg/test-phases/");
100 HgPhase[] v = readPhases(repo);
101 printPhases(v);
102 }
103
104 private static void printPhases(HgPhase[] phase) {
105 for (int i = 0; i < phase.length; i++) {
106 System.out.printf("rev:%3d, phase:%s\n", i, phase[i]);
107 }
108 }
109
110 }