KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > gen > ClashingImportsTest


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.java.source.gen;
21
22 import com.sun.source.tree.*;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.lang.model.element.Modifier;
28 import javax.lang.model.element.TypeElement;
29 import org.netbeans.api.java.source.transform.Transformer;
30 import org.netbeans.junit.NbTestSuite;
31 import org.openide.filesystems.FileStateInvalidException;
32
33 /**
34  *
35  * @author Dusan Balek
36  */

37 public class ClashingImportsTest extends GeneratorTest {
38
39     /** Creates a new instance of ClashingImportsTest */
40     public ClashingImportsTest(String JavaDoc name) {
41         super(name);
42     }
43     
44     public static NbTestSuite suite() {
45         NbTestSuite suite = new NbTestSuite(ClashingImportsTest.class);
46         return suite;
47     }
48     
49     protected void setUp() throws Exception JavaDoc {
50         super.setUp();
51     }
52
53     public void testAddImport() throws IOException JavaDoc {
54         testFile = getFile(getSourceDir(), getSourcePckg() + "ClashingImports.java");
55         process(
56             new Transformer<Void JavaDoc, Object JavaDoc>() {
57                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
58                     if ("<init>".contentEquals(node.getName())) {
59                         BlockTree body = node.getBody();
60                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
61                         for(StatementTree st : body.getStatements())
62                             stats.add(st);
63                         TypeElement e = env.getElements().getTypeElement("java.awt.List");
64                         ExpressionTree type = make.QualIdent(e);
65                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "awtList", type, null));
66                         changes.rewrite(body, make.Block(stats, false));
67                     }
68                     return null;
69                 }
70             }
71         );
72         assertFiles("testAddImport_ClashingImports.pass");
73     }
74
75     public void testAddClashingImport() throws IOException JavaDoc {
76         testFile = getFile(getSourceDir(), getSourcePckg() + "ClashingImports2.java");
77         process(
78             new Transformer<Void JavaDoc, Object JavaDoc>() {
79                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
80                     if ("<init>".contentEquals(node.getName())) {
81                         BlockTree body = node.getBody();
82                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
83                         for(StatementTree st : body.getStatements())
84                             stats.add(st);
85                         TypeElement e = env.getElements().getTypeElement("java.util.List");
86                         ExpressionTree type = make.QualIdent(e);
87                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "list", type, null));
88                         changes.rewrite(body, make.Block(stats, false));
89                     }
90                     return null;
91                 }
92             }
93         );
94         assertFiles("testAddClashingImport_ClashingImports.pass");
95     }
96
97     public void testAddClashingImport2() throws IOException JavaDoc {
98         testFile = getFile(getSourceDir(), getSourcePckg() + "ClashingImports3.java");
99         process(
100             new Transformer<Void JavaDoc, Object JavaDoc>() {
101                 public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
102                     if ("<init>".contentEquals(node.getName())) {
103                         BlockTree body = node.getBody();
104                         List JavaDoc<StatementTree> stats = new ArrayList JavaDoc<StatementTree>();
105                         for(StatementTree st : body.getStatements())
106                             stats.add(st);
107                         TypeElement e = env.getElements().getTypeElement("java.awt.List");
108                         ExpressionTree type = make.QualIdent(e);
109                         stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "awtList", type, null));
110                         changes.rewrite(body, make.Block(stats, false));
111                     }
112                     return null;
113                 }
114             }
115         );
116         assertFiles("testAddClashingImport2.pass");
117     }
118     
119     String JavaDoc getSourcePckg() {
120         return "org/netbeans/test/codegen/";
121     }
122
123     String JavaDoc getGoldenPckg() {
124         return "org/netbeans/jmi/javamodel/codegen/ClashingImportsTest/";
125     }
126
127     void assertFiles(final String JavaDoc aGoldenFile) throws IOException JavaDoc, FileStateInvalidException {
128         assertFile("File is not correctly generated.",
129                 getTestFile(),
130                 getFile(getGoldenDir(), getGoldenPckg() + aGoldenFile),
131                 getWorkDir(),
132                 new WhitespaceIgnoringDiff()
133                 );
134     }
135     
136 }
137
Popular Tags