View Javadoc
1   /*
2    * Copyright (C) 2015, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  		// Unfiltered projects should have them all.
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 		// Filtered projects should have foo & b
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 }