KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Modifier JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import junit.textui.TestRunner;
26 import org.netbeans.jmi.javamodel.Expression;
27 import org.netbeans.jmi.javamodel.ForStatement;
28 import org.netbeans.jmi.javamodel.JavaClass;
29 import org.netbeans.jmi.javamodel.JavaModelPackage;
30 import org.netbeans.jmi.javamodel.LocalVarDeclaration;
31 import org.netbeans.jmi.javamodel.LocalVariable;
32 import org.netbeans.jmi.javamodel.Method;
33 import org.netbeans.jmi.javamodel.MethodClass;
34 import org.netbeans.jmi.javamodel.MultipartId;
35 import org.netbeans.jmi.javamodel.OperatorEnum;
36 import org.netbeans.jmi.javamodel.Statement;
37 import org.netbeans.jmi.javamodel.StatementBlock;
38 import org.netbeans.jmi.javamodel.UnaryExpression;
39 import org.netbeans.junit.NbTestCase;
40 import org.netbeans.junit.NbTestSuite;
41 import org.openide.filesystems.FileStateInvalidException;
42
43 /**
44  *
45  * @author Pavel Flaska
46  */

47 public class FormatterTest extends NbTestCase {
48     
49     /** Need to be defined because of JUnit */
50     public FormatterTest() {
51         super("FormatterTest");
52     }
53     
54     public static NbTestSuite suite() {
55         NbTestSuite suite = new NbTestSuite(FormatterTest.class);
56         return suite;
57     }
58     
59     JavaClass clazz;
60     JavaModelPackage pkg;
61     MethodClass methodProxy;
62     
63     protected void setUp() {
64         clazz = Utility.findClass("org.netbeans.test.codegen.FormattedClass");
65         pkg = (JavaModelPackage) clazz.refImmediatePackage();
66         methodProxy = pkg.getMethod();
67     }
68
69     private Statement createIfStatem() {
70         StatementBlock then1 = pkg.getStatementBlock().createStatementBlock(Collections.singletonList(pkg.getEmptyStatement().createEmptyStatement()));
71         StatementBlock else1 = pkg.getStatementBlock().createStatementBlock(Collections.singletonList(pkg.getEmptyStatement().createEmptyStatement()));
72         UnaryExpression left = pkg.getIntLiteral().createIntLiteral(12);
73         UnaryExpression right = pkg.getIntLiteral().createIntLiteral(5);
74         Expression cond = pkg.getInfixExpression().createInfixExpression(left, OperatorEnum.NEQUAL, right);
75         return pkg.getIfStatement().createIfStatement(cond, then1, else1);
76     }
77     
78     public void testForStatement() throws java.io.IOException JavaDoc, FileStateInvalidException {
79         boolean fail = true;
80         Utility.beginTrans(true);
81         try {
82             MultipartId id = pkg.getMultipartId().createMultipartId("int", null, null);
83             MultipartId id2 = pkg.getMultipartId().createMultipartId("int", null, null);
84             MultipartId id3 = pkg.getMultipartId().createMultipartId("int", null, null);
85             LocalVariable lv = pkg.getLocalVariable().createLocalVariable(
86                 "i", // name
87
null, // annotations
88
false, // isFinal
89
id, // typeName
90
0, // dimCount
91
null,
92                 "0"
93             );
94             List JavaDoc varList = new ArrayList JavaDoc(1);
95             varList.add(lv);
96             LocalVarDeclaration lvd = pkg.getLocalVarDeclaration().createLocalVarDeclaration(false, id2, varList);
97             List JavaDoc initList = new ArrayList JavaDoc(1);
98             initList.add(lvd);
99             List JavaDoc steps = new ArrayList JavaDoc(1);
100             // i == 0
101
UnaryExpression left = pkg.getMultipartId().createMultipartId("i", null, null);
102             UnaryExpression stepVar = pkg.getMultipartId().createMultipartId("i", null, null);
103             Expression right = pkg.getIntLiteral().createIntLiteral(5);
104             Expression cond = pkg.getInfixExpression().createInfixExpression(left, OperatorEnum.LTE, right);
105             steps.add(pkg.getPostfixExpression().createPostfixExpression(OperatorEnum.INC, stepVar));
106             ForStatement forStmt = pkg.getForStatement().createForStatement(cond, steps, pkg.getEmptyStatement().createEmptyStatement(), initList);
107             List JavaDoc statem = new ArrayList JavaDoc();
108             statem.add(forStmt);
109             statem.add(createIfStatem());
110             StatementBlock block = pkg.getStatementBlock().createStatementBlock(statem);
111             Method forova = methodProxy.createMethod(
112                 "forovaMethod", // name
113
null, // annotations
114
Modifier.PUBLIC, // modifiers
115
"no javadoc", //javaDocText
116
null, // javaDoc
117
block, // body
118
null, // bodyText
119
null, // typeArguments
120
null, // parameters
121
null, // exceptions names
122
id3, // typeName
123
0 // dims
124
);
125             clazz.getFeatures().add(forova);
126             fail = false;
127         }
128         finally {
129             Utility.endTrans(fail);
130         }
131         assertFile("File is not correctly generated.",
132             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/FormattedClass.java"),
133             getGoldenFile("FormattedClass.pass"),
134             getWorkDir()
135         );
136     }
137
138     
139     /**
140      * @param args the command line arguments
141      */

142     public static void main(String JavaDoc[] args) {
143         TestRunner.run(suite());
144     }
145     
146 }
147
Popular Tags