001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.api.java.source.gen;
043:
044: import com.sun.source.tree.*;
045: import java.io.File;
046: import java.util.ArrayList;
047: import java.util.Collections;
048: import java.util.EnumSet;
049: import java.util.List;
050: import javax.lang.model.element.Modifier;
051: import javax.lang.model.type.TypeKind;
052: import junit.textui.TestRunner;
053: import org.netbeans.api.java.source.TestUtilities;
054: import org.netbeans.modules.java.source.transform.Transformer;
055: import org.netbeans.junit.NbTestSuite;
056:
057: /**
058: * Tests indentation of newly added elements
059: * @author Max Sauer
060: */
061: public class IndentAddedElemTest extends GeneratorTest {
062:
063: public IndentAddedElemTest(String name) {
064: super (name);
065: }
066:
067: /**
068: * Adds tests to suite
069: * @return created suite
070: */
071: public static NbTestSuite suite() {
072: //NbTestSuite suite = new NbTestSuite();
073: NbTestSuite suite = new NbTestSuite(IndentAddedElemTest.class);
074: //suite.addTest(new IndentMethodTest("testAddMethodToEmpty"));
075: return suite;
076: }
077:
078: /**
079: * Adding of methods
080: */
081: public void testAddMethodToEmpty() throws Exception {
082: testFile = new File(getWorkDir(), "Test.java");
083: TestUtilities.copyStringToFile(testFile,
084: "package com.max.test.alfa;\n\n"
085: + "public class Test {\n" + "}\n");
086: String golden = "package com.max.test.alfa;\n\n"
087: + "public class Test {\n" + "\n"
088: + " public double Eval(int param) {\n" + " }\n"
089: + "}\n";
090:
091: process(new Transformer<Void, Object>() {
092: @Override
093: public Void visitClass(ClassTree node, Object p) {
094: super .visitClass(node, p);
095: if ("Test".contentEquals(node.getSimpleName())) {
096: ModifiersTree parMods = make.Modifiers(Collections
097: .<Modifier> emptySet(), Collections
098: .<AnnotationTree> emptyList());
099: VariableTree param = make.Variable(parMods,
100: "param", make.PrimitiveType(TypeKind.INT),
101: null);
102: List<VariableTree> parList = new ArrayList<VariableTree>(
103: 1);
104: parList.add(param);
105: MethodTree member = make
106: .Method(
107: make
108: .Modifiers(
109: Collections
110: .singleton(Modifier.PUBLIC), // modifiers
111: Collections
112: .<AnnotationTree> emptyList() // annotations
113: ), // modifiers and annotations
114: "Eval", // name
115: make.PrimitiveType(TypeKind.DOUBLE), // return type
116: Collections
117: .<TypeParameterTree> emptyList(), // type parameters for parameters
118: parList, // parameters
119: Collections
120: .<ExpressionTree> emptyList(), // throws
121: make
122: .Block(
123: Collections
124: .<StatementTree> emptyList(),
125: false), // empty statement block
126: null // default value - not applicable here, used by annotations
127: );
128:
129: ClassTree copy = make.addClassMember(node, member);
130: this .copy.rewrite(node, copy);
131: }
132: return null;
133: }
134: }
135:
136: );
137: String res = TestUtilities.copyFileToString(testFile);
138: assertEquals(golden, res);
139: }
140:
141: /**
142: * Adding of fields
143: */
144: public void testAddFieldEmpty() throws Exception {
145: testFile = new File(getWorkDir(), "Test.java");
146: TestUtilities.copyStringToFile(testFile,
147: "package com.max.test.alfa;\n\n"
148: + "public class Test {\n" + "}\n");
149: String golden = "package com.max.test.alfa;\n\n"
150: + "public class Test {\n"
151: + " private double value;\n" + "}\n";
152:
153: process(new Transformer<Void, Object>() {
154: @Override
155: public Void visitClass(ClassTree node, Object p) {
156: super .visitClass(node, p);
157: if ("Test".contentEquals(node.getSimpleName())) {
158: ModifiersTree modTree = make.Modifiers(EnumSet
159: .of(Modifier.PRIVATE));
160: VariableTree member = make.Variable(make.Modifiers(
161: modTree, Collections
162: .<AnnotationTree> emptyList()),
163: "value", //name
164: make.PrimitiveType(TypeKind.DOUBLE), null);
165:
166: ClassTree copy = make.addClassMember(node, member);
167: this .copy.rewrite(node, copy);
168: }
169: return null;
170: }
171: }
172:
173: );
174: String res = TestUtilities.copyFileToString(testFile);
175: assertEquals(golden, res);
176: }
177:
178: String getGoldenPckg() {
179: return "";
180: }
181:
182: String getSourcePckg() {
183: return "";
184: }
185:
186: /**
187: * @param args the command line arguments
188: */
189: public static void main(String[] args) {
190: TestRunner.run(suite());
191: }
192:
193: }
|