1 19 20 package org.netbeans.jmi.javamodel.usages; 21 22 import java.io.PrintStream ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import junit.textui.TestRunner; 28 import org.netbeans.jmi.javamodel.JavaClass; 29 import org.netbeans.jmi.javamodel.codegen.Utility; 30 import org.netbeans.junit.NbTestCase; 31 import org.netbeans.junit.NbTestSuite; 32 33 34 37 public class FindSubTypesTest extends NbTestCase { 38 39 public static final String [] SUBTYPES_ABSTRACT_COLLECTION = { 40 "org.netbeans.test.subtypes.AbstractList", 41 "org.netbeans.test.subtypes.AbstractSet", 42 "org.netbeans.test.subtypes.HashMap.Values" 43 }; 44 45 public static final String [] SUBTYPES_ABSTRACT_COLLECTION_TRANSITIVE = { 46 "org.netbeans.test.subtypes.AbstractList", 47 "org.netbeans.test.subtypes.AbstractSet", 48 "org.netbeans.test.subtypes.HashMap.Values", 49 "org.netbeans.test.subtypes.ArrayList", 50 "org.netbeans.test.subtypes.HashSet" 51 }; 52 53 public static final String [] SUBTYPES_COLLECTION = { 54 "org.netbeans.test.subtypes.Set", 55 "org.netbeans.test.subtypes.List", 56 "org.netbeans.test.subtypes.AbstractCollection" 57 }; 58 59 public static final String [] SUBTYPES_COLLECTION_TRANSITIVE = { 60 "org.netbeans.test.subtypes.AbstractList", 61 "org.netbeans.test.subtypes.AbstractCollection", 62 "org.netbeans.test.subtypes.List", 63 "org.netbeans.test.subtypes.HashMap.Values", 64 "org.netbeans.test.subtypes.AbstractSet", 65 "org.netbeans.test.subtypes.Set", 66 "org.netbeans.test.subtypes.HashSet", 67 "org.netbeans.test.subtypes.ArrayList" 68 }; 69 70 public static final String [] SUBCLASSES_ABSTRACT_COLLECTION = { 71 "org.netbeans.test.subtypes.AbstractSet", 72 "org.netbeans.test.subtypes.HashMap.Values", 73 "org.netbeans.test.subtypes.AbstractList" 74 }; 75 76 public static final String [] SUBCLASSES_COLLECTION = {}; 77 public static final String [] IMPLEMENTORS_ABSTRACT_COLLECTION = {}; 78 public static final String [] IMPLEMENTORS_COLLECTION = { 79 "org.netbeans.test.subtypes.Set", 80 "org.netbeans.test.subtypes.List", 81 "org.netbeans.test.subtypes.AbstractCollection" 82 }; 83 84 private JavaClass abstractCol; 85 private JavaClass col; 86 87 88 public FindSubTypesTest(String name) { 89 super(name); 90 } 91 92 public static NbTestSuite suite() { 93 return new NbTestSuite(FindSubTypesTest.class); 94 } 95 96 97 public static void main(java.lang.String [] args) { 98 TestRunner.run(suite()); 99 } 100 101 public void setUp() { 102 abstractCol = Utility.findClass("org.netbeans.test.subtypes.AbstractCollection"); 103 col = Utility.findClass("org.netbeans.test.subtypes.Collection"); 104 } 105 106 public void testFindAbstractCollectionSubTypes() { 107 compare(abstractCol.findSubTypes(false), SUBTYPES_ABSTRACT_COLLECTION); 108 } 109 110 public void testFindAbstractCollectionSubTypesTrans() { 111 compare(abstractCol.findSubTypes(true), SUBTYPES_ABSTRACT_COLLECTION_TRANSITIVE); 112 } 113 114 public void testFindCollectionSubTypes() { 115 compare(col.findSubTypes(false), SUBTYPES_COLLECTION); 116 } 117 118 public void testFindCollectionSubTypesTrans() { 119 compare(col.findSubTypes(true), SUBTYPES_COLLECTION_TRANSITIVE); 120 } 121 122 public void testFindAbstractCollectionSubClasses() { 123 compare(abstractCol.getSubClasses(), SUBCLASSES_ABSTRACT_COLLECTION); 124 } 125 126 public void testFindCollectionSubClasses() { 127 compare(col.getSubClasses(), SUBCLASSES_COLLECTION); 128 } 129 130 public void testFindAbstractCollectionImplementors() { 131 compare(abstractCol.getImplementors(), IMPLEMENTORS_ABSTRACT_COLLECTION); 132 } 133 134 public void testFindCollectionImplementors() { 135 compare(col.getImplementors(), IMPLEMENTORS_COLLECTION); 136 } 137 138 private void compare(Collection col, String [] classNames) { 139 PrintStream log = getLog(); 140 HashSet names = new HashSet (Arrays.asList(classNames)); 141 142 boolean passed = true; 143 for (Iterator it = col.iterator(); it.hasNext();) { 144 String name = ((JavaClass) it.next()).getName(); 145 if (!names.remove(name)) { 146 log.println("Unexpected type found: " + name); 147 passed = false; 148 } 149 } 150 151 if (!names.isEmpty()) { 152 log.println(); 153 log.println("Missing types:"); 154 passed = false; 155 for (Iterator it = names.iterator(); it.hasNext();) { 156 log.println(it.next()); 157 } 158 } 159 if (!passed) { 160 fail("Test failed, see log file for details"); 161 } 162 } 163 } 164 | Popular Tags |