OSDN Git Service

Automated import from //branches/master/...@141414,141414
authorRicky Ng-Adam <>
Wed, 25 Mar 2009 03:14:26 +0000 (20:14 -0700)
committerThe Android Open Source Project <initial-contribution@android.com>
Wed, 25 Mar 2009 03:14:26 +0000 (20:14 -0700)
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/common/project/AndroidManifestParser.java
eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/tests/AdtTestData.java
eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/common/project/AndroidManifestParserTest.java
eclipse/plugins/com.android.ide.eclipse.tests/unittests/data/AndroidManifest-instrumentation.xml [new file with mode: 0644]
eclipse/plugins/com.android.ide.eclipse.tests/unittests/data/AndroidManifest-testapp.xml [new file with mode: 0644]

index fe11e69..fa7e9b9 100644 (file)
@@ -16,6 +16,7 @@
 
 package com.android.ide.eclipse.common.project;
 
+import com.android.ide.eclipse.adt.AdtPlugin;
 import com.android.ide.eclipse.common.AndroidConstants;
 import com.android.ide.eclipse.common.project.XmlErrorHandler.XmlErrorListener;
 import com.android.sdklib.SdkConstants;
@@ -579,7 +580,6 @@ public class AndroidManifestParser {
 
             ManifestHandler manifestHandler = new ManifestHandler(manifestFile,
                     errorListener, gatherData, javaProject, markErrors);
-
             parser.parse(new InputSource(manifestFile.getContents()), manifestHandler);
             
             // get the result from the handler
@@ -590,10 +590,15 @@ public class AndroidManifestParser {
                     manifestHandler.getApiLevelRequirement(), manifestHandler.getInstrumentations(),
                     manifestHandler.getUsesLibraries());
         } catch (ParserConfigurationException e) {
+            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
+                    "Bad parser configuration for %s", manifestFile.getFullPath());
         } catch (SAXException e) {
+            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
+                    "Parser exception for %s", manifestFile.getFullPath());
         } catch (IOException e) {
-        } finally {
-        }
+            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
+                    "I/O error for %s", manifestFile.getFullPath());
+        } 
 
         return null;
     }
@@ -633,11 +638,15 @@ public class AndroidManifestParser {
                     manifestHandler.getApiLevelRequirement(), manifestHandler.getInstrumentations(),
                     manifestHandler.getUsesLibraries());
         } catch (ParserConfigurationException e) {
+            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
+                    "Bad parser configuration for %s", manifestFile.getAbsolutePath());
         } catch (SAXException e) {
+            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
+                    "Parser exception for %s", manifestFile.getAbsolutePath());
         } catch (IOException e) {
-        } finally {
-        }
-
+            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
+                    "I/O error for %s", manifestFile.getAbsolutePath());
+        } 
         return null;
     }
 
@@ -660,10 +669,12 @@ public class AndroidManifestParser {
                 boolean gatherData,
                 boolean markErrors)
             throws CoreException {
+        
+        IFile manifestFile = getManifest(javaProject.getProject());
+        
         try {
             SAXParser parser = sParserFactory.newSAXParser();
-            
-            IFile manifestFile = getManifest(javaProject.getProject());
+
             if (manifestFile != null) {
                 ManifestHandler manifestHandler = new ManifestHandler(manifestFile,
                         errorListener, gatherData, javaProject, markErrors);
@@ -678,10 +689,15 @@ public class AndroidManifestParser {
                         manifestHandler.getInstrumentations(), manifestHandler.getUsesLibraries());
             }
         } catch (ParserConfigurationException e) {
+            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
+                    "Bad parser configuration for %s", manifestFile.getFullPath());
         } catch (SAXException e) {
+            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
+                    "Parser exception for %s", manifestFile.getFullPath());
         } catch (IOException e) {
-        } finally {
-        }
+            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
+                    "I/O error for %s", manifestFile.getFullPath());
+        } 
         
         return null;
     }
index 262ef65..d86d585 100644 (file)
  */
 package com.android.ide.eclipse.tests;
 
+import com.android.ide.eclipse.common.AndroidConstants;
+
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.Platform;
+
 import java.io.File;
+import java.io.IOException;
 import java.net.URL;
 import java.util.logging.Logger;
 
@@ -45,12 +51,29 @@ public class AdtTestData {
         // accessed normally
         mOsRootDataPath = System.getProperty("test_data");
         if (mOsRootDataPath == null) {
-            sLogger.info("Cannot find test_data directory, init to class loader");
+            sLogger.info("Cannot find test_data environment variable, init to class loader");
             URL url = this.getClass().getClassLoader().getResource("data");  //$NON-NLS-1$
-            mOsRootDataPath = url.getFile();
+
+                if (Platform.isRunning()) {
+                    sLogger.info("Running as an Eclipse Plug-in JUnit test, using FileLocator");
+                    try {
+                        mOsRootDataPath = FileLocator.resolve(url).getFile();
+                    } catch (IOException e) {
+                        sLogger.warning("IOException while using FileLocator, reverting to url");
+                        mOsRootDataPath = url.getFile();
+                    }
+                } else {
+                    sLogger.info("Running as an plain JUnit test, using url as-is");
+                    mOsRootDataPath = url.getFile();                        
+                }
+        }
+        
+        if (mOsRootDataPath.equals(AndroidConstants.WS_SEP + "data")) {
+            sLogger.warning("Resource data not found using class loader!, Defaulting to no path");
         }
+        
         if (!mOsRootDataPath.endsWith(File.separator)) {
-            sLogger.info("Fixing test_data env variable does not end with path separator");
+            sLogger.info("Fixing test_data env variable (does not end with path separator)");
             mOsRootDataPath = mOsRootDataPath.concat(File.separator);
         }
     }
index 516e448..7e8b0af 100644 (file)
 
 package com.android.ide.eclipse.common.project;
 
-import junit.framework.TestCase;
+import com.android.ide.eclipse.tests.AdtTestData;
 
-import com.android.ide.eclipse.mock.FileMock;
+import junit.framework.TestCase;
 
 /**
  * Tests for {@link AndroidManifestParser}
  */
 public class AndroidManifestParserTest extends TestCase {
-    private AndroidManifestParser mManifest;
+    private AndroidManifestParser mManifestTestApp;
+    private AndroidManifestParser mManifestInstrumentation;
     
-    private static final String PACKAGE_NAME = "com.android.testapp"; //$NON-NLS-1$
+    private static final String INSTRUMENTATION_XML = "AndroidManifest-instrumentation.xml";  //$NON-NLS-1$
+    private static final String TESTAPP_XML = "AndroidManifest-testapp.xml";  //$NON-NLS-1$
+    private static final String PACKAGE_NAME =  "com.android.testapp"; //$NON-NLS-1$
     private static final String ACTIVITY_NAME = "com.android.testapp.MainActivity"; //$NON-NLS-1$
     private static final String LIBRARY_NAME = "android.test.runner"; //$NON-NLS-1$
     private static final String INSTRUMENTATION_NAME = "android.test.InstrumentationTestRunner"; //$NON-NLS-1$
@@ -35,60 +38,46 @@ public class AndroidManifestParserTest extends TestCase {
     protected void setUp() throws Exception {
         super.setUp();
         
-        // create the test data
-        StringBuilder sb = new StringBuilder();
-        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");  //$NON-NLS-1$
-        sb.append("<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n");  //$NON-NLS-1$
-        sb.append("          package=\""); //$NON-NLS-1$
-        sb.append(PACKAGE_NAME);
-        sb.append("\">\n");  //$NON-NLS-1$
-        sb.append("  <application android:icon=\"@drawable/icon\">\n");  //$NON-NLS-1$
-        sb.append("    <activity android:name=\"");  //$NON-NLS-1$
-        sb.append(ACTIVITY_NAME);
-        sb.append("\" android:label=\"@string/app_name\">\n");  //$NON-NLS-1$
-        sb.append("      <intent-filter>\n");  //$NON-NLS-1$
-        sb.append("        <action android:name=\"android.intent.action.MAIN\" />\n");  //$NON-NLS-1$
-        sb.append("          <category android:name=\"android.intent.category.LAUNCHER\" />\"\n");  //$NON-NLS-1$
-        sb.append("          <category android:name=\"android.intent.category.DEFAULT\" />\n");  //$NON-NLS-1$
-        sb.append("      </intent-filter>\n");  //$NON-NLS-1$
-        sb.append("    </activity>\n");  //$NON-NLS-1$
-        sb.append("    <uses-library android:name=\""); //$NON-NLS-1$
-        sb.append(LIBRARY_NAME);
-        sb.append("\" />\n");  //$NON-NLS-1$
-        sb.append("  </application>"); //$NON-NLS-1$
-        sb.append("  <instrumentation android:name=\""); //$NON-NLS-1$
-        sb.append(INSTRUMENTATION_NAME);
-        sb.append("\"\n");
-        sb.append("                   android:targetPackage=\"com.example.android.apis\"\n");
-        sb.append("                   android:label=\"Tests for Api Demos.\"/>\n");
-        sb.append("</manifest>\n");  //$NON-NLS-1$
-
-        FileMock mockFile = new FileMock("AndroidManifest.xml", sb.toString().getBytes());
+        String testFilePath = AdtTestData.getInstance().getTestFilePath(
+                TESTAPP_XML);
+        mManifestTestApp = AndroidManifestParser.parseForData(testFilePath);
+        assertNotNull(mManifestTestApp);
         
-        mManifest = AndroidManifestParser.parseForData(mockFile);
-        assertNotNull(mManifest);
+        testFilePath = AdtTestData.getInstance().getTestFilePath(
+                INSTRUMENTATION_XML);
+        mManifestInstrumentation = AndroidManifestParser.parseForData(testFilePath);
+        assertNotNull(mManifestInstrumentation);
     }
 
+    public void testGetInstrumentationInformation() {
+        assertEquals(1, mManifestInstrumentation.getInstrumentations().length);
+        assertEquals(INSTRUMENTATION_NAME, mManifestTestApp.getInstrumentations()[0]); 
+    }
+    
     public void testGetPackage() {
-        assertEquals("com.android.testapp", mManifest.getPackage());
+        assertEquals(PACKAGE_NAME, mManifestTestApp.getPackage());
     }
 
     public void testGetActivities() {
-        assertEquals(1, mManifest.getActivities().length);
-        assertEquals(ACTIVITY_NAME, mManifest.getActivities()[0]); 
+        assertEquals(1, mManifestTestApp.getActivities().length);
+        assertEquals(ACTIVITY_NAME, mManifestTestApp.getActivities()[0]); 
     }
 
     public void testGetLauncherActivity() {
-        assertEquals(ACTIVITY_NAME, mManifest.getLauncherActivity()); 
+        assertEquals(ACTIVITY_NAME, mManifestTestApp.getLauncherActivity()); 
     }
     
     public void testGetUsesLibraries() {
-        assertEquals(1, mManifest.getUsesLibraries().length);
-        assertEquals(LIBRARY_NAME, mManifest.getUsesLibraries()[0]); 
+        assertEquals(1, mManifestTestApp.getUsesLibraries().length);
+        assertEquals(LIBRARY_NAME, mManifestTestApp.getUsesLibraries()[0]); 
     }
     
     public void testGetInstrumentations() {
-        assertEquals(1, mManifest.getInstrumentations().length);
-        assertEquals(INSTRUMENTATION_NAME, mManifest.getInstrumentations()[0]); 
+        assertEquals(1, mManifestTestApp.getInstrumentations().length);
+        assertEquals(INSTRUMENTATION_NAME, mManifestTestApp.getInstrumentations()[0]); 
+    }
+    
+    public void testGetPackageName() {
+        assertEquals(PACKAGE_NAME, mManifestTestApp.getPackage());
     }
 }
diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/data/AndroidManifest-instrumentation.xml b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/data/AndroidManifest-instrumentation.xml
new file mode 100644 (file)
index 0000000..b380f96
--- /dev/null
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.AndroidProject.tests">
+
+    <!--
+    This declares that this app uses the instrumentation test runner targeting
+    the package of com.android.samples.  To run the tests use the command:
+    "adb shell am instrument -w com.android.samples.tests/android.test.InstrumentationTestRunner"
+    -->
+    <instrumentation android:name="android.test.InstrumentationTestRunner"
+                     android:targetPackage="com.android.AndroidProject"
+                     android:label="Sample test for deployment."/>
+
+    <application>
+        <uses-library android:name="android.test.runner" />
+    </application>
+
+</manifest>
diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/data/AndroidManifest-testapp.xml b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/data/AndroidManifest-testapp.xml
new file mode 100644 (file)
index 0000000..8ae7012
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.testapp">
+  <application android:icon="@drawable/icon">
+    <activity android:name="com.android.testapp.MainActivity"
+              android:label="@string/app_name">
+      <intent-filter>
+        <action android:name="android.intent.action.MAIN" />
+          <category android:name="android.intent.category.LAUNCHER" />"
+          <category android:name="android.intent.category.DEFAULT" />
+      </intent-filter>
+    </activity>
+    <uses-library android:name="android.test.runner"/>
+  </application>"
+  <instrumentation android:name="android.test.InstrumentationTestRunner"
+                   android:targetPackage="com.example.android.apis"
+                   android:label="Tests for Api Demos."/>
+</manifest>
\ No newline at end of file