| 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 static com.android.manifmerger.PlaceholderHandler.KeyBasedValueResolver; |
| 20 |
|
| 21 |
import com.android.annotations.NonNull; |
| 22 |
import com.android.annotations.Nullable; |
| 23 |
import com.android.ide.common.blame.SourceFile; |
| 24 |
import com.google.common.base.Optional; |
| 25 |
|
| 26 |
import org.xml.sax.SAXException; |
| 27 |
|
| 28 |
import java.io.IOException; |
| 29 |
import java.util.Collections; |
| 30 |
import java.util.List; |
| 31 |
|
| 32 |
import javax.xml.parsers.ParserConfigurationException; |
| 33 |
|
| 34 |
/** |
| 35 |
* Utilities for testing ManifestMerge classes. |
| 36 |
*/ |
| 37 |
public class TestUtils { |
| 38 |
|
| 39 |
private static final KeyResolver<String> NULL_RESOLVER = new KeyResolver<String>() { |
| 40 |
@Nullable |
| 41 |
@Override |
| 42 |
public String resolve(String key) { |
| 43 |
return null; |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public List<String> getKeys() { |
| 48 |
return Collections.emptyList(); |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
private static final KeyBasedValueResolver<ManifestSystemProperty> NO_PROPERTY_RESOLVER = |
| 53 |
new KeyBasedValueResolver<ManifestSystemProperty>() { |
| 54 |
@Nullable |
| 55 |
@Override |
| 56 |
public String getValue(@NonNull ManifestSystemProperty key) { |
| 57 |
return null; |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
static SourceFile sourceFile(Class sourceClass, String location) { |
| 62 |
return new SourceFile(sourceClass.getSimpleName() + "#" + location); |
| 63 |
} |
| 64 |
|
| 65 |
static XmlDocument xmlDocumentFromString( |
| 66 |
SourceFile location, |
| 67 |
String input) throws IOException, SAXException, ParserConfigurationException { |
| 68 |
|
| 69 |
return XmlLoader.load( |
| 70 |
NULL_RESOLVER, NO_PROPERTY_RESOLVER, location, input, XmlDocument.Type.MAIN, |
| 71 |
Optional.<String>absent() /* mainManifestPackageName */); |
| 72 |
} |
| 73 |
|
| 74 |
static XmlDocument xmlLibraryFromString( |
| 75 |
SourceFile location, |
| 76 |
String input) throws IOException, SAXException, ParserConfigurationException { |
| 77 |
|
| 78 |
return XmlLoader.load( |
| 79 |
NULL_RESOLVER, NO_PROPERTY_RESOLVER, location, input, XmlDocument.Type.LIBRARY, |
| 80 |
Optional.<String>absent() /* mainManifestPackageName */); |
| 81 |
} |
| 82 |
|
| 83 |
static XmlDocument xmlDocumentFromString( |
| 84 |
SourceFile location, |
| 85 |
String input, |
| 86 |
XmlDocument.Type type, |
| 87 |
Optional<String> mainManifestPackageName) throws IOException, SAXException, ParserConfigurationException { |
| 88 |
|
| 89 |
return XmlLoader.load(NULL_RESOLVER, NO_PROPERTY_RESOLVER, location, input, type, mainManifestPackageName); |
| 90 |
} |
| 91 |
|
| 92 |
static XmlDocument xmlDocumentFromString( |
| 93 |
@NonNull KeyResolver<String> selectors, |
| 94 |
@NonNull SourceFile location, |
| 95 |
String input) throws IOException, SAXException, ParserConfigurationException { |
| 96 |
|
| 97 |
return XmlLoader.load(selectors, NO_PROPERTY_RESOLVER, location, input, |
| 98 |
XmlDocument.Type.LIBRARY, Optional.<String>absent() /* mainManifestPackageName */); |
| 99 |
} |
| 100 |
|
| 101 |
} |