http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-alternative-and-stereotypes/README.md ---------------------------------------------------------------------- diff --git a/examples/cdi-alternative-and-stereotypes/README.md b/examples/cdi-alternative-and-stereotypes/README.md index c728514..f6aecb4 100644 --- a/examples/cdi-alternative-and-stereotypes/README.md +++ b/examples/cdi-alternative-and-stereotypes/README.md @@ -1,126 +1,126 @@ -# Introduction - -CDI is a revolution for Java EE world. This specification is the best one to avoid coupling between classes. - -This example simply aims to override bindings at runtime to simplify mocking work. - -It uses two kind of mocks: -1) a mock with no implementation in the classloader -2) a mock with an implementation in the classloader - -The mock answer from CDI is called *alternative*. - -Annotating `@Alternative` a class will mean it will replace any implementation if there is no other implementation -or if it is forced (through `META-INF/beans.xml`). - -# Code explanation -## main code - -We use an EJB `Jouney` to modelize a journey where the vehicle and the society can change. Here an EJB is used simply -because it simplifies the test. A jouney wraps the vehicle and society information. - -We define then two interfaces to inject it in the `Journey` EJB: `Vehicle` and `Society`. - -Finally we add an implementation for `Scociety` interface: `LowCostCompanie`. - -If we don't go further `Journey` object will not be able to be created because no `Vehicle` implementation is available. - -Note: if we suppose we have a `Vehicle` implementation, the injected Society should be `LowCostCompanie`. - -## test code - -The goal here is to test our `Journey` EJB. So we have to provide a `Vehicle` implementation: `SuperCar`. - -We want to force the `Society` implementation (for any reason) by our test implementation: `AirOpenEJB`. - -One solution could simply be to add `@Alternative` annotation on `AirOpenEJB` and activate it through -the `META-INF/beans.xml` file. - -Here we want to write more explicit code. So we want to replace the `@Alternative` annotation by `@Mock` one. - -So we simply define an `@Mock` annotation for classes, resolvable at runtime which is a stereotype (`@Stereotype`) -which replace `@Alternative`. - -Here is the annotation: - - @Stereotype // we define a stereotype - @Retention(RUNTIME) // resolvable at runtime - @Target(TYPE) // this annotation is a class level one - @Alternative // it replace @Alternative - public @interface Mock {} - -Note: you can add more CDI annotations after `@Alternative` and it will get the behavior expected (the scope for instance). - -So now we have our `@Mock` annotation which is a stereotype able to replace `@Alternative` annotation -we simply add this annotation to our mocks. - -If you run it now you'll have this exception: - - javax.enterprise.inject.UnsatisfiedResolutionException: Api type [org.superbiz.cdi.stereotype.Vehicle] is not found with the qualifiers - Qualifiers: [@javax.enterprise.inject.Default()] - for injection into Field Injection Point, field name : vehicle, Bean Owner : [Journey, Name:null, WebBeans Type:ENTERPRISE, API Types:[java.lang.Object,org.superbiz.cdi.stereotype.Journey], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]] - -It means the stereotype is not activated. To do it simply add it to your `META-INF/beans.xml`: - - <alternatives> - <stereotype>org.superbiz.cdi.stereotype.Mock</stereotype> - </alternatives> - -Note: if you don't specify `AirOpenEJB` as `@Alternative` (done through our mock annotation) you'll get this exception: - - Caused by: javax.enterprise.inject.AmbiguousResolutionException: There is more than one api type with : org.superbiz.cdi.stereotype.Society with qualifiers : Qualifiers: [@javax.enterprise.inject.Default()] - for injection into Field Injection Point, field name : society, Bean Owner : [Journey, Name:null, WebBeans Type:ENTERPRISE, API Types:[org.superbiz.cdi.stereotype.Journey,java.lang.Object], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]] - found beans: - AirOpenEJB, Name:null, WebBeans Type:MANAGED, API Types:[org.superbiz.cdi.stereotype.Society,org.superbiz.cdi.stereotype.AirOpenEJB,java.lang.Object], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default] - LowCostCompanie, Name:null, WebBeans Type:MANAGED, API Types:[org.superbiz.cdi.stereotype.Society,org.superbiz.cdi.stereotype.LowCostCompanie,java.lang.Object], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default] - -which simply means two implementations are available for the same injection point (`Journey.society`). - -# Conclusion - -With CDI it is really easy to define annotations with a strong meaning. You can define business annotations -or simply technical annotations to simplify your code (as we did with the mock annotation). - -Note: if for instance you used qualifiers to inject societies you could have put all these qualifiers on -the mock class or defined a `@SocietyMock` annotation to be able to inject the same implementation for -all qualifiers in your tests. - -# Output - - Running org.superbiz.cdi.stereotype.StereotypeTest - Apache OpenEJB 7.0.0-SNAPSHOT build: 20111030-07:54 - http://tomee.apache.org/ - INFO - openejb.home = /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes - INFO - openejb.base = /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes - INFO - Using 'javax.ejb.embeddable.EJBContainer=true' - INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) - INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) - INFO - Found EjbModule in classpath: /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes/target/test-classes - INFO - Found EjbModule in classpath: /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes/target/classes - INFO - Beginning load: /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes/target/test-classes - INFO - Beginning load: /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes/target/classes - INFO - Configuring enterprise application: /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes - INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) - INFO - Auto-creating a container for bean cdi-alternative-and-stereotypes_test.Comp: Container(type=MANAGED, id=Default Managed Container) - INFO - Configuring Service(id=Default Singleton Container, type=Container, provider-id=Default Singleton Container) - INFO - Auto-creating a container for bean Journey: Container(type=SINGLETON, id=Default Singleton Container) - INFO - Enterprise application "/opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes" loaded. - INFO - Assembling app: /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes - INFO - Jndi(name="java:global/cdi-alternative-and-stereotypes/cdi-alternative-and-stereotypes_test.Comp!org.apache.openejb.BeanContext$Comp") - INFO - Jndi(name="java:global/cdi-alternative-and-stereotypes/cdi-alternative-and-stereotypes_test.Comp") - INFO - Jndi(name="java:global/cdi-alternative-and-stereotypes/cdi-alternative-and-stereotypes.Comp!org.apache.openejb.BeanContext$Comp") - INFO - Jndi(name="java:global/cdi-alternative-and-stereotypes/cdi-alternative-and-stereotypes.Comp") - INFO - Jndi(name="java:global/cdi-alternative-and-stereotypes/Journey!org.superbiz.cdi.stereotype.Journey") - INFO - Jndi(name="java:global/cdi-alternative-and-stereotypes/Journey") - INFO - Jndi(name="java:global/EjbModule162291475/org.superbiz.cdi.stereotype.StereotypeTest!org.superbiz.cdi.stereotype.StereotypeTest") - INFO - Jndi(name="java:global/EjbModule162291475/org.superbiz.cdi.stereotype.StereotypeTest") - INFO - Created Ejb(deployment-id=cdi-alternative-and-stereotypes_test.Comp, ejb-name=cdi-alternative-and-stereotypes_test.Comp, container=Default Managed Container) - INFO - Created Ejb(deployment-id=cdi-alternative-and-stereotypes.Comp, ejb-name=cdi-alternative-and-stereotypes.Comp, container=Default Managed Container) - INFO - Created Ejb(deployment-id=org.superbiz.cdi.stereotype.StereotypeTest, ejb-name=org.superbiz.cdi.stereotype.StereotypeTest, container=Default Managed Container) - INFO - Created Ejb(deployment-id=Journey, ejb-name=Journey, container=Default Singleton Container) - INFO - Started Ejb(deployment-id=cdi-alternative-and-stereotypes_test.Comp, ejb-name=cdi-alternative-and-stereotypes_test.Comp, container=Default Managed Container) - INFO - Started Ejb(deployment-id=cdi-alternative-and-stereotypes.Comp, ejb-name=cdi-alternative-and-stereotypes.Comp, container=Default Managed Container) - INFO - Started Ejb(deployment-id=org.superbiz.cdi.stereotype.StereotypeTest, ejb-name=org.superbiz.cdi.stereotype.StereotypeTest, container=Default Managed Container) - INFO - Started Ejb(deployment-id=Journey, ejb-name=Journey, container=Default Singleton Container) - INFO - Deployed Application(path=/opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes) - INFO - Undeploying app: /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes +# Introduction + +CDI is a revolution for Java EE world. This specification is the best one to avoid coupling between classes. + +This example simply aims to override bindings at runtime to simplify mocking work. + +It uses two kind of mocks: +1) a mock with no implementation in the classloader +2) a mock with an implementation in the classloader + +The mock answer from CDI is called *alternative*. + +Annotating `@Alternative` a class will mean it will replace any implementation if there is no other implementation +or if it is forced (through `META-INF/beans.xml`). + +# Code explanation +## main code + +We use an EJB `Jouney` to modelize a journey where the vehicle and the society can change. Here an EJB is used simply +because it simplifies the test. A jouney wraps the vehicle and society information. + +We define then two interfaces to inject it in the `Journey` EJB: `Vehicle` and `Society`. + +Finally we add an implementation for `Scociety` interface: `LowCostCompanie`. + +If we don't go further `Journey` object will not be able to be created because no `Vehicle` implementation is available. + +Note: if we suppose we have a `Vehicle` implementation, the injected Society should be `LowCostCompanie`. + +## test code + +The goal here is to test our `Journey` EJB. So we have to provide a `Vehicle` implementation: `SuperCar`. + +We want to force the `Society` implementation (for any reason) by our test implementation: `AirOpenEJB`. + +One solution could simply be to add `@Alternative` annotation on `AirOpenEJB` and activate it through +the `META-INF/beans.xml` file. + +Here we want to write more explicit code. So we want to replace the `@Alternative` annotation by `@Mock` one. + +So we simply define an `@Mock` annotation for classes, resolvable at runtime which is a stereotype (`@Stereotype`) +which replace `@Alternative`. + +Here is the annotation: + + @Stereotype // we define a stereotype + @Retention(RUNTIME) // resolvable at runtime + @Target(TYPE) // this annotation is a class level one + @Alternative // it replace @Alternative + public @interface Mock {} + +Note: you can add more CDI annotations after `@Alternative` and it will get the behavior expected (the scope for instance). + +So now we have our `@Mock` annotation which is a stereotype able to replace `@Alternative` annotation +we simply add this annotation to our mocks. + +If you run it now you'll have this exception: + + javax.enterprise.inject.UnsatisfiedResolutionException: Api type [org.superbiz.cdi.stereotype.Vehicle] is not found with the qualifiers + Qualifiers: [@javax.enterprise.inject.Default()] + for injection into Field Injection Point, field name : vehicle, Bean Owner : [Journey, Name:null, WebBeans Type:ENTERPRISE, API Types:[java.lang.Object,org.superbiz.cdi.stereotype.Journey], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]] + +It means the stereotype is not activated. To do it simply add it to your `META-INF/beans.xml`: + + <alternatives> + <stereotype>org.superbiz.cdi.stereotype.Mock</stereotype> + </alternatives> + +Note: if you don't specify `AirOpenEJB` as `@Alternative` (done through our mock annotation) you'll get this exception: + + Caused by: javax.enterprise.inject.AmbiguousResolutionException: There is more than one api type with : org.superbiz.cdi.stereotype.Society with qualifiers : Qualifiers: [@javax.enterprise.inject.Default()] + for injection into Field Injection Point, field name : society, Bean Owner : [Journey, Name:null, WebBeans Type:ENTERPRISE, API Types:[org.superbiz.cdi.stereotype.Journey,java.lang.Object], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]] + found beans: + AirOpenEJB, Name:null, WebBeans Type:MANAGED, API Types:[org.superbiz.cdi.stereotype.Society,org.superbiz.cdi.stereotype.AirOpenEJB,java.lang.Object], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default] + LowCostCompanie, Name:null, WebBeans Type:MANAGED, API Types:[org.superbiz.cdi.stereotype.Society,org.superbiz.cdi.stereotype.LowCostCompanie,java.lang.Object], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default] + +which simply means two implementations are available for the same injection point (`Journey.society`). + +# Conclusion + +With CDI it is really easy to define annotations with a strong meaning. You can define business annotations +or simply technical annotations to simplify your code (as we did with the mock annotation). + +Note: if for instance you used qualifiers to inject societies you could have put all these qualifiers on +the mock class or defined a `@SocietyMock` annotation to be able to inject the same implementation for +all qualifiers in your tests. + +# Output + + Running org.superbiz.cdi.stereotype.StereotypeTest + Apache OpenEJB 7.0.0-SNAPSHOT build: 20111030-07:54 + http://tomee.apache.org/ + INFO - openejb.home = /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes + INFO - openejb.base = /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes + INFO - Using 'javax.ejb.embeddable.EJBContainer=true' + INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) + INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) + INFO - Found EjbModule in classpath: /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes/target/test-classes + INFO - Found EjbModule in classpath: /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes/target/classes + INFO - Beginning load: /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes/target/test-classes + INFO - Beginning load: /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes/target/classes + INFO - Configuring enterprise application: /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes + INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) + INFO - Auto-creating a container for bean cdi-alternative-and-stereotypes_test.Comp: Container(type=MANAGED, id=Default Managed Container) + INFO - Configuring Service(id=Default Singleton Container, type=Container, provider-id=Default Singleton Container) + INFO - Auto-creating a container for bean Journey: Container(type=SINGLETON, id=Default Singleton Container) + INFO - Enterprise application "/opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes" loaded. + INFO - Assembling app: /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes + INFO - Jndi(name="java:global/cdi-alternative-and-stereotypes/cdi-alternative-and-stereotypes_test.Comp!org.apache.openejb.BeanContext$Comp") + INFO - Jndi(name="java:global/cdi-alternative-and-stereotypes/cdi-alternative-and-stereotypes_test.Comp") + INFO - Jndi(name="java:global/cdi-alternative-and-stereotypes/cdi-alternative-and-stereotypes.Comp!org.apache.openejb.BeanContext$Comp") + INFO - Jndi(name="java:global/cdi-alternative-and-stereotypes/cdi-alternative-and-stereotypes.Comp") + INFO - Jndi(name="java:global/cdi-alternative-and-stereotypes/Journey!org.superbiz.cdi.stereotype.Journey") + INFO - Jndi(name="java:global/cdi-alternative-and-stereotypes/Journey") + INFO - Jndi(name="java:global/EjbModule162291475/org.superbiz.cdi.stereotype.StereotypeTest!org.superbiz.cdi.stereotype.StereotypeTest") + INFO - Jndi(name="java:global/EjbModule162291475/org.superbiz.cdi.stereotype.StereotypeTest") + INFO - Created Ejb(deployment-id=cdi-alternative-and-stereotypes_test.Comp, ejb-name=cdi-alternative-and-stereotypes_test.Comp, container=Default Managed Container) + INFO - Created Ejb(deployment-id=cdi-alternative-and-stereotypes.Comp, ejb-name=cdi-alternative-and-stereotypes.Comp, container=Default Managed Container) + INFO - Created Ejb(deployment-id=org.superbiz.cdi.stereotype.StereotypeTest, ejb-name=org.superbiz.cdi.stereotype.StereotypeTest, container=Default Managed Container) + INFO - Created Ejb(deployment-id=Journey, ejb-name=Journey, container=Default Singleton Container) + INFO - Started Ejb(deployment-id=cdi-alternative-and-stereotypes_test.Comp, ejb-name=cdi-alternative-and-stereotypes_test.Comp, container=Default Managed Container) + INFO - Started Ejb(deployment-id=cdi-alternative-and-stereotypes.Comp, ejb-name=cdi-alternative-and-stereotypes.Comp, container=Default Managed Container) + INFO - Started Ejb(deployment-id=org.superbiz.cdi.stereotype.StereotypeTest, ejb-name=org.superbiz.cdi.stereotype.StereotypeTest, container=Default Managed Container) + INFO - Started Ejb(deployment-id=Journey, ejb-name=Journey, container=Default Singleton Container) + INFO - Deployed Application(path=/opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes) + INFO - Undeploying app: /opt/dev/openejb/openejb-trunk/examples/cdi-alternative-and-stereotypes
http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Journey.java ---------------------------------------------------------------------- diff --git a/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Journey.java b/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Journey.java index d5f3645..e987985 100644 --- a/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Journey.java +++ b/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Journey.java @@ -1,38 +1,38 @@ -/** - * 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.stereotype; - -import javax.ejb.Singleton; -import javax.inject.Inject; - -@Singleton -public class Journey { - - @Inject - private Vehicle vehicle; - @Inject - private Society society; - - public String vehicle() { - return vehicle.name(); - } - - public String category() { - return society.category(); - } -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.stereotype; + +import javax.ejb.Singleton; +import javax.inject.Inject; + +@Singleton +public class Journey { + + @Inject + private Vehicle vehicle; + @Inject + private Society society; + + public String vehicle() { + return vehicle.name(); + } + + public String category() { + return society.category(); + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/LowCostCompanie.java ---------------------------------------------------------------------- diff --git a/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/LowCostCompanie.java b/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/LowCostCompanie.java index f3d634e..ab818be 100644 --- a/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/LowCostCompanie.java +++ b/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/LowCostCompanie.java @@ -1,26 +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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.stereotype; - -public class LowCostCompanie implements Society { - - @Override - public String category() { - return "maybe you'll leave..."; - } -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.stereotype; + +public class LowCostCompanie implements Society { + + @Override + public String category() { + return "maybe you'll leave..."; + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Society.java ---------------------------------------------------------------------- diff --git a/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Society.java b/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Society.java index d6e7007..441c788 100644 --- a/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Society.java +++ b/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Society.java @@ -1,23 +1,23 @@ -/** - * 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.stereotype; - -public interface Society { - - String category(); -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.stereotype; + +public interface Society { + + String category(); +} http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Vehicle.java ---------------------------------------------------------------------- diff --git a/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Vehicle.java b/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Vehicle.java index ce1a39c..83c3764 100644 --- a/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Vehicle.java +++ b/examples/cdi-alternative-and-stereotypes/src/main/java/org/superbiz/cdi/stereotype/Vehicle.java @@ -1,23 +1,23 @@ -/** - * 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.stereotype; - -public interface Vehicle { - - String name(); -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.stereotype; + +public interface Vehicle { + + String name(); +} http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/AirOpenEJB.java ---------------------------------------------------------------------- diff --git a/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/AirOpenEJB.java b/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/AirOpenEJB.java index e289ddb..3a85074 100644 --- a/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/AirOpenEJB.java +++ b/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/AirOpenEJB.java @@ -1,39 +1,39 @@ -/** - * 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.stereotype; - -/** - * without @Mock annotation which specifies this class as an alternative - * you'll have this exception: - * <p/> - * Caused by: javax.enterprise.inject.AmbiguousResolutionException: There is more than one api type with : org.superbiz.cdi.stereotype.Society with qualifiers : Qualifiers: [@javax.enterprise.inject.Default()] - * for injection into Field Injection Point, field name : society, Bean Owner : [Journey, Name:null, WebBeans Type:ENTERPRISE, API Types:[org.superbiz.cdi.stereotype.Journey,java.lang.Object], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]] - * found beans: - * AirOpenEJB, Name:null, WebBeans Type:MANAGED, API Types:[org.superbiz.cdi.stereotype.Society,org.superbiz.cdi.stereotype.AirOpenEJB,java.lang.Object], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default] - * LowCostCompanie, Name:null, WebBeans Type:MANAGED, API Types:[org.superbiz.cdi.stereotype.Society,org.superbiz.cdi.stereotype.LowCostCompanie,java.lang.Object], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default] - * <p/> - * because 2 implementations match the same injection point (Journey.society). - */ -@Mock -public class AirOpenEJB implements Society { - - @Override - public String category() { - return "simply the best"; - } -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.stereotype; + +/** + * without @Mock annotation which specifies this class as an alternative + * you'll have this exception: + * <p/> + * Caused by: javax.enterprise.inject.AmbiguousResolutionException: There is more than one api type with : org.superbiz.cdi.stereotype.Society with qualifiers : Qualifiers: [@javax.enterprise.inject.Default()] + * for injection into Field Injection Point, field name : society, Bean Owner : [Journey, Name:null, WebBeans Type:ENTERPRISE, API Types:[org.superbiz.cdi.stereotype.Journey,java.lang.Object], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]] + * found beans: + * AirOpenEJB, Name:null, WebBeans Type:MANAGED, API Types:[org.superbiz.cdi.stereotype.Society,org.superbiz.cdi.stereotype.AirOpenEJB,java.lang.Object], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default] + * LowCostCompanie, Name:null, WebBeans Type:MANAGED, API Types:[org.superbiz.cdi.stereotype.Society,org.superbiz.cdi.stereotype.LowCostCompanie,java.lang.Object], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default] + * <p/> + * because 2 implementations match the same injection point (Journey.society). + */ +@Mock +public class AirOpenEJB implements Society { + + @Override + public String category() { + return "simply the best"; + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/Mock.java ---------------------------------------------------------------------- diff --git a/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/Mock.java b/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/Mock.java index 7b116f9..65c5581 100644 --- a/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/Mock.java +++ b/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/Mock.java @@ -1,38 +1,38 @@ -/** - * 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.stereotype; - -import javax.enterprise.inject.Alternative; -import javax.enterprise.inject.Stereotype; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import static java.lang.annotation.ElementType.TYPE; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -// defining a stereotype for class level -@Stereotype -@Retention(RUNTIME) -@Target(TYPE) - -// here define all annotations you want to replace by this one. -// this stereotype define an alternative -@Alternative -public @interface Mock { - -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.stereotype; + +import javax.enterprise.inject.Alternative; +import javax.enterprise.inject.Stereotype; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +// defining a stereotype for class level +@Stereotype +@Retention(RUNTIME) +@Target(TYPE) + +// here define all annotations you want to replace by this one. +// this stereotype define an alternative +@Alternative +public @interface Mock { + +} http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/StereotypeTest.java ---------------------------------------------------------------------- diff --git a/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/StereotypeTest.java b/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/StereotypeTest.java index eef8799..8802232 100644 --- a/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/StereotypeTest.java +++ b/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/StereotypeTest.java @@ -1,56 +1,56 @@ -/** - * 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.stereotype; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - -import javax.ejb.embeddable.EJBContainer; -import javax.naming.NamingException; - -import static org.junit.Assert.assertEquals; - -public class StereotypeTest { - - private static EJBContainer container; - private static Journey journey; - - @BeforeClass - public static void start() throws NamingException { - container = EJBContainer.createEJBContainer(); - journey = (Journey) container.getContext().lookup("java:global/cdi-alternative-and-stereotypes/Journey"); - } - - @AfterClass - public static void shutdown() { - if (container != null) { - container.close(); - } - } - - @Test - public void assertVehicleInjected() { - assertEquals("the fastest", journey.vehicle()); - } - - @Test - public void assertMockOverrideWorks() { - assertEquals("simply the best", journey.category()); - } -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.stereotype; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import javax.ejb.embeddable.EJBContainer; +import javax.naming.NamingException; + +import static org.junit.Assert.assertEquals; + +public class StereotypeTest { + + private static EJBContainer container; + private static Journey journey; + + @BeforeClass + public static void start() throws NamingException { + container = EJBContainer.createEJBContainer(); + journey = (Journey) container.getContext().lookup("java:global/cdi-alternative-and-stereotypes/Journey"); + } + + @AfterClass + public static void shutdown() { + if (container != null) { + container.close(); + } + } + + @Test + public void assertVehicleInjected() { + assertEquals("the fastest", journey.vehicle()); + } + + @Test + public void assertMockOverrideWorks() { + assertEquals("simply the best", journey.category()); + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/SuperCar.java ---------------------------------------------------------------------- diff --git a/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/SuperCar.java b/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/SuperCar.java index 2d14191..e88753d 100644 --- a/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/SuperCar.java +++ b/examples/cdi-alternative-and-stereotypes/src/test/java/org/superbiz/cdi/stereotype/SuperCar.java @@ -1,27 +1,27 @@ -/** - * 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.stereotype; - -@Mock -public class SuperCar implements Vehicle { - - @Override - public String name() { - return "the fastest"; - } -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.stereotype; + +@Mock +public class SuperCar implements Vehicle { + + @Override + public String name() { + return "the fastest"; + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-application-scope/README.md ---------------------------------------------------------------------- diff --git a/examples/cdi-application-scope/README.md b/examples/cdi-application-scope/README.md index 736be8a..245ceaa 100644 --- a/examples/cdi-application-scope/README.md +++ b/examples/cdi-application-scope/README.md @@ -1,133 +1,133 @@ -Title: CDI @ApplicationScoped - -This example show the use of `@ApplicationScoped` annotation for injected objects. An object -which is defined as `@ApplicationScoped` is created once for the duration of the application. - -# Example - -This example depicts a similar scenario to cdi-request-scope. A restaurant guest orders -a soup from the waiter. The waiter then delivers the soup back to the guest. Another -guest can order the same soup that was ordered by the previous client - this is where -the application scope is used. - -## Waiter - -The `Waiter` session bean receives a request from the test class via the `orderSoup()` method -and sets the name for the `soup` field. The `orderWhatTheOtherGuyHad()` method returns -the name of the `soup` field. - - @Stateless - public class Waiter { - - @Inject - public Soup soup; - - public String orderSoup(String name){ - soup.setName(name); - return soup.getName(); - } - - public String orderWhatTheOtherGuyHad() { - String name = soup.getName(); - return name; - } - } - -## Soup - -The `Soup` class is an injectable POJO, defined as `@ApplicationScoped`. This means that an instance -will be created only once for the duration of the whole application. Try changing the `@ApplicationScoped` -annotation to `@RequestScoped` and see what happens. - - @ApplicationScoped - public class Soup { - - private String name = "Soup of the day"; - - @PostConstruct - public void afterCreate() { - System.out.println("Soup created"); - } - - public String getName() { - return name; - } - - public void setName(String name){ - this.name = name; - } - } - - -# Test Case - -This is the entry class for this example. First a soup is ordered via `orderSoup()` method. -This initiates the `soup` field. Next, `orderWhatTheOtherGuyHad()` method returns the soup -from the application context. - - public class RestaurantTest { - - private static String TOMATO_SOUP = "Tomato Soup"; - private EJBContainer container; - - @EJB - private Waiter joe; - - @Before - public void startContainer() throws Exception { - container = EJBContainer.createEJBContainer(); - container.getContext().bind("inject", this); - } - - @Test - public void orderSoup(){ - String someSoup = joe.orderSoup(TOMATO_SOUP); - assertEquals(TOMATO_SOUP, someSoup); - - String sameSoup = joe.orderWhatTheOtherGuyHad(); - assertEquals(TOMATO_SOUP, sameSoup); - } - - @After - public void closeContainer() throws Exception { - container.close(); - } - } - -# Running - -In the output you can see that there is just one `Soup` instance created - one for -the whole application. - - ------------------------------------------------------- - T E S T S - ------------------------------------------------------- - Running org.superbiz.cdi.applicationscope.RestaurantTest - Apache OpenEJB 7.0.0-SNAPSHOT build: 20111224-11:09 - http://tomee.apache.org/ - INFO - openejb.home = C:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope - INFO - openejb.base = C:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope - INFO - Using 'javax.ejb.embeddable.EJBContainer=true' - INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) - INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) - INFO - Found EjbModule in classpath: c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope\target\classes - INFO - Beginning load: c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope\target\classes - INFO - Configuring enterprise application: c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope - INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) - INFO - Auto-creating a container for bean cdi-application-scope.Comp: Container(type=MANAGED, id=Default Managed Container) - INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) - INFO - Auto-creating a container for bean Waiter: Container(type=STATELESS, id=Default Stateless Container) - INFO - Enterprise application "c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope" loaded. - INFO - Assembling app: c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope - INFO - Jndi(name="java:global/cdi-application-scope/Waiter!org.superbiz.cdi.applicationscope.Waiter") - INFO - Jndi(name="java:global/cdi-application-scope/Waiter") - INFO - Created Ejb(deployment-id=Waiter, ejb-name=Waiter, container=Default Stateless Container) - INFO - Started Ejb(deployment-id=Waiter, ejb-name=Waiter, container=Default Stateless Container) - INFO - Deployed Application(path=c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope) - Soup created - INFO - Undeploying app: c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope - Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.42 sec - - Results : - - Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 +Title: CDI @ApplicationScoped + +This example show the use of `@ApplicationScoped` annotation for injected objects. An object +which is defined as `@ApplicationScoped` is created once for the duration of the application. + +# Example + +This example depicts a similar scenario to cdi-request-scope. A restaurant guest orders +a soup from the waiter. The waiter then delivers the soup back to the guest. Another +guest can order the same soup that was ordered by the previous client - this is where +the application scope is used. + +## Waiter + +The `Waiter` session bean receives a request from the test class via the `orderSoup()` method +and sets the name for the `soup` field. The `orderWhatTheOtherGuyHad()` method returns +the name of the `soup` field. + + @Stateless + public class Waiter { + + @Inject + public Soup soup; + + public String orderSoup(String name){ + soup.setName(name); + return soup.getName(); + } + + public String orderWhatTheOtherGuyHad() { + String name = soup.getName(); + return name; + } + } + +## Soup + +The `Soup` class is an injectable POJO, defined as `@ApplicationScoped`. This means that an instance +will be created only once for the duration of the whole application. Try changing the `@ApplicationScoped` +annotation to `@RequestScoped` and see what happens. + + @ApplicationScoped + public class Soup { + + private String name = "Soup of the day"; + + @PostConstruct + public void afterCreate() { + System.out.println("Soup created"); + } + + public String getName() { + return name; + } + + public void setName(String name){ + this.name = name; + } + } + + +# Test Case + +This is the entry class for this example. First a soup is ordered via `orderSoup()` method. +This initiates the `soup` field. Next, `orderWhatTheOtherGuyHad()` method returns the soup +from the application context. + + public class RestaurantTest { + + private static String TOMATO_SOUP = "Tomato Soup"; + private EJBContainer container; + + @EJB + private Waiter joe; + + @Before + public void startContainer() throws Exception { + container = EJBContainer.createEJBContainer(); + container.getContext().bind("inject", this); + } + + @Test + public void orderSoup(){ + String someSoup = joe.orderSoup(TOMATO_SOUP); + assertEquals(TOMATO_SOUP, someSoup); + + String sameSoup = joe.orderWhatTheOtherGuyHad(); + assertEquals(TOMATO_SOUP, sameSoup); + } + + @After + public void closeContainer() throws Exception { + container.close(); + } + } + +# Running + +In the output you can see that there is just one `Soup` instance created - one for +the whole application. + + ------------------------------------------------------- + T E S T S + ------------------------------------------------------- + Running org.superbiz.cdi.applicationscope.RestaurantTest + Apache OpenEJB 7.0.0-SNAPSHOT build: 20111224-11:09 + http://tomee.apache.org/ + INFO - openejb.home = C:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope + INFO - openejb.base = C:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope + INFO - Using 'javax.ejb.embeddable.EJBContainer=true' + INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) + INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) + INFO - Found EjbModule in classpath: c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope\target\classes + INFO - Beginning load: c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope\target\classes + INFO - Configuring enterprise application: c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope + INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) + INFO - Auto-creating a container for bean cdi-application-scope.Comp: Container(type=MANAGED, id=Default Managed Container) + INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) + INFO - Auto-creating a container for bean Waiter: Container(type=STATELESS, id=Default Stateless Container) + INFO - Enterprise application "c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope" loaded. + INFO - Assembling app: c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope + INFO - Jndi(name="java:global/cdi-application-scope/Waiter!org.superbiz.cdi.applicationscope.Waiter") + INFO - Jndi(name="java:global/cdi-application-scope/Waiter") + INFO - Created Ejb(deployment-id=Waiter, ejb-name=Waiter, container=Default Stateless Container) + INFO - Started Ejb(deployment-id=Waiter, ejb-name=Waiter, container=Default Stateless Container) + INFO - Deployed Application(path=c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope) + Soup created + INFO - Undeploying app: c:\Users\Daniel\workspaces\openejb\openejb\examples\cdi-application-scope + Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.42 sec + + Results : + + Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-application-scope/src/main/java/org/superbiz/cdi/applicationscope/Soup.java ---------------------------------------------------------------------- diff --git a/examples/cdi-application-scope/src/main/java/org/superbiz/cdi/applicationscope/Soup.java b/examples/cdi-application-scope/src/main/java/org/superbiz/cdi/applicationscope/Soup.java index 9fa87a7..122e9fc 100644 --- a/examples/cdi-application-scope/src/main/java/org/superbiz/cdi/applicationscope/Soup.java +++ b/examples/cdi-application-scope/src/main/java/org/superbiz/cdi/applicationscope/Soup.java @@ -1,39 +1,39 @@ -/** - * 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.applicationscope; - -import javax.annotation.PostConstruct; -import javax.enterprise.context.ApplicationScoped; - -@ApplicationScoped -public class Soup { - - private String name = "Soup of the day"; - - @PostConstruct - public void afterCreate() { - System.out.println("Soup created"); - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.applicationscope; + +import javax.annotation.PostConstruct; +import javax.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class Soup { + + private String name = "Soup of the day"; + + @PostConstruct + public void afterCreate() { + System.out.println("Soup created"); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-application-scope/src/main/java/org/superbiz/cdi/applicationscope/Waiter.java ---------------------------------------------------------------------- diff --git a/examples/cdi-application-scope/src/main/java/org/superbiz/cdi/applicationscope/Waiter.java b/examples/cdi-application-scope/src/main/java/org/superbiz/cdi/applicationscope/Waiter.java index 55d9df2..effe242 100644 --- a/examples/cdi-application-scope/src/main/java/org/superbiz/cdi/applicationscope/Waiter.java +++ b/examples/cdi-application-scope/src/main/java/org/superbiz/cdi/applicationscope/Waiter.java @@ -1,38 +1,38 @@ -/** - * 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.applicationscope; - -import javax.ejb.Stateless; -import javax.inject.Inject; - -@Stateless -public class Waiter { - - @Inject - public Soup soup; - - public String orderSoup(String name) { - soup.setName(name); - return soup.getName(); - } - - public String orderWhatTheOtherGuyHad() { - String name = soup.getName(); - return name; - } - -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.applicationscope; + +import javax.ejb.Stateless; +import javax.inject.Inject; + +@Stateless +public class Waiter { + + @Inject + public Soup soup; + + public String orderSoup(String name) { + soup.setName(name); + return soup.getName(); + } + + public String orderWhatTheOtherGuyHad() { + String name = soup.getName(); + return name; + } + +} http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-application-scope/src/test/java/org/superbiz/cdi/applicationscope/RestaurantTest.java ---------------------------------------------------------------------- diff --git a/examples/cdi-application-scope/src/test/java/org/superbiz/cdi/applicationscope/RestaurantTest.java b/examples/cdi-application-scope/src/test/java/org/superbiz/cdi/applicationscope/RestaurantTest.java index 2a2bdcc..abd863a 100644 --- a/examples/cdi-application-scope/src/test/java/org/superbiz/cdi/applicationscope/RestaurantTest.java +++ b/examples/cdi-application-scope/src/test/java/org/superbiz/cdi/applicationscope/RestaurantTest.java @@ -1,55 +1,55 @@ -/** - * 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.applicationscope; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import javax.ejb.EJB; -import javax.ejb.embeddable.EJBContainer; - -import static org.junit.Assert.assertEquals; - -public class RestaurantTest { - - private static String TOMATO_SOUP = "Tomato Soup"; - private EJBContainer container; - - @EJB - private Waiter joe; - - @Before - public void startContainer() throws Exception { - container = EJBContainer.createEJBContainer(); - container.getContext().bind("inject", this); - } - - @Test - public void orderSoup() { - String someSoup = joe.orderSoup(TOMATO_SOUP); - assertEquals(TOMATO_SOUP, someSoup); - - String sameSoup = joe.orderWhatTheOtherGuyHad(); - assertEquals(TOMATO_SOUP, sameSoup); - } - - @After - public void closeContainer() throws Exception { - container.close(); - } -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.applicationscope; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import javax.ejb.EJB; +import javax.ejb.embeddable.EJBContainer; + +import static org.junit.Assert.assertEquals; + +public class RestaurantTest { + + private static String TOMATO_SOUP = "Tomato Soup"; + private EJBContainer container; + + @EJB + private Waiter joe; + + @Before + public void startContainer() throws Exception { + container = EJBContainer.createEJBContainer(); + container.getContext().bind("inject", this); + } + + @Test + public void orderSoup() { + String someSoup = joe.orderSoup(TOMATO_SOUP); + assertEquals(TOMATO_SOUP, someSoup); + + String sameSoup = joe.orderWhatTheOtherGuyHad(); + assertEquals(TOMATO_SOUP, sameSoup); + } + + @After + public void closeContainer() throws Exception { + container.close(); + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-basic/src/main/java/org/superbiz/cdi/basic/Course.java ---------------------------------------------------------------------- diff --git a/examples/cdi-basic/src/main/java/org/superbiz/cdi/basic/Course.java b/examples/cdi-basic/src/main/java/org/superbiz/cdi/basic/Course.java index dc3113a..c2e527f 100644 --- a/examples/cdi-basic/src/main/java/org/superbiz/cdi/basic/Course.java +++ b/examples/cdi-basic/src/main/java/org/superbiz/cdi/basic/Course.java @@ -1,54 +1,54 @@ -/** - * 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.basic; - -import javax.annotation.PostConstruct; -import javax.ejb.Stateless; -import javax.inject.Inject; - -@Stateless -public class Course { - - @Inject - private Faculty faculty; - - private String courseName; - - private int capacity; - - @PostConstruct - private void init() { - assert faculty != null; - - // These strings can be externalized - // We'll see how to do that later - this.courseName = "CDI 101 - Introduction to CDI"; - this.capacity = 100; - } - - public String getCourseName() { - return courseName; - } - - public int getCapacity() { - return capacity; - } - - public Faculty getFaculty() { - return faculty; - } -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.basic; + +import javax.annotation.PostConstruct; +import javax.ejb.Stateless; +import javax.inject.Inject; + +@Stateless +public class Course { + + @Inject + private Faculty faculty; + + private String courseName; + + private int capacity; + + @PostConstruct + private void init() { + assert faculty != null; + + // These strings can be externalized + // We'll see how to do that later + this.courseName = "CDI 101 - Introduction to CDI"; + this.capacity = 100; + } + + public String getCourseName() { + return courseName; + } + + public int getCapacity() { + return capacity; + } + + public Faculty getFaculty() { + return faculty; + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-basic/src/main/java/org/superbiz/cdi/basic/Faculty.java ---------------------------------------------------------------------- diff --git a/examples/cdi-basic/src/main/java/org/superbiz/cdi/basic/Faculty.java b/examples/cdi-basic/src/main/java/org/superbiz/cdi/basic/Faculty.java index e0adf28..6b6cbec 100644 --- a/examples/cdi-basic/src/main/java/org/superbiz/cdi/basic/Faculty.java +++ b/examples/cdi-basic/src/main/java/org/superbiz/cdi/basic/Faculty.java @@ -1,45 +1,45 @@ -/** - * 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.basic; - -import javax.annotation.PostConstruct; -import java.util.ArrayList; -import java.util.List; - -public class Faculty { - - private List<String> facultyMembers; - - private String facultyName; - - @PostConstruct - public void initialize() { - this.facultyMembers = new ArrayList<String>(); - facultyMembers.add("Ian Schultz"); - facultyMembers.add("Diane Reyes"); - facultyName = "Computer Science"; - } - - public List<String> getFacultyMembers() { - return facultyMembers; - } - - public String getFacultyName() { - return facultyName; - } - -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.basic; + +import javax.annotation.PostConstruct; +import java.util.ArrayList; +import java.util.List; + +public class Faculty { + + private List<String> facultyMembers; + + private String facultyName; + + @PostConstruct + public void initialize() { + this.facultyMembers = new ArrayList<String>(); + facultyMembers.add("Ian Schultz"); + facultyMembers.add("Diane Reyes"); + facultyName = "Computer Science"; + } + + public List<String> getFacultyMembers() { + return facultyMembers; + } + + public String getFacultyName() { + return facultyName; + } + +} http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-basic/src/test/java/org/superbiz/cdi/basic/CourseTest.java ---------------------------------------------------------------------- diff --git a/examples/cdi-basic/src/test/java/org/superbiz/cdi/basic/CourseTest.java b/examples/cdi-basic/src/test/java/org/superbiz/cdi/basic/CourseTest.java index cb042f4..d6a68c8 100644 --- a/examples/cdi-basic/src/test/java/org/superbiz/cdi/basic/CourseTest.java +++ b/examples/cdi-basic/src/test/java/org/superbiz/cdi/basic/CourseTest.java @@ -1,71 +1,71 @@ -/** - * 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.basic; - -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import javax.ejb.EJB; -import javax.ejb.embeddable.EJBContainer; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -public class CourseTest { - - private static EJBContainer container; - - @EJB - private Course course; - - @BeforeClass - public static void start() { - container = EJBContainer.createEJBContainer(); - } - - @Before - public void setUp() throws Exception { - container.getContext().bind("inject", this); - } - - @Test - public void test() { - - // Was the EJB injected? - assertTrue(course != null); - - // Was the Course @PostConstruct called? - assertNotNull(course.getCourseName()); - assertTrue(course.getCapacity() > 0); - - // Was a Faculty instance injected into Course? - final Faculty faculty = course.getFaculty(); - assertTrue(faculty != null); - - // Was the @PostConstruct called on Faculty? - assertEquals(faculty.getFacultyName(), "Computer Science"); - assertEquals(faculty.getFacultyMembers().size(), 2); - } - - @AfterClass - public static void stop() { - container.close(); - } -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.basic; + +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import javax.ejb.EJB; +import javax.ejb.embeddable.EJBContainer; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class CourseTest { + + private static EJBContainer container; + + @EJB + private Course course; + + @BeforeClass + public static void start() { + container = EJBContainer.createEJBContainer(); + } + + @Before + public void setUp() throws Exception { + container.getContext().bind("inject", this); + } + + @Test + public void test() { + + // Was the EJB injected? + assertTrue(course != null); + + // Was the Course @PostConstruct called? + assertNotNull(course.getCourseName()); + assertTrue(course.getCapacity() > 0); + + // Was a Faculty instance injected into Course? + final Faculty faculty = course.getFaculty(); + assertTrue(faculty != null); + + // Was the @PostConstruct called on Faculty? + assertEquals(faculty.getFacultyName(), "Computer Science"); + assertEquals(faculty.getFacultyMembers().size(), 2); + } + + @AfterClass + public static void stop() { + container.close(); + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-ejbcontext-jaas/src/main/java/org/superbiz/cdi/ejbcontext/LogginServlet.java ---------------------------------------------------------------------- diff --git a/examples/cdi-ejbcontext-jaas/src/main/java/org/superbiz/cdi/ejbcontext/LogginServlet.java b/examples/cdi-ejbcontext-jaas/src/main/java/org/superbiz/cdi/ejbcontext/LogginServlet.java index a1aa109..f4e2c52 100644 --- a/examples/cdi-ejbcontext-jaas/src/main/java/org/superbiz/cdi/ejbcontext/LogginServlet.java +++ b/examples/cdi-ejbcontext-jaas/src/main/java/org/superbiz/cdi/ejbcontext/LogginServlet.java @@ -1,39 +1,39 @@ -/** - * 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.ejbcontext; - -import javax.inject.Inject; -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - -@WebServlet(urlPatterns = "/ejbcontext") -public class LogginServlet extends HttpServlet { - - @Inject - private PrinciaplEjb bean; - - @Override - protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - req.login(req.getParameter("myUser"), req.getParameter("myPass")); - // think to persist the information in the session if you need it later - resp.getWriter().write("logged user ==> " + bean.info() + "; isUserInRole(admin)? " + req.isUserInRole("admin")); - } -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.ejbcontext; + +import javax.inject.Inject; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet(urlPatterns = "/ejbcontext") +public class LogginServlet extends HttpServlet { + + @Inject + private PrinciaplEjb bean; + + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.login(req.getParameter("myUser"), req.getParameter("myPass")); + // think to persist the information in the session if you need it later + resp.getWriter().write("logged user ==> " + bean.info() + "; isUserInRole(admin)? " + req.isUserInRole("admin")); + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/6e2a4f7c/examples/cdi-ejbcontext-jaas/src/main/java/org/superbiz/cdi/ejbcontext/PrinciaplEjb.java ---------------------------------------------------------------------- diff --git a/examples/cdi-ejbcontext-jaas/src/main/java/org/superbiz/cdi/ejbcontext/PrinciaplEjb.java b/examples/cdi-ejbcontext-jaas/src/main/java/org/superbiz/cdi/ejbcontext/PrinciaplEjb.java index 6dd11f5..d3b9075 100644 --- a/examples/cdi-ejbcontext-jaas/src/main/java/org/superbiz/cdi/ejbcontext/PrinciaplEjb.java +++ b/examples/cdi-ejbcontext-jaas/src/main/java/org/superbiz/cdi/ejbcontext/PrinciaplEjb.java @@ -1,32 +1,32 @@ -/** - * 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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.superbiz.cdi.ejbcontext; - -import javax.annotation.Resource; -import javax.ejb.EJBContext; -import javax.ejb.Stateless; - -@Stateless -public class PrinciaplEjb { - - @Resource - private EJBContext context; - - public String info() { - return context.getCallerPrincipal().getName(); - } -} +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.superbiz.cdi.ejbcontext; + +import javax.annotation.Resource; +import javax.ejb.EJBContext; +import javax.ejb.Stateless; + +@Stateless +public class PrinciaplEjb { + + @Resource + private EJBContext context; + + public String info() { + return context.getCallerPrincipal().getName(); + } +}