Debian Codegraph @

sdk-common/src/test/java/com/android/ide/common/res2/BaseTestCase.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 org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22
23 import com.android.annotations.NonNull;
24 import com.android.testutils.TestUtils;
25 import com.google.common.base.Charsets;
26 import com.google.common.collect.ListMultimap;
27 import com.google.common.io.Files;
28
29 import java.awt.image.BufferedImage;
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.regex.Matcher;
34
35 import javax.imageio.ImageIO;
36
37 public abstract class BaseTestCase {
38
39 protected void verifyResourceExists(DataMap<? extends DataItem> dataMap,
40 String... dataItemKeys) {
41 ListMultimap<String, ? extends DataItem> map = dataMap.getDataMap();
42
43 for (String resKey : dataItemKeys) {
44 List<? extends DataItem> items = map.get(resKey);
45 assertTrue("resource '" + resKey + "' is missing!", !items.isEmpty());
46 }
47 }
48
49 /**
50 * Compares two resource maps.
51 *
52 * if <var>fullCompare</var> is <code>true</code> then it'll make sure the multimaps contains
53 * the same number of items, otherwise it'll only checks that each resource key is present
54 * in both maps.
55 *
56 * @param dataMap1 the first resource Map
57 * @param dataMap2 the second resource Map
58 * @param fullCompare whether a full compare is requested.
59 */
60 protected void compareResourceMaps(DataMap<? extends DataItem> dataMap1,
61 DataMap<? extends DataItem> dataMap2,
62 boolean fullCompare) {
63 assertEquals(dataMap1.size(), dataMap2.size());
64
65 // compare the resources are all the same
66 ListMultimap<String, ? extends DataItem> map1 = dataMap1.getDataMap();
67 ListMultimap<String, ? extends DataItem> map2 = dataMap2.getDataMap();
68 for (String key : map1.keySet()) {
69 List<? extends DataItem> items1 = map1.get(key);
70 List<? extends DataItem> items2 = map2.get(key);
71 if (fullCompare) {
72 assertEquals("Wrong size for " + key, items1.size(), items2.size());
73 } else {
74 boolean map1HasItem = !items1.isEmpty();
75 boolean map2HasItem = !items2.isEmpty();
76 assertEquals("resource " + key + " missing from one map", map1HasItem, map2HasItem);
77 }
78 }
79 }
80
81 protected static void checkImageColor(File file, int expectedColor) throws IOException {
82 assertTrue("File '" + file.getAbsolutePath() + "' does not exist.", file.isFile());
83
84 BufferedImage image = ImageIO.read(file);
85 int rgb = image.getRGB(0, 0);
86 assertEquals(String.format("Expected: 0x%08X, actual: 0x%08X for file %s",
87 expectedColor, rgb, file),
88 expectedColor, rgb);
89 }
90
91 /**
92 * Returns a folder containing a merger blob data for the given test data folder.
93 *
94 * The folder is expected to contain a blob file.
95 *
96 * This is to work around the fact that the merger blob data contains full path, but we don't
97 * know where this project is located on the drive. This rewrites the blob to contain the
98 * actual folder.
99 * (The blobs written in the test data contains placeholders for the path root and path
100 * separators)
101 *
102 * @param folder the folder to use as the root folder when recreating paths.
103 *
104 * @return a new file that contains the merge blob
105 * @throws java.io.IOException
106 */
107 protected static File getMergedBlobFolder(@NonNull File folder) throws IOException {
108 File originalMerger = new File(folder, DataMerger.FN_MERGER_XML);
109 return getMergedBlobFolder(folder, originalMerger);
110 }
111
112 /**
113 * Returns a folder containing a merger blob data for the given test data folder, and the given
114 * merger file.
115 *
116 * This is to work around the fact that the merger blob data contains full path, but we don't
117 * know where this project is located on the drive. This rewrites the blob to contain the
118 * actual folder.
119 * (The blobs written in the test data contains placeholders for the path root and path
120 * separators)
121 *
122 * @param folder the folder to use as the root folder when recreating paths.
123 * @param mergerFile the merger file.
124 *
125 * @return a new file that contains the merge blob
126 * @throws java.io.IOException
127 */
128 protected static File getMergedBlobFolder(@NonNull File folder, @NonNull File mergerFile)
129 throws IOException {
130
131 String content = Files.toString(mergerFile, Charsets.UTF_8);
132
133 // search and replace $TOP$ with the root and $SEP$ with the platform separator.
134 content = content.replaceAll(
135 "\\$TOP\\$", Matcher.quoteReplacement(folder.getAbsolutePath())).
136 replaceAll("\\$SEP\\$", Matcher.quoteReplacement(File.separator));
137
138 File tempFolder = TestUtils.createTempDirDeletedOnExit();
139 Files.write(content, new File(tempFolder, DataMerger.FN_MERGER_XML), Charsets.UTF_8);
140
141 return tempFolder;
142 }
143
144 /**
145 * Post {@link #getMergedBlobFolder(java.io.File, java.io.File)} check. After the DataMerger is created
146 * from the file generated, this checks that the file replacement works and all the files are
147 * where they are supposed to be.
148 *
149 * @param dataMerger the data merger
150 */
151 protected void checkSourceFolders(
152 DataMerger<? extends DataItem, ? extends DataFile, ? extends DataSet> dataMerger) {
153
154 // Loop on all the data sets.
155 for (DataSet<? extends DataItem, ? extends DataFile> set : dataMerger.getDataSets()) {
156 // get the source files and verify they exists.
157 List<File> files = set.getSourceFiles();
158 for (File file : files) {
159 assertTrue("Not a folder: " + file.getAbsolutePath(), file.isDirectory());
160 }
161
162 // for each source file, also check that the files inside are in fact inside
163 // them. We don't check if those files are there though because the tests could
164 // be testing with missing files to simulate updates.
165 ListMultimap<String, ? extends DataItem> itemMap = set.getDataMap();
166
167 for (DataItem item : itemMap.values()) {
168 DataFile dataFile = item.getSource();
169 File file = dataFile.getFile();
170
171 assertNotNull("Not in source file: " + file.getAbsolutePath(),
172 set.findMatchingSourceFile(file));
173 }
174 }
175 }
176
177 protected static void checkLogger(RecordingLogger logger) {
178 if (!logger.getErrorMsgs().isEmpty()) {
179 assertTrue(logger.getErrorMsgs().get(0), false);
180 }
181 }
182 }