Debian Codegraph @

brut.apktool/apktool-lib/src/test/java/brut/androlib/BaseTest.java

raw
1 /*
2 * Copyright (C) 2010 Ryszard Wiśniewski <[email protected]>
3 * Copyright (C) 2010 Connor Tumbleson <[email protected]>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package brut.androlib;
18
19 import brut.androlib.meta.MetaInfo;
20 import brut.common.BrutException;
21 import brut.directory.ExtFile;
22 import brut.directory.FileDirectory;
23 import org.custommonkey.xmlunit.*;
24 import org.xml.sax.SAXException;
25
26 import java.io.File;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.Reader;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.logging.Logger;
33
34 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
35 import static org.junit.Assert.*;
36
37 public class BaseTest {
38
39 protected void compareUnknownFiles() throws BrutException {
40 MetaInfo control = new Androlib().readMetaFile(sTestOrigDir);
41 MetaInfo test = new Androlib().readMetaFile(sTestNewDir);
42 assertNotNull(control.unknownFiles);
43 assertNotNull(test.unknownFiles);
44
45 Map<String, String> controlFiles = control.unknownFiles;
46 Map<String, String> testFiles = test.unknownFiles;
47 assertEquals(controlFiles.size(), testFiles.size());
48
49 // Make sure that the compression methods are still the same
50 for (Map.Entry<String, String> controlEntry : controlFiles.entrySet()) {
51 assertEquals(controlEntry.getValue(), testFiles.get(controlEntry.getKey()));
52 }
53 }
54
55 protected void compareBinaryFolder(String path, boolean res) throws BrutException, IOException {
56 boolean exists = true;
57
58 String prefixPath = "";
59 if (res) {
60 prefixPath = File.separatorChar + "res" + File.separatorChar;
61 }
62
63 String location = prefixPath + path;
64
65 FileDirectory fileDirectory = new FileDirectory(sTestOrigDir, location);
66
67 Set<String> files = fileDirectory.getFiles(true);
68 for (String filename : files) {
69
70 File control = new File((sTestOrigDir + location), filename);
71 File test = new File((sTestNewDir + location), filename);
72
73 if (! test.isFile() || ! control.isFile()) {
74 exists = false;
75 }
76 }
77
78 assertTrue(exists);
79 }
80
81 protected void compareResFolder(String path) throws BrutException, IOException {
82 compareBinaryFolder(path, true);
83 }
84
85 protected void compareLibsFolder(String path) throws BrutException, IOException {
86 compareBinaryFolder(File.separatorChar + path, false);
87 }
88
89 protected void compareAssetsFolder(String path) throws BrutException, IOException {
90 compareBinaryFolder(File.separatorChar + "assets" + File.separatorChar + path, false);
91 }
92
93 protected void compareValuesFiles(String path) throws BrutException {
94 compareXmlFiles("res/" + path, new ElementNameAndAttributeQualifier("name"));
95 }
96
97 protected void compareXmlFiles(String path) throws BrutException {
98 compareXmlFiles(path, null);
99 }
100
101 protected void checkFolderExists(String path) {
102 File f = new File(sTestNewDir, path);
103
104 assertTrue(f.isDirectory());
105 }
106
107 protected boolean isTransparent(int pixel) {
108 return pixel >> 24 == 0x00;
109 }
110
111 private void compareXmlFiles(String path, ElementQualifier qualifier) throws BrutException {
112 DetailedDiff diff;
113 try {
114 Reader control = new FileReader(new File(sTestOrigDir, path));
115 Reader test = new FileReader(new File(sTestNewDir, path));
116
117 if (qualifier == null) {
118 XMLUnit.setIgnoreWhitespace(true);
119 XMLUnit.setIgnoreAttributeOrder(true);
120 XMLUnit.setCompareUnmatched(false);
121 assertXMLEqual(control, test);
122 return;
123 }
124
125 diff = new DetailedDiff(new Diff(control, test));
126 } catch (SAXException | IOException ex) {
127 throw new BrutException(ex);
128 }
129
130 diff.overrideElementQualifier(qualifier);
131 assertTrue(path + ": " + diff.getAllDifferences().toString(), diff.similar());
132 }
133
134 protected static ExtFile sTmpDir;
135 protected static ExtFile sTestOrigDir;
136 protected static ExtFile sTestNewDir;
137
138 protected final static Logger LOGGER = Logger.getLogger(BaseTest.class.getName());
139 }