1 19 20 package org.netbeans.modules.j2ee.ejbcore.test; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import org.netbeans.junit.NbTestCase; 25 import org.netbeans.modules.j2ee.api.ejbjar.EjbProjectConstants; 26 import org.netbeans.modules.java.source.usages.IndexUtil; 27 import org.openide.filesystems.FileObject; 28 import org.openide.filesystems.FileUtil; 29 import org.openide.filesystems.Repository; 30 import org.openide.util.Lookup; 31 import org.openide.util.lookup.Lookups; 32 import org.openide.util.lookup.ProxyLookup; 33 34 40 public class TestBase extends NbTestCase { 41 42 private EjbJarProviderImpl ejbJarProvider; 43 private ClassPathProviderImpl classPathProvider; 44 private FileOwnerQueryImpl fileOwnerQuery; 45 46 protected FileObject dataDir; 47 protected FileObject testFO; 48 49 private TestModule testModuleEJB14; 50 private TestModule testModuleEJB50; 51 52 static { 53 System.setProperty("org.openide.util.Lookup", Lkp.class.getName()); 55 assertEquals("Unable to set the default lookup!", Lkp.class, Lookup.getDefault().getClass()); 56 setLookups(); 57 assertEquals(RepositoryImpl.class, Lookup.getDefault().lookup(Repository.class).getClass()); 58 assertEquals("The default Repository is not our repository!", RepositoryImpl.class, Repository.getDefault().getClass()); 59 } 60 61 public TestBase(String name) { 62 super(name); 63 } 64 65 public TestModule ejb14() { 66 if (testModuleEJB14 == null) { 67 testModuleEJB14 = new TestModule(dataDir.getFileObject("EJBModule_1_4"), EjbProjectConstants.J2EE_14_LEVEL); 68 } 69 activate(testModuleEJB14); 70 return testModuleEJB14; 71 } 72 73 public TestModule ejb50() { 74 if (testModuleEJB50 == null) { 75 testModuleEJB50 = new TestModule(dataDir.getFileObject("EJBModule_5_0"), EjbProjectConstants.JAVA_EE_5_LEVEL); 76 } 77 activate(testModuleEJB50); 78 return testModuleEJB50; 79 } 80 81 protected void setUp() throws IOException { 82 clearWorkDir(); 83 File file = new File (getWorkDir(),"cache"); file.mkdirs(); 85 IndexUtil.setCacheFolder(file); 86 ejbJarProvider = new EjbJarProviderImpl(); 87 classPathProvider = new ClassPathProviderImpl(); 88 fileOwnerQuery = new FileOwnerQueryImpl(); 89 setLookups( 90 ejbJarProvider, 91 classPathProvider, 92 fileOwnerQuery, 93 new FakeJavaDataLoaderPool() 94 ); 95 dataDir = FileUtil.toFileObject(getDataDir()); 96 FileObject workDir = FileUtil.toFileObject(getWorkDir()); 97 testFO = workDir.createData("TestClass.java"); 98 } 99 100 public static void setLookups(Object ... lookups) { 101 ((Lkp)Lookup.getDefault()).setProxyLookups(Lookups.fixed(lookups)); 102 } 103 104 public static final class Lkp extends ProxyLookup { 105 106 private final Repository repository = new RepositoryImpl(); 107 108 public Lkp() { 109 setProxyLookups(new Lookup[0]); 110 } 111 112 private void setProxyLookups(Lookup... lookups) { 113 Lookup[] allLookups = new Lookup[lookups.length + 3]; 114 ClassLoader classLoader = TestBase.class.getClassLoader(); 115 allLookups[0] = Lookups.singleton(classLoader); 116 allLookups[1] = Lookups.singleton(repository); 117 System.arraycopy(lookups, 0, allLookups, 2, lookups.length); 118 allLookups[allLookups.length - 1] = Lookups.metaInfServices(classLoader); 119 setLookups(allLookups); 120 } 121 122 } 123 124 private void activate(TestModule testModule) { 125 fileOwnerQuery.setProject(testModule.project); 126 ejbJarProvider.setEjbModule(testModule.javaeeLevel, testModule.deploymentDescriptor, testModule.sources); 127 classPathProvider.setClassPath(testModule.sources); 128 } 129 130 protected static class TestModule { 131 132 private final String javaeeLevel; 133 private final FileObject deploymentDescriptor; 134 private final FileObject[] sources; 135 private final ProjectImpl project; 136 137 public TestModule(FileObject projectDir, String javaeeLevel) { 138 this.javaeeLevel = javaeeLevel; 139 this.deploymentDescriptor = projectDir.getFileObject("src/conf/ejb-jar.xml"); 140 this.sources = new FileObject[] {projectDir.getFileObject("src/java")}; 141 this.project = new ProjectImpl("2.1"); 142 project.setProjectDirectory(projectDir); 143 } 144 145 public FileObject getDeploymentDescriptor() { return deploymentDescriptor; } 146 public FileObject[] getSources() { return sources; } 147 148 } 149 150 } 151 | Popular Tags |