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 5.8KB

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