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