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 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   * Test {@link AttributesNode}
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 }