1 19 package org.netbeans.jmi.javamodel.codegen; 20 21 import java.lang.reflect.Modifier ; 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.List ; 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 47 public class FormatterTest extends NbTestCase { 48 49 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 , 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", null, false, id, 0, null, 92 "0" 93 ); 94 List varList = new ArrayList (1); 95 varList.add(lv); 96 LocalVarDeclaration lvd = pkg.getLocalVarDeclaration().createLocalVarDeclaration(false, id2, varList); 97 List initList = new ArrayList (1); 98 initList.add(lvd); 99 List steps = new ArrayList (1); 100 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 statem = new ArrayList (); 108 statem.add(forStmt); 109 statem.add(createIfStatem()); 110 StatementBlock block = pkg.getStatementBlock().createStatementBlock(statem); 111 Method forova = methodProxy.createMethod( 112 "forovaMethod", null, Modifier.PUBLIC, "no javadoc", null, block, null, null, null, null, id3, 0 ); 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 142 public static void main(String [] args) { 143 TestRunner.run(suite()); 144 } 145 146 } 147 | Popular Tags |