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 junit.framework.TestCase;
019:
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.List;
023: import java.util.Arrays;
024:
025: /**
026: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
027: */
028: public class InterceptorBindingInfoComparatorTest extends TestCase {
029: private ArrayList<InterceptorBindingInfo> expected;
030:
031: public void testHighLevelOrdering() throws Exception {
032:
033: ArrayList<InterceptorBindingInfo> expected = new ArrayList<InterceptorBindingInfo>();
034:
035: InterceptorBindingInfo info = null;
036:
037: // Package Interceptors (aka Default Interceptors) ///////////////
038: info = add(expected, new InterceptorBindingInfo());
039: info.ejbName = "*";
040: info.interceptors.add("DefaultInterceptorUno");
041:
042: // Class Interceptors ////////////////////////////////////////////
043: info = add(expected, new InterceptorBindingInfo());
044: info.ejbName = "BeanOne";
045: info.interceptors.add("BeanOneClassInterceptorUno");
046:
047: // Method Interceptors (no params) ///////////////////////////////
048: info = add(expected, new InterceptorBindingInfo());
049: info.ejbName = "BeanTwo";
050: info.interceptors.add("BeanTwoMethodInterceptorDos");
051: info.method = new NamedMethodInfo();
052: info.method.methodName = "ping";
053:
054: // Method Interceptors (params) ///////////////////////////////
055: info = add(expected, new InterceptorBindingInfo());
056: info.ejbName = "BeanTwo";
057: info.interceptors.add("BeanTwoMethodInterceptorDos");
058: info.method = new NamedMethodInfo();
059: info.method.methodName = "ping";
060: info.method.methodParams = new ArrayList<String>();
061: info.method.methodParams.add("java.lang.String");
062:
063: ArrayList<InterceptorBindingInfo> actual = new ArrayList<InterceptorBindingInfo>(
064: expected);
065: Collections.shuffle(actual);
066: Collections
067: .sort(
068: actual,
069: new InterceptorBindingBuilder.IntercpetorBindingComparator());
070:
071: for (int i = 0; i < actual.size(); i++) {
072: InterceptorBindingInfo a = actual.get(i);
073: InterceptorBindingInfo e = expected.get(i);
074: assertSame(e, a);
075: }
076: }
077:
078: public void testInnerLevelOrdering() {
079:
080: ArrayList<InterceptorBindingInfo> expected = new ArrayList<InterceptorBindingInfo>();
081: ArrayList<InterceptorBindingInfo> actual = new ArrayList<InterceptorBindingInfo>(
082: 7);
083: for (int i = 0; i < 7; i++)
084: actual.add(null);
085:
086: InterceptorBindingInfo info = null;
087:
088: // Addition (class-level)
089: info = add(expected, new InterceptorBindingInfo());
090: info.ejbName = "BeanOne";
091: info.interceptors.add("BeanOneClassInterceptorUno");
092: actual.set(3, info);
093:
094: // Addition (class-level)
095: info = add(expected, new InterceptorBindingInfo());
096: info.ejbName = "BeanOne";
097: info.interceptors.add("BeanOneClassInterceptorDos");
098: actual.set(4, info);
099:
100: // Addition (class-level) + Exclusion (default level)
101: info = add(expected, new InterceptorBindingInfo());
102: info.ejbName = "BeanOne";
103: info.excludeDefaultInterceptors = true;
104: info.interceptors.add("BeanOneClassInterceptorTres");
105: actual.set(5, info);
106:
107: // Addition
108: info = add(expected, new InterceptorBindingInfo());
109: info.ejbName = "BeanOne";
110: info.interceptors.add("BeanOneClassInterceptorQuatro");
111: actual.set(6, info);
112:
113: // Exclusion (class level) + Addition (class-level)
114: // [this would be pointless, but good to test]
115: info = add(expected, new InterceptorBindingInfo());
116: info.ejbName = "BeanOne";
117: info.excludeClassInterceptors = true;
118: info.interceptors.add("BeanOneClassInterceptorCinco");
119: actual.set(2, info);
120:
121: // Exclusion (class and default levels)
122: // [excluding both is top of the exclusions at this level]
123: info = add(expected, new InterceptorBindingInfo());
124: info.ejbName = "BeanOne";
125: info.excludeDefaultInterceptors = true;
126: info.excludeClassInterceptors = true;
127: actual.set(1, info);
128:
129: // Excplicit Order (class level and default level)
130: // [trumps all bindings at this level and above]
131: info = add(expected, new InterceptorBindingInfo());
132: info.ejbName = "BeanOne";
133: info.interceptorOrder.add("TotalOrderingDefaultInterceptor");
134: actual.set(0, info);
135:
136: Collections
137: .sort(
138: actual,
139: new InterceptorBindingBuilder.IntercpetorBindingComparator());
140:
141: for (int i = 0; i < actual.size(); i++) {
142: InterceptorBindingInfo a = actual.get(i);
143: InterceptorBindingInfo e = expected.get(i);
144: assertSame(e, a);
145: }
146: }
147:
148: private InterceptorBindingInfo add(List list,
149: InterceptorBindingInfo bindingInfo) {
150: list.add(bindingInfo);
151: return bindingInfo;
152: }
153: }
|