1 19 20 package org.netbeans.api.java.source.gen; 21 22 import com.sun.source.tree.*; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.List ; 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 37 public class ClashingImportsTest extends GeneratorTest { 38 39 40 public ClashingImportsTest(String 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 { 50 super.setUp(); 51 } 52 53 public void testAddImport() throws IOException { 54 testFile = getFile(getSourceDir(), getSourcePckg() + "ClashingImports.java"); 55 process( 56 new Transformer<Void , Object >() { 57 public Void visitMethod(MethodTree node, Object p) { 58 if ("<init>".contentEquals(node.getName())) { 59 BlockTree body = node.getBody(); 60 List <StatementTree> stats = new ArrayList <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 { 76 testFile = getFile(getSourceDir(), getSourcePckg() + "ClashingImports2.java"); 77 process( 78 new Transformer<Void , Object >() { 79 public Void visitMethod(MethodTree node, Object p) { 80 if ("<init>".contentEquals(node.getName())) { 81 BlockTree body = node.getBody(); 82 List <StatementTree> stats = new ArrayList <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 { 98 testFile = getFile(getSourceDir(), getSourcePckg() + "ClashingImports3.java"); 99 process( 100 new Transformer<Void , Object >() { 101 public Void visitMethod(MethodTree node, Object p) { 102 if ("<init>".contentEquals(node.getName())) { 103 BlockTree body = node.getBody(); 104 List <StatementTree> stats = new ArrayList <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 getSourcePckg() { 120 return "org/netbeans/test/codegen/"; 121 } 122 123 String getGoldenPckg() { 124 return "org/netbeans/jmi/javamodel/codegen/ClashingImportsTest/"; 125 } 126 127 void assertFiles(final String aGoldenFile) throws IOException , 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 |