1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.gitrepo;
44
45 import static java.nio.charset.StandardCharsets.UTF_8;
46 import static org.junit.Assert.assertTrue;
47 import static org.junit.Assert.fail;
48
49 import java.io.ByteArrayInputStream;
50 import java.io.IOException;
51 import java.net.URI;
52 import java.util.HashSet;
53 import java.util.Set;
54
55 import org.junit.Test;
56 import org.xml.sax.SAXException;
57
58 public class ManifestParserTest {
59
60 @Test
61 public void testManifestParser() throws Exception {
62 String baseUrl = "https://git.google.com/";
63 StringBuilder xmlContent = new StringBuilder();
64 Set<String> results = new HashSet<>();
65 xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
66 .append("<manifest>")
67 .append("<remote name=\"remote1\" fetch=\".\" />")
68 .append("<default revision=\"master\" remote=\"remote1\" />")
69 .append("<project path=\"foo\" name=\"")
70 .append("foo")
71 .append("\" groups=\"a,test\" />")
72 .append("<project path=\"bar\" name=\"")
73 .append("bar")
74 .append("\" groups=\"notdefault\" />")
75 .append("<project path=\"foo/a\" name=\"")
76 .append("a")
77 .append("\" groups=\"a\" />")
78 .append("<project path=\"b\" name=\"")
79 .append("b")
80 .append("\" groups=\"b\" />")
81 .append("</manifest>");
82
83 ManifestParser parser = new ManifestParser(
84 null, null, "master", baseUrl, null, null);
85 parser.read(new ByteArrayInputStream(xmlContent.toString().getBytes(UTF_8)));
86
87 results.clear();
88 results.add("foo");
89 results.add("bar");
90 results.add("foo/a");
91 results.add("b");
92 for (RepoProject proj : parser.getProjects()) {
93 String msg = String.format(
94 "project \"%s\" should be included in unfiltered projects",
95 proj.getPath());
96 assertTrue(msg, results.contains(proj.getPath()));
97 results.remove(proj.getPath());
98 }
99 assertTrue(
100 "Unfiltered projects shouldn't contain any unexpected results",
101 results.isEmpty());
102
103 results.clear();
104 results.add("foo");
105 results.add("b");
106 for (RepoProject proj : parser.getFilteredProjects()) {
107 String msg = String.format(
108 "project \"%s\" should be included in filtered projects",
109 proj.getPath());
110 assertTrue(msg, results.contains(proj.getPath()));
111 results.remove(proj.getPath());
112 }
113 assertTrue(
114 "Filtered projects shouldn't contain any unexpected results",
115 results.isEmpty());
116 }
117
118 @Test
119 public void testManifestParserWithMissingFetchOnRemote() throws Exception {
120 String baseUrl = "https://git.google.com/";
121 StringBuilder xmlContent = new StringBuilder();
122 xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
123 .append("<manifest>")
124 .append("<remote name=\"remote1\" />")
125 .append("<default revision=\"master\" remote=\"remote1\" />")
126 .append("<project path=\"foo\" name=\"").append("foo")
127 .append("\" groups=\"a,test\" />")
128 .append("<project path=\"bar\" name=\"").append("bar")
129 .append("\" groups=\"notdefault\" />")
130 .append("<project path=\"foo/a\" name=\"").append("a")
131 .append("\" groups=\"a\" />")
132 .append("<project path=\"b\" name=\"").append("b")
133 .append("\" groups=\"b\" />").append("</manifest>");
134
135 ManifestParser parser = new ManifestParser(null, null, "master",
136 baseUrl, null, null);
137 try {
138 parser.read(new ByteArrayInputStream(
139 xmlContent.toString().getBytes(UTF_8)));
140 fail("ManifestParser did not throw exception for missing fetch");
141 } catch (IOException e) {
142 assertTrue(e.getCause() instanceof SAXException);
143 assertTrue(e.getCause().getMessage()
144 .contains("is missing fetch attribute"));
145 }
146 }
147
148 void testNormalize(String in, String want) {
149 URI got = ManifestParser.normalizeEmptyPath(URI.create(in));
150 if (!got.toString().equals(want)) {
151 fail(String.format("normalize(%s) = %s want %s", in, got, want));
152 }
153 }
154
155 @Test
156 public void testNormalizeEmptyPath() {
157 testNormalize("http://a.b", "http://a.b/");
158 testNormalize("http://a.b/", "http://a.b/");
159 testNormalize("", "");
160 testNormalize("a/b", "a/b");
161 }
162 }