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