KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > codegen > indent > SurroundExistingBodyTest


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.indent;
20
21 import java.util.Collections JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
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 /**
39  *
40  * @author Pavel Flaska
41  */

42 public class SurroundExistingBodyTest extends NbTestCase {
43     
44     JavaClass clazz;
45     JavaModelPackage pkg;
46     
47     /**
48      * Creates a new instance of SurroundExistingBodyTest
49      */

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 JavaDoc, FileStateInvalidException {
65         boolean fail = true;
66         Utility.beginTrans(true);
67         try {
68             Constructor constructor = (Constructor) clazz.getContents().get(0);
69             
70             List JavaDoc statements = constructor.getBody().getStatements();
71             StatementBlockClass sbc = pkg.getStatementBlock();
72             // catch (Exception e) -- parameter
73
Parameter p = pkg.getParameter().createParameter("e", null, false, pkg.getMultipartId().createMultipartId("java.lang.Exception", null, null), 0, false);
74             // try body
75
StatementBlock bodyBody = sbc.createStatementBlock();
76             // adding statements to try body
77
Object JavaDoc[] 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             // finally body
83
StatementBlock finallyBody = sbc.createStatementBlock();
84             finallyBody.getStatements().add(pkg.getEmptyStatement().createEmptyStatement());
85             // catch body
86
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