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.assertEquals;
47 import static org.junit.Assert.assertTrue;
48 import static org.junit.Assert.fail;
49
50 import java.io.ByteArrayInputStream;
51 import java.io.IOException;
52 import java.net.URI;
53 import java.util.HashSet;
54 import java.util.Set;
55 import java.util.stream.Collectors;
56 import java.util.stream.Stream;
57
58 import org.junit.Test;
59 import org.xml.sax.SAXException;
60
61 public class ManifestParserTest {
62
63 @Test
64 public void testManifestParser() throws Exception {
65 String baseUrl = "https://git.google.com/";
66 StringBuilder xmlContent = new StringBuilder();
67 Set<String> results = new HashSet<>();
68 xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
69 .append("<manifest>")
70 .append("<remote name=\"remote1\" fetch=\".\" />")
71 .append("<default revision=\"master\" remote=\"remote1\" />")
72 .append("<project path=\"foo\" name=\"")
73 .append("foo")
74 .append("\" groups=\"a,test\" />")
75 .append("<project path=\"bar\" name=\"")
76 .append("bar")
77 .append("\" groups=\"notdefault\" />")
78 .append("<project path=\"foo/a\" name=\"")
79 .append("a")
80 .append("\" groups=\"a\" />")
81 .append("<project path=\"b\" name=\"")
82 .append("b")
83 .append("\" groups=\"b\" />")
84 .append("</manifest>");
85
86 ManifestParser parser = new ManifestParser(
87 null, null, "master", baseUrl, null, null);
88 parser.read(new ByteArrayInputStream(xmlContent.toString().getBytes(UTF_8)));
89
90 results.clear();
91 results.add("foo");
92 results.add("bar");
93 results.add("foo/a");
94 results.add("b");
95 for (RepoProject proj : parser.getProjects()) {
96 String msg = String.format(
97 "project \"%s\" should be included in unfiltered projects",
98 proj.getPath());
99 assertTrue(msg, results.contains(proj.getPath()));
100 results.remove(proj.getPath());
101 }
102 assertTrue(
103 "Unfiltered projects shouldn't contain any unexpected results",
104 results.isEmpty());
105
106 results.clear();
107 results.add("foo");
108 results.add("b");
109 for (RepoProject proj : parser.getFilteredProjects()) {
110 String msg = String.format(
111 "project \"%s\" should be included in filtered projects",
112 proj.getPath());
113 assertTrue(msg, results.contains(proj.getPath()));
114 results.remove(proj.getPath());
115 }
116 assertTrue(
117 "Filtered projects shouldn't contain any unexpected results",
118 results.isEmpty());
119 }
120
121 @Test
122 public void testManifestParserWithMissingFetchOnRemote() throws Exception {
123 String baseUrl = "https://git.google.com/";
124 StringBuilder xmlContent = new StringBuilder();
125 xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
126 .append("<manifest>")
127 .append("<remote name=\"remote1\" />")
128 .append("<default revision=\"master\" remote=\"remote1\" />")
129 .append("<project path=\"foo\" name=\"").append("foo")
130 .append("\" groups=\"a,test\" />")
131 .append("<project path=\"bar\" name=\"").append("bar")
132 .append("\" groups=\"notdefault\" />")
133 .append("<project path=\"foo/a\" name=\"").append("a")
134 .append("\" groups=\"a\" />")
135 .append("<project path=\"b\" name=\"").append("b")
136 .append("\" groups=\"b\" />").append("</manifest>");
137
138 ManifestParser parser = new ManifestParser(null, null, "master",
139 baseUrl, null, null);
140 try {
141 parser.read(new ByteArrayInputStream(
142 xmlContent.toString().getBytes(UTF_8)));
143 fail("ManifestParser did not throw exception for missing fetch");
144 } catch (IOException e) {
145 assertTrue(e.getCause() instanceof SAXException);
146 assertTrue(e.getCause().getMessage()
147 .contains("is missing fetch attribute"));
148 }
149 }
150
151 @Test
152 public void testRemoveProject() throws Exception {
153 StringBuilder xmlContent = new StringBuilder();
154 xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
155 .append("<manifest>")
156 .append("<remote name=\"remote1\" fetch=\".\" />")
157 .append("<default revision=\"master\" remote=\"remote1\" />")
158 .append("<project path=\"foo\" name=\"foo\" />")
159 .append("<project path=\"bar\" name=\"bar\" />")
160 .append("<remove-project name=\"foo\" />")
161 .append("<project path=\"foo\" name=\"baz\" />")
162 .append("</manifest>");
163
164 ManifestParser parser = new ManifestParser(null, null, "master",
165 "https://git.google.com/", null, null);
166 parser.read(new ByteArrayInputStream(
167 xmlContent.toString().getBytes(UTF_8)));
168
169 assertEquals(Stream.of("bar", "baz").collect(Collectors.toSet()),
170 parser.getProjects().stream().map(RepoProject::getName)
171 .collect(Collectors.toSet()));
172 }
173
174 void testNormalize(String in, String want) {
175 URI got = ManifestParser.normalizeEmptyPath(URI.create(in));
176 if (!got.toString().equals(want)) {
177 fail(String.format("normalize(%s) = %s want %s", in, got, want));
178 }
179 }
180
181 @Test
182 public void testNormalizeEmptyPath() {
183 testNormalize("http://a.b", "http://a.b/");
184 testNormalize("http://a.b/", "http://a.b/");
185 testNormalize("", "");
186 testNormalize("a/b", "a/b");
187 }
188 }