1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.test.suitebuilder;
18 
19 import junit.framework.TestCase;
20 
21 import java.lang.reflect.Method;
22 
23 public class AssignableFromTest extends TestCase {
24     private AssignableFrom assignableFrom;
25 
26 
setUp()27     protected void setUp() throws Exception {
28         super.setUp();
29         assignableFrom = new AssignableFrom(Animal.class);
30     }
31 
testSelfIsAssignable()32     public void testSelfIsAssignable() throws Exception {
33         assertTrue(assignableFrom.apply(testMethodFor(Animal.class)));
34     }
35 
testSubclassesAreAssignable()36     public void testSubclassesAreAssignable() throws Exception {
37         assertTrue(assignableFrom.apply(testMethodFor(Mammal.class)));
38         assertTrue(assignableFrom.apply(testMethodFor(Human.class)));
39     }
40 
testNotAssignable()41     public void testNotAssignable() throws Exception {
42         assertFalse(assignableFrom.apply(testMethodFor(Pencil.class)));
43     }
44 
testImplementorsAreAssignable()45     public void testImplementorsAreAssignable() throws Exception {
46         assignableFrom = new AssignableFrom(WritingInstrument.class);
47 
48         assertTrue(assignableFrom.apply(testMethodFor(Pencil.class)));
49         assertTrue(assignableFrom.apply(testMethodFor(Pen.class)));
50     }
51 
testMethodFor(Class<? extends TestCase> aClass)52     private TestMethod testMethodFor(Class<? extends TestCase> aClass)
53             throws NoSuchMethodException {
54         Method method = aClass.getMethod("testX");
55         return new TestMethod(method, aClass);
56     }
57 
58     private class Animal extends TestCase {
testX()59         public void testX() {
60         }
61     }
62 
63     private class Mammal extends Animal {
testX()64         public void testX() {
65         }
66     }
67 
68     private class Human extends Mammal {
testX()69         public void testX() {
70         }
71     }
72 
73     private interface WritingInstrument {
74     }
75 
76     private class Pencil extends TestCase implements WritingInstrument {
testX()77         public void testX() {
78         }
79     }
80 
81     private class Pen extends TestCase implements WritingInstrument {
testX()82         public void testX() {
83         }
84     }
85 }
86