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 org.eclipse.jgit.attributes.Attribute.State.SET;
46 import static org.eclipse.jgit.attributes.Attribute.State.UNSET;
47 import static org.junit.Assert.assertEquals;
48
49 import java.io.ByteArrayInputStream;
50 import java.io.IOException;
51 import java.io.InputStream;
52
53 import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
54 import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
55 import org.eclipse.jgit.treewalk.TreeWalk;
56 import org.junit.After;
57 import org.junit.Test;
58
59
60
61
62 public class AttributesNodeTest {
63 private static final TreeWalk DUMMY_WALK = new TreeWalk(
64 new InMemoryRepository(new DfsRepositoryDescription("FooBar")));
65
66 private static final Attribute A_SET_ATTR = new Attribute("A", SET);
67
68 private static final Attribute A_UNSET_ATTR = new Attribute("A", UNSET);
69
70 private static final Attribute B_SET_ATTR = new Attribute("B", SET);
71
72 private static final Attribute B_UNSET_ATTR = new Attribute("B", UNSET);
73
74 private static final Attribute C_VALUE_ATTR = new Attribute("C", "value");
75
76 private static final Attribute C_VALUE2_ATTR = new Attribute("C", "value2");
77
78 private InputStream is;
79
80 @After
81 public void after() throws IOException {
82 if (is != null)
83 is.close();
84 }
85
86 @Test
87 public void testBasic() throws IOException {
88 String attributeFileContent = "*.type1 A -B C=value\n"
89 + "*.type2 -A B C=value2";
90
91 is = new ByteArrayInputStream(attributeFileContent.getBytes());
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
100 @Test
101 public void testNegativePattern() throws IOException {
102 String attributeFileContent = "!*.type1 A -B C=value\n"
103 + "!*.type2 -A B C=value2";
104
105 is = new ByteArrayInputStream(attributeFileContent.getBytes());
106 AttributesNode node = new AttributesNode();
107 node.parse(is);
108 assertAttribute("file.type1", node, new Attributes());
109 assertAttribute("file.type2", node, new Attributes());
110 }
111
112 @Test
113 public void testEmptyNegativeAttributeKey() throws IOException {
114 String attributeFileContent = "*.type1 - \n"
115 + "*.type2 - -A";
116 is = new ByteArrayInputStream(attributeFileContent.getBytes());
117 AttributesNode node = new AttributesNode();
118 node.parse(is);
119 assertAttribute("file.type1", node, new Attributes());
120 assertAttribute("file.type2", node, asSet(A_UNSET_ATTR));
121 }
122
123 @Test
124 public void testEmptyValueKey() throws IOException {
125 String attributeFileContent = "*.type1 = \n"
126 + "*.type2 =value\n"
127 + "*.type3 attr=\n";
128 is = new ByteArrayInputStream(attributeFileContent.getBytes());
129 AttributesNode node = new AttributesNode();
130 node.parse(is);
131 assertAttribute("file.type1", node, new Attributes());
132 assertAttribute("file.type2", node, new Attributes());
133 assertAttribute("file.type3", node, asSet(new Attribute("attr", "")));
134 }
135
136 @Test
137 public void testEmptyLine() throws IOException {
138 String attributeFileContent = "*.type1 A -B C=value\n"
139 + "\n"
140 + " \n"
141 + "*.type2 -A B C=value2";
142
143 is = new ByteArrayInputStream(attributeFileContent.getBytes());
144 AttributesNode node = new AttributesNode();
145 node.parse(is);
146 assertAttribute("file.type1", node,
147 asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
148 assertAttribute("file.type2", node,
149 asSet(A_UNSET_ATTR, B_SET_ATTR, C_VALUE2_ATTR));
150 }
151
152 @Test
153 public void testTabSeparator() throws IOException {
154 String attributeFileContent = "*.type1 \tA -B\tC=value\n"
155 + "*.type2\t -A\tB C=value2\n"
156 + "*.type3 \t\t B\n"
157 + "*.type3\t-A";
158
159 is = new ByteArrayInputStream(attributeFileContent.getBytes());
160 AttributesNode node = new AttributesNode();
161 node.parse(is);
162 assertAttribute("file.type1", node,
163 asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
164 assertAttribute("file.type2", node,
165 asSet(A_UNSET_ATTR, B_SET_ATTR, C_VALUE2_ATTR));
166 assertAttribute("file.type3", node, asSet(A_UNSET_ATTR, B_SET_ATTR));
167 }
168
169 private void assertAttribute(String path, AttributesNode node,
170 Attributes attrs) throws IOException {
171 Attributes attributes = new Attributes();
172 new AttributesHandler(DUMMY_WALK).mergeAttributes(node, path, false,
173 attributes);
174 assertEquals(attrs, attributes);
175 }
176
177 static Attributes asSet(Attribute... attrs) {
178 return new Attributes(attrs);
179 }
180
181 }