Debian Codegraph @

build-system/manifest-merger/src/test/java/com/android/manifmerger/PostValidatorTest.java

raw
1 /*
2 * Copyright (C) 2014 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.manifmerger;
18
19 import com.android.SdkConstants;
20 import com.android.utils.ILogger;
21 import com.google.common.base.Joiner;
22
23 import junit.framework.TestCase;
24
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.w3c.dom.Element;
28 import org.w3c.dom.Node;
29 import org.xml.sax.SAXException;
30
31 import java.io.IOException;
32
33 import javax.xml.parsers.ParserConfigurationException;
34
35 /**
36 * Tests for the {@link com.android.manifmerger.PostValidator} class.
37 */
38 public class PostValidatorTest extends TestCase {
39
40 @Mock
41 ILogger mILogger;
42
43 @Override
44 protected void setUp() throws Exception {
45 super.setUp();
46 MockitoAnnotations.initMocks(this);
47 }
48
49 public void testIncorrectRemove()
50 throws ParserConfigurationException, SAXException, IOException {
51
52 String main = ""
53 + "<manifest\n"
54 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
55 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
56 + " package=\"com.example.lib3\">\n"
57 + "\n"
58 + " <application android:label=\"@string/lib_name\" />\n"
59 + "\n"
60 + " <activity android:name=\"activityOne\" tools:remove=\"exported\"/>\n"
61 + "\n"
62 + "</manifest>";
63
64 String library = ""
65 + "<manifest\n"
66 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
67 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
68 + " package=\"com.example.lib3\">\n"
69 + "\n"
70 + " <application android:label=\"@string/lib_name\" />\n"
71 + "\n"
72 + "\n"
73 + " <activity android:name=\"activityOne\"/>"
74 + "\n"
75 + "</manifest>";
76
77 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
78 TestUtils.sourceFile(getClass(), "testIncorrectRemoveMain"), main);
79
80 XmlDocument libraryDocument = TestUtils.xmlDocumentFromString(
81 TestUtils.sourceFile(getClass(), "testIncorrectRemoveLib"), library);
82
83 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mILogger);
84 mainDocument.merge(libraryDocument, mergingReportBuilder);
85
86 PostValidator.validate(mainDocument, mergingReportBuilder);
87 for (MergingReport.Record record : mergingReportBuilder.build().getLoggingRecords()) {
88 if (record.getSeverity() == MergingReport.Record.Severity.WARNING
89 && record.toString().contains("PostValidatorTest#testIncorrectRemoveMain:8")) {
90 return;
91 }
92 }
93 fail("No reference to faulty PostValidatorTest#testIncorrectRemoveMain:8 found in: \n" +
94 Joiner.on("\n ").join(mergingReportBuilder.build().getLoggingRecords()));
95 }
96
97 public void testIncorrectReplace()
98 throws ParserConfigurationException, SAXException, IOException {
99
100 String main = ""
101 + "<manifest\n"
102 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
103 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
104 + " package=\"com.example.lib3\">\n"
105 + "\n"
106 + " <application android:label=\"@string/lib_name\" />\n"
107 + "\n"
108 + " <activity android:name=\"activityOne\" "
109 + " android:exported=\"false\""
110 + " tools:replace=\"exported\"/>\n"
111 + "\n"
112 + "</manifest>";
113
114 String library = ""
115 + "<manifest\n"
116 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
117 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
118 + " package=\"com.example.lib3\">\n"
119 + "\n"
120 + " <application android:label=\"@string/lib_name\" />\n"
121 + "\n"
122 + " <activity android:name=\"activityOne\"/>"
123 + "\n"
124 + "</manifest>";
125
126 XmlDocument mainDocument = TestUtils.xmlDocumentFromString(
127 TestUtils.sourceFile(getClass(), "testIncorrectReplaceMain"), main);
128
129 XmlDocument libraryDocument = TestUtils.xmlDocumentFromString(
130 TestUtils.sourceFile(getClass(), "testIncorrectReplaceLib"), library);
131
132 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mILogger);
133 mainDocument.merge(libraryDocument, mergingReportBuilder);
134
135 PostValidator.validate(mainDocument, mergingReportBuilder);
136 for (MergingReport.Record record : mergingReportBuilder.build().getLoggingRecords()) {
137 if (record.getSeverity() == MergingReport.Record.Severity.WARNING
138 && record.toString().contains("PostValidatorTest#testIncorrectReplaceMain:8")) {
139 return;
140 }
141 }
142 fail("No reference to faulty PostValidatorTest#testIncorrectRemoveMain:8 found in: \n" +
143 Joiner.on("\n ").join(mergingReportBuilder.build().getLoggingRecords()));
144 }
145
146 public void testApplicationInvalidOrder()
147 throws ParserConfigurationException, SAXException, IOException {
148
149 String input = ""
150 + "<manifest\n"
151 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
152 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
153 + " package=\"com.example.lib3\">\n"
154 + "\n"
155 + " <activity android:name=\"activityOne\"/>"
156 + "\n"
157 + " <application android:label=\"@string/lib_name\" />\n"
158 + "\n"
159 + " <uses-sdk minSdkVersion=\"14\"/>"
160 + "\n"
161 + "</manifest>";
162
163 XmlDocument xmlDocument = TestUtils.xmlDocumentFromString(
164 TestUtils.sourceFile(getClass(), "testApplicationInvalidOrder"), input);
165
166 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mILogger);
167 PostValidator.validate(xmlDocument, mergingReportBuilder);
168 // ensure application element is last.
169 Node lastChild = xmlDocument.getRootNode().getXml().getLastChild();
170 while(lastChild.getNodeType() != Node.ELEMENT_NODE) {
171 lastChild = lastChild.getPreviousSibling();
172 }
173 OrphanXmlElement xmlElement = new OrphanXmlElement((Element) lastChild);
174 assertEquals(ManifestModel.NodeTypes.APPLICATION, xmlElement.getType());
175 }
176
177 public void testApplicationInvalidOrder_withComments()
178 throws ParserConfigurationException, SAXException, IOException {
179
180 String input = ""
181 + "<manifest\n"
182 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
183 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
184 + " package=\"com.example.lib3\">\n"
185 + "\n"
186 + " <activity android:name=\"activityOne\"/>"
187 + "\n"
188 + " <!-- with comments ! -->"
189 + " <application android:label=\"@string/lib_name\" />\n"
190 + "\n"
191 + " <uses-sdk minSdkVersion=\"14\"/>"
192 + "\n"
193 + "</manifest>";
194
195 XmlDocument xmlDocument = TestUtils.xmlDocumentFromString(
196 TestUtils.sourceFile(getClass(), "testApplicationInvalidOrder"), input);
197
198 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mILogger);
199 PostValidator.validate(xmlDocument, mergingReportBuilder);
200 System.out.println(xmlDocument.prettyPrint());
201 // ensure application element is last.
202 Node lastChild = xmlDocument.getRootNode().getXml().getLastChild();
203 while(lastChild.getNodeType() != Node.ELEMENT_NODE) {
204 lastChild = lastChild.getPreviousSibling();
205 }
206 OrphanXmlElement xmlElement = new OrphanXmlElement((Element) lastChild);
207 assertEquals(ManifestModel.NodeTypes.APPLICATION, xmlElement.getType());
208 // check the comment was also moved.
209 assertEquals(Node.COMMENT_NODE, lastChild.getPreviousSibling().getNodeType());
210 }
211
212 public void testApplicationValidOrder()
213 throws ParserConfigurationException, SAXException, IOException {
214
215 String input = ""
216 + "<manifest\n"
217 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
218 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
219 + " package=\"com.example.lib3\">\n"
220 + "\n"
221 + " <activity android:name=\"activityOne\"/>"
222 + "\n"
223 + " <uses-sdk minSdkVersion=\"14\"/>"
224 + "\n"
225 + " <application android:label=\"@string/lib_name\" />\n"
226 + "\n"
227 + "</manifest>";
228
229 XmlDocument xmlDocument = TestUtils.xmlDocumentFromString(
230 TestUtils.sourceFile(getClass(), "testApplicationValidOrder"), input);
231
232 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mILogger);
233 PostValidator.validate(xmlDocument, mergingReportBuilder);
234 // ensure application element is last.
235 Node lastChild = xmlDocument.getRootNode().getXml().getLastChild();
236 while(lastChild.getNodeType() != Node.ELEMENT_NODE) {
237 lastChild = lastChild.getPreviousSibling();
238 }
239 OrphanXmlElement xmlElement = new OrphanXmlElement((Element) lastChild);
240 assertEquals(ManifestModel.NodeTypes.APPLICATION, xmlElement.getType());
241 }
242
243 public void testUsesSdkInvalidOrder()
244 throws ParserConfigurationException, SAXException, IOException {
245
246 String input = ""
247 + "<manifest\n"
248 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
249 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
250 + " package=\"com.example.lib3\">\n"
251 + "\n"
252 + " <activity android:name=\"activityOne\"/>"
253 + "\n"
254 + " <application android:label=\"@string/lib_name\" />\n"
255 + "\n"
256 + " <uses-sdk minSdkVersion=\"14\"/>"
257 + "\n"
258 + "</manifest>";
259
260 XmlDocument xmlDocument = TestUtils.xmlDocumentFromString(
261 TestUtils.sourceFile(getClass(), "testUsesSdkInvalidOrder"), input);
262
263 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mILogger);
264 PostValidator.validate(xmlDocument, mergingReportBuilder);
265 // ensure uses-sdk element is first.
266 Node firstChild = xmlDocument.getRootNode().getXml().getFirstChild();
267 while(firstChild.getNodeType() != Node.ELEMENT_NODE) {
268 firstChild = firstChild.getNextSibling();
269 }
270 OrphanXmlElement xmlElement = new OrphanXmlElement((Element) firstChild);
271 assertEquals(ManifestModel.NodeTypes.USES_SDK, xmlElement.getType());
272 }
273
274 public void testUsesSdkInvalidOrder_withComments()
275 throws ParserConfigurationException, SAXException, IOException {
276
277 String input = ""
278 + "<manifest\n"
279 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
280 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
281 + " package=\"com.example.lib3\">\n"
282 + "\n"
283 + " <activity android:name=\"activityOne\"/>"
284 + "\n"
285 + " <application android:label=\"@string/lib_name\" />\n"
286 + "\n"
287 + " <!-- with comments ! -->"
288 + " <uses-sdk minSdkVersion=\"14\"/>"
289 + "\n"
290 + "</manifest>";
291
292 XmlDocument xmlDocument = TestUtils.xmlDocumentFromString(
293 TestUtils.sourceFile(getClass(), "testUsesSdkInvalidOrder"), input);
294
295 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mILogger);
296 PostValidator.validate(xmlDocument, mergingReportBuilder);
297 System.out.println(xmlDocument.prettyPrint());
298 // ensure uses-sdk element is first.
299 Node firstChild = xmlDocument.getRootNode().getXml().getFirstChild();
300 while(firstChild.getNodeType() != Node.ELEMENT_NODE) {
301 firstChild = firstChild.getNextSibling();
302 }
303 OrphanXmlElement xmlElement = new OrphanXmlElement((Element) firstChild);
304 assertEquals(ManifestModel.NodeTypes.USES_SDK, xmlElement.getType());
305 // check the comment was also moved.
306 assertEquals(Node.COMMENT_NODE, firstChild.getPreviousSibling().getNodeType());
307 }
308
309 public void testUsesSdkValidOrder()
310 throws ParserConfigurationException, SAXException, IOException {
311
312 String input = ""
313 + "<manifest\n"
314 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
315 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
316 + " package=\"com.example.lib3\">\n"
317 + "\n"
318 + " <uses-sdk minSdkVersion=\"14\"/>"
319 + "\n"
320 + " <activity android:name=\"activityOne\"/>"
321 + "\n"
322 + " <application android:label=\"@string/lib_name\" />\n"
323 + "\n"
324 + "</manifest>";
325
326 XmlDocument xmlDocument = TestUtils.xmlDocumentFromString(
327 TestUtils.sourceFile(getClass(), "testUsesSdkValidOrder"), input);
328
329 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mILogger);
330 PostValidator.validate(xmlDocument, mergingReportBuilder);
331 // ensure uses-sdk element is first.
332 Node firstChild = xmlDocument.getRootNode().getXml().getFirstChild();
333 while(firstChild.getNodeType() != Node.ELEMENT_NODE) {
334 firstChild = firstChild.getNextSibling();
335 }
336 OrphanXmlElement xmlElement = new OrphanXmlElement((Element) firstChild);
337 assertEquals(ManifestModel.NodeTypes.USES_SDK, xmlElement.getType());
338 }
339
340 public void testAndroidNamespacePresence()
341 throws ParserConfigurationException, SAXException, IOException {
342
343 String input = ""
344 + "<manifest\n"
345 + " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
346 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
347 + " package=\"com.example.lib3\">\n"
348 + "\n"
349 + " <uses-sdk minSdkVersion=\"14\"/>"
350 + "\n"
351 + " <application android:label=\"@string/lib_name\" />\n"
352 + "\n"
353 + "</manifest>";
354
355 XmlDocument xmlDocument = TestUtils.xmlDocumentFromString(
356 TestUtils.sourceFile(getClass(), "testApplicationInvalidOrder"), input);
357
358 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mILogger);
359 PostValidator.validate(xmlDocument, mergingReportBuilder);
360 // ensure application element is last.
361 String attribute = xmlDocument.getRootNode().getXml().getAttribute("xmlns:android");
362 assertEquals(SdkConstants.ANDROID_URI, attribute);
363 }
364
365 public void testAndroidNamespacePresence_differentPrefix()
366 throws ParserConfigurationException, SAXException, IOException {
367
368 String input = ""
369 + "<manifest\n"
370 + " xmlns:A=\"http://schemas.android.com/apk/res/android\"\n"
371 + " xmlns:tools=\"http://schemas.android.com/tools\"\n"
372 + " package=\"com.example.lib3\">\n"
373 + "\n"
374 + " <uses-sdk A:minSdkVersion=\"14\"/>"
375 + "\n"
376 + " <application A:label=\"@string/lib_name\" />\n"
377 + "\n"
378 + "</manifest>";
379
380 XmlDocument xmlDocument = TestUtils.xmlDocumentFromString(
381 TestUtils.sourceFile(getClass(), "testApplicationInvalidOrder"), input);
382
383 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mILogger);
384 PostValidator.validate(xmlDocument, mergingReportBuilder);
385 // ensure application element is last.
386 String attribute = xmlDocument.getRootNode().getXml().getAttribute("xmlns:A");
387 assertEquals(SdkConstants.ANDROID_URI, attribute);
388 }
389
390 public void testAndroidNamespaceAbsence()
391 throws ParserConfigurationException, SAXException, IOException {
392
393 String input = ""
394 + "<manifest\n"
395 + " package=\"com.example.lib3\">\n"
396 + "\n"
397 + " <application />\n"
398 + "\n"
399 + "</manifest>";
400
401 XmlDocument xmlDocument = TestUtils.xmlDocumentFromString(
402 TestUtils.sourceFile(getClass(), "testApplicationInvalidOrder"), input);
403
404 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mILogger);
405 PostValidator.validate(xmlDocument, mergingReportBuilder);
406 // ensure application element is last.
407 String attribute = xmlDocument.getRootNode().getXml().getAttribute("xmlns:android");
408 assertEquals(SdkConstants.ANDROID_URI, attribute);
409 }
410 }