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.

AttributesNodeTest.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2014, Obeo. 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.attributes;
  11. import static java.nio.charset.StandardCharsets.UTF_8;
  12. import static org.eclipse.jgit.attributes.Attribute.State.SET;
  13. import static org.eclipse.jgit.attributes.Attribute.State.UNSET;
  14. import static org.junit.Assert.assertEquals;
  15. import java.io.ByteArrayInputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
  19. import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
  20. import org.eclipse.jgit.treewalk.TreeWalk;
  21. import org.junit.After;
  22. import org.junit.Test;
  23. /**
  24. * Test {@link AttributesNode}
  25. */
  26. public class AttributesNodeTest {
  27. private static final TreeWalk DUMMY_WALK = new TreeWalk(
  28. new InMemoryRepository(new DfsRepositoryDescription("FooBar")));
  29. private static final Attribute A_SET_ATTR = new Attribute("A", SET);
  30. private static final Attribute A_UNSET_ATTR = new Attribute("A", UNSET);
  31. private static final Attribute B_SET_ATTR = new Attribute("B", SET);
  32. private static final Attribute B_UNSET_ATTR = new Attribute("B", UNSET);
  33. private static final Attribute C_VALUE_ATTR = new Attribute("C", "value");
  34. private static final Attribute C_VALUE2_ATTR = new Attribute("C", "value2");
  35. private InputStream is;
  36. @After
  37. public void after() throws IOException {
  38. if (is != null)
  39. is.close();
  40. }
  41. @Test
  42. public void testBasic() throws IOException {
  43. String attributeFileContent = "*.type1 A -B C=value\n"
  44. + "*.type2 -A B C=value2";
  45. is = new ByteArrayInputStream(attributeFileContent.getBytes(UTF_8));
  46. AttributesNode node = new AttributesNode();
  47. node.parse(is);
  48. assertAttribute("file.type1", node,
  49. asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
  50. assertAttribute("file.type2", node,
  51. asSet(A_UNSET_ATTR, B_SET_ATTR, C_VALUE2_ATTR));
  52. }
  53. @Test
  54. public void testNegativePattern() throws IOException {
  55. String attributeFileContent = "!*.type1 A -B C=value\n"
  56. + "!*.type2 -A B C=value2";
  57. is = new ByteArrayInputStream(attributeFileContent.getBytes(UTF_8));
  58. AttributesNode node = new AttributesNode();
  59. node.parse(is);
  60. assertAttribute("file.type1", node, new Attributes());
  61. assertAttribute("file.type2", node, new Attributes());
  62. }
  63. @Test
  64. public void testEmptyNegativeAttributeKey() throws IOException {
  65. String attributeFileContent = "*.type1 - \n" //
  66. + "*.type2 - -A";
  67. is = new ByteArrayInputStream(attributeFileContent.getBytes(UTF_8));
  68. AttributesNode node = new AttributesNode();
  69. node.parse(is);
  70. assertAttribute("file.type1", node, new Attributes());
  71. assertAttribute("file.type2", node, asSet(A_UNSET_ATTR));
  72. }
  73. @Test
  74. public void testEmptyValueKey() throws IOException {
  75. String attributeFileContent = "*.type1 = \n" //
  76. + "*.type2 =value\n"//
  77. + "*.type3 attr=\n";
  78. is = new ByteArrayInputStream(attributeFileContent.getBytes(UTF_8));
  79. AttributesNode node = new AttributesNode();
  80. node.parse(is);
  81. assertAttribute("file.type1", node, new Attributes());
  82. assertAttribute("file.type2", node, new Attributes());
  83. assertAttribute("file.type3", node, asSet(new Attribute("attr", "")));
  84. }
  85. @Test
  86. public void testEmptyLine() throws IOException {
  87. String attributeFileContent = "*.type1 A -B C=value\n" //
  88. + "\n" //
  89. + " \n" //
  90. + "*.type2 -A B C=value2";
  91. is = new ByteArrayInputStream(attributeFileContent.getBytes(UTF_8));
  92. AttributesNode node = new AttributesNode();
  93. node.parse(is);
  94. assertAttribute("file.type1", node,
  95. asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
  96. assertAttribute("file.type2", node,
  97. asSet(A_UNSET_ATTR, B_SET_ATTR, C_VALUE2_ATTR));
  98. }
  99. @Test
  100. public void testTabSeparator() throws IOException {
  101. String attributeFileContent = "*.type1 \tA -B\tC=value\n"
  102. + "*.type2\t -A\tB C=value2\n" //
  103. + "*.type3 \t\t B\n" //
  104. + "*.type3\t-A";//
  105. is = new ByteArrayInputStream(attributeFileContent.getBytes(UTF_8));
  106. AttributesNode node = new AttributesNode();
  107. node.parse(is);
  108. assertAttribute("file.type1", node,
  109. asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
  110. assertAttribute("file.type2", node,
  111. asSet(A_UNSET_ATTR, B_SET_ATTR, C_VALUE2_ATTR));
  112. assertAttribute("file.type3", node, asSet(A_UNSET_ATTR, B_SET_ATTR));
  113. }
  114. @Test
  115. public void testDoubleAsteriskAtEnd() throws IOException {
  116. String attributeFileContent = "dir/** \tA -B\tC=value";
  117. is = new ByteArrayInputStream(attributeFileContent.getBytes(UTF_8));
  118. AttributesNode node = new AttributesNode();
  119. node.parse(is);
  120. assertAttribute("dir", node,
  121. asSet(new Attribute[]{}));
  122. assertAttribute("dir/", node,
  123. asSet(new Attribute[]{}));
  124. assertAttribute("dir/file.type1", node,
  125. asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
  126. assertAttribute("dir/sub/", node,
  127. asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
  128. assertAttribute("dir/sub/file.type1", node,
  129. asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
  130. }
  131. private void assertAttribute(String path, AttributesNode node,
  132. Attributes attrs) throws IOException {
  133. Attributes attributes = new Attributes();
  134. new AttributesHandler(DUMMY_WALK).mergeAttributes(node, path, false,
  135. attributes);
  136. assertEquals(attrs, attributes);
  137. }
  138. static Attributes asSet(Attribute... attrs) {
  139. return new Attributes(attrs);
  140. }
  141. }