001: // Copyright 2006 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.services;
016:
017: import java.io.IOException;
018: import java.util.NoSuchElementException;
019:
020: import org.apache.tapestry.ioc.test.IOCTestCase;
021: import org.testng.annotations.Test;
022:
023: public class MethodIteratorTest extends IOCTestCase {
024: static interface Play extends Runnable {
025: public void jump();
026: }
027:
028: static interface Runnable2 {
029: public void run();
030: }
031:
032: static interface Runnable3 extends Runnable, Runnable2 {
033: }
034:
035: static interface ToString {
036: public String toString();
037: }
038:
039: static interface Openable {
040: public void open();
041: }
042:
043: static interface OpenableWithError {
044: public void open() throws IOException;
045: }
046:
047: static interface CombinedOpeneable extends Openable,
048: OpenableWithError {
049: }
050:
051: @Test
052: public void simple_interface() {
053: MethodIterator mi = new MethodIterator(Runnable.class);
054:
055: assertTrue(mi.hasNext());
056:
057: MethodSignature actual = mi.next();
058:
059: assertEquals(
060: new MethodSignature(void.class, "run", null, null),
061: actual);
062:
063: assertFalse(mi.hasNext());
064:
065: try {
066: mi.next();
067: } catch (NoSuchElementException ex) {
068: //
069: }
070:
071: assertEquals(false, mi.getToString());
072: }
073:
074: @Test
075: public void inherited_methods_from_super _interface() {
076: MethodIterator mi = new MethodIterator(Play.class);
077:
078: assertTrue(mi.hasNext());
079:
080: // Problematic because the order in which they are returned is
081: // JDK specific and not defined! Perhaps we should sort by alpha?
082:
083: MethodSignature actual = mi.next();
084:
085: assertEquals(
086: new MethodSignature(void.class, "jump", null, null),
087: actual);
088:
089: assertTrue(mi.hasNext());
090:
091: actual = mi.next();
092:
093: assertEquals(
094: new MethodSignature(void.class, "run", null, null),
095: actual);
096:
097: assertFalse(mi.hasNext());
098:
099: assertEquals(false, mi.getToString());
100: }
101:
102: @Test
103: public void duplicate_methods_filtered_out() {
104: MethodIterator mi = new MethodIterator(Runnable3.class);
105:
106: MethodSignature actual = mi.next();
107:
108: assertEquals(
109: new MethodSignature(void.class, "run", null, null),
110: actual);
111:
112: assertEquals(false, mi.getToString());
113: }
114:
115: @Test
116: public void to_string_method_identified() {
117: MethodIterator mi = new MethodIterator(ToString.class);
118:
119: // Show that this is known immediately.
120:
121: assertEquals(true, mi.getToString());
122:
123: MethodSignature actual = mi.next();
124:
125: assertEquals(new MethodSignature(String.class, "toString",
126: null, null), actual);
127:
128: }
129:
130: @Test
131: public void inherited_methods_filtered_if_less_specific() {
132: MethodIterator mi = new MethodIterator(CombinedOpeneable.class);
133:
134: MethodSignature actual = mi.next();
135:
136: assertEquals(new MethodSignature(void.class, "open", null,
137: new Class[] { IOException.class }), actual);
138:
139: assertEquals(false, mi.hasNext());
140: }
141: }
|