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: *
017: */
018:
019: package org.apache.jorphan.reflect;
020:
021: import java.util.Map;
022: import java.util.Properties;
023:
024: import org.apache.jmeter.junit.JMeterTestCase;
025: import org.apache.jorphan.logging.LoggingManager;
026: import org.apache.jorphan.util.JMeterError;
027:
028: /*
029: * Unit tests for classes that use Functors
030: *
031: */
032: public class TestFunctor extends JMeterTestCase {
033:
034: interface HasName {
035: String getName();
036: }
037:
038: interface HasString {
039: String getString(String s);
040: }
041:
042: class Test1 implements HasName {
043: private final String name;
044:
045: public Test1() {
046: this ("");
047: }
048:
049: public Test1(String s) {
050: name = s;
051: }
052:
053: public String getName() {
054: return name;
055: }
056:
057: public String getString(String s) {
058: return s;
059: }
060: }
061:
062: class Test1a extends Test1 {
063: Test1a() {
064: super ("1a");
065: }
066:
067: Test1a(String s) {
068: super ("1a:" + s);
069: }
070:
071: public String getName() {
072: return super .getName() + ".";
073: }
074: }
075:
076: static class Test2 implements HasName, HasString {
077: private final String name;
078:
079: public Test2() {
080: this ("");
081: }
082:
083: public Test2(String s) {
084: name = s;
085: }
086:
087: public String getName() {
088: return name;
089: }
090:
091: public String getString(String s) {
092: return s;
093: }
094: }
095:
096: public TestFunctor(String arg0) {
097: super (arg0);
098: }
099:
100: public void setUp() {
101: LoggingManager.setPriority("FATAL_ERROR", LoggingManager
102: .removePrefix(Functor.class.getName()));
103: }
104:
105: public void testName() throws Exception {
106: Functor f1 = new Functor("getName");
107: Functor f2 = new Functor("getName");
108: Functor f1a = new Functor("getName");
109: Test1 t1 = new Test1("t1");
110: Test2 t2 = new Test2("t2");
111: Test1a t1a = new Test1a("aa");
112: assertEquals("t1", f1.invoke(t1));
113: //assertEquals("t1",f1.invoke());
114: try {
115: f1.invoke(t2);
116: fail("Should have generated error");
117: } catch (JMeterError e) {
118:
119: }
120: assertEquals("t2", f2.invoke(t2));
121: //assertEquals("t2",f2.invoke());
122: assertEquals("1a:aa.", f1a.invoke(t1a));
123: //assertEquals("1a:aa.",f1a.invoke());
124: try {
125: f1a.invoke(t1);// can't call invoke using super class
126: fail("Should have generated error");
127: } catch (JMeterError e) {
128:
129: }
130: // OK (currently) to invoke using sub-class
131: assertEquals("1a:aa.", f1.invoke(t1a));
132: //assertEquals("1a:aa.",f1.invoke());// N.B. returns different result from before
133: }
134:
135: public void testNameTypes() throws Exception {
136: Functor f = new Functor("getString",
137: new Class[] { String.class });
138: Functor f2 = new Functor("getString");// Args will be provided later
139: Test1 t1 = new Test1("t1");
140: assertEquals("x1", f.invoke(t1, new String[] { "x1" }));
141: try {
142: assertEquals("x1", f.invoke(t1));
143: fail("Should have generated an Exception");
144: } catch (JMeterError ok) {
145: }
146: assertEquals("x2", f2.invoke(t1, new String[] { "x2" }));
147: try {
148: assertEquals("x2", f2.invoke(t1));
149: fail("Should have generated an Exception");
150: } catch (JMeterError ok) {
151: }
152: }
153:
154: public void testObjectName() throws Exception {
155: Test1 t1 = new Test1("t1");
156: Test2 t2 = new Test2("t2");
157: Functor f1 = new Functor(t1, "getName");
158: assertEquals("t1", f1.invoke(t1));
159: assertEquals("t1", f1.invoke(t2)); // should use original object
160: }
161:
162: // Check how Class definition behaves
163: public void testClass() throws Exception {
164: Test1 t1 = new Test1("t1");
165: Test1 t1a = new Test1a("t1a");
166: Test2 t2 = new Test2("t2");
167: Functor f1 = new Functor(HasName.class, "getName");
168: assertEquals("t1", f1.invoke(t1));
169: assertEquals("1a:t1a.", f1.invoke(t1a));
170: assertEquals("t2", f1.invoke(t2));
171: try {
172: f1.invoke();
173: fail("Should have failed");
174: } catch (IllegalStateException ok) {
175:
176: }
177: Functor f2 = new Functor(HasString.class, "getString");
178: assertEquals("xyz", f2.invoke(t2, new String[] { "xyz" }));
179: try {
180: f2.invoke(t1, new String[] { "xyz" });
181: fail("Should have failed");
182: } catch (JMeterError ok) {
183:
184: }
185: Functor f3 = new Functor(t2, "getString");
186: assertEquals("xyz", f3.invoke(t2, new Object[] { "xyz" }));
187:
188: Properties p = new Properties();
189: p.put("Name", "Value");
190: Functor fk = new Functor(Map.Entry.class, "getKey");
191: Functor fv = new Functor(Map.Entry.class, "getValue");
192: Object o = p.entrySet().iterator().next();
193: assertEquals("Name", fk.invoke(o));
194: assertEquals("Value", fv.invoke(o));
195: }
196:
197: public void testBadParameters() throws Exception {
198: try {
199: new Functor(null);
200: fail("should have generated IllegalArgumentException;");
201: } catch (IllegalArgumentException ok) {
202: }
203: try {
204: new Functor(null, new Class[] {});
205: fail("should have generated IllegalArgumentException;");
206: } catch (IllegalArgumentException ok) {
207: }
208: try {
209: new Functor(null, new Object[] {});
210: fail("should have generated IllegalArgumentException;");
211: } catch (IllegalArgumentException ok) {
212: }
213: try {
214: new Functor(String.class, null);
215: fail("should have generated IllegalArgumentException;");
216: } catch (IllegalArgumentException ok) {
217: }
218: try {
219: new Functor(new Object(), null);
220: fail("should have generated IllegalArgumentException;");
221: } catch (IllegalArgumentException ok) {
222: }
223: try {
224: new Functor(new Object(), null, new Class[] {});
225: fail("should have generated IllegalArgumentException;");
226: } catch (IllegalArgumentException ok) {
227: }
228: try {
229: new Functor(new Object(), null, new Object[] {});
230: fail("should have generated IllegalArgumentException;");
231: } catch (IllegalArgumentException ok) {
232: }
233: }
234:
235: public void testIllegalState() throws Exception {
236: Functor f = new Functor("method");
237: try {
238: f.invoke();
239: fail("should have generated IllegalStateException;");
240: } catch (IllegalStateException ok) {
241: }
242: try {
243: f.invoke(new Object[] {});
244: fail("should have generated IllegalStateException;");
245: } catch (IllegalStateException ok) {
246: }
247: }
248: }
|