| 1 |
/* |
| 2 |
* Copyright (C) 2015 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 static org.junit.Assert.assertNotNull; |
| 20 |
import static org.junit.Assert.assertTrue; |
| 21 |
import static org.junit.Assert.fail; |
| 22 |
|
| 23 |
import com.android.annotations.NonNull; |
| 24 |
import com.android.annotations.Nullable; |
| 25 |
import com.android.utils.FileUtils; |
| 26 |
import com.google.common.base.Function; |
| 27 |
import com.google.common.collect.ImmutableList; |
| 28 |
import com.google.common.collect.Iterables; |
| 29 |
import com.google.common.io.Closeables; |
| 30 |
import com.google.common.io.Files; |
| 31 |
|
| 32 |
import org.junit.runner.RunWith; |
| 33 |
import org.w3c.dom.Document; |
| 34 |
|
| 35 |
import java.io.BufferedReader; |
| 36 |
import java.io.BufferedWriter; |
| 37 |
import java.io.File; |
| 38 |
import java.io.FileWriter; |
| 39 |
import java.io.IOException; |
| 40 |
import java.io.InputStream; |
| 41 |
import java.io.InputStreamReader; |
| 42 |
import java.util.ArrayList; |
| 43 |
import java.util.Arrays; |
| 44 |
import java.util.Collection; |
| 45 |
import java.util.Collections; |
| 46 |
import java.util.HashMap; |
| 47 |
import java.util.List; |
| 48 |
import java.util.Map; |
| 49 |
|
| 50 |
public class ManifestMergerTestUtil { |
| 51 |
|
| 52 |
/** |
| 53 |
* Delimiter that indicates the test must fail. An XML output and errors are still generated and |
| 54 |
* checked. |
| 55 |
*/ |
| 56 |
private static final String DELIM_FAILS = "fails"; |
| 57 |
|
| 58 |
/** |
| 59 |
* Delimiter that starts a library XML content. The delimiter name must be in the form {@code |
| 60 |
* @libSomeName} and it will be used as the base for the test file name. Using separate lib |
| 61 |
* names is encouraged since it makes the error output easier to read. |
| 62 |
*/ |
| 63 |
private static final String DELIM_LIB = "lib"; |
| 64 |
|
| 65 |
/** |
| 66 |
* Delimiter that starts the main manifest XML content. |
| 67 |
*/ |
| 68 |
private static final String DELIM_MAIN = "main"; |
| 69 |
|
| 70 |
/** |
| 71 |
* Delimiter that starts an overlay XML content. The delimiter must follow the same rules as |
| 72 |
* {@link #DELIM_LIB} |
| 73 |
*/ |
| 74 |
private static final String DELIM_OVERLAY = "overlay"; |
| 75 |
|
| 76 |
/** |
| 77 |
* Delimiter that starts the resulting XML content, whatever is generated by the merge. |
| 78 |
*/ |
| 79 |
private static final String DELIM_RESULT = "result"; |
| 80 |
|
| 81 |
/** |
| 82 |
* Delimiter that starts the SdkLog output. The logger prints each entry on its lines, prefixed |
| 83 |
* with E for errors, W for warnings and P for regular printfs. |
| 84 |
*/ |
| 85 |
private static final String DELIM_ERRORS = "errors"; |
| 86 |
|
| 87 |
/** |
| 88 |
* Delimiter for starts a section that declares how to inject an attribute. The section is |
| 89 |
* composed of one or more lines with the syntax: "/node/node|attr-URI attrName=attrValue". This |
| 90 |
* is essentially a pseudo XPath-like expression. |
| 91 |
*/ |
| 92 |
private static final String DELIM_INJECT_ATTR = "inject"; |
| 93 |
|
| 94 |
/** |
| 95 |
* Delimiter for a section that declares how to toggle a ManifMerger option. The section is |
| 96 |
* composed of one or more lines with the syntax: "functionName=false|true". |
| 97 |
*/ |
| 98 |
private static final String DELIM_FEATURES = "features"; |
| 99 |
|
| 100 |
/** |
| 101 |
* Delimiter for a section that declares how to override the package. The section is composed of |
| 102 |
* one line containing the new package name. |
| 103 |
*/ |
| 104 |
private static final String DELIM_PACKAGE = "package"; |
| 105 |
|
| 106 |
/** |
| 107 |
* Loads test data for a given test case. |
| 108 |
* The input (main + libs) are stored in temp files. |
| 109 |
* A new destination temp file is created to store the actual result output. |
| 110 |
* The expected result is actually kept in a string. |
| 111 |
* <p> |
| 112 |
* Data File Syntax: |
| 113 |
* <ul> |
| 114 |
* <li> Lines starting with # are ignored (anywhere, as long as # is the first char). |
| 115 |
* <li> Lines before the first {@code @delimiter} are ignored. |
| 116 |
* <li> Empty lines just after the {@code @delimiter} |
| 117 |
* and before the first < XML line are ignored. |
| 118 |
* <li> Valid delimiters are {@code @main} for the XML of the main app manifest. |
| 119 |
* <li> Following delimiters are {@code @libXYZ}, read in the order of definition. |
| 120 |
* The name can be anything as long as it starts with "{@code @lib}". |
| 121 |
* </ul> |
| 122 |
* |
| 123 |
* @param testDataDirectory The resource directory name the data file is located in. |
| 124 |
* @param filename The test data filename. If no extension is provided, this will |
| 125 |
* try with .xml or .txt. Must not be null. |
| 126 |
* @param className The simple name of the test class, |
| 127 |
* the manifest files include it in their output. |
| 128 |
* @return A new {@link ManifestMergerTestUtil.TestFiles} instance. Must not be null. |
| 129 |
* @throws Exception when things fail to load properly. |
| 130 |
*/ |
| 131 |
@NonNull |
| 132 |
static TestFiles loadTestData( |
| 133 |
@NonNull String testDataDirectory, |
| 134 |
@NonNull String filename, |
| 135 |
@NonNull String className) throws Exception { |
| 136 |
|
| 137 |
String resName = testDataDirectory + File.separator + filename; |
| 138 |
InputStream is = null; |
| 139 |
BufferedReader reader = null; |
| 140 |
BufferedWriter writer = null; |
| 141 |
|
| 142 |
try { |
| 143 |
is = ManifestMergerTestUtil.class.getResourceAsStream(resName); |
| 144 |
if (is == null && !filename.endsWith(".xml")) { |
| 145 |
String resName2 = resName + ".xml"; |
| 146 |
is = ManifestMergerTestUtil.class.getResourceAsStream(resName2); |
| 147 |
if (is != null) { |
| 148 |
filename = resName2; |
| 149 |
} |
| 150 |
} |
| 151 |
if (is == null && !filename.endsWith(".txt")) { |
| 152 |
String resName3 = resName + ".txt"; |
| 153 |
is = ManifestMergerTestUtil.class.getResourceAsStream(resName3); |
| 154 |
if (is != null) { |
| 155 |
filename = resName3; |
| 156 |
} |
| 157 |
} |
| 158 |
assertNotNull("Test data file not found for " + filename, is); |
| 159 |
|
| 160 |
reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); |
| 161 |
|
| 162 |
final File tempDir = Files.createTempDir(); |
| 163 |
tempDir.deleteOnExit(); |
| 164 |
Runtime.getRuntime().addShutdownHook(new Thread() { |
| 165 |
@Override |
| 166 |
public void run() { |
| 167 |
try { |
| 168 |
FileUtils.deletePath(tempDir); |
| 169 |
} catch (IOException e) { |
| 170 |
throw new AssertionError(e); |
| 171 |
} |
| 172 |
} |
| 173 |
}); |
| 174 |
tempDir.deleteOnExit(); |
| 175 |
|
| 176 |
String line = null; |
| 177 |
String delimiter = null; |
| 178 |
boolean skipEmpty = true; |
| 179 |
|
| 180 |
boolean shouldFail = false; |
| 181 |
Map<String, Boolean> features = new HashMap<String, Boolean>(); |
| 182 |
String packageOverride = null; |
| 183 |
Map<String, String> injectAttributes = new HashMap<String, String>(); |
| 184 |
StringBuilder expectedResult = new StringBuilder(); |
| 185 |
StringBuilder expectedErrors = new StringBuilder(); |
| 186 |
File mainFile = null; |
| 187 |
File actualResultFile = null; |
| 188 |
List<File> libFiles = new ArrayList<File>(); |
| 189 |
List<File> overlayFiles = new ArrayList<File>(); |
| 190 |
int tempIndex = 0; |
| 191 |
|
| 192 |
while ((line = reader.readLine()) != null) { |
| 193 |
if (skipEmpty && line.trim().isEmpty()) { |
| 194 |
continue; |
| 195 |
} |
| 196 |
if (!line.isEmpty() && line.charAt(0) == '#') { |
| 197 |
continue; |
| 198 |
} |
| 199 |
if (!line.isEmpty() && line.charAt(0) == '@') { |
| 200 |
delimiter = line.substring(1); |
| 201 |
assertTrue( |
| 202 |
"Unknown delimiter @" + delimiter + " in " + filename, |
| 203 |
delimiter.startsWith(DELIM_OVERLAY) || |
| 204 |
delimiter.startsWith(DELIM_LIB) || |
| 205 |
delimiter.equals(DELIM_MAIN) || |
| 206 |
delimiter.equals(DELIM_RESULT) || |
| 207 |
delimiter.equals(DELIM_ERRORS) || |
| 208 |
delimiter.equals(DELIM_FAILS) || |
| 209 |
delimiter.equals(DELIM_FEATURES) || |
| 210 |
delimiter.equals(DELIM_INJECT_ATTR) || |
| 211 |
delimiter.equals(DELIM_PACKAGE)); |
| 212 |
|
| 213 |
skipEmpty = true; |
| 214 |
|
| 215 |
if (writer != null) { |
| 216 |
writer.close(); |
| 217 |
writer = null; |
| 218 |
} |
| 219 |
|
| 220 |
if (delimiter.equals(DELIM_FAILS)) { |
| 221 |
shouldFail = true; |
| 222 |
|
| 223 |
} else if (!delimiter.equals(DELIM_ERRORS) && |
| 224 |
!delimiter.equals(DELIM_FEATURES) && |
| 225 |
!delimiter.equals(DELIM_INJECT_ATTR) && |
| 226 |
!delimiter.equals(DELIM_PACKAGE)) { |
| 227 |
File tempFile = new File(tempDir, String.format("%1$s%2$d_%3$s.xml", |
| 228 |
className, |
| 229 |
tempIndex++, |
| 230 |
delimiter.replaceAll("[^a-zA-Z0-9_-]", "") |
| 231 |
)); |
| 232 |
tempFile.deleteOnExit(); |
| 233 |
|
| 234 |
if (delimiter.startsWith(DELIM_OVERLAY)) { |
| 235 |
overlayFiles.add(tempFile); |
| 236 |
} else if (delimiter.startsWith(DELIM_LIB)) { |
| 237 |
libFiles.add(tempFile); |
| 238 |
|
| 239 |
} else if (delimiter.equals(DELIM_MAIN)) { |
| 240 |
mainFile = tempFile; |
| 241 |
|
| 242 |
} else if (delimiter.equals(DELIM_RESULT)) { |
| 243 |
actualResultFile = tempFile; |
| 244 |
|
| 245 |
} else { |
| 246 |
fail("Unexpected data file delimiter @" + delimiter + |
| 247 |
" in " + filename); |
| 248 |
} |
| 249 |
|
| 250 |
if (!delimiter.equals(DELIM_RESULT)) { |
| 251 |
writer = new BufferedWriter(new FileWriter(tempFile)); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
continue; |
| 256 |
} |
| 257 |
if (delimiter != null && |
| 258 |
skipEmpty && |
| 259 |
!line.isEmpty() && |
| 260 |
line.charAt(0) != '#' && |
| 261 |
line.charAt(0) != '@') { |
| 262 |
skipEmpty = false; |
| 263 |
} |
| 264 |
if (writer != null) { |
| 265 |
writer.write(line); |
| 266 |
writer.write('\n'); |
| 267 |
} else if (DELIM_RESULT.equals(delimiter)) { |
| 268 |
expectedResult.append(line).append('\n'); |
| 269 |
} else if (DELIM_ERRORS.equals(delimiter)) { |
| 270 |
expectedErrors.append(line).append('\n'); |
| 271 |
} else if (DELIM_INJECT_ATTR.equals(delimiter)) { |
| 272 |
String[] in = line.split("="); |
| 273 |
if (in != null && in.length == 2) { |
| 274 |
injectAttributes.put(in[0], "null".equals(in[1]) ? null : in[1]); |
| 275 |
} |
| 276 |
} else if (DELIM_FEATURES.equals(delimiter)) { |
| 277 |
String[] in = line.split("="); |
| 278 |
if (in != null && in.length == 2) { |
| 279 |
features.put(in[0], Boolean.parseBoolean(in[1])); |
| 280 |
} |
| 281 |
} else if (DELIM_PACKAGE.equals(delimiter)) { |
| 282 |
if (packageOverride == null) { |
| 283 |
packageOverride = line; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
assertNotNull("Missing @" + DELIM_MAIN + " in " + filename, mainFile); |
| 289 |
|
| 290 |
assert mainFile != null; |
| 291 |
|
| 292 |
Collections.sort(libFiles); |
| 293 |
|
| 294 |
return new ManifestMergerTestUtil.TestFiles( |
| 295 |
shouldFail, |
| 296 |
overlayFiles.toArray(new File[overlayFiles.size()]), |
| 297 |
mainFile, |
| 298 |
libFiles.toArray(new File[libFiles.size()]), |
| 299 |
features, |
| 300 |
injectAttributes, |
| 301 |
packageOverride, |
| 302 |
actualResultFile, |
| 303 |
expectedResult.toString(), |
| 304 |
expectedErrors.toString()); |
| 305 |
|
| 306 |
} finally { |
| 307 |
Closeables.closeQuietly(reader); |
| 308 |
Closeables.closeQuietly(is); |
| 309 |
if (writer != null) { |
| 310 |
writer.close(); |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
static class TestFiles { |
| 317 |
private final File[] mOverlayFiles; |
| 318 |
private final File mMain; |
| 319 |
private final File[] mLibs; |
| 320 |
private final Map<String, String> mInjectAttributes; |
| 321 |
private final String mPackageOverride; |
| 322 |
private final File mActualResult; |
| 323 |
private final String mExpectedResult; |
| 324 |
private final String mExpectedErrors; |
| 325 |
private final boolean mShouldFail; |
| 326 |
private final Map<String, Boolean> mFeatures; |
| 327 |
|
| 328 |
/** Files used by a given test case. */ |
| 329 |
public TestFiles( |
| 330 |
boolean shouldFail, |
| 331 |
@NonNull File[] overlayFiles, |
| 332 |
@NonNull File main, |
| 333 |
@NonNull File[] libs, |
| 334 |
@NonNull Map<String, Boolean> features, |
| 335 |
@NonNull Map<String, String> injectAttributes, |
| 336 |
@Nullable String packageOverride, |
| 337 |
@Nullable File actualResult, |
| 338 |
@NonNull String expectedResult, |
| 339 |
@NonNull String expectedErrors) { |
| 340 |
mShouldFail = shouldFail; |
| 341 |
mMain = main; |
| 342 |
mLibs = libs; |
| 343 |
mFeatures = features; |
| 344 |
mPackageOverride = packageOverride; |
| 345 |
mInjectAttributes = injectAttributes; |
| 346 |
mActualResult = actualResult; |
| 347 |
mExpectedResult = expectedResult; |
| 348 |
mExpectedErrors = expectedErrors; |
| 349 |
mOverlayFiles = overlayFiles; |
| 350 |
} |
| 351 |
|
| 352 |
public boolean getShouldFail() { |
| 353 |
return mShouldFail; |
| 354 |
} |
| 355 |
|
| 356 |
@NonNull |
| 357 |
public File[] getOverlayFiles() { |
| 358 |
return mOverlayFiles; |
| 359 |
} |
| 360 |
|
| 361 |
@NonNull |
| 362 |
public File getMain() { |
| 363 |
return mMain; |
| 364 |
} |
| 365 |
|
| 366 |
@NonNull |
| 367 |
public File[] getLibs() { |
| 368 |
return mLibs; |
| 369 |
} |
| 370 |
|
| 371 |
@NonNull |
| 372 |
public Map<String, Boolean> getFeatures() { |
| 373 |
return mFeatures; |
| 374 |
} |
| 375 |
|
| 376 |
@NonNull |
| 377 |
public Map<String, String> getInjectAttributes() { |
| 378 |
return mInjectAttributes; |
| 379 |
} |
| 380 |
|
| 381 |
@Nullable |
| 382 |
public String getPackageOverride() { |
| 383 |
return mPackageOverride; |
| 384 |
} |
| 385 |
|
| 386 |
@Nullable |
| 387 |
public File getActualResult() { |
| 388 |
return mActualResult; |
| 389 |
} |
| 390 |
|
| 391 |
@NonNull |
| 392 |
public String getExpectedResult() { |
| 393 |
return mExpectedResult; |
| 394 |
} |
| 395 |
|
| 396 |
public String getExpectedErrors() { |
| 397 |
return mExpectedErrors; |
| 398 |
} |
| 399 |
|
| 400 |
// Try to delete any temp file potentially created. |
| 401 |
public void cleanup() { |
| 402 |
try { |
| 403 |
if (mMain != null && mMain.isFile()) { |
| 404 |
FileUtils.delete(mMain); |
| 405 |
} |
| 406 |
|
| 407 |
if (mActualResult != null && mActualResult.isFile()) { |
| 408 |
FileUtils.delete(mActualResult); |
| 409 |
} |
| 410 |
|
| 411 |
for (File f : mLibs) { |
| 412 |
if (f != null && f.isFile()) { |
| 413 |
FileUtils.delete(f); |
| 414 |
} |
| 415 |
} |
| 416 |
} catch (IOException e) { |
| 417 |
throw new AssertionError(e); |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
static Collection<Object[]> transformParameters(String[] params) { |
| 423 |
|
| 424 |
return ImmutableList.copyOf( |
| 425 |
Iterables.transform(Arrays.asList(params), |
| 426 |
new Function<Object, Object[]>() { |
| 427 |
@Override |
| 428 |
public Object[] apply(Object input) { |
| 429 |
return new Object[]{input}; |
| 430 |
} |
| 431 |
})); |
| 432 |
} |
| 433 |
} |