aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/attributes/AttributesNodeTest.java
blob: dbbcb75da9d0c57b710a2113a0b487430f8d60f0 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (C) 2014, Obeo. and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.attributes;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.eclipse.jgit.attributes.Attribute.State.SET;
import static org.eclipse.jgit.attributes.Attribute.State.UNSET;
import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.junit.After;
import org.junit.Test;

/**
 * Test {@link AttributesNode}
 */
public class AttributesNodeTest {
	private static final TreeWalk DUMMY_WALK = new TreeWalk(
			new InMemoryRepository(new DfsRepositoryDescription("FooBar")));

	private static final Attribute A_SET_ATTR = new Attribute("A", SET);

	private static final Attribute A_UNSET_ATTR = new Attribute("A", UNSET);

	private static final Attribute B_SET_ATTR = new Attribute("B", SET);

	private static final Attribute B_UNSET_ATTR = new Attribute("B", UNSET);

	private static final Attribute C_VALUE_ATTR = new Attribute("C", "value");

	private static final Attribute C_VALUE2_ATTR = new Attribute("C", "value2");

	private InputStream is;

	@After
	public void after() throws IOException {
		if (is != null)
			is.close();
	}

	@Test
	public void testBasic() throws IOException {
		String attributeFileContent = "*.type1 A -B C=value\n"
				+ "*.type2 -A B C=value2";

		is = new ByteArrayInputStream(attributeFileContent.getBytes(UTF_8));
		AttributesNode node = new AttributesNode();
		node.parse(is);
		assertAttribute("file.type1", node,
				asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
		assertAttribute("file.type2", node,
				asSet(A_UNSET_ATTR, B_SET_ATTR, C_VALUE2_ATTR));
	}

	@Test
	public void testNegativePattern() throws IOException {
		String attributeFileContent = "!*.type1 A -B C=value\n"
				+ "!*.type2 -A B C=value2";

		is = new ByteArrayInputStream(attributeFileContent.getBytes(UTF_8));
		AttributesNode node = new AttributesNode();
		node.parse(is);
		assertAttribute("file.type1", node, new Attributes());
		assertAttribute("file.type2", node, new Attributes());
	}

	@Test
	public void testEmptyNegativeAttributeKey() throws IOException {
		String attributeFileContent = "*.type1 - \n" //
				+ "*.type2 -   -A";
		is = new ByteArrayInputStream(attributeFileContent.getBytes(UTF_8));
		AttributesNode node = new AttributesNode();
		node.parse(is);
		assertAttribute("file.type1", node, new Attributes());
		assertAttribute("file.type2", node, asSet(A_UNSET_ATTR));
	}

	@Test
	public void testEmptyValueKey() throws IOException {
		String attributeFileContent = "*.type1 = \n" //
				+ "*.type2 =value\n"//
				+ "*.type3 attr=\n";
		is = new ByteArrayInputStream(attributeFileContent.getBytes(UTF_8));
		AttributesNode node = new AttributesNode();
		node.parse(is);
		assertAttribute("file.type1", node, new Attributes());
		assertAttribute("file.type2", node, new Attributes());
		assertAttribute("file.type3", node, asSet(new Attribute("attr", "")));
	}

	@Test
	public void testEmptyLine() throws IOException {
		String attributeFileContent = "*.type1 A -B C=value\n" //
				+ "\n" //
				+ "    \n" //
				+ "*.type2 -A B C=value2";

		is = new ByteArrayInputStream(attributeFileContent.getBytes(UTF_8));
		AttributesNode node = new AttributesNode();
		node.parse(is);
		assertAttribute("file.type1", node,
				asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
		assertAttribute("file.type2", node,
				asSet(A_UNSET_ATTR, B_SET_ATTR, C_VALUE2_ATTR));
	}

	@Test
	public void testTabSeparator() throws IOException {
		String attributeFileContent = "*.type1 \tA -B\tC=value\n"
				+ "*.type2\t -A\tB C=value2\n" //
				+ "*.type3  \t\t   B\n" //
				+ "*.type3\t-A";//

		is = new ByteArrayInputStream(attributeFileContent.getBytes(UTF_8));
		AttributesNode node = new AttributesNode();
		node.parse(is);
		assertAttribute("file.type1", node,
				asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
		assertAttribute("file.type2", node,
				asSet(A_UNSET_ATTR, B_SET_ATTR, C_VALUE2_ATTR));
		assertAttribute("file.type3", node, asSet(A_UNSET_ATTR, B_SET_ATTR));
	}

	@Test
	public void testDoubleAsteriskAtEnd() throws IOException {
		String attributeFileContent = "dir/** \tA -B\tC=value";

		is = new ByteArrayInputStream(attributeFileContent.getBytes(UTF_8));
		AttributesNode node = new AttributesNode();
		node.parse(is);
		assertAttribute("dir", node,
				asSet(new Attribute[]{}));
		assertAttribute("dir/", node,
				asSet(new Attribute[]{}));
		assertAttribute("dir/file.type1", node,
				asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
		assertAttribute("dir/sub/", node,
				asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
		assertAttribute("dir/sub/file.type1", node,
				asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR));
	}

	private void assertAttribute(String path, AttributesNode node,
			Attributes attrs) throws IOException {
		Attributes attributes = new Attributes();
		new AttributesHandler(DUMMY_WALK,
				() -> DUMMY_WALK.getTree(CanonicalTreeParser.class))
						.mergeAttributes(node, path, false, attributes);
		assertEquals(attrs, attributes);
	}

	static Attributes asSet(Attribute... attrs) {
		return new Attributes(attrs);
	}

}