KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > codegen > AnonClassTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
26 import java.lang.reflect.Modifier JavaDoc;
27
28 /**
29  *
30  * @author Pavel Flaska
31  */

32 public class AnonClassTest extends NbTestCase {
33
34     /** Need to be defined because of JUnit */
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 JavaDoc {
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 JavaDoc {
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     /**
113      * @param args the command line arguments
114      */

115     public static void main(String JavaDoc[] args) {
116         TestRunner.run(suite());
117     }
118     
119 }
120
Popular Tags