001: /*
002: * Copyright 2003,2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package net.sf.cglib.reflect;
017:
018: import java.lang.reflect.Method;
019: import junit.framework.*;
020:
021: /**
022: * @version $Id: TestDelegates.java,v 1.4 2004/06/24 21:15:16 herbyderby Exp $
023: */
024: public class TestDelegates extends net.sf.cglib.CodeGenTestCase {
025:
026: public interface StringMaker {
027: Object newInstance(char[] buf, int offset, int count);
028: }
029:
030: public void testConstructor() throws Throwable {
031: StringMaker maker = (StringMaker) ConstructorDelegate.create(
032: String.class, StringMaker.class);
033: assertTrue("nil".equals(maker.newInstance("vanilla"
034: .toCharArray(), 2, 3)));
035: }
036:
037: public interface Substring {
038: String substring(int start, int end);
039: }
040:
041: public interface Substring2 {
042: Object anyNameAllowed(int start, int end);
043: }
044:
045: public interface IndexOf {
046: int indexOf(String str, int fromIndex);
047: }
048:
049: public void testFancy() throws Throwable {
050: Substring delegate = (Substring) MethodDelegate.create("CGLIB",
051: "substring", Substring.class);
052: assertTrue("LI".equals(delegate.substring(2, 4)));
053: }
054:
055: public void testFancyNames() throws Throwable {
056: Substring2 delegate = (Substring2) MethodDelegate.create(
057: "CGLIB", "substring", Substring2.class);
058: assertTrue("LI".equals(delegate.anyNameAllowed(2, 4)));
059: }
060:
061: public void testFancyTypes() throws Throwable {
062: String test = "abcabcabc";
063: IndexOf delegate = (IndexOf) MethodDelegate.create(test,
064: "indexOf", IndexOf.class);
065: assertTrue(delegate.indexOf("ab", 1) == test.indexOf("ab", 1));
066: }
067:
068: public void testEquals() throws Throwable {
069: String test = "abc";
070: MethodDelegate mc1 = MethodDelegate.create(test, "indexOf",
071: IndexOf.class);
072: MethodDelegate mc2 = MethodDelegate.create(test, "indexOf",
073: IndexOf.class);
074: MethodDelegate mc3 = MethodDelegate.create("other", "indexOf",
075: IndexOf.class);
076: MethodDelegate mc4 = MethodDelegate.create(test, "substring",
077: Substring.class);
078: MethodDelegate mc5 = MethodDelegate.create(test, "substring",
079: Substring2.class);
080: assertTrue(mc1.equals(mc2));
081: assertTrue(!mc1.equals(mc3));
082: assertTrue(!mc1.equals(mc4));
083: assertTrue(mc4.equals(mc5));
084: }
085:
086: public static interface MainDelegate {
087: int main(String[] args);
088: }
089:
090: public static class MainTest {
091: public static int alternateMain(String[] args) {
092: return 7;
093: }
094: }
095:
096: public void testStaticDelegate() throws Throwable {
097: MainDelegate start = (MainDelegate) MethodDelegate
098: .createStatic(MainTest.class, "alternateMain",
099: MainDelegate.class);
100: assertTrue(start.main(null) == 7);
101: }
102:
103: public static interface Listener {
104: public void onEvent();
105: }
106:
107: public static class Publisher {
108: public int test = 0;
109: private MulticastDelegate event = MulticastDelegate
110: .create(Listener.class);
111:
112: public void addListener(Listener listener) {
113: event = event.add(listener);
114: }
115:
116: public void removeListener(Listener listener) {
117: event = event.remove(listener);
118: }
119:
120: public void fireEvent() {
121: ((Listener) event).onEvent();
122: }
123: }
124:
125: public void testPublisher() throws Throwable {
126: final Publisher p = new Publisher();
127: Listener l1 = new Listener() {
128: public void onEvent() {
129: p.test++;
130: }
131: };
132: p.addListener(l1);
133: p.addListener(l1);
134: p.fireEvent();
135: assertTrue(p.test == 2);
136: p.removeListener(l1);
137: p.fireEvent();
138: assertTrue(p.test == 3);
139: }
140:
141: public static interface SuperSimple {
142: public int execute();
143: }
144:
145: public void testMulticastReturnValue() throws Throwable {
146: SuperSimple ss1 = new SuperSimple() {
147: public int execute() {
148: return 1;
149: }
150: };
151: SuperSimple ss2 = new SuperSimple() {
152: public int execute() {
153: return 2;
154: }
155: };
156: MulticastDelegate multi = MulticastDelegate
157: .create(SuperSimple.class);
158: multi = multi.add(ss1);
159: multi = multi.add(ss2);
160: assertTrue(((SuperSimple) multi).execute() == 2);
161: multi = multi.remove(ss1);
162: multi = multi.add(ss1);
163: assertTrue(((SuperSimple) multi).execute() == 1);
164: }
165:
166: public TestDelegates(String testName) {
167: super (testName);
168: }
169:
170: public static void main(String[] args) {
171: junit.textui.TestRunner.run(suite());
172: }
173:
174: public static Test suite() {
175: return new TestSuite(TestDelegates.class);
176: }
177:
178: public void perform(ClassLoader loader) throws Throwable {
179: }
180:
181: public void testFailOnMemoryLeak() throws Throwable {
182: }
183:
184: }
|