1 19 package org.netbeans.jmi.javamodel.codegen.indent; 20 21 import java.util.Collections ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import org.netbeans.jmi.javamodel.Catch; 25 import org.netbeans.jmi.javamodel.Constructor; 26 import org.netbeans.jmi.javamodel.JavaClass; 27 import org.netbeans.jmi.javamodel.JavaModelPackage; 28 import org.netbeans.jmi.javamodel.NewClassExpression; 29 import org.netbeans.jmi.javamodel.Parameter; 30 import org.netbeans.jmi.javamodel.StatementBlock; 31 import org.netbeans.jmi.javamodel.StatementBlockClass; 32 import org.netbeans.jmi.javamodel.TryStatement; 33 import org.netbeans.junit.NbTestCase; 34 import org.netbeans.junit.NbTestSuite; 35 import org.openide.filesystems.FileStateInvalidException; 36 import org.netbeans.jmi.javamodel.codegen.*; 37 38 42 public class SurroundExistingBodyTest extends NbTestCase { 43 44 JavaClass clazz; 45 JavaModelPackage pkg; 46 47 50 public SurroundExistingBodyTest() { 51 super("SurroundExistingBodyTest"); 52 } 53 54 public static NbTestSuite suite() { 55 NbTestSuite suite = new NbTestSuite(SurroundExistingBodyTest.class); 56 return suite; 57 } 58 59 protected void setUp() { 60 clazz = (JavaClass) Utility.findClass("org.netbeans.test.codegen.indent.SurroundExistingBody"); 61 pkg = (JavaModelPackage) clazz.refImmediatePackage(); 62 } 63 64 public void testSurroundBlockWithTry() throws java.io.IOException , FileStateInvalidException { 65 boolean fail = true; 66 Utility.beginTrans(true); 67 try { 68 Constructor constructor = (Constructor) clazz.getContents().get(0); 69 70 List statements = constructor.getBody().getStatements(); 71 StatementBlockClass sbc = pkg.getStatementBlock(); 72 Parameter p = pkg.getParameter().createParameter("e", null, false, pkg.getMultipartId().createMultipartId("java.lang.Exception", null, null), 0, false); 74 StatementBlock bodyBody = sbc.createStatementBlock(); 76 Object [] arr = statements.toArray(); 78 for (int i = 1; i < arr.length; i++) { 79 statements.remove(arr[i]); 80 bodyBody.getStatements().add(arr[i]); 81 } 82 StatementBlock finallyBody = sbc.createStatementBlock(); 84 finallyBody.getStatements().add(pkg.getEmptyStatement().createEmptyStatement()); 85 StatementBlock catchBody = sbc.createStatementBlock(); 87 NewClassExpression e = pkg.getNewClassExpression().createNewClassExpression( 88 "Throwable", 89 Collections.singletonList(pkg.getStringLiteral().createStringLiteral("Bad day!")), 90 null, 91 null, 92 null 93 ); 94 catchBody.getStatements().add(pkg.getThrowStatement().createThrowStatement(e)); 95 statements.add(pkg.getTryStatement().createTryStatement(bodyBody, Collections.singletonList(pkg.getCatch().createCatch(p, catchBody)), finallyBody)); 96 fail = false; 97 } 98 finally { 99 Utility.endTrans(fail); 100 } 101 assertFile("File is not correctly generated.", 102 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/SurroundExistingBody.java"), 103 getGoldenFile("SurroundExistingBody_SurroundExistingBodyTest.pass"), 104 getWorkDir() 105 ); 106 } 107 108 } 109 | Popular Tags |