001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.assembler.classic;
017:
018: import static org.apache.openejb.assembler.classic.MethodTransactionBuilder.normalize;
019: import junit.framework.TestCase;
020: import org.apache.openejb.config.ConfigurationFactory;
021: import org.apache.openejb.jee.EjbJar;
022: import org.apache.openejb.jee.StatelessBean;
023: import org.apache.openejb.jee.ContainerTransaction;
024: import org.apache.openejb.jee.TransAttribute;
025: import org.apache.openejb.core.CoreDeploymentInfo;
026: import static org.apache.openejb.assembler.classic.MethodInfoUtil.*;
027: import org.apache.openejb.spi.ContainerSystem;
028: import org.apache.openejb.loader.SystemInstance;
029:
030: import javax.ejb.Local;
031: import javax.ejb.Remote;
032: import javax.ejb.TransactionAttribute;
033: import static javax.ejb.TransactionAttributeType.*;
034: import java.lang.reflect.Method;
035: import java.util.Map;
036: import java.util.List;
037:
038: /**
039: * @version $Rev$ $Date$
040: */
041: public class TransactionAttributesTest extends TestCase {
042: private Map<Method, MethodAttributeInfo> attributes;
043:
044: public void test() throws Exception {
045: Assembler assembler = new Assembler();
046: ConfigurationFactory config = new ConfigurationFactory();
047:
048: assembler.createProxyFactory(config
049: .configureService(ProxyFactoryInfo.class));
050: assembler.createTransactionManager(config
051: .configureService(TransactionServiceInfo.class));
052: assembler.createSecurityService(config
053: .configureService(SecurityServiceInfo.class));
054:
055: EjbJar ejbJar = new EjbJar();
056: ejbJar.addEnterpriseBean(new StatelessBean(Color.class));
057: ejbJar.addEnterpriseBean(new StatelessBean(Red.class));
058: ejbJar.addEnterpriseBean(new StatelessBean(Crimson.class));
059: ejbJar.addEnterpriseBean(new StatelessBean(Scarlet.class));
060: List<ContainerTransaction> declared = ejbJar
061: .getAssemblyDescriptor().getContainerTransaction();
062:
063: declared.add(new ContainerTransaction(TransAttribute.REQUIRED,
064: "*", "*", "*"));
065: declared.add(new ContainerTransaction(TransAttribute.SUPPORTS,
066: "*", "Crimson", "*"));
067: declared.add(new ContainerTransaction(TransAttribute.SUPPORTS,
068: Color.class.getName(), "Scarlet", "*"));
069: declared.add(new ContainerTransaction(TransAttribute.NEVER,
070: Red.class.getName(), "Scarlet", "red"));
071: declared.add(new ContainerTransaction(TransAttribute.REQUIRED,
072: "Scarlet", Scarlet.class.getMethod("scarlet")));
073:
074: EjbJarInfo ejbJarInfo = config.configureApplication(ejbJar);
075: assembler.createApplication(ejbJarInfo);
076:
077: loadAttributes(ejbJarInfo, "Color");
078:
079: assertAttribute("Never", Color.class.getMethod("color"));
080: assertAttribute("RequiresNew", Color.class.getMethod("color",
081: Object.class));
082: assertAttribute("Mandatory", Color.class.getMethod("color",
083: String.class));
084: assertAttribute("Mandatory", Color.class.getMethod("color",
085: Boolean.class));
086: assertAttribute("Mandatory", Color.class.getMethod("color",
087: Integer.class));
088:
089: loadAttributes(ejbJarInfo, "Red");
090:
091: assertAttribute("Never", Red.class.getMethod("color"));
092: assertAttribute("Required", Red.class.getMethod("color",
093: Object.class));
094: assertAttribute("Mandatory", Red.class.getMethod("color",
095: String.class));
096: assertAttribute("Mandatory", Red.class.getMethod("color",
097: Boolean.class));
098: assertAttribute("Mandatory", Red.class.getMethod("color",
099: Integer.class));
100: assertAttribute("RequiresNew", Red.class.getMethod("red"));
101: assertAttribute("Required", Red.class.getMethod("red",
102: Object.class));
103: assertAttribute("Required", Red.class.getMethod("red",
104: String.class));
105:
106: loadAttributes(ejbJarInfo, "Crimson");
107:
108: assertAttribute("Supports", Crimson.class.getMethod("color"));
109: assertAttribute("Supports", Crimson.class.getMethod("color",
110: Object.class));
111: assertAttribute("Supports", Crimson.class.getMethod("color",
112: String.class));
113: assertAttribute("Supports", Crimson.class.getMethod("color",
114: Boolean.class));
115: assertAttribute("Supports", Crimson.class.getMethod("color",
116: Integer.class));
117: assertAttribute("RequiresNew", Crimson.class.getMethod("red"));
118: assertAttribute("Supports", Crimson.class.getMethod("red",
119: Object.class));
120: assertAttribute("Supports", Crimson.class.getMethod("red",
121: String.class));
122: assertAttribute("RequiresNew", Crimson.class
123: .getMethod("crimson"));
124: assertAttribute("Supports", Crimson.class.getMethod("crimson",
125: String.class));
126:
127: loadAttributes(ejbJarInfo, "Scarlet");
128:
129: assertAttribute("Never", Scarlet.class.getMethod("color"));
130: assertAttribute("Required", Scarlet.class.getMethod("color",
131: Object.class));
132: assertAttribute("Supports", Scarlet.class.getMethod("color",
133: String.class));
134: assertAttribute("Supports", Scarlet.class.getMethod("color",
135: Boolean.class));
136: assertAttribute("Supports", Scarlet.class.getMethod("color",
137: Integer.class));
138: assertAttribute("RequiresNew", Scarlet.class.getMethod("red"));
139: assertAttribute("Never", Scarlet.class.getMethod("red",
140: Object.class));
141: assertAttribute("Never", Scarlet.class.getMethod("red",
142: String.class));
143: assertAttribute("Required", Scarlet.class.getMethod("scarlet"));
144: assertAttribute("NotSupported", Scarlet.class.getMethod(
145: "scarlet", String.class));
146:
147: }
148:
149: private void loadAttributes(EjbJarInfo ejbJarInfo,
150: String deploymentId) {
151: ContainerSystem system = SystemInstance.get().getComponent(
152: ContainerSystem.class);
153: CoreDeploymentInfo deploymentInfo = (CoreDeploymentInfo) system
154: .getDeploymentInfo(deploymentId);
155: List<MethodTransactionInfo> infos = normalize(ejbJarInfo.methodTransactions);
156: attributes = resolveAttributes(infos, deploymentInfo);
157: }
158:
159: private void assertAttribute(String attribute, Method method) {
160: MethodTransactionInfo info = (MethodTransactionInfo) attributes
161: .get(method);
162: assertEquals(method.toString(), attribute, info.transAttribute);
163: }
164:
165: @Local
166: public static interface ColorLocal {
167: }
168:
169: @Remote
170: public static interface ColorRemote {
171: }
172:
173: @TransactionAttribute(MANDATORY)
174: public static class Color implements ColorLocal, ColorRemote {
175:
176: @TransactionAttribute(NEVER)
177: public void color() {
178: }
179:
180: @TransactionAttribute(REQUIRES_NEW)
181: public void color(Object o) {
182: }
183:
184: public void color(String s) {
185: }
186:
187: public void color(Boolean b) {
188: }
189:
190: public void color(Integer i) {
191: }
192: }
193:
194: public static class Red extends Color {
195:
196: public void color(Object o) {
197: super .color(o);
198: }
199:
200: @TransactionAttribute(REQUIRES_NEW)
201: public void red() {
202: }
203:
204: public void red(Object o) {
205: }
206:
207: public void red(String s) {
208: }
209:
210: }
211:
212: @TransactionAttribute(NOT_SUPPORTED)
213: public static class Crimson extends Red {
214:
215: public void color() {
216: }
217:
218: public void color(String s) {
219: }
220:
221: @TransactionAttribute(REQUIRES_NEW)
222: public void crimson() {
223: }
224:
225: public void crimson(String s) {
226: }
227: }
228:
229: @TransactionAttribute(NOT_SUPPORTED)
230: public static class Scarlet extends Red {
231:
232: @TransactionAttribute(REQUIRES_NEW)
233: public void scarlet() {
234: }
235:
236: public void scarlet(String s) {
237: }
238: }
239:
240: }
|