You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ManifestParserTest.java 6.9KB

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