1 19 package org.netbeans.jmi.javamodel.codegen; 20 21 import junit.textui.TestRunner; 22 import org.netbeans.jmi.javamodel.*; 23 import org.netbeans.junit.NbTestCase; 24 import org.netbeans.junit.NbTestSuite; 25 import java.io.IOException ; 26 import java.lang.reflect.Modifier ; 27 28 32 public class AnonClassTest extends NbTestCase { 33 34 35 public AnonClassTest() { 36 super("AnonClassTest"); 37 } 38 39 public static NbTestSuite suite() { 40 NbTestSuite suite = new NbTestSuite(AnonClassTest.class); 41 return suite; 42 } 43 44 Method method; 45 JavaModelPackage pkg; 46 47 protected void setUp() { 48 JavaClass clazz = Utility.findClass("org.netbeans.test.codegen.AnonClassTestClass"); 49 pkg = (JavaModelPackage) clazz.refImmediatePackage(); 50 method = (Method) clazz.getContents().iterator().next(); 51 } 52 53 public void testRead() { 54 Utility.beginTrans(false); 55 try { 56 LocalVarDeclaration varDecl = (LocalVarDeclaration) method.getBody().getStatements().get(0); 57 LocalVariable var = (LocalVariable) varDecl.getVariables().get(0); 58 NewClassExpression nce = (NewClassExpression) var.getInitialValue(); 59 ClassDefinition cd = nce.getClassDefinition(); 60 assertEquals("Number of features does not match: ", 3, cd.getFeatures().size()); 61 } finally { 62 Utility.endTrans(); 63 } 64 } 65 66 public void testChange() throws IOException { 67 boolean fail = true; 68 Utility.beginTrans(true); 69 try { 70 LocalVarDeclaration varDecl = (LocalVarDeclaration) method.getBody().getStatements().get(0); 71 LocalVariable var = (LocalVariable) varDecl.getVariables().get(0); 72 NewClassExpression nce = (NewClassExpression) var.getInitialValue(); 73 ClassDefinition cd = nce.getClassDefinition(); 74 Method compare = (Method) cd.getContents().get(0); 75 ((Parameter) compare.getParameters().get(0)).setName("param1"); 76 ((Parameter) compare.getParameters().get(1)).setName("param2"); 77 fail = false; 78 } 79 finally { 80 Utility.endTrans(fail); 81 } 82 assertFile("File is not correctly generated.", 83 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnonClassTestClass.java"), 84 getGoldenFile("AnonClass1.pass"), 85 getWorkDir() 86 ); 87 } 88 89 public void testCreate() throws IOException { 90 boolean fail = true; 91 Utility.beginTrans(true); 92 try { 93 LocalVarDeclaration varDecl = pkg.getLocalVarDeclaration().createLocalVarDeclaration(true, null, null); 94 varDecl.setType(pkg.getType().resolve("java.lang.Object")); 95 ClassDefinition cd = pkg.getClassDefinition().createClassDefinition(); 96 NewClassExpression nce = pkg.getNewClassExpression().createNewClassExpression("java.lang.Object", null, null, null, cd); 97 LocalVariable var = pkg.getLocalVariable().createLocalVariable("test", null, false, null, 0, nce, null); 98 varDecl.getVariables().add(var); 99 cd.getContents().add(pkg.getMethod().createMethod("toString", null, Modifier.PUBLIC, null, null, null, "return null;", null, null, null, pkg.getMultipartId().createMultipartId("String", null, null), 0)); 100 method.getBody().getStatements().add(0, varDecl); 101 fail = false; 102 } finally { 103 Utility.endTrans(fail); 104 } 105 assertFile("File is not correctly generated.", 106 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnonClassTestClass.java"), 107 getGoldenFile("AnonClass2.pass"), 108 getWorkDir() 109 ); 110 } 111 112 115 public static void main(String [] args) { 116 TestRunner.run(suite()); 117 } 118 119 } 120 | Popular Tags |