1 19 package org.netbeans.jmi.javamodel.codegen.indent; 20 import java.util.Collections ; 21 import java.util.List ; 22 import org.netbeans.jmi.javamodel.Catch; 23 import org.netbeans.jmi.javamodel.Constructor; 24 import org.netbeans.jmi.javamodel.JavaClass; 25 import org.netbeans.jmi.javamodel.JavaModelPackage; 26 import org.netbeans.jmi.javamodel.NewClassExpression; 27 import org.netbeans.jmi.javamodel.Parameter; 28 import org.netbeans.jmi.javamodel.StatementBlock; 29 import org.netbeans.jmi.javamodel.StatementBlockClass; 30 import org.netbeans.jmi.javamodel.TryStatement; 31 import org.netbeans.jmi.javamodel.codegen.Utility; 32 import org.netbeans.junit.NbTestCase; 33 import org.netbeans.junit.NbTestSuite; 34 import org.openide.filesystems.FileStateInvalidException; 35 36 41 public class Indent1Test extends NbTestCase { 42 43 JavaClass clazz; 44 JavaModelPackage pkg; 45 46 47 public Indent1Test() { 48 super("Indent1Test"); 49 } 50 51 public static NbTestSuite suite() { 52 NbTestSuite suite = new NbTestSuite(Indent1Test.class); 53 return suite; 54 } 55 56 protected void setUp() { 57 clazz = (JavaClass) Utility.findClass("org.netbeans.test.codegen.indent.Indent1"); 58 pkg = (JavaModelPackage) clazz.refImmediatePackage(); 59 } 60 61 76 public void testIndentTryBlock() throws java.io.IOException , FileStateInvalidException { 77 boolean fail = true; 78 Utility.beginTrans(true); 79 try { 80 Constructor constructor = (Constructor) clazz.getContents().get(0); 81 List statements = constructor.getBody().getStatements(); 82 StatementBlockClass sbc = pkg.getStatementBlock(); 83 Parameter p = pkg.getParameter().createParameter("e", null, false, pkg.getMultipartId().createMultipartId("java.lang.Exception", null, null), 0, false); 84 StatementBlock bodyBody = sbc.createStatementBlock(); 85 bodyBody.getStatements().add(pkg.getEmptyStatement().createEmptyStatement()); 86 StatementBlock finallyBody = sbc.createStatementBlock(); 87 finallyBody.getStatements().add(pkg.getEmptyStatement().createEmptyStatement()); 88 StatementBlock catchBody = sbc.createStatementBlock(); 89 catchBody.getStatements().add(pkg.getEmptyStatement().createEmptyStatement()); 90 statements.add(pkg.getTryStatement().createTryStatement(bodyBody, Collections.singletonList(pkg.getCatch().createCatch(p, catchBody)), finallyBody)); 91 fail = false; 92 } 93 finally { 94 Utility.endTrans(fail); 95 } 96 assertFile("File is not correctly generated.", 97 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/Indent1.java"), 98 getGoldenFile("testIndentTryBlock_Indent1Test.pass"), 99 getWorkDir() 100 ); 101 } 102 103 113 public void testThrowsStatement() throws java.io.IOException , FileStateInvalidException { 114 boolean fail = true; 115 Utility.beginTrans(true); 116 try { 117 Constructor constructor = (Constructor) clazz.getContents().get(0); 118 List catches = ((TryStatement) constructor.getBody().getStatements().get(0)).getCatches(); 119 List catchBodyStmts = ((Catch) catches.get(0)).getBody().getStatements(); 120 NewClassExpression e = pkg.getNewClassExpression().createNewClassExpression( 121 "Throwable", 122 Collections.singletonList(pkg.getStringLiteral().createStringLiteral("Bad day!")), 123 null, 124 null, 125 null 126 ); 127 catchBodyStmts.add(1, pkg.getThrowStatement().createThrowStatement(e)); 128 fail = false; 129 } 130 finally { 131 Utility.endTrans(fail); 132 } 133 assertFile("File is not correctly generated.", 134 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/Indent1.java"), 135 getGoldenFile("testThrowStatement_Indent1Test.pass"), 136 getWorkDir() 137 ); 138 } 139 } 140 | Popular Tags |