KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
22 import java.lang.reflect.Modifier JavaDoc;
23 import org.netbeans.jmi.javamodel.ClassDefinition;
24 import org.netbeans.jmi.javamodel.ExpressionStatement;
25 import org.netbeans.jmi.javamodel.JavaClass;
26 import org.netbeans.jmi.javamodel.JavaModelPackage;
27 import org.netbeans.jmi.javamodel.Method;
28 import org.netbeans.jmi.javamodel.NewClassExpression;
29 import org.netbeans.jmi.javamodel.PrimitiveTypeKindEnum;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.junit.NbTestSuite;
32 import org.openide.filesystems.FileStateInvalidException;
33
34 /**
35  *
36  * @author Pavel Flaska
37  */

38 public class NewClassWithAnnTest extends NbTestCase {
39     
40     /** Creates a new instance of NewClassWithAnnTest */
41     public NewClassWithAnnTest() {
42         super("NewClassWithAnnTest");
43     }
44     
45     public static NbTestSuite suite() {
46         NbTestSuite suite = new NbTestSuite(NewClassWithAnnTest.class);
47         return suite;
48     }
49     
50     JavaClass clazz;
51     JavaModelPackage pkg;
52     
53     protected void setUp() {
54         clazz = Utility.findClass("org.netbeans.test.codegen.NewClassWithAnn");
55         pkg = (JavaModelPackage) clazz.refImmediatePackage();
56     }
57     
58     public void testAddBody() throws IOException JavaDoc, FileStateInvalidException {
59         boolean fail = true;
60         Utility.beginTrans(true);
61         try {
62             Method m = (Method) clazz.getContents().get(0);
63             ExpressionStatement st = (ExpressionStatement) m.getBody().getStatements().get(0);
64             NewClassExpression nce = (NewClassExpression) st.getExpression();
65             ClassDefinition cd = pkg.getClassDefinition().createClassDefinition();
66             cd.setSuperClassName(pkg.getMultipartId().createMultipartId("java.lang.Runnable", null, null));
67             Method m1 = pkg.getMethod().createMethod("run", null, Modifier.PUBLIC, null, null,
68                     null, null, null, null, null, pkg.getMultipartId().createMultipartId("void", null, null), 0);
69             cd.getContents().add(m1);
70             nce.setClassDefinition(cd);
71             fail = false;
72         }
73         finally {
74             Utility.endTrans(fail);
75         }
76         assertFile("File is not correctly generated.",
77             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/NewClassWithAnn.java"),
78             getGoldenFile("testAddBody_NewClassWithAnn.pass"),
79             getWorkDir()
80         );
81     }
82 }
83
Popular Tags