You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2009/01/28 21:36:32 UTC

svn commit: r738614 - in /openejb/trunk/openejb3/container/openejb-core/src: main/java/org/apache/openejb/assembler/classic/ main/java/org/apache/openejb/config/ main/java/org/apache/openejb/config/rules/ main/resources/org/apache/openejb/config/rules/...

Author: dblevins
Date: Wed Jan 28 20:36:31 2009
New Revision: 738614

URL: http://svn.apache.org/viewvc?rev=738614&view=rev
Log:
OPENEJB-906: Validation: Referring to a bean via its bean class (pre 3.1)
OPENEJB-991: Validation: @EJB mistakenly applied to a javax.ejb.EJBObject interface
OPENEJB-992: Validation: @EJB mistakenly applied to a javax.ejb.EJBLocalObject interface
OPENEJB-993: Validation: @EJB mistakenly applied to a non-interface

Added:
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/AnnotationValidations.java
    openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidEjbRefTest.java   (contents, props changed)
      - copied, changed from r735612, openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidInterfacesTest.java
    openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/ValidationAssertions.java
Modified:
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EjbResolver.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
    openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties
    openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidInterfacesTest.java

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EjbResolver.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EjbResolver.java?rev=738614&r1=738613&r2=738614&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EjbResolver.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EjbResolver.java Wed Jan 28 20:36:31 2009
@@ -138,6 +138,13 @@
     private String resolveInterface(Reference ref) {
         String id = null;
 
+        if (ref.getInterface() == null) {
+            // TODO: Support home-only refs.  Could only happen for EJB 2.x Entity beans
+            // All other beans are required to have create methods in their home interfaces
+            // and we would have used it to discover the remote interface when creating the ref 
+            return null;
+        }
+
         List<Interfaces> matches = this.interfaces.get(new Interfaces(ref.getHome(), ref.getInterface()));
         if (matches != null && matches.size() > 0){
 

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java?rev=738614&r1=738613&r2=738614&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java Wed Jan 28 20:36:31 2009
@@ -162,7 +162,7 @@
 public class AnnotationDeployer implements DynamicDeployer {
     public static final Logger logger = Logger.getInstance(LogCategory.OPENEJB_STARTUP, AnnotationDeployer.class.getPackage().getName());
     public static final Logger startupLogger = Logger.getInstance(LogCategory.OPENEJB_STARTUP_CONFIG, "org.apache.openejb.util.resources");
-    private static final ThreadLocal<ValidationContext> validationContext = new ThreadLocal<ValidationContext>();
+    private static final ThreadLocal<DeploymentModule> currentModule = new ThreadLocal<DeploymentModule>();
 
     private final DiscoverAnnotatedBeans discoverAnnotatedBeans;
     private final ProcessAnnotatedBeans processAnnotatedBeans;
@@ -175,34 +175,43 @@
     }
 
     public AppModule deploy(AppModule appModule) throws OpenEJBException {
-        getValidationContext().set(appModule.getValidation());
+        setModule(appModule);
         try {
             appModule = discoverAnnotatedBeans.deploy(appModule);
             appModule = envEntriesPropertiesDeployer.deploy(appModule);
             appModule = processAnnotatedBeans.deploy(appModule);
             return appModule;
         } finally {
-            getValidationContext().remove();
+            removeModule();
         }
     }
 
     public WebModule deploy(WebModule webModule) throws OpenEJBException {
-        getValidationContext().set(webModule.getValidation());
+        setModule(webModule);
         try {
             webModule = discoverAnnotatedBeans.deploy(webModule);
             webModule = envEntriesPropertiesDeployer.deploy(webModule);
             webModule = processAnnotatedBeans.deploy(webModule);
             return webModule;
         } finally {
-            getValidationContext().remove();
+            removeModule();
         }
     }
 
-    public static ThreadLocal<ValidationContext> getValidationContext() {
-//        Throwable throwable = new Throwable();
-//        throwable.fillInStackTrace();
-//        throwable.printStackTrace();
-        return validationContext;
+    public static DeploymentModule getModule() {
+        return currentModule.get();
+    }
+
+    private static void setModule(DeploymentModule module) {
+        currentModule.set(module);
+    }
+
+    private static void removeModule() {
+        currentModule.remove();
+    }
+
+    private static ValidationContext getValidationContext() {
+        return getModule().getValidation();
     }
 
     public static class DiscoverAnnotatedBeans implements DynamicDeployer {
@@ -231,35 +240,35 @@
 
         public AppModule deploy(AppModule appModule) throws OpenEJBException {
             for (EjbModule ejbModule : appModule.getEjbModules()) {
-                getValidationContext().set(ejbModule.getValidation());
+                setModule(ejbModule);
                 try {
                     deploy(ejbModule);
                 } finally {
-                    getValidationContext().remove();
+                    removeModule();
                 }
             }
             for (ClientModule clientModule : appModule.getClientModules()) {
-                getValidationContext().set(clientModule.getValidation());
+                setModule(clientModule);
                 try {
                     deploy(clientModule);
                 } finally {
-                    getValidationContext().remove();
+                    removeModule();
                 }
             }
             for (ConnectorModule connectorModule : appModule.getResourceModules()) {
-                getValidationContext().set(connectorModule.getValidation());
+                setModule(connectorModule);
                 try {
                     deploy(connectorModule);
                 } finally {
-                    getValidationContext().remove();
+                    removeModule();
                 }
             }
             for (WebModule webModule : appModule.getWebModules()) {
-                getValidationContext().set(webModule.getValidation());
+                setModule(webModule);
                 try {
                     deploy(webModule);
                 } finally {
-                    getValidationContext().remove();
+                    removeModule();
                 }
             }
             return appModule;
@@ -457,7 +466,7 @@
         }
 
         private boolean isValidAnnotationUsage(Class annotationClass, Class<?> beanClass, String ejbName, EjbModule ejbModule) {
-            List<Class<? extends Annotation>> annotations = new ArrayList(asList(Stateless.class, Stateful.class, MessageDriven.class));
+            List<Class<? extends Annotation>> annotations = new ArrayList(asList(Singleton.class, Stateless.class, Stateful.class, MessageDriven.class));
             annotations.remove(annotationClass);
 
             Map<String, Class<? extends Annotation>> names = new HashMap<String, Class<? extends Annotation>>();
@@ -473,6 +482,8 @@
                     secondEjbName = getEjbName((Stateful) annotation, beanClass);
                 } else if (annotation instanceof Stateless) {
                     secondEjbName = getEjbName((Stateless) annotation, beanClass);
+                } else if (annotation instanceof Singleton) {
+                    secondEjbName = getEjbName((Singleton) annotation, beanClass);
                 } else if (annotation instanceof MessageDriven) {
                     secondEjbName = getEjbName((MessageDriven) annotation, beanClass);
                 }
@@ -522,35 +533,35 @@
 
         public AppModule deploy(AppModule appModule) throws OpenEJBException {
             for (EjbModule ejbModule : appModule.getEjbModules()) {
-                getValidationContext().set(ejbModule.getValidation());
+                setModule(ejbModule);
                 try {
                     deploy(ejbModule);
                 } finally {
-                    getValidationContext().remove();
+                    removeModule();
                 }
             }
             for (ClientModule clientModule : appModule.getClientModules()) {
-                getValidationContext().set(clientModule.getValidation());
+                setModule(clientModule);
                 try {
                     deploy(clientModule);
                 } finally {
-                    getValidationContext().remove();
+                    removeModule();
                 }
             }
             for (ConnectorModule connectorModule : appModule.getResourceModules()) {
-                getValidationContext().set(connectorModule.getValidation());
+                setModule(connectorModule);
                 try {
                     deploy(connectorModule);
                 } finally {
-                    getValidationContext().remove();
+                    removeModule();
                 }
             }
             for (WebModule webModule : appModule.getWebModules()) {
-                getValidationContext().set(webModule.getValidation());
+                setModule(webModule);
                 try {
                     deploy(webModule);
                 } finally {
-                    getValidationContext().remove();
+                    removeModule();
                 }
             }
             return appModule;
@@ -1836,17 +1847,21 @@
          */
         private void buildEjbRef(JndiConsumer consumer, EJB ejb, Member member) {
 
+            // TODO: Looks like we aren't looking for an existing ejb-ref or ejb-local-ref
+            // we need to do this to support overriding.
+            
             /**
              * Was @EJB used at a class level witout specifying the 'name' or 'beanInterface' attributes?
              */
+            String name = consumer.getJndiConsumerName();
             if (member == null) {
                 boolean shouldReturn = false;
                 if (ejb.name().equals("")) {
-                    getValidationContext().get().fail(consumer.getJndiConsumerName(), "ejbAnnotation.onClassWithNoName");
+                    fail(name, "ejbAnnotation.onClassWithNoName");
                     shouldReturn = true;
                 }
                 if (ejb.beanInterface().equals(Object.class)) {
-                    getValidationContext().get().fail(consumer.getJndiConsumerName(), "ejbAnnotation.onClassWithNoBeanInterface");
+                    fail(name, "ejbAnnotation.onClassWithNoBeanInterface");
                     shouldReturn = true;
                 }
                 if (shouldReturn) return;
@@ -1860,12 +1875,20 @@
             // figuring out what to do with it.
             ejbRef.setRefType(EjbReference.Type.UNKNOWN);
 
+            // Get the ejb-ref-name
+            String refName = ejb.name();
+            if (refName.equals("")) {
+                refName = (member == null) ? null : member.getDeclaringClass().getName() + "/" + member.getName();
+            }
+            ejbRef.setEjbRefName(refName);
+
             if (member != null) {
                 // Set the member name where this will be injected
                 InjectionTarget target = new InjectionTarget();
                 target.setInjectionTargetClass(member.getDeclaringClass().getName());
                 target.setInjectionTargetName(member.getName());
                 ejbRef.getInjectionTarget().add(target);
+
             }
 
             Class<?> interfce = ejb.beanInterface();
@@ -1873,6 +1896,10 @@
                 interfce = (member == null) ? null : member.getType();
             }
 
+            if (interfce != null && !isValidEjbInterface(name, interfce, ejbRef.getName())) {
+                return;
+            }
+
             if (interfce != null && !interfce.equals(Object.class)) {
                 if (EJBHome.class.isAssignableFrom(interfce)) {
                     ejbRef.setHome(interfce.getName());
@@ -1904,13 +1931,6 @@
                 }
             }
 
-            // Get the ejb-ref-name
-            String refName = ejb.name();
-            if (refName.equals("")) {
-                refName = (member == null) ? null : member.getDeclaringClass().getName() + "/" + member.getName();
-            }
-            ejbRef.setEjbRefName(refName);
-
             // Set the ejb-link, if any
             String ejbName = ejb.beanName();
             if (ejbName.equals("")) {
@@ -1958,6 +1978,48 @@
             }
         }
 
+        private boolean isValidEjbInterface(String b, Class clazz, String refName) {
+            if (!clazz.isInterface()) {
+
+                DeploymentModule module = getModule();
+                if (module instanceof EjbModule) {
+                    Set<String> beanClasses = new HashSet<String>();
+                    EjbModule ejbModule = (EjbModule) module;
+                    for (EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
+                        beanClasses.add(bean.getEjbClass());
+                    }
+
+                    if (beanClasses.contains(clazz.getName())){
+                        fail(b, "ann.ejb.beanClass", clazz.getName(), refName);
+                    } else {
+                        fail(b, "ann.ejb.notInterface", clazz.getName(), refName);
+                    }
+                } else {
+                    fail(b, "ann.ejb.notInterface", clazz.getName(), refName);
+                }
+
+                return false;
+
+            } else if (EJBObject.class.isAssignableFrom(clazz)) {
+
+                fail(b, "ann.ejb.ejbObject", clazz.getName(), refName);
+
+                return false;
+
+            } else if (EJBLocalObject.class.isAssignableFrom(clazz)) {
+
+                fail(b, "ann.ejb.ejbLocalObject", clazz.getName(), refName);
+
+                return false;
+            }
+
+            return true;
+        }
+
+        private void fail(String component, String key, Object... details) {
+            getValidationContext().fail(component, key, details);
+        }
+
         /**
          * Process @Resource into either <resource-ref> or <resource-env-ref> for the given member (field or method) or class
          *
@@ -1978,11 +2040,11 @@
             if (member == null) {
                 boolean shouldReturn = false;
                 if (resource.name().equals("")) {
-                    getValidationContext().get().fail(consumer.getJndiConsumerName(), "resourceAnnotation.onClassWithNoName");
+                    fail(consumer.getJndiConsumerName(), "resourceAnnotation.onClassWithNoName");
                     shouldReturn = true;
                 }
                 if (resource.type().equals(Object.class)) {
-                    getValidationContext().get().fail(consumer.getJndiConsumerName(), "resourceAnnotation.onClassWithNoType");
+                    fail(consumer.getJndiConsumerName(), "resourceAnnotation.onClassWithNoType");
                     shouldReturn = true;
                 }
                 if (shouldReturn) return;
@@ -1998,10 +2060,10 @@
                     Class type = member.getType();
                     boolean shouldReturn = false;
                     if (EntityManager.class.isAssignableFrom(type)) {
-                        getValidationContext().get().fail(consumer.getJndiConsumerName(), "resourceRef.onEntityManager", refName);
+                        fail(consumer.getJndiConsumerName(), "resourceRef.onEntityManager", refName);
                         shouldReturn = true;
                     } else if (EntityManagerFactory.class.isAssignableFrom(type)) {
-                        getValidationContext().get().fail(consumer.getJndiConsumerName(), "resourceRef.onEntityManagerFactory", refName);
+                        fail(consumer.getJndiConsumerName(), "resourceRef.onEntityManagerFactory", refName);
                         shouldReturn = true;
                     }
                     if (shouldReturn) return;
@@ -2126,7 +2188,7 @@
              * Was @PersistenceUnit used at a class level without specifying the 'name' attribute?
              */
             if (refName == null && member == null) {
-                getValidationContext().get().fail(consumer.getJndiConsumerName(), "presistenceUnitAnnotation.onClassWithNoName", persistenceUnit.unitName());
+                fail(consumer.getJndiConsumerName(), "presistenceUnitAnnotation.onClassWithNoName", persistenceUnit.unitName());
                 return;
             }
 
@@ -2145,7 +2207,7 @@
                     /**
                      * Was @PersistenceUnit mistakenly used when @PersistenceContext should have been used?
                      */
-                    ValidationContext validationContext = AnnotationDeployer.getValidationContext().get();
+                    ValidationContext validationContext = getValidationContext();
                     String jndiConsumerName = consumer.getJndiConsumerName();
                     String name = persistenceUnitRef.getName();
                     validationContext.fail(jndiConsumerName, "presistenceUnitAnnotation.onEntityManager", name);
@@ -2153,7 +2215,7 @@
                     /**
                      * Was @PersistenceUnit mistakenly used for something that isn't an EntityManagerFactory?
                      */
-                    getValidationContext().get().fail(consumer.getJndiConsumerName(), "presistenceUnitAnnotation.onNonEntityManagerFactory", persistenceUnitRef.getName());
+                    fail(consumer.getJndiConsumerName(), "presistenceUnitAnnotation.onNonEntityManagerFactory", persistenceUnitRef.getName());
                 } else {
                     // Set the member name where this will be injected
                     InjectionTarget target = new InjectionTarget();
@@ -2189,7 +2251,7 @@
              * Was @PersistenceContext used at a class level without specifying the 'name' attribute?
              */
             if (refName == null && member == null) {
-                getValidationContext().get().fail(consumer.getJndiConsumerName(), "presistenceContextAnnotation.onClassWithNoName", persistenceContext.unitName());
+                fail(consumer.getJndiConsumerName(), "presistenceContextAnnotation.onClassWithNoName", persistenceContext.unitName());
                 return;
             }
 
@@ -2247,12 +2309,12 @@
                     /**
                      * Was @PersistenceContext mistakenly used when @PersistenceUnit should have been used?
                      */
-                    getValidationContext().get().fail(consumer.getJndiConsumerName(), "presistenceContextAnnotation.onEntityManagerFactory", persistenceContextRef.getName());
+                    fail(consumer.getJndiConsumerName(), "presistenceContextAnnotation.onEntityManagerFactory", persistenceContextRef.getName());
                 } else if (!EntityManager.class.isAssignableFrom(type)) {
                     /**
                      * Was @PersistenceContext mistakenly used for something that isn't an EntityManager?
                      */
-                    getValidationContext().get().fail(consumer.getJndiConsumerName(), "presistenceContextAnnotation.onNonEntityManager", persistenceContextRef.getName());
+                    fail(consumer.getJndiConsumerName(), "presistenceContextAnnotation.onNonEntityManager", persistenceContextRef.getName());
                 } else {
                     // Set the member name where this will be injected
                     InjectionTarget target = new InjectionTarget();

Added: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/AnnotationValidations.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/AnnotationValidations.java?rev=738614&view=auto
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/AnnotationValidations.java (added)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/AnnotationValidations.java Wed Jan 28 20:36:31 2009
@@ -0,0 +1,26 @@
+/**
+ * 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 org.apache.openejb.config.rules;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class AnnotationValidations {
+
+
+
+}

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties?rev=738614&r1=738613&r2=738614&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties Wed Jan 28 20:36:31 2009
@@ -740,6 +740,24 @@
 2.xml.businessLocal.notInterface = The value of <business-local> is not an interface: {0}
 3.xml.businessLocal.notInterface = The <business-local> element of the ejb-jar.xml must be an interface.  Classes, abstract classes or enums are not allowed.  Either convert {0} to an interface or remove the related <business-local> xml tag from your ejb-jar.xml
 
+
+# TODO: add the apostrophes back in "beans"
+1.ann.ejb.ejbObject = @EJB mistakenly refers to a beans javax.ejb.EJBObject interface
+2.ann.ejb.ejbObject = @EJB mistakenly refers to a beans javax.ejb.EJBObject interface {0}.  Use the EJBHome interface instead.
+3.ann.ejb.ejbObject = When using the @EJB annotation to refer to older EJB 2.x component interfaces, it is the the home interface should be referenced/injected via @EJB.  The home interface is then used in code to create a reference to the {0} interface.  Change the reference type of {1} to the beans home interface.
+
+1.ann.ejb.ejbLocalObject = @EJB mistakenly refers to a beans javax.ejb.EJBLocalObject interface
+2.ann.ejb.ejbLocalObject = @EJB mistakenly refers to a beans javax.ejb.EJBLocalObject interface {0}.  Use the EJBLocalHome interface instead.
+3.ann.ejb.ejbLocalObject = When using the @EJB annotation to refer to older EJB 2.x component interfaces, it is the the local home interface should be referenced/injected via @EJB.  The local home interface is then used in code to create a reference to the {0} interface.  Change the reference type of {1} to the beans local home interface.
+
+1.ann.ejb.beanClass = @EJB mistakenly refers to a bean class
+2.ann.ejb.beanClass = @EJB mistakenly refers to a bean class {0}.  Use the beans business interface instead
+3.ann.ejb.beanClass = The @EJB annotation cannot be used to directly refer to the bean class itself and must instead refer to the bean using one of its business interfaces.  Change the type of the {1} reference from {0} to a business interface of that bean.
+
+1.ann.ejb.notInterface = @EJB mistakenly refers to a non-interface
+2.ann.ejb.notInterface = @EJB mistakenly refers to a non-interface.  Use the beans business interface instead
+3.ann.ejb.notInterface = The @EJB annotation cannot be used to directly refer to the bean class itself and must instead refer to the bean using one of its business interfaces.  Change the type of the {1} reference from {0} to a business interface of that bean.
+
 # fail(enterpriseBean.getEjbName(), "dependsOn.noSuchEjb", ejbName);
 1.dependsOn.noSuchEjb = Singleton @DependsOn refers to non-existant EJB:
 2.dependsOn.noSuchEjb = Singleton @DependsOn refers to non-existant EJB: {0}

Copied: openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidEjbRefTest.java (from r735612, openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidInterfacesTest.java)
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidEjbRefTest.java?p2=openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidEjbRefTest.java&p1=openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidInterfacesTest.java&r1=735612&r2=738614&rev=738614&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidInterfacesTest.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidEjbRefTest.java Wed Jan 28 20:36:31 2009
@@ -16,214 +16,105 @@
  */
 package org.apache.openejb.config.rules;
 
-import static org.apache.openejb.util.Join.join;
 import junit.framework.TestCase;
 import org.apache.openejb.assembler.classic.ContainerSystemInfo;
 import org.apache.openejb.assembler.classic.StatelessSessionContainerInfo;
 import org.apache.openejb.config.ConfigurationFactory;
 import org.apache.openejb.config.ValidationFailedException;
-import org.apache.openejb.config.ValidationFailure;
+import static org.apache.openejb.config.rules.ValidationAssertions.assertFailures;
 import org.apache.openejb.jee.EjbJar;
 import org.apache.openejb.jee.StatelessBean;
-import org.apache.openejb.OpenEJBException;
 
+import javax.ejb.EJB;
 import javax.ejb.EJBHome;
 import javax.ejb.EJBLocalHome;
 import javax.ejb.EJBLocalObject;
 import javax.ejb.EJBObject;
 import javax.ejb.Local;
 import javax.ejb.Remote;
-import java.util.List;
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
+import java.util.List;
 
 /**
  * @version $Rev$ $Date$
  */
-public class InvalidInterfacesTest extends TestCase {
+public class InvalidEjbRefTest extends TestCase {
+
     private ConfigurationFactory config;
 
-    public void testCorrectInterfaces() throws Exception {
+    public void test() throws Exception {
 
         EjbJar ejbJar = new EjbJar();
-        StatelessBean bean = ejbJar.addEnterpriseBean(new StatelessBean(FooBean.class));
-        bean.setHomeAndRemote(FooEJBHome.class, FooEJBObject.class);
-        bean.setHomeAndLocal(FooEJBLocalHome.class, FooEJBLocalObject.class);
-        bean.addBusinessLocal(FooLocal.class.getName());
-        bean.addBusinessRemote(FooRemote.class.getName());
-
-        try {
-            config.configureApplication(ejbJar);
-        } catch (ValidationFailedException e) {
-            for (ValidationFailure failure : e.getFailures()) {
-                System.out.println("failure = " + failure.getMessageKey());
-            }
-            fail("There should be no validation failures");
-        }
-
-    }
-
-    public void testBusinessLocal() throws Exception {
-
-        List<String> expectedKeys = new ArrayList<String>();
-        expectedKeys.add("xml.remote.businessLocal");
-        expectedKeys.add("xml.home.businessLocal");
-        expectedKeys.add("xml.local.businessLocal");
-        expectedKeys.add("xml.localHome.businessLocal");
-
-        validate(FooLocal.class, expectedKeys);
-    }
 
-    public void testBusinessRemote() throws Exception {
+        ejbJar.addEnterpriseBean(new StatelessBean(EjbRefBean.class));
 
-        List<String> expectedKeys = new ArrayList<String>();
-        expectedKeys.add("xml.remote.businessRemote");
-        expectedKeys.add("xml.home.businessRemote");
-        expectedKeys.add("xml.local.businessRemote");
-        expectedKeys.add("xml.localHome.businessRemote");
-
-        validate(FooRemote.class, expectedKeys);
-    }
-
-    public void testEJBObject() throws Exception {
+        StatelessBean fooBean = ejbJar.addEnterpriseBean(new StatelessBean(FooBean.class));
+        fooBean.setHomeAndRemote(FooEJBHome.class, FooEJBObject.class);
+        fooBean.setHomeAndLocal(FooEJBLocalHome.class, FooEJBLocalObject.class);
+        fooBean.addBusinessLocal(FooLocal.class.getName());
+        fooBean.addBusinessRemote(FooRemote.class.getName());
 
         List<String> expectedKeys = new ArrayList<String>();
-        expectedKeys.add("xml.home.ejbObject");
-        expectedKeys.add("xml.local.ejbObject");
-        expectedKeys.add("xml.localHome.ejbObject");
-        expectedKeys.add("xml.businessLocal.ejbObject");
-        expectedKeys.add("xml.businessRemote.ejbObject");
+        expectedKeys.add("ann.ejb.ejbObject");
+        expectedKeys.add("ann.ejb.ejbLocalObject");
+        expectedKeys.add("ann.ejb.beanClass");
+        expectedKeys.add("ann.ejb.notInterface");
 
-        validate(FooEJBObject.class, expectedKeys);
-    }
-
-    public void testEJBHome() throws Exception {
-
-        List<String> expectedKeys = new ArrayList<String>();
-        expectedKeys.add("xml.remote.ejbHome");
-        expectedKeys.add("xml.local.ejbHome");
-        expectedKeys.add("xml.localHome.ejbHome");
-        expectedKeys.add("xml.businessLocal.ejbHome");
-        expectedKeys.add("xml.businessRemote.ejbHome");
-
-        validate(FooEJBHome.class, expectedKeys);
+        try {
+            config.configureApplication(ejbJar);
+            fail("A ValidationFailedException should have been thrown");
+        } catch (ValidationFailedException e) {
+            assertFailures(expectedKeys, e);
+        }
     }
 
-    public void testEJBLocalHome() throws Exception {
-
-        List<String> expectedKeys = new ArrayList<String>();
-        expectedKeys.add("xml.remote.ejbLocalHome");
-        expectedKeys.add("xml.home.ejbLocalHome");
-        expectedKeys.add("xml.local.ejbLocalHome");
-        expectedKeys.add("xml.businessLocal.ejbLocalHome");
-        expectedKeys.add("xml.businessRemote.ejbLocalHome");
-
-        validate(FooEJBLocalHome.class, expectedKeys);
+    public void setUp() throws Exception {
+        config = new ConfigurationFactory(true);
+        ContainerSystemInfo containerSystem = config.getOpenEjbConfiguration().containerSystem;
+        containerSystem.containers.add(config.configureService(StatelessSessionContainerInfo.class));
     }
 
-    public void testEJBLocalObject() throws Exception {
 
-        List<String> expectedKeys = new ArrayList<String>();
-        expectedKeys.add("xml.remote.ejbLocalObject");
-        expectedKeys.add("xml.home.ejbLocalObject");
-        expectedKeys.add("xml.localHome.ejbLocalObject");
-        expectedKeys.add("xml.businessLocal.ejbLocalObject");
-        expectedKeys.add("xml.businessRemote.ejbLocalObject");
+    public static class EjbRefBean implements EjbRefBeanLocal {
+        // invalid
+        @EJB
+        private FooBean fooBean;
 
-        validate(FooEJBLocalObject.class, expectedKeys);
-    }
+        // valid
+        @EJB
+        private FooEJBHome fooEJBHome;
 
-    public void testUnkown() throws Exception {
+        // invalid
+        @EJB
+        private FooEJBObject fooEJBObject;
 
-        List<String> expectedKeys = new ArrayList<String>();
-        expectedKeys.add("xml.remote.unknown");
-        expectedKeys.add("xml.home.unknown");
-        expectedKeys.add("xml.localHome.unknown");
-        expectedKeys.add("xml.local.unknown");
+        // valid
+        @EJB
+        private FooEJBLocalHome fooEJBLocalHome;
 
-        validate(FooUnknown.class, expectedKeys);
-    }
+        // invalid
+        @EJB
+        private FooEJBLocalObject fooEJBLocalObject;
 
-    public void testBeanClass() throws Exception {
+        // valid
+        @EJB
+        private FooRemote fooRemote;
 
-        List<String> expectedKeys = new ArrayList<String>();
-        expectedKeys.add("xml.remote.beanClass");
-        expectedKeys.add("xml.home.beanClass");
-        expectedKeys.add("xml.localHome.beanClass");
-        expectedKeys.add("xml.local.beanClass");
-        expectedKeys.add("xml.businessRemote.beanClass");
-        expectedKeys.add("xml.businessLocal.beanClass");
+        // valid
+        @EJB
+        private FooLocal fooLocal;
 
-        validate(FooBean.class, expectedKeys);
-    }
-
-    public void testNotInterface() throws Exception {
-
-        List<String> expectedKeys = new ArrayList<String>();
-        expectedKeys.add("xml.remote.notInterface");
-        expectedKeys.add("xml.home.notInterface");
-        expectedKeys.add("xml.localHome.notInterface");
-        expectedKeys.add("xml.local.notInterface");
-        expectedKeys.add("xml.businessRemote.notInterface");
-        expectedKeys.add("xml.businessLocal.notInterface");
+        // valid
+        @EJB
+        private FooUnknown fooUnknown;
 
-        validate(FooClass.class, expectedKeys);
+        // invalid
+        @EJB
+        private FooClass fooClass;
     }
 
-    private void validate(Class interfaceClass, List<String> expectedKeys) throws OpenEJBException {
-        Collections.sort(expectedKeys);
+    public static interface EjbRefBeanLocal {
 
-        EjbJar ejbJar = new EjbJar();
-        StatelessBean bean = ejbJar.addEnterpriseBean(new StatelessBean(FooBean.class));
-        bean.setHomeAndLocal(interfaceClass, interfaceClass);
-        bean.setHomeAndRemote(interfaceClass, interfaceClass);
-        bean.addBusinessLocal(interfaceClass);
-        bean.addBusinessRemote(interfaceClass);
-
-        try {
-            config.configureApplication(ejbJar);
-            fail("A ValidationFailedException should have been thrown");
-        } catch (ValidationFailedException e) {
-            ValidationFailure[] failures = e.getFailures();
-            List<String> actualKeys = new ArrayList<String>();
-            for (ValidationFailure failure : failures) {
-                actualKeys.add(failure.getMessageKey());
-            }
-
-            Collections.sort(actualKeys);
-
-            String actual = join("\n", actualKeys);
-            String expected = join("\n", expectedKeys);
-
-            assertEquals("Keys do not match", expected, actual);
-
-            // Check for the expected keys
-            for (String key : expectedKeys) {
-                assertTrue("Missing key: "+key, actualKeys.contains(key));
-            }
-
-            assertEquals("Number of failures don't match", expectedKeys.size(), actualKeys.size());
-
-            // Ensure the i18n message is there by checking
-            // the key is not in the getMessage() output
-            for (ValidationFailure failure : e.getFailures()) {
-                String key = failure.getMessageKey();
-
-                for (Integer level : Arrays.asList(1, 2, 3)) {
-                    String message = failure.getMessage(level);
-                    assertFalse("No message text (key=" + key + ", level=" + level + "): " + message, message.contains(key));
-                    assertFalse("Not all parameters substituted (key=" + key + ", level=" + level + "): " + message, message.matches(".*\\{[0-9]\\}.*"));
-                }
-
-            }
-        }
-    }
-
-    public void setUp() throws Exception {
-        config = new ConfigurationFactory(true);
-        ContainerSystemInfo containerSystem = config.getOpenEjbConfiguration().containerSystem;
-        containerSystem.containers.add(config.configureService(StatelessSessionContainerInfo.class));
     }
 
     public static class FooBean {
@@ -231,12 +122,14 @@
     }
 
     public static interface FooEJBHome extends EJBHome {
+        FooEJBObject create();
     }
 
     public static interface FooEJBObject extends EJBObject {
     }
 
     public static interface FooEJBLocalHome extends EJBLocalHome {
+        FooEJBLocalObject create();
     }
 
     public static interface FooEJBLocalObject extends EJBLocalObject {
@@ -256,4 +149,4 @@
     public static class FooClass {
 
     }
-}
+}
\ No newline at end of file

Propchange: openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidEjbRefTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author Id Revision HeadURL

Propchange: openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidEjbRefTest.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Modified: openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidInterfacesTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidInterfacesTest.java?rev=738614&r1=738613&r2=738614&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidInterfacesTest.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/InvalidInterfacesTest.java Wed Jan 28 20:36:31 2009
@@ -172,7 +172,6 @@
     }
 
     private void validate(Class interfaceClass, List<String> expectedKeys) throws OpenEJBException {
-        Collections.sort(expectedKeys);
 
         EjbJar ejbJar = new EjbJar();
         StatelessBean bean = ejbJar.addEnterpriseBean(new StatelessBean(FooBean.class));
@@ -185,38 +184,7 @@
             config.configureApplication(ejbJar);
             fail("A ValidationFailedException should have been thrown");
         } catch (ValidationFailedException e) {
-            ValidationFailure[] failures = e.getFailures();
-            List<String> actualKeys = new ArrayList<String>();
-            for (ValidationFailure failure : failures) {
-                actualKeys.add(failure.getMessageKey());
-            }
-
-            Collections.sort(actualKeys);
-
-            String actual = join("\n", actualKeys);
-            String expected = join("\n", expectedKeys);
-
-            assertEquals("Keys do not match", expected, actual);
-
-            // Check for the expected keys
-            for (String key : expectedKeys) {
-                assertTrue("Missing key: "+key, actualKeys.contains(key));
-            }
-
-            assertEquals("Number of failures don't match", expectedKeys.size(), actualKeys.size());
-
-            // Ensure the i18n message is there by checking
-            // the key is not in the getMessage() output
-            for (ValidationFailure failure : e.getFailures()) {
-                String key = failure.getMessageKey();
-
-                for (Integer level : Arrays.asList(1, 2, 3)) {
-                    String message = failure.getMessage(level);
-                    assertFalse("No message text (key=" + key + ", level=" + level + "): " + message, message.contains(key));
-                    assertFalse("Not all parameters substituted (key=" + key + ", level=" + level + "): " + message, message.matches(".*\\{[0-9]\\}.*"));
-                }
-
-            }
+            ValidationAssertions.assertFailures(expectedKeys, e);
         }
     }
 

Added: openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/ValidationAssertions.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/ValidationAssertions.java?rev=738614&view=auto
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/ValidationAssertions.java (added)
+++ openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/rules/ValidationAssertions.java Wed Jan 28 20:36:31 2009
@@ -0,0 +1,82 @@
+/**
+ * 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 org.apache.openejb.config.rules;
+
+import org.apache.openejb.config.ValidationFailedException;
+import org.apache.openejb.config.ValidationException;
+import org.apache.openejb.config.ValidationFailure;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Arrays;
+
+import junit.framework.Assert;
+
+/**
+ * @version $Rev$ $Date$
+*/
+public class ValidationAssertions {
+
+    public static void assertFailures(List<String> expectedKeys, ValidationFailedException e) {
+        assertValidation(expectedKeys, e, e.getFailures());
+    }
+
+    public static void assertErrors(List<String> expectedKeys, ValidationFailedException e) {
+        assertValidation(expectedKeys, e, e.getErrors());
+    }
+
+    public static void assertWarnings(List<String> expectedKeys, ValidationFailedException e) {
+        assertValidation(expectedKeys, e, e.getWarnings());
+    }
+
+    private static void assertValidation(List<String> expectedKeys, ValidationFailedException e, ValidationException[] failures) {
+        List<String> actualKeys = new ArrayList<String>();
+        for (ValidationException failure : failures) {
+            actualKeys.add(failure.getMessageKey());
+        }
+
+        Collections.sort(expectedKeys);
+        
+        Collections.sort(actualKeys);
+
+        String actual = org.apache.openejb.util.Join.join("\n", actualKeys);
+        String expected = org.apache.openejb.util.Join.join("\n", expectedKeys);
+
+        Assert.assertEquals("Keys do not match", expected, actual);
+
+        // Check for the expected keys
+        for (String key : expectedKeys) {
+            Assert.assertTrue("Missing key: "+key, actualKeys.contains(key));
+        }
+
+        Assert.assertEquals("Number of failures don't match", expectedKeys.size(), actualKeys.size());
+
+        // Ensure the i18n message is there by checking
+        // the key is not in the getMessage() output
+        for (ValidationFailure failure : e.getFailures()) {
+            String key = failure.getMessageKey();
+
+            for (Integer level : Arrays.asList(1, 2, 3)) {
+                String message = failure.getMessage(level);
+                Assert.assertFalse("No message text (key=" + key + ", level=" + level + "): " + message, message.contains(key));
+                Assert.assertFalse("Not all parameters substituted (key=" + key + ", level=" + level + "): " + message, message.matches(".*\\{[0-9]\\}.*"));
+            }
+
+        }
+    }
+}