Author: markt
Date: Mon Jul  8 12:26:08 2013
New Revision: 1500690

URL: http://svn.apache.org/r1500690
Log:
Add a unit test for ImportHandler and fix some bugs it idenfifed:
- missing i18n message
- importing a package that is not referenced elsewhere failed
- ambiguous imports where not detected

Added:
    tomcat/trunk/test/javax/el/TestImportHandler.java   (with props)
Modified:
    tomcat/trunk/java/javax/el/ImportHandler.java
    tomcat/trunk/java/javax/el/LocalStrings.properties

Modified: tomcat/trunk/java/javax/el/ImportHandler.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/javax/el/ImportHandler.java?rev=1500690&r1=1500689&r2=1500690&view=diff
==============================================================================
--- tomcat/trunk/java/javax/el/ImportHandler.java (original)
+++ tomcat/trunk/java/javax/el/ImportHandler.java Mon Jul  8 12:26:08 2013
@@ -19,6 +19,7 @@ package javax.el;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -112,8 +113,15 @@ public class ImportHandler {
         // Import ambiguity is handled at resolution, not at import
         Package p = Package.getPackage(name);
         if (p == null) {
-            throw new IllegalArgumentException(Util.message(
-                    null, "importHandler.invalidPackage", name));
+            // Either the package does not exist or no class has been loaded
+            // from that package. Check if the package exists.
+            ClassLoader cl = Thread.currentThread().getContextClassLoader();
+            String path = name.replace('.', '/');
+            URL url = cl.getResource(path);
+            if (url == null) {
+                throw new ELException(Util.message(
+                        null, "importHandler.invalidPackage", name));
+            }
         }
         packages.add(name);
     }
@@ -123,13 +131,11 @@ public class ImportHandler {
         Class<?> result = clazzes.get(name);
 
         if (result == null) {
-            // Search the package imports
+            // Search the package imports - note there may be multiple matches
+            // (which correctly triggers an error)
             for (String p : packages) {
                 String className = p + '.' + name;
                 result = findClass(className);
-                if (result != null) {
-                    break;
-                }
             }
         }
 

Modified: tomcat/trunk/java/javax/el/LocalStrings.properties
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/javax/el/LocalStrings.properties?rev=1500690&r1=1500689&r2=1500690&view=diff
==============================================================================
--- tomcat/trunk/java/javax/el/LocalStrings.properties (original)
+++ tomcat/trunk/java/javax/el/LocalStrings.properties Mon Jul  8 12:26:08 2013
@@ -37,6 +37,7 @@ importHandler.classNotFound=The class [{
 importHandler.invalidClass=The class [{0}] must be public, non-abstract and 
not an interface
 importHandler.invalidClassName=Name of class to import [{0}] must include a 
package
 importHandler.invalidClassNameForStatic=The class [{0}] specified for static 
import [{1}] is not valid
+importHandler.invalidPackage=The package [{0}] could not be found
 importHandler.invalidStaticName=Name of static method or field to import [{0}] 
must include a class
 importHandler.staticNotFound=The static import [{0}] could not be found in 
class [{1}] for import [{2}]
 

Added: tomcat/trunk/test/javax/el/TestImportHandler.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/javax/el/TestImportHandler.java?rev=1500690&view=auto
==============================================================================
--- tomcat/trunk/test/javax/el/TestImportHandler.java (added)
+++ tomcat/trunk/test/javax/el/TestImportHandler.java Mon Jul  8 12:26:08 2013
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.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,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package javax.el;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.tomcat.util.res.StringManager;
+
+public class TestImportHandler {
+
+    /**
+     * java.lang should be imported by default
+     */
+    @Test
+    public void testResolveClass01() {
+        ImportHandler handler = new ImportHandler();
+
+        Class<?> result = handler.resolveClass("String");
+
+        Assert.assertEquals(String.class, result);
+    }
+
+
+    /**
+     * Resolve an unknown class
+     */
+    @Test
+    public void testResolveClass02() {
+        ImportHandler handler = new ImportHandler();
+
+        Class<?> result = handler.resolveClass("Foo");
+
+        Assert.assertNull(result);
+    }
+
+
+    /**
+     * Conflict on resolution.
+     */
+    @Test(expected=ELException.class)
+    public void testResolveClass03() {
+        ImportHandler handler = new ImportHandler();
+
+        handler.importPackage("org.apache.tomcat.util");
+        handler.importPackage("org.apache.jasper.util");
+
+        handler.resolveClass("ExceptionUtils");
+    }
+
+
+    /**
+     * Import a valid class.
+     */
+    @Test
+    public void testImportClass01() {
+        ImportHandler handler = new ImportHandler();
+
+        handler.importClass("org.apache.tomcat.util.res.StringManager");
+
+        Class<?> result = handler.resolveClass("StringManager");
+
+        Assert.assertEquals(StringManager.class, result);
+    }
+
+
+    /**
+     * Import an invalid class.
+     */
+    @Test(expected=ELException.class)
+    public void testImportClass02() {
+        ImportHandler handler = new ImportHandler();
+
+        handler.importClass("org.apache.tomcat.util.res.StringManagerX");
+    }
+
+
+    /**
+     * Import conflicting classes
+     */
+    @Test(expected=ELException.class)
+    public void testImportClass03() {
+        ImportHandler handler = new ImportHandler();
+
+        handler.importClass("org.apache.tomcat.util.ExceptionUtils");
+        handler.importClass("org.apache.jasper.util.ExceptionUtils");
+    }
+
+
+    /**
+     * Import an invalid [ackage.
+     */
+    @Test(expected=ELException.class)
+    public void testImportPackage01() {
+        ImportHandler handler = new ImportHandler();
+
+        handler.importPackage("org.apache.tomcat.foo");
+    }
+}

Propchange: tomcat/trunk/test/javax/el/TestImportHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to