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