Author: rmannibucau
Date: Mon Nov 5 13:12:28 2012
New Revision: 1405783
URL: http://svn.apache.org/viewvc?rev=1405783&view=rev
Log:
adding @Descriptors annotation to ease altDD creation
Added:
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/Descriptor.java
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/Descriptors.java
(contents, props changed)
- copied, changed from r1405723,
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/AppResource.java
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/junit/
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/junit/DescriptorTest.java
openejb/trunk/openejb/container/openejb-core/src/test/resources/descriptor-resources.xml
Modified:
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/ApplicationComposer.java
openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SimpleApplicationTest.java
Modified:
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/ApplicationComposer.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/ApplicationComposer.java?rev=1405783&r1=1405782&r2=1405783&view=diff
==============================================================================
---
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/ApplicationComposer.java
(original)
+++
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/ApplicationComposer.java
Mon Nov 5 13:12:28 2012
@@ -73,8 +73,12 @@ import org.junit.runners.model.TestClass
import javax.naming.Context;
import java.lang.reflect.Field;
+import java.net.URL;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.Properties;
import static
org.apache.openejb.config.DeploymentsResolver.DEPLOYMENTS_CLASSPATH_PROPERTY;
@@ -143,6 +147,15 @@ public class ApplicationComposer extends
}
}
+ for (FrameworkMethod method :
testClass.getAnnotatedMethods(Descriptors.class)) {
+ final Class<?> returnType = method.getMethod().getReturnType();
+ if (!returnType.equals(WebModule.class) &&
!returnType.equals(EjbModule.class)
+ && !returnType.equals(WebApp.class) &&
!returnType.equals(EjbJar.class)
+ && !returnType.equals(AppModule.class)) {
+ errors.add(new Exception("@Descriptors can't be used on " +
returnType.getName()));
+ }
+ }
+
for (FrameworkMethod method :
testClass.getAnnotatedMethods(Classes.class)) {
final Class<?> returnType = method.getMethod().getReturnType();
if (!returnType.equals(WebModule.class) &&
!returnType.equals(EjbModule.class)
@@ -290,6 +303,8 @@ public class ApplicationComposer extends
SystemInstance.get().setComponent((Class<Object>) key, value);
}
+ final Map<String, URL> additionalDescriptors =
descriptorsToMap(testClass.getJavaClass().getAnnotation(Descriptors.class));
+
Application application = null;
int webModulesNb = 0;
@@ -310,6 +325,10 @@ public class ApplicationComposer extends
}
final WebModule webModule = new WebModule(webapp, root,
Thread.currentThread().getContextClassLoader(), "", root);
+
+ webModule.getAltDDs().putAll(additionalDescriptors);
+
webModule.getAltDDs().putAll(descriptorsToMap(method.getAnnotation(Descriptors.class)));
+
if (classesAnnotation != null) {
webModule.setFinder(finderFromClasses(classesAnnotation.value()));
}
@@ -318,12 +337,20 @@ public class ApplicationComposer extends
webModulesNb++;
final WebModule webModule = (WebModule) obj;
+
+ webModule.getAltDDs().putAll(additionalDescriptors);
+
webModule.getAltDDs().putAll(descriptorsToMap(method.getAnnotation(Descriptors.class)));
+
if (classesAnnotation != null) {
webModule.setFinder(finderFromClasses(classesAnnotation.value()));
}
DeploymentLoader.addWebModule(webModule, appModule);
} else if (obj instanceof EjbModule) {
final EjbModule ejbModule = (EjbModule) obj;
+
+ ejbModule.getAltDDs().putAll(additionalDescriptors);
+
ejbModule.getAltDDs().putAll(descriptorsToMap(method.getAnnotation(Descriptors.class)));
+
if (classesAnnotation != null) {
ejbModule.setFinder(finderFromClasses(classesAnnotation.value()));
}
@@ -335,6 +362,10 @@ public class ApplicationComposer extends
setId(ejbJar, method);
final EjbModule ejbModule = new EjbModule(ejbJar);
+
+ ejbModule.getAltDDs().putAll(additionalDescriptors);
+
ejbModule.getAltDDs().putAll(descriptorsToMap(method.getAnnotation(Descriptors.class)));
+
appModule.getEjbModules().add(ejbModule);
if (classesAnnotation != null) {
ejbModule.setFinder(finderFromClasses(classesAnnotation.value()));
@@ -410,6 +441,9 @@ public class ApplicationComposer extends
// we can probably go further here
final AppModule module = (AppModule) obj;
+ module.getAltDDs().putAll(additionalDescriptors);
+
module.getAltDDs().putAll(descriptorsToMap(method.getAnnotation(Descriptors.class)));
+
if (module.getWebModules().size() > 0) {
webModulesNb++;
}
@@ -563,6 +597,18 @@ public class ApplicationComposer extends
}
}
+ private static Map<String, URL> descriptorsToMap(final Descriptors
descriptors) {
+ if (descriptors != null) {
+ final Map<String, URL> dds = new HashMap<String, URL>();
+ final ClassLoader loader =
Thread.currentThread().getContextClassLoader();
+ for (Descriptor descriptor : descriptors.value()) {
+ dds.put(descriptor.name(),
loader.getResource(descriptor.path()));
+ }
+ return dds;
+ }
+ return Collections.emptyMap();
+ }
+
private static IAnnotationFinder finderFromClasses(final Class<?>[] value)
{
return new AnnotationFinder(new ClassesArchive(value)).link();
}
Added:
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/Descriptor.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/Descriptor.java?rev=1405783&view=auto
==============================================================================
---
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/Descriptor.java
(added)
+++
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/Descriptor.java
Mon Nov 5 13:12:28 2012
@@ -0,0 +1,29 @@
+/*
+ * 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.junit;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target({ ElementType.METHOD, ElementType.TYPE })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Descriptor {
+ String name();
+ String path();
+}
Copied:
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/Descriptors.java
(from r1405723,
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/AppResource.java)
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/Descriptors.java?p2=openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/Descriptors.java&p1=openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/AppResource.java&r1=1405723&r2=1405783&rev=1405783&view=diff
==============================================================================
---
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/AppResource.java
(original)
+++
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/Descriptors.java
Mon Nov 5 13:12:28 2012
@@ -21,7 +21,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
-@Target(ElementType.FIELD)
+@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
-public @interface AppResource {
+public @interface Descriptors {
+ Descriptor[] value() default {};
}
Propchange:
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/Descriptors.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/junit/DescriptorTest.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/junit/DescriptorTest.java?rev=1405783&view=auto
==============================================================================
---
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/junit/DescriptorTest.java
(added)
+++
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/junit/DescriptorTest.java
Mon Nov 5 13:12:28 2012
@@ -0,0 +1,38 @@
+package org.apache.openejb.junit;
+
+import org.apache.openejb.assembler.classic.AppInfo;
+import org.apache.openejb.assembler.classic.Assembler;
+import org.apache.openejb.jee.EjbJar;
+import org.apache.openejb.loader.SystemInstance;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.sql.DataSource;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+@RunWith(ApplicationComposer.class)
+public class DescriptorTest {
+ @Module
+ @Descriptors(@Descriptor(name = "resources.xml", path =
"descriptor-resources.xml"))
+ public EjbJar classes() {
+ return new EjbJar();
+ }
+
+ @Resource
+ private DataSource ds;
+
+ @Test
+ public void checkDDWasHere() throws SQLException {
+ assertNotNull(ds);
+ final Connection connection = ds.getConnection();
+ final String url = connection.getMetaData().getURL();
+ assertEquals("jdbc:hsqldb:mem:descriptors", url);
+ connection.close();
+ }
+}
Added:
openejb/trunk/openejb/container/openejb-core/src/test/resources/descriptor-resources.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/test/resources/descriptor-resources.xml?rev=1405783&view=auto
==============================================================================
---
openejb/trunk/openejb/container/openejb-core/src/test/resources/descriptor-resources.xml
(added)
+++
openejb/trunk/openejb/container/openejb-core/src/test/resources/descriptor-resources.xml
Mon Nov 5 13:12:28 2012
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+
+-->
+<resources>
+ <Resource type="DataSource" id="jdbc/descriptors">
+ JdbcUrl = jdbc:hsqldb:mem:descriptors
+ JtaManaged = false
+ </Resource>
+</resources>
\ No newline at end of file
Modified:
openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SimpleApplicationTest.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SimpleApplicationTest.java?rev=1405783&r1=1405782&r2=1405783&view=diff
==============================================================================
---
openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SimpleApplicationTest.java
(original)
+++
openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SimpleApplicationTest.java
Mon Nov 5 13:12:28 2012
@@ -17,18 +17,14 @@
package org.apache.openejb.server.cxf.rs;
import org.apache.cxf.jaxrs.client.WebClient;
-import org.apache.openejb.OpenEjbContainer;
-import org.apache.openejb.assembler.classic.WebAppBuilder;
import org.apache.openejb.jee.WebApp;
import org.apache.openejb.junit.ApplicationComposer;
import org.apache.openejb.junit.Classes;
-import org.apache.openejb.junit.Component;
-import org.apache.openejb.junit.Configuration;
+import org.apache.openejb.junit.EnableServices;
import org.apache.openejb.junit.Module;
import org.apache.openejb.server.cxf.rs.beans.MyRESTApplication;
import org.apache.openejb.server.cxf.rs.beans.RestWithInjections;
import org.apache.openejb.server.cxf.rs.beans.SimpleEJB;
-import org.apache.openejb.web.LightweightWebAppBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -37,26 +33,14 @@ import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import java.io.InputStream;
import java.io.StringWriter;
-import java.util.Properties;
import static org.junit.Assert.assertEquals;
+@EnableServices
@RunWith(ApplicationComposer.class)
public class SimpleApplicationTest {
public static final String BASE_URL = "http://localhost:4204/foo/my-app";
- @Component
- public WebAppBuilder webAppBuilder() {
- return new LightweightWebAppBuilder();
- }
-
- @Configuration
- public Properties configuration() {
- final Properties properties = new Properties();
- properties.setProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE,
"true");
- return properties;
- }
-
@Module
@Classes({ RestWithInjections.class, SimpleEJB.class })
public WebApp war() {