From 5c41ae8adffb2e47787181c0276620a287378b13 Mon Sep 17 00:00:00 2001 From: Mark Struberg Date: Fri, 17 Nov 2023 19:02:49 +0100 Subject: [PATCH 1/2] add an integration test for optional beans if a class creates a NoClassDefFoundError or ClassNotFoundException then it's simply not an eligible bean class. --- .../3rdpartydep/pom.xml | 32 ++++++++ .../optional/thirdparty/ThirdPartyApi.java | 10 +++ .../src/main/resources/META-INF/beans.xml | 25 ++++++ .../app/pom.xml | 47 +++++++++++ .../app/src/main/resources/META-INF/beans.xml | 25 ++++++ .../app/OptionalDependencyResolutionTest.java | 78 +++++++++++++++++++ .../lib_with_optional_dep/pom.xml | 41 ++++++++++ .../tomee/itests/optional/optlib/Bicycle.java | 30 +++++++ .../tomee/itests/optional/optlib/Car.java | 43 ++++++++++ .../itests/optional/optlib/SomeBean.java | 30 +++++++ .../tomee/itests/optional/optlib/Vehicle.java | 25 ++++++ .../src/main/resources/META-INF/beans.xml | 25 ++++++ .../openejb-itests-optional-classes/pom.xml | 61 +++++++++++++++ itests/pom.xml | 1 + 14 files changed, 473 insertions(+) create mode 100644 itests/openejb-itests-optional-classes/3rdpartydep/pom.xml create mode 100644 itests/openejb-itests-optional-classes/3rdpartydep/src/main/java/org/apache/tomee/itests/optional/thirdparty/ThirdPartyApi.java create mode 100644 itests/openejb-itests-optional-classes/3rdpartydep/src/main/resources/META-INF/beans.xml create mode 100644 itests/openejb-itests-optional-classes/app/pom.xml create mode 100644 itests/openejb-itests-optional-classes/app/src/main/resources/META-INF/beans.xml create mode 100644 itests/openejb-itests-optional-classes/app/src/test/java/org/apache/tomee/itests/optional/app/OptionalDependencyResolutionTest.java create mode 100644 itests/openejb-itests-optional-classes/lib_with_optional_dep/pom.xml create mode 100644 itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/Bicycle.java create mode 100644 itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/Car.java create mode 100644 itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/SomeBean.java create mode 100644 itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/Vehicle.java create mode 100644 itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/resources/META-INF/beans.xml create mode 100644 itests/openejb-itests-optional-classes/pom.xml diff --git a/itests/openejb-itests-optional-classes/3rdpartydep/pom.xml b/itests/openejb-itests-optional-classes/3rdpartydep/pom.xml new file mode 100644 index 00000000000..fceac6c3632 --- /dev/null +++ b/itests/openejb-itests-optional-classes/3rdpartydep/pom.xml @@ -0,0 +1,32 @@ + + + + + + + 4.0.0 + + + org.apache.tomee.itests + openejb-itests-optional-classes + 10.0.0-M1-SNAPSHOT + + + opt-3rdparty + \ No newline at end of file diff --git a/itests/openejb-itests-optional-classes/3rdpartydep/src/main/java/org/apache/tomee/itests/optional/thirdparty/ThirdPartyApi.java b/itests/openejb-itests-optional-classes/3rdpartydep/src/main/java/org/apache/tomee/itests/optional/thirdparty/ThirdPartyApi.java new file mode 100644 index 00000000000..07626561b6e --- /dev/null +++ b/itests/openejb-itests-optional-classes/3rdpartydep/src/main/java/org/apache/tomee/itests/optional/thirdparty/ThirdPartyApi.java @@ -0,0 +1,10 @@ +package org.apache.tomee.itests.optional.thirdparty; + +/** + * This is an example of an optional 3rd party library + * used in the opt-lib project. + */ +public interface ThirdPartyApi { + + int meaningOfLife(); +} \ No newline at end of file diff --git a/itests/openejb-itests-optional-classes/3rdpartydep/src/main/resources/META-INF/beans.xml b/itests/openejb-itests-optional-classes/3rdpartydep/src/main/resources/META-INF/beans.xml new file mode 100644 index 00000000000..3ebaaf5c5b3 --- /dev/null +++ b/itests/openejb-itests-optional-classes/3rdpartydep/src/main/resources/META-INF/beans.xml @@ -0,0 +1,25 @@ + + + + + diff --git a/itests/openejb-itests-optional-classes/app/pom.xml b/itests/openejb-itests-optional-classes/app/pom.xml new file mode 100644 index 00000000000..4a8cfd0349e --- /dev/null +++ b/itests/openejb-itests-optional-classes/app/pom.xml @@ -0,0 +1,47 @@ + + + + + + + 4.0.0 + + + org.apache.tomee.itests + openejb-itests-optional-classes + 10.0.0-M1-SNAPSHOT + + + opt-app + + + + org.apache.tomee.itests + opt-lib + ${project.version} + + + org.apache.tomee + openejb-core + ${project.version} + test + + + + \ No newline at end of file diff --git a/itests/openejb-itests-optional-classes/app/src/main/resources/META-INF/beans.xml b/itests/openejb-itests-optional-classes/app/src/main/resources/META-INF/beans.xml new file mode 100644 index 00000000000..3ebaaf5c5b3 --- /dev/null +++ b/itests/openejb-itests-optional-classes/app/src/main/resources/META-INF/beans.xml @@ -0,0 +1,25 @@ + + + + + diff --git a/itests/openejb-itests-optional-classes/app/src/test/java/org/apache/tomee/itests/optional/app/OptionalDependencyResolutionTest.java b/itests/openejb-itests-optional-classes/app/src/test/java/org/apache/tomee/itests/optional/app/OptionalDependencyResolutionTest.java new file mode 100644 index 00000000000..58fd3eaede2 --- /dev/null +++ b/itests/openejb-itests-optional-classes/app/src/test/java/org/apache/tomee/itests/optional/app/OptionalDependencyResolutionTest.java @@ -0,0 +1,78 @@ +/* + * 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.tomee.itests.optional.app; + +import java.util.Properties; +import java.util.Set; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import org.apache.openejb.OpenEjbContainer; +import org.apache.openejb.core.LocalInitialContext; +import org.apache.openejb.core.LocalInitialContextFactory; +import org.apache.tomee.itests.optional.optlib.Car; +import org.apache.tomee.itests.optional.optlib.SomeBean; +import org.apache.tomee.itests.optional.optlib.Vehicle; +import org.junit.Test; + +import jakarta.enterprise.inject.spi.Bean; +import jakarta.enterprise.inject.spi.BeanManager; +import jakarta.enterprise.inject.spi.CDI; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * @author Mark Struberg + */ +public class OptionalDependencyResolutionTest { + + @Test + public void testContainerBoot() throws NamingException { + // we use classic discovery via the classpath + Properties p = new Properties(); + p.put(Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName()); + p.put(LocalInitialContext.ON_CLOSE, LocalInitialContext.Close.DESTROY.name()); + try + { + this.getClass().getClassLoader().loadClass("org.apache.openejb.server.ServiceManager"); + p.put(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, "true"); + } + catch (final Exception e) + { + // ignored + } + + Context context = new InitialContext(p); + + final BeanManager beanManager = CDI.current().getBeanManager(); + assertNotNull(beanManager); + + final Set> someBeanBeans = beanManager.getBeans(SomeBean.class); + assertTrue(!someBeanBeans.isEmpty()); + + // as there should be a NoClassDefFound of the ThirdPartyApi.class we did not pick up + // the Car but only the Bicycle + final Set> optionalBeanBeans = beanManager.getBeans(Vehicle.class); + assertEquals(1, optionalBeanBeans.size()); + final Vehicle vehicle = CDI.current().select(Vehicle.class).get(); + assertFalse(vehicle.motorized()); + } +} diff --git a/itests/openejb-itests-optional-classes/lib_with_optional_dep/pom.xml b/itests/openejb-itests-optional-classes/lib_with_optional_dep/pom.xml new file mode 100644 index 00000000000..29e200d3304 --- /dev/null +++ b/itests/openejb-itests-optional-classes/lib_with_optional_dep/pom.xml @@ -0,0 +1,41 @@ + + + + + + + 4.0.0 + + + org.apache.tomee.itests + openejb-itests-optional-classes + 10.0.0-M1-SNAPSHOT + + + opt-lib + + + + org.apache.tomee.itests + opt-3rdparty + ${project.version} + true + + + \ No newline at end of file diff --git a/itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/Bicycle.java b/itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/Bicycle.java new file mode 100644 index 00000000000..238513945e0 --- /dev/null +++ b/itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/Bicycle.java @@ -0,0 +1,30 @@ +/* + * 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.tomee.itests.optional.optlib; + +import jakarta.enterprise.context.ApplicationScoped; + +/** + * + */ +@ApplicationScoped +public class Bicycle implements Vehicle { + @Override + public boolean motorized() { + return false; + } +} diff --git a/itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/Car.java b/itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/Car.java new file mode 100644 index 00000000000..2dc8a21028d --- /dev/null +++ b/itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/Car.java @@ -0,0 +1,43 @@ +/* + * 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.tomee.itests.optional.optlib; + +import org.apache.tomee.itests.optional.thirdparty.ThirdPartyApi; + +import jakarta.annotation.Priority; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Alternative; + +/** + * This bean will not get picked up in the app as the ThirdPartyApi class + * will not be available over there! + */ +@ApplicationScoped +@Alternative +@Priority(1000) +public class Car implements Vehicle, ThirdPartyApi { + + @Override + public int meaningOfLife() { + return 42; + } + + @Override + public boolean motorized() { + return true; + } +} diff --git a/itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/SomeBean.java b/itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/SomeBean.java new file mode 100644 index 00000000000..cae506de585 --- /dev/null +++ b/itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/SomeBean.java @@ -0,0 +1,30 @@ +/* + * 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.tomee.itests.optional.optlib; + +import jakarta.enterprise.context.ApplicationScoped; + +/** + * Some sample bean + */ +@ApplicationScoped +public class SomeBean { + + public String doSomething() { + return "nice"; + } +} diff --git a/itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/Vehicle.java b/itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/Vehicle.java new file mode 100644 index 00000000000..87c2388990d --- /dev/null +++ b/itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/java/org/apache/tomee/itests/optional/optlib/Vehicle.java @@ -0,0 +1,25 @@ +/* + * 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.tomee.itests.optional.optlib; + +/** + * @author Mark Struberg + */ +public interface Vehicle { + + boolean motorized(); +} diff --git a/itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/resources/META-INF/beans.xml b/itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/resources/META-INF/beans.xml new file mode 100644 index 00000000000..3ebaaf5c5b3 --- /dev/null +++ b/itests/openejb-itests-optional-classes/lib_with_optional_dep/src/main/resources/META-INF/beans.xml @@ -0,0 +1,25 @@ + + + + + diff --git a/itests/openejb-itests-optional-classes/pom.xml b/itests/openejb-itests-optional-classes/pom.xml new file mode 100644 index 00000000000..b8b65aa08f4 --- /dev/null +++ b/itests/openejb-itests-optional-classes/pom.xml @@ -0,0 +1,61 @@ + + + + + + + 4.0.0 + + + org.apache.tomee + itests + 10.0.0-M1-SNAPSHOT + + + org.apache.tomee.itests + openejb-itests-optional-classes + + pom + TomEE :: iTests :: Optional Classes + + + ${project.groupId}.itests.optional + + + + 3rdpartydep + lib_with_optional_dep + app + + + + + org.apache.tomee + jakartaee-api + ${version.jakartaee-api} + provided + + + junit + junit + runtime + + + + diff --git a/itests/pom.xml b/itests/pom.xml index 534a846c389..f2ee999eb44 100644 --- a/itests/pom.xml +++ b/itests/pom.xml @@ -51,6 +51,7 @@ itest-util tomee-security-itests tomee-microprofile-itests + openejb-itests-optional-classes From 059249a7a4c02c71b8cbac3b20a8397a90005adc Mon Sep 17 00:00:00 2001 From: Richard Zowalla Date: Fri, 19 Jan 2024 10:13:57 +0100 Subject: [PATCH 2/2] Fix missing license header --- .../optional/thirdparty/ThirdPartyApi.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/itests/openejb-itests-optional-classes/3rdpartydep/src/main/java/org/apache/tomee/itests/optional/thirdparty/ThirdPartyApi.java b/itests/openejb-itests-optional-classes/3rdpartydep/src/main/java/org/apache/tomee/itests/optional/thirdparty/ThirdPartyApi.java index 07626561b6e..935b6dc3fa2 100644 --- a/itests/openejb-itests-optional-classes/3rdpartydep/src/main/java/org/apache/tomee/itests/optional/thirdparty/ThirdPartyApi.java +++ b/itests/openejb-itests-optional-classes/3rdpartydep/src/main/java/org/apache/tomee/itests/optional/thirdparty/ThirdPartyApi.java @@ -1,3 +1,19 @@ +/* + * 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.tomee.itests.optional.thirdparty; /**