KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Modifier JavaDoc;
22 import java.util.Collections JavaDoc;
23 import org.netbeans.jmi.javamodel.JavaClass;
24 import org.netbeans.jmi.javamodel.JavaModelPackage;
25 import org.netbeans.jmi.javamodel.Method;
26 import org.netbeans.jmi.javamodel.codegen.Utility;
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.junit.NbTestSuite;
29 import org.openide.filesystems.FileStateInvalidException;
30
31 /**
32  * Tests indentation of newly generated body text in method.
33  *
34  * @author Pavel Flaska
35   */

36 public class MethodBodyTextTest extends NbTestCase {
37     
38     JavaClass clazz;
39     JavaModelPackage pkg;
40     
41     /** Creates a new instance of MethodBodyTextTest */
42     public MethodBodyTextTest() {
43         super("MethodBodyTextTest");
44     }
45     
46     public static NbTestSuite suite() {
47         NbTestSuite suite = new NbTestSuite(MethodBodyTextTest.class);
48         return suite;
49     }
50     
51     protected void setUp() {
52         clazz = (JavaClass) Utility.findClass("org.netbeans.test.codegen.indent.MethodBodyText");
53         pkg = (JavaModelPackage) clazz.refImmediatePackage();
54     }
55     
56     public void testSetBodyText() throws java.io.IOException JavaDoc, FileStateInvalidException {
57         boolean fail = true;
58         Utility.beginTrans(true);
59         try {
60             Method m = (Method) clazz.getContents().get(0);
61             m.setBodyText("\n\n// nothing\n");
62             fail = false;
63         }
64         finally {
65             Utility.endTrans(fail);
66         }
67         assertFile("File is not correctly generated.",
68             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/MethodBodyText.java"),
69             getGoldenFile("testSetBodyText_MethodBodyTextTest.pass"),
70             getWorkDir()
71         );
72     }
73
74     public void testCreateWithBodyText() throws java.io.IOException JavaDoc, FileStateInvalidException {
75         boolean fail = true;
76         Utility.beginTrans(true);
77         StringBuffer JavaDoc body = new StringBuffer JavaDoc();
78         body.append("\n// TODO add your own implementation\n\n\n");
79         try {
80             Method m = pkg.getMethod().createMethod(
81                     "method2",
82                     Collections.EMPTY_LIST,
83                     Modifier.PUBLIC,
84                     null, // javadoc text
85
null, // javadoc
86
null, // object body
87
body.toString(), // string body
88
Collections.EMPTY_LIST, // type params
89
Collections.EMPTY_LIST, // parameters
90
Collections.EMPTY_LIST, // exceptions
91
pkg.getMultipartId().createMultipartId("void", null, null), // type
92
0);
93
94             clazz.getContents().add(m);
95             fail = false;
96         }
97         finally {
98             Utility.endTrans(fail);
99         }
100         assertFile("File is not correctly generated.",
101             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/MethodBodyText.java"),
102             getGoldenFile("testCreateWithBodyText_MethodBodyTextTest.pass"),
103             getWorkDir()
104         );
105     }
106     
107     public void testModifyBodyText() throws java.io.IOException JavaDoc, FileStateInvalidException {
108         boolean fail = true;
109         Utility.beginTrans(true);
110         try {
111             Method m = (Method) clazz.getContents().get(1);
112             m.setBodyText(m.getBodyText() + "// TODO no new lines around");
113             fail = false;
114         }
115         finally {
116             Utility.endTrans(fail);
117         }
118         assertFile("File is not correctly generated.",
119             Utility.getFile(getDataDir(), "org/netbeans/test/codegen/indent/MethodBodyText.java"),
120             getGoldenFile("testModifyBodyText_MethodBodyTextTest.pass"),
121             getWorkDir()
122         );
123     }
124 }
125
Popular Tags