1 19 20 package org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator; 21 22 import org.netbeans.modules.j2ee.persistence.action.*; 23 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategySupport; 24 import com.sun.source.tree.ClassTree; 25 import com.sun.source.tree.CompilationUnitTree; 26 import com.sun.source.tree.Tree; 27 import java.io.File ; 28 import javax.lang.model.element.Element; 29 import javax.lang.model.element.TypeElement; 30 import org.netbeans.api.java.source.CancellableTask; 31 import org.netbeans.api.java.source.JavaSource; 32 import org.netbeans.api.java.source.JavaSource.Phase; 33 import org.netbeans.api.java.source.TreeMaker; 34 import org.netbeans.api.java.source.WorkingCopy; 35 import org.netbeans.jackpot.test.TestUtilities; 36 import org.netbeans.modules.j2ee.persistence.util.AbstractTask; 37 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategy; 38 import org.openide.filesystems.FileUtil; 39 40 45 public class EntityManagerGenerationStrategySupportTest extends EntityManagerGenerationTestSupport{ 46 47 public EntityManagerGenerationStrategySupportTest(String testName) { 48 super(testName); 49 } 50 51 public void testGetAnnotationOnClass() throws Exception { 52 53 final String annotation = "java.lang.Deprecated"; 55 File testFile = new File (getWorkDir(), "Test.java"); 56 57 TestUtilities.copyStringToFile(testFile, 58 "package org.netbeans.test;\n\n" + 59 "import java.util.*;\n\n" + 60 "@" + annotation + "\n" + 61 "public class Test {\n" + 62 "}" 63 ); 64 65 searchAnnotation(testFile, annotation, true); 66 } 67 68 public void testGetAnnotationOnField() throws Exception { 69 70 final String annotation = "java.lang.Deprecated"; 72 File testFile = new File (getWorkDir(), "Test.java"); 73 74 TestUtilities.copyStringToFile(testFile, 75 "package org.netbeans.test;\n\n" + 76 "import java.util.*;\n\n" + 77 "public class Test {\n" + 78 "@" + annotation + "\n" + 79 "Object myField;\n" + 80 "}" 81 ); 82 83 searchAnnotation(testFile, annotation, true); 84 85 } 86 87 public void testGetAnnotationOnMethod() throws Exception { 88 89 final String annotation = "java.lang.Deprecated"; 91 File testFile = new File (getWorkDir(), "Test.java"); 92 93 TestUtilities.copyStringToFile(testFile, 94 "package org.netbeans.test;\n\n" + 95 "import java.util.*;\n\n" + 96 "public class Test {\n" + 97 "@" + annotation + "\n" + 98 "Object method(){\n" + 99 "return null;\n" + 100 "}\n" + 101 "}" 102 ); 103 104 searchAnnotation(testFile, annotation, true); 105 106 } 107 108 109 public void testGetField() throws Exception { 110 111 final String field = "java.lang.String"; 113 File testFile = new File (getWorkDir(), "Test.java"); 114 115 TestUtilities.copyStringToFile(testFile, 116 "package org.netbeans.test;\n\n" + 117 "import java.util.*;\n\n" + 118 "public class Test {\n" + 119 "private " + field + " myField;\n" + 120 "}" 121 ); 122 123 searchField(testFile, field, true); 124 searchField(testFile, "java.lang.Object", false); 126 127 } 128 129 private void searchAnnotation(File testFile, final String annotation, final boolean expectSuccess) throws Exception { 130 JavaSource targetSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 131 132 CancellableTask task = new TaskSupport() { 133 void doAsserts(EntityManagerGenerationStrategySupport strategy) { 134 Element result = strategy.getAnnotation(annotation); 135 if (expectSuccess){ 136 assertNotNull(result); 137 assertTrue(((TypeElement)result).getQualifiedName().contentEquals(annotation)); 138 } else { 139 assertNull(result); 140 } 141 } 142 }; 143 144 targetSource.runModificationTask(task); 145 } 146 147 private void searchField(File testFile, final String field, final boolean expectSuccess) throws Exception { 148 JavaSource targetSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 149 150 CancellableTask task = new TaskSupport() { 151 void doAsserts(EntityManagerGenerationStrategySupport strategy) { 152 Element result = strategy.getField(field); 153 if (expectSuccess){ 154 assertNotNull(result); 155 assertTrue(((TypeElement)result).getQualifiedName().contentEquals(field)); 156 } else { 157 assertNull(result); 158 } 159 } 160 }; 161 162 targetSource.runModificationTask(task); 163 } 164 165 private abstract class TaskSupport extends AbstractTask<WorkingCopy> { 167 168 public void run(WorkingCopy workingCopy) throws Exception { 169 170 workingCopy.toPhase(Phase.RESOLVED); 171 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 172 TreeMaker make = workingCopy.getTreeMaker(); 173 174 for (Tree typeDeclaration : cut.getTypeDecls()){ 175 if (Tree.Kind.CLASS == typeDeclaration.getKind()){ 176 ClassTree clazz = (ClassTree) typeDeclaration; 177 EntityManagerGenerationStrategySupport strategy = 178 (EntityManagerGenerationStrategySupport) getStrategy(workingCopy, make, clazz, new GenerationOptions()); 179 doAsserts(strategy); 180 } else { 181 fail("No class found"); } 183 } 184 } 185 186 abstract void doAsserts(EntityManagerGenerationStrategySupport strategy); 187 188 } 189 190 public static class StubEntityManagerGenerationStrategy extends EntityManagerGenerationStrategySupport{ 191 192 public ClassTree generate() { 193 return null; 194 } 195 196 } 197 198 protected Class <? extends EntityManagerGenerationStrategy> getStrategyClass() { 199 return StubEntityManagerGenerationStrategy.class; 200 } 201 } 202 | Popular Tags |