1 19 package org.netbeans.modules.apisupport.metainfservices; 20 21 import java.net.URL ; 22 import java.net.URLClassLoader ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.List ; 27 import java.util.Set ; 28 import javax.jmi.reflect.RefClass; 29 import javax.jmi.reflect.RefException; 30 import javax.jmi.reflect.RefFeatured; 31 import javax.jmi.reflect.RefObject; 32 import javax.jmi.reflect.RefPackage; 33 import org.netbeans.jmi.javamodel.ClassDefinition; 34 import org.netbeans.jmi.javamodel.Constructor; 35 import org.netbeans.jmi.javamodel.Element; 36 import org.netbeans.jmi.javamodel.ElementPartKind; 37 import org.netbeans.jmi.javamodel.Field; 38 import org.netbeans.jmi.javamodel.JavaClass; 39 import org.netbeans.jmi.javamodel.JavaDoc; 40 import org.netbeans.jmi.javamodel.Method; 41 import org.netbeans.jmi.javamodel.MultipartId; 42 import org.netbeans.jmi.javamodel.Resource; 43 import org.netbeans.junit.NbTestCase; 44 import org.openide.filesystems.FileObject; 45 import org.openide.filesystems.FileSystem; 46 import org.openide.filesystems.FileUtil; 47 import org.openide.filesystems.LocalFileSystem; 48 import org.openide.util.Lookup; 49 import org.openide.util.lookup.Lookups; 50 51 55 public class ExportActionTest extends NbTestCase { 56 57 public ExportActionTest(String testName) { 58 super(testName); 59 } 60 61 protected void setUp() throws Exception { 62 clearWorkDir(); 63 } 64 65 protected void tearDown() throws Exception { 66 } 67 68 public void testGenerateFiles() throws Exception { 69 LocalFileSystem lfs = new LocalFileSystem(); 70 lfs.setRootDirectory(getWorkDir()); 71 72 FileSystem fs = lfs; 73 FileObject src = FileUtil.createFolder(fs.getRoot(), "src"); 74 75 ArrayList <String > files = new ArrayList <String >(); 76 files.add("META-INF/services/java.lang.Object"); 77 files.add("META-INF/services/java.lang.Runnable"); 78 79 ExportAction.createFiles(R.class.getName(), files, src); 80 81 URLClassLoader loader = new URLClassLoader (new URL [] { src.getURL() }, getClass().getClassLoader()); 82 Lookup l = Lookups.metaInfServices(loader); 83 84 Runnable r = l.lookup(Runnable .class); 85 86 assertNotNull("Runnable found", r); 87 assertEquals("It is my class", R.class, r.getClass()); 88 89 90 ExportAction.createFiles(Q.class.getName(), files, src); 91 92 l = Lookups.metaInfServices(loader); 93 Set <Class <? extends Runnable >> all = l.lookupResult(Runnable .class).allClasses(); 94 95 assertEquals(2, all.size()); 96 assertTrue("Q is there", all.contains(Q.class)); 97 assertTrue("R is there", all.contains(R.class)); 98 } 99 100 public void testRemovesAnnotations() throws Exception { 101 JavaClassImpl impl = new JavaClassImpl(); 102 impl.setName("org.tst.Test"); 103 impl.setSimpleName("Test"); 104 105 JavaClassImpl par = new JavaClassImpl(); 106 par.setName("org.par.Parent <X,Y>"); 107 par.setSimpleName("Parent <X,Y>"); 108 109 impl.setSuperClass(par); 110 111 JavaClassImpl obj = new JavaClassImpl(); 112 obj.setName("java.lang.Object"); 113 obj.setSimpleName("Object"); 114 115 par.setSuperClass(obj); 116 117 ArrayList <String > names = new ArrayList <String >(); 118 ExportAction.findInterfaces(impl, names); 119 120 assertEquals("Three", 3, names.size()); 121 122 for (String n : names) { 123 if (n.indexOf("<") >= 0) { 124 fail("Contains wrong char: " + n); 125 } 126 if (n.endsWith(" ")) { 127 fail("Ends with space:[" + n + "]"); 128 } 129 } 130 } 131 132 public static class R extends Object implements Runnable { 133 public void run() { 134 } 135 } 136 137 public static class Q implements Runnable { 138 public void run() { 139 } 140 } 141 142 private static final class JavaClassImpl implements JavaClass { 143 private String simpleName; 144 private String name; 145 private JavaClass superClass; 146 public List <JavaClass> interfaces = Collections.emptyList(); 147 148 public Collection findSubTypes(boolean recursively) { 149 fail("Not implemented"); 150 return null; 151 } 152 153 public boolean isInterface() { 154 fail("Not implemented"); 155 return false; 156 } 157 158 public void setInterface(boolean newValue) { 159 fail("Not implemented"); 160 } 161 162 public String getSimpleName() { 163 return simpleName; 164 } 165 166 public void setSimpleName(String newValue) { 167 simpleName = newValue; 168 } 169 170 public boolean isInner() { 171 return false; 172 } 173 174 public Collection getSubClasses() { 175 fail("Not implemented"); 176 return null; 177 } 178 179 public Collection getImplementors() { 180 fail("Not implemented"); 181 return null; 182 } 183 184 public int getStartOffset() { 185 return 0; 186 } 187 188 public int getEndOffset() { 189 return 0; 190 } 191 192 public Resource getResource() { 193 fail("Not implemented"); 194 return null; 195 } 196 197 public int getPartStartOffset(ElementPartKind part) { 198 fail("Not implemented"); 199 return 0; 200 } 201 202 public int getPartEndOffset(ElementPartKind part) { 203 fail("Not implemented"); 204 return 0; 205 } 206 207 public void replaceChild(Element oldChild, Element newChild) { 208 } 209 210 public List getChildren() { 211 fail("Not implemented"); 212 return null; 213 } 214 215 public boolean isValid() { 216 return true; 217 } 218 219 public Element duplicate() { 220 try { 221 return (JavaClass)clone(); 222 } catch (CloneNotSupportedException ex) { 223 throw new IllegalStateException (ex); 224 } 225 } 226 227 public boolean refIsInstanceOf(RefObject refObject, boolean b) { 228 fail("Not implemented"); 229 return false; 230 } 231 232 public RefClass refClass() { 233 fail("No"); 234 return null; 235 } 236 237 public RefFeatured refImmediateComposite() { 238 fail("No"); 239 return null; 240 } 241 242 public RefFeatured refOutermostComposite() { 243 fail("No"); 244 return null; 245 } 246 247 public void refDelete() { 248 } 249 250 public void refSetValue(RefObject refObject, Object object) { 251 } 252 253 public void refSetValue(String string, Object object) { 254 } 255 256 public Object refGetValue(RefObject refObject) { 257 fail("No"); 258 return null; 259 } 260 261 public Object refGetValue(String string) { 262 fail("No"); 263 return null; 264 } 265 266 public Object refInvokeOperation(RefObject refObject, List list) throws RefException { 267 fail("No"); 268 return null; 269 } 270 271 public Object refInvokeOperation(String string, List list) throws RefException { 272 fail("No"); 273 return null; 274 } 275 276 public RefObject refMetaObject() { 277 fail("No"); 278 return null; 279 } 280 281 public RefPackage refImmediatePackage() { 282 fail("No"); 283 return null; 284 } 285 286 public RefPackage refOutermostPackage() { 287 fail("No"); 288 return null; 289 } 290 291 public String refMofId() { 292 fail("No"); 293 return null; 294 } 295 296 public Collection refVerifyConstraints(boolean b) { 297 fail("No"); 298 return null; 299 } 300 301 public boolean isDeprecated() { 302 fail("No"); 303 return false; 304 } 305 306 public void setDeprecated(boolean newValue) { 307 } 308 309 public ClassDefinition getDeclaringClass() { 310 fail("No"); 311 return null; 312 } 313 314 public int getModifiers() { 315 fail("No"); 316 return 0; 317 } 318 319 public void setModifiers(int newValue) { 320 } 321 322 public String getJavadocText() { 323 fail("No"); 324 return null; 325 } 326 327 public void setJavadocText(String newValue) { 328 } 329 330 public JavaDoc getJavadoc() { 331 fail("No"); 332 return null; 333 } 334 335 public void setJavadoc(JavaDoc newValue) { 336 } 337 338 public List getAnnotations() { 339 fail("No"); 340 return null; 341 } 342 343 public String getName() { 344 return name; 345 } 346 347 public void setName(String newValue) { 348 this.name = newValue; 349 } 350 351 public Collection getReferences() { 352 fail("No"); 353 return null; 354 } 355 356 public Field getField(String name, boolean includeSupertypes) { 357 fail("No"); 358 return null; 359 } 360 361 public Method getMethod(String name, List parameters, boolean includeSupertypes) { 362 fail("No"); 363 return null; 364 } 365 366 public JavaClass getInnerClass(String simpleName, boolean includeSupertypes) { 367 fail("No"); 368 return null; 369 } 370 371 public Constructor getConstructor(List parameters, boolean includeSupertypes) { 372 fail("No"); 373 return null; 374 } 375 376 public boolean isSubTypeOf(ClassDefinition javaClass) { 377 fail("No"); 378 return false; 379 } 380 381 public List getContents() { 382 fail("No"); 383 return null; 384 } 385 386 public MultipartId getSuperClassName() { 387 fail("No"); 388 return null; 389 } 390 391 public void setSuperClassName(MultipartId newValue) { 392 } 393 394 public List getInterfaceNames() { 395 fail("No"); 396 return null; 397 } 398 399 public List getFeatures() { 400 fail("No"); 401 return null; 402 } 403 404 public List getInterfaces() { 405 return interfaces; 406 } 407 408 public JavaClass getSuperClass() { 409 return superClass; 410 } 411 412 public void setSuperClass(JavaClass newValue) { 413 this.superClass = newValue; 414 } 415 416 public List getTypeParameters() { 417 fail("No"); 418 return null; 419 } 420 421 } 422 } 423 | Popular Tags |