View Javadoc
1   /*
2    * Copyright (C) 2014, Obeo.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Test {@link AttributesNode}
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 }