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.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  		// Unfiltered projects should have them all.
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 		// Filtered projects should have foo & b
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 }