Debian Codegraph @

sdk-common/src/test/java/com/android/ide/common/res2/ResourceSetTest.java

raw
1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.ide.common.res2;
18
19 import static java.io.File.separator;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24
25 import com.android.ide.common.blame.SourceFilePosition;
26 import com.android.ide.common.blame.SourcePosition;
27 import com.android.testutils.TestUtils;
28 import com.android.utils.XmlUtils;
29
30 import org.junit.Test;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.util.List;
35
36 public class ResourceSetTest extends BaseTestCase {
37
38 @Test
39 public void testBaseResourceSetByCount() throws Exception {
40 ResourceSet resourceSet = getBaseResourceSet();
41 assertEquals(34, resourceSet.size());
42 }
43
44 @Test
45 public void testBaseResourceSetWithNormalizationByName() throws Exception {
46 ResourceSet resourceSet = getBaseResourceSet();
47
48 verifyResourceExists(resourceSet,
49 "drawable/icon",
50 "drawable/patch",
51 "raw/foo",
52 "layout/main",
53 "layout/layout_ref",
54 "layout/alias_replaced_by_file",
55 "layout/file_replaced_by_alias",
56 "drawable/color_drawable",
57 "drawable/drawable_ref",
58 "color/color",
59 "string/basic_string",
60 "string/xliff_string",
61 "string/styled_string",
62 "string/two",
63 "string/many",
64 "style/style",
65 "array/string_array",
66 "array/integer_array",
67 "array/my_colors",
68 "attr/dimen_attr",
69 "attr/string_attr",
70 "attr/enum_attr",
71 "attr/flag_attr",
72 "attr/blah",
73 "attr/blah2",
74 "attr/flagAttr",
75 "declare-styleable/declare_styleable",
76 "dimen/dimen",
77 "dimen-sw600dp-v13/offset",
78 "id/item_id",
79 "integer/integer",
80 "plurals/plurals",
81 "plurals/plurals_with_bad_quantity"
82 );
83 }
84
85 @Test
86 public void testDupResourceSet() throws Exception {
87 File root = TestUtils.getRoot("resources", "dupSet");
88
89 ResourceSet set = new ResourceSet("main", null);
90 set.addSource(new File(root, "res1"));
91 set.addSource(new File(root, "res2"));
92 boolean gotException = false;
93 RecordingLogger logger = new RecordingLogger();
94 try {
95 set.loadFromFiles(logger);
96 } catch (DuplicateDataException e) {
97 gotException = true;
98
99
100
101
102 String message = e.getMessage();
103 // Clean up paths etc for unit test
104 int index = message.indexOf("dupSet");
105 assertTrue(index != -1);
106 String prefix = message.substring(0, index);
107 message = message.replace(prefix, "<PREFIX>").replace('\\','/');
108 assertEquals("<PREFIX>dupSet/res1/drawable/icon.png\t<PREFIX>dupSet/res2/drawable/icon.png: "
109 + "Error: Duplicate resources", message);
110 }
111
112 checkLogger(logger);
113 assertTrue(gotException);
114 }
115
116 @Test
117 public void testBrokenSet() throws Exception {
118 File root = TestUtils.getRoot("resources", "brokenSet");
119
120 ResourceSet set = new ResourceSet("main", null);
121 set.addSource(root);
122
123 boolean gotException = false;
124 RecordingLogger logger = new RecordingLogger();
125 try {
126 set.loadFromFiles(logger);
127 } catch (MergingException e) {
128 gotException = true;
129 assertEquals(new File(root, "values" + separator + "dimens.xml").getAbsolutePath() +
130 ":1:1: Error: Content is not allowed in prolog.",
131 e.getMessage());
132 }
133
134 assertTrue("ResourceSet processing should have failed, but didn't", gotException);
135 assertFalse(logger.getErrorMsgs().isEmpty());
136 }
137
138 @Test
139 public void testBrokenSet2() throws Exception {
140 File root = TestUtils.getRoot("resources", "brokenSet2");
141
142 ResourceSet set = new ResourceSet("main", null);
143 set.addSource(root);
144
145 boolean gotException = false;
146 RecordingLogger logger = new RecordingLogger();
147 try {
148 set.loadFromFiles(logger);
149 } catch (MergingException e) {
150 gotException = true;
151 assertEquals(new File(root, "values" + separator + "values.xml").getAbsolutePath() +
152 ": Error: Found item String/app_name more than one time",
153 e.getMessage());
154 }
155
156 assertTrue("ResourceSet processing should have failed, but didn't", gotException);
157 assertFalse(logger.getErrorMsgs().isEmpty());
158 }
159
160 @Test
161 public void testBrokenSet3() throws Exception {
162 File root = TestUtils.getRoot("resources", "brokenSet3");
163
164 ResourceSet set = new ResourceSet("main", null);
165 set.addSource(root);
166
167 boolean gotException = false;
168 RecordingLogger logger = new RecordingLogger();
169 try {
170 set.loadFromFiles(logger);
171 } catch (MergingException e) {
172 gotException = true;
173 assertEquals(new File(root, "values" + separator + "values.xml").getAbsolutePath() +
174 ": Error: Found item Attr/d_common_attr more than one time",
175 e.getMessage());
176 }
177
178 assertTrue("ResourceSet processing should have failed, but didn't", gotException);
179 assertFalse(logger.getErrorMsgs().isEmpty());
180 }
181
182 @Test
183 public void testBrokenSet4() throws Exception {
184 File root = TestUtils.getRoot("resources", "brokenSet4");
185
186 ResourceSet set = new ResourceSet("main", null);
187 set.addSource(root);
188
189 boolean gotException = false;
190 RecordingLogger logger = new RecordingLogger();
191 try {
192 set.loadFromFiles(logger);
193 } catch (MergingException e) {
194 gotException = true;
195 assertEquals(new File(root, "values" + separator + "values.xml").getAbsolutePath() +
196 ":7:6: Error: The element type \"declare-styleable\" "
197 + "must be terminated by the matching end-tag \"</declare-styleable>\".",
198 e.getMessage());
199 }
200
201 assertTrue("ResourceSet processing should have failed, but didn't", gotException);
202 assertFalse(logger.getErrorMsgs().isEmpty());
203 }
204
205 @Test
206 public void testBrokenSetBadType() throws Exception {
207 File root = TestUtils.getRoot("resources", "brokenSetBadType");
208
209 ResourceSet set = new ResourceSet("main", null);
210 set.addSource(root);
211
212 boolean gotException = false;
213 RecordingLogger logger = new RecordingLogger();
214 try {
215 set.loadFromFiles(logger);
216 } catch (MergingException e) {
217 gotException = true;
218 assertEquals(new File(root, "values" + separator + "dimens.xml").getAbsolutePath() +
219 ": Error: Unsupported type 'dimenot'",
220 e.getMessage());
221 }
222
223 assertTrue("ResourceSet processing should have failed, but didn't", gotException);
224 assertFalse(logger.getErrorMsgs().isEmpty());
225 }
226
227 @Test
228 public void testBrokenSetBadType2() throws Exception {
229 File root = TestUtils.getRoot("resources", "brokenSetBadType2");
230
231 ResourceSet set = new ResourceSet("main", null);
232 set.addSource(root);
233
234 boolean gotException = false;
235 RecordingLogger logger = new RecordingLogger();
236 try {
237 set.loadFromFiles(logger);
238 } catch (MergingException e) {
239 gotException = true;
240 assertEquals(new File(root, "values" + separator + "dimens.xml").getAbsolutePath() +
241 ": Error: Unsupported type 'dimenot2'",
242 e.getMessage());
243 }
244
245 assertTrue("ResourceSet processing should have failed, but didn't", gotException);
246 assertFalse(logger.getErrorMsgs().isEmpty());
247 }
248
249 @Test
250 public void testTrackSourcePositions() throws IOException, MergingException {
251 File root = TestUtils.getRoot("resources", "baseSet");
252
253 // By default, track positions.
254 ResourceSet resourceSet = new ResourceSet("main", null);
255 resourceSet.addSource(root);
256 RecordingLogger logger = new RecordingLogger();
257 resourceSet.loadFromFiles(logger);
258
259 checkLogger(logger);
260 String stringKey = "string/basic_string";
261 List<ResourceItem> resources = resourceSet.getDataMap().get(stringKey);
262 assertNotNull(resources);
263 assertFalse(resources.isEmpty());
264 assertEquals(new SourcePosition(13, 4, 529, 13, 53, 578),
265 XmlUtils.getSourceFilePosition(resources.get(0).getValue()).getPosition());
266
267 // Try without positions.
268 resourceSet = new ResourceSet("main", null);
269 resourceSet.addSource(root);
270 resourceSet.setTrackSourcePositions(false);
271 logger = new RecordingLogger();
272 resourceSet.loadFromFiles(logger);
273
274 resources = resourceSet.getDataMap().get(stringKey);
275 assertNotNull(resources);
276 assertFalse(resources.isEmpty());
277 assertEquals(SourceFilePosition.UNKNOWN,
278 XmlUtils.getSourceFilePosition(resources.get(0).getValue()));
279 }
280
281 static ResourceSet getBaseResourceSet() throws MergingException, IOException {
282 File root = TestUtils.getRoot("resources", "baseSet");
283
284 ResourceSet resourceSet = new ResourceSet("main", null);
285 resourceSet.addSource(root);
286 RecordingLogger logger = new RecordingLogger();
287 resourceSet.loadFromFiles(logger);
288
289 checkLogger(logger);
290
291 return resourceSet;
292 }
293 }