package org.eclipse.emf.test.tools.merger.ast; import java.util.Hashtable; import junit.framework.TestCase; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.ImportDeclaration; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.Document; import org.eclipse.text.edits.MalformedTreeException; import org.eclipse.text.edits.TextEdit; /** * Tests setting / clearing static flags of import declarations. */ public class ASTImportStaticFlagTest extends TestCase { protected String source = "package org.eclipse;\n" + "import static org.eclipse.ClassA.A;\n" + "import org.eclipse.ClassB.B;\n" + "class X {}"; protected String expectedSource = "package org.eclipse;\n" + "import org.eclipse.ClassA.A;\n" + "import static org.eclipse.ClassB.B;\n" + "class X {}"; protected CompilationUnit cu; @Override protected void setUp() throws Exception { adjustCompilerOptions(); cu = parse(source); } public void testChangeImportStaticFlags() throws MalformedTreeException, BadLocationException { ASTRewrite rewriter = ASTRewrite.create(cu.getAST()); ImportDeclaration importDeclaration1 = (ImportDeclaration)cu.imports().get(0); ImportDeclaration importDeclaration2 = (ImportDeclaration)cu.imports().get(1); assertTrue(Boolean.TRUE.equals(rewriter.get(importDeclaration1, ImportDeclaration.STATIC_PROPERTY))); assertTrue(Boolean.FALSE.equals(rewriter.get(importDeclaration2, ImportDeclaration.STATIC_PROPERTY))); rewriter.set(importDeclaration1, ImportDeclaration.STATIC_PROPERTY, false, null); rewriter.set(importDeclaration2, ImportDeclaration.STATIC_PROPERTY, true, null); Document doc = new Document(source); TextEdit edit = rewriter.rewriteAST(doc, null); edit.apply(doc); assertEquals(expectedSource, doc.get()); } protected CompilationUnit parse(String source) { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(source.toCharArray()); return (CompilationUnit) parser.createAST(null); } protected void adjustCompilerOptions() { Hashtable options = JavaCore.getOptions(); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5); JavaCore.setOptions(options); } }