From a51d4ddc3cc08e445eb426a6a6108fad3fa842b9 Mon Sep 17 00:00:00 2001 From: Xavier Ducrohet Date: Mon, 17 May 2010 14:35:48 -0700 Subject: [PATCH] Refactor the manifest parser test in the correct package. And don't make it use the Parser Helper that's eclipse specific. Also fixed the versionCode parsing (bug introduced in previous CL) Change-Id: Ie472d7d6c4847e3fae660873cca7d71e801a2a34 --- .../android/sdklib/xml/AndroidManifestParser.java | 31 ++++++++++++++++++++-- .../testdata/AndroidManifest-instrumentation.xml | 0 .../sdklib}/testdata/AndroidManifest-testapp.xml | 0 .../sdklib/xml}/AndroidManifestParserTest.java | 23 ++++++++-------- 4 files changed, 40 insertions(+), 14 deletions(-) rename {eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse => sdkmanager/libs/sdklib/tests/com/android/sdklib}/testdata/AndroidManifest-instrumentation.xml (100%) rename {eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse => sdkmanager/libs/sdklib/tests/com/android/sdklib}/testdata/AndroidManifest-testapp.xml (100%) rename {eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/project => sdkmanager/libs/sdklib/tests/com/android/sdklib/xml}/AndroidManifestParserTest.java (83%) diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/AndroidManifestParser.java b/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/AndroidManifestParser.java index 2330020e6..3b9cd0bfb 100644 --- a/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/AndroidManifestParser.java +++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/xml/AndroidManifestParser.java @@ -35,6 +35,7 @@ import org.xml.sax.helpers.DefaultHandler; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; @@ -142,7 +143,7 @@ public class AndroidManifestParser { AndroidManifest.ATTRIBUTE_VERSIONCODE, true); if (tmp != null) { try { - mManifestData.mVersionCode = Integer.getInteger(tmp); + mManifestData.mVersionCode = Integer.valueOf(tmp); } catch (NumberFormatException e) { // keep null in the field. } @@ -427,7 +428,6 @@ public class AndroidManifestParser { AndroidManifest.ATTRIBUTE_LARGESCREENS, true /*hasNamespace*/)); } - /** * Searches through the attributes list for a particular one and returns its value. * @param attributes the attribute list to search through @@ -525,6 +525,33 @@ public class AndroidManifestParser { } /** + * Parses the Android Manifest from an {@link InputStream}, and returns a {@link ManifestData} + * object containing the result of the parsing. + * + * @param manifestFileStream the {@link InputStream} representing the manifest file. + * @return + * @throws StreamException + * @throws IOException + * @throws SAXException + * @throws ParserConfigurationException + */ + public static ManifestData parse(InputStream manifestFileStream) + throws SAXException, IOException, StreamException, ParserConfigurationException { + if (manifestFileStream != null) { + SAXParser parser = sParserFactory.newSAXParser(); + + ManifestData data = new ManifestData(); + + ManifestHandler manifestHandler = new ManifestHandler(null, data, null); + parser.parse(new InputSource(manifestFileStream), manifestHandler); + + return data; + } + + return null; + } + + /** * Returns an {@link IAbstractFile} object representing the manifest for the given project. * * @param project The project containing the manifest file. diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/testdata/AndroidManifest-instrumentation.xml b/sdkmanager/libs/sdklib/tests/com/android/sdklib/testdata/AndroidManifest-instrumentation.xml similarity index 100% rename from eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/testdata/AndroidManifest-instrumentation.xml rename to sdkmanager/libs/sdklib/tests/com/android/sdklib/testdata/AndroidManifest-instrumentation.xml diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/testdata/AndroidManifest-testapp.xml b/sdkmanager/libs/sdklib/tests/com/android/sdklib/testdata/AndroidManifest-testapp.xml similarity index 100% rename from eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/testdata/AndroidManifest-testapp.xml rename to sdkmanager/libs/sdklib/tests/com/android/sdklib/testdata/AndroidManifest-testapp.xml diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/project/AndroidManifestParserTest.java b/sdkmanager/libs/sdklib/tests/com/android/sdklib/xml/AndroidManifestParserTest.java similarity index 83% rename from eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/project/AndroidManifestParserTest.java rename to sdkmanager/libs/sdklib/tests/com/android/sdklib/xml/AndroidManifestParserTest.java index c52fbb26b..c2ee9433e 100644 --- a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/project/AndroidManifestParserTest.java +++ b/sdkmanager/libs/sdklib/tests/com/android/sdklib/xml/AndroidManifestParserTest.java @@ -1,11 +1,11 @@ /* * Copyright (C) 2007 The Android Open Source Project * - * Licensed under the Eclipse Public License, Version 1.0 (the "License"); + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.eclipse.org/org/documents/epl-v10.php + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -14,23 +14,21 @@ * limitations under the License. */ -package com.android.ide.eclipse.adt.internal.project; +package com.android.sdklib.xml; -import com.android.ide.eclipse.adt.internal.project.AndroidManifestHelper; -import com.android.ide.eclipse.tests.AdtTestData; -import com.android.sdklib.xml.ManifestData; +import java.io.InputStream; import junit.framework.TestCase; /** - * Tests for {@link AndroidManifestHelper} + * Tests for {@link AndroidManifestParser} */ public class AndroidManifestParserTest extends TestCase { private ManifestData mManifestTestApp; private ManifestData mManifestInstrumentation; private static final String TESTDATA_PATH = - "com/android/ide/eclipse/testdata/"; //$NON-NLS-1$ + "/com/android/sdklib/testdata/"; //$NON-NLS-1$ private static final String INSTRUMENTATION_XML = TESTDATA_PATH + "AndroidManifest-instrumentation.xml"; //$NON-NLS-1$ private static final String TESTAPP_XML = TESTDATA_PATH + @@ -46,12 +44,13 @@ public class AndroidManifestParserTest extends TestCase { protected void setUp() throws Exception { super.setUp(); - String testFilePath = AdtTestData.getInstance().getTestFilePath(TESTAPP_XML); - mManifestTestApp = AndroidManifestHelper.parseForData(testFilePath); + InputStream manifestStream = this.getClass().getResourceAsStream(TESTAPP_XML); + + mManifestTestApp = AndroidManifestParser.parse(manifestStream); assertNotNull(mManifestTestApp); - testFilePath = AdtTestData.getInstance().getTestFilePath(INSTRUMENTATION_XML); - mManifestInstrumentation = AndroidManifestHelper.parseForData(testFilePath); + manifestStream = this.getClass().getResourceAsStream(INSTRUMENTATION_XML); + mManifestInstrumentation = AndroidManifestParser.parse(manifestStream); assertNotNull(mManifestInstrumentation); } -- 2.11.0