KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Collections JavaDoc;
21 import java.util.List JavaDoc;
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 /**
37  * Tests indentation of newly generated elements.
38  *
39  * @author Pavel Flaska
40  */

41 public class Indent1Test extends NbTestCase {
42     
43     JavaClass clazz;
44     JavaModelPackage pkg;
45     
46     /** Creates a new instance of Indent1Test */
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     /**
62      * Creates the body in constructor:
63      *
64      * <code>
65      * public Indent1() {
66      * try {
67      * ;
68      * } catch (java.lang.Exception e) {
69      * ;
70      * } finally {
71      * ;
72      * }
73      * </code>
74      *
75      */

76     public void testIndentTryBlock() throws java.io.IOException JavaDoc, FileStateInvalidException {
77         boolean fail = true;
78         Utility.beginTrans(true);
79         try {
80             Constructor constructor = (Constructor) clazz.getContents().get(0);
81             List JavaDoc 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     /**
104      * Adds new <code>throw</code> statement to catch section:
105      *
106      * <code>
107      * } catch (java.lang.Exception e) {
108      * ;
109      * throw new Throwable("Bad day!");
110      * } finally {
111      * </code>
112      */

113     public void testThrowsStatement() throws java.io.IOException JavaDoc, FileStateInvalidException {
114         boolean fail = true;
115         Utility.beginTrans(true);
116         try {
117             Constructor constructor = (Constructor) clazz.getContents().get(0);
118             List JavaDoc catches = ((TryStatement) constructor.getBody().getStatements().get(0)).getCatches();
119             List JavaDoc 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