comparison test/org/tmatesoft/hg/test/TestConfigFiles.java @ 609:e4a71afd3c71

Test TODOs: test for ConfigFile (covering %include and %unset directives)
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 08 May 2013 17:11:45 +0200
parents
children 7fc7fba4df30
comparison
equal deleted inserted replaced
608:e1b29756f901 609:e4a71afd3c71
1 /*
2 * Copyright (c) 2013 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 java.io.File;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.junit.Assert;
24 import org.junit.Ignore;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.tmatesoft.hg.internal.BasicSessionContext;
28 import org.tmatesoft.hg.internal.ConfigFile;
29 import org.tmatesoft.hg.repo.HgRepoConfig;
30 import org.tmatesoft.hg.repo.HgRepoConfig.PathsSection;
31 import org.tmatesoft.hg.repo.HgRepoConfig.Section;
32 import org.tmatesoft.hg.repo.HgRepository;
33 import org.tmatesoft.hg.util.Pair;
34
35 /**
36 *
37 * @author Artem Tikhomirov
38 * @author TMate Software Ltd.
39 */
40 public class TestConfigFiles {
41
42 @Rule
43 public ErrorCollectorExt errorCollector = new ErrorCollectorExt();
44
45 @Test
46 public void testConfigFile() throws Exception {
47 ConfigFile configFile = new ConfigFile(new BasicSessionContext(null));
48 configFile.addLocation(new File(Configuration.get().getTestDataDir(), "sample.rc"));
49 // section1 has key1 unset, key2 overridden from included, key4 from second occurence
50 HashMap<String, String> section1 = new HashMap<String, String>();
51 section1.put("key2", "alternative value 2");
52 section1.put("key3", "value 3");
53 section1.put("key4", "value 4");
54 // section2 comes from included config
55 HashMap<String, String> section2 = new HashMap<String, String>();
56 section2.put("key1", "value 1-2");
57 HashMap<String, String> section3 = new HashMap<String, String>();
58 section3.put("key1", "value 1-3");
59 HashMap<String, HashMap<String,String>> sections = new HashMap<String, HashMap<String,String>>();
60 sections.put("section1", section1);
61 sections.put("section2", section2);
62 sections.put("section3", section3);
63 //
64 for (String s : configFile.getSectionNames()) {
65 // System.out.printf("[%s]\n", s);
66 final HashMap<String, String> m = sections.remove(s);
67 errorCollector.assertTrue(m != null);
68 for (Map.Entry<String, String> e : configFile.getSection(s).entrySet()) {
69 // System.out.printf("%s = %s\n", e.getKey(), e.getValue());
70 if (m.containsKey(e.getKey())) {
71 errorCollector.assertEquals(m.remove(e.getKey()), e.getValue());
72 } else {
73 errorCollector.fail("Unexpected key:" + e.getKey());
74 }
75 }
76 }
77 errorCollector.assertEquals(0, sections.size());
78 errorCollector.assertEquals(0, section1.size());
79 errorCollector.assertEquals(0, section2.size());
80 errorCollector.assertEquals(0, section3.size());
81 }
82
83 @Test
84 @Ignore("just a dump for now, to compare values visually")
85 public void testRepositoryConfig() throws Exception {
86 HgRepository repo = Configuration.get().own();
87 final HgRepoConfig cfg = repo.getConfiguration();
88 Assert.assertNotNull(cfg.getPaths());
89 Assert.assertNotNull(cfg.getExtensions());
90 final Section dne = cfg.getSection("does-not-exist");
91 Assert.assertNotNull(dne);
92 Assert.assertFalse(dne.exists());
93 for (Pair<String, String> p : cfg.getSection("ui")) {
94 System.out.printf("%s = %s\n", p.first(), p.second());
95 }
96 final PathsSection p = cfg.getPaths();
97 System.out.printf("Known paths: %d. default: %s(%s), default-push: %s(%s)\n", p.getKeys().size(), p.getDefault(), p.hasDefault(), p.getDefaultPush(), p.hasDefaultPush());
98 for (String k : cfg.getPaths().getKeys()) {
99 System.out.println(k);
100 }
101 Assert.assertFalse(p.hasDefault() ^ p.getDefault() != null);
102 Assert.assertFalse(p.hasDefaultPush() ^ p.getDefaultPush() != null);
103 }
104 }