1 19 20 package org.netbeans.spi.java.project.classpath.support; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.List ; 27 import org.netbeans.api.java.classpath.ClassPath; 28 import org.netbeans.junit.NbTestCase; 29 import org.netbeans.spi.java.classpath.ClassPathFactory; 30 import org.netbeans.spi.java.classpath.ClassPathImplementation; 31 import org.netbeans.spi.project.support.ant.AntBasedTestUtil; 32 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 33 import org.openide.filesystems.FileObject; 34 import org.netbeans.api.project.TestUtil; 35 import org.netbeans.spi.project.support.ant.AntProjectHelper; 36 import org.netbeans.spi.project.support.ant.EditableProperties; 37 import org.netbeans.spi.project.support.ant.ProjectGenerator; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.util.Lookup; 40 41 45 public class ProjectClassPathImplementationTest extends NbTestCase { 46 47 private static final String PROP_NAME_1 = "classpath1"; private static final String PROP_NAME_2 = "classpath2"; 50 public ProjectClassPathImplementationTest(String testName) { 51 super(testName); 52 } 53 54 private FileObject scratch; 55 private FileObject projdir; 56 private FileObject[] cpRoots1; 57 private FileObject[] cpRoots2; 58 private AntProjectHelper helper; 59 private PropertyEvaluator evaluator; 60 61 protected void setUp() throws Exception { 62 super.setUp(); 63 TestUtil.setLookup(new Object [] { 64 new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation(), 65 AntBasedTestUtil.testAntBasedProjectType(), 66 }); 67 } 68 69 protected void tearDown() throws Exception { 70 scratch = null; 71 projdir = null; 72 cpRoots1 = null; 73 cpRoots2 = null; 74 helper = null; 75 evaluator = null; 76 TestUtil.setLookup(Lookup.EMPTY); 77 super.tearDown(); 78 } 79 80 81 private void prepareProject () throws IOException { 82 scratch = TestUtil.makeScratchDir(this); 83 projdir = scratch.createFolder("proj"); cpRoots1 = new FileObject[2]; 85 cpRoots1[0] = scratch.createFolder("cpRoot1"); cpRoots1[1] = scratch.createFolder("cpRoot2"); cpRoots2 = new FileObject[2]; 88 cpRoots2[0] = scratch.createFolder("cpRoot3"); cpRoots2[1] = scratch.createFolder("cpRoot4"); helper = ProjectGenerator.createProject(projdir, "test"); evaluator = helper.getStandardPropertyEvaluator(); 92 setClassPath(new String [] {PROP_NAME_1, PROP_NAME_2}, new FileObject[][] {cpRoots1, cpRoots2}); 93 } 94 95 public void testBootClassPathImplementation () throws Exception { 96 prepareProject(); 97 ClassPathImplementation cpImpl = ProjectClassPathSupport.createPropertyBasedClassPathImplementation( 98 FileUtil.toFile(helper.getProjectDirectory()), evaluator, new String [] {PROP_NAME_1, PROP_NAME_2}); 99 ClassPath cp = ClassPathFactory.createClassPath(cpImpl); 100 FileObject[] fo = cp.getRoots(); 101 List <FileObject> expected = new ArrayList <FileObject>(); 102 expected.addAll(Arrays.asList(cpRoots1)); 103 expected.addAll(Arrays.asList(cpRoots2)); 104 assertEquals ("Wrong ClassPath roots",expected, Arrays.asList(fo)); cpRoots1 = new FileObject[] {cpRoots1[0]}; 106 setClassPath(new String [] {PROP_NAME_1}, new FileObject[][]{cpRoots1}); 107 fo = cp.getRoots(); 108 expected = new ArrayList <FileObject>(); 109 expected.addAll(Arrays.asList(cpRoots1)); 110 expected.addAll(Arrays.asList(cpRoots2)); 111 assertEquals ("Wrong ClassPath roots",expected, Arrays.asList(fo)); cpRoots2 = new FileObject[] {cpRoots2[0]}; 113 setClassPath(new String [] {PROP_NAME_2}, new FileObject[][]{cpRoots2}); 114 fo = cp.getRoots(); 115 expected = new ArrayList <FileObject>(); 116 expected.addAll(Arrays.asList(cpRoots1)); 117 expected.addAll(Arrays.asList(cpRoots2)); 118 assertEquals ("Wrong ClassPath roots",expected, Arrays.asList(fo)); } 120 121 123 private void setClassPath (String [] propNames, FileObject[][] cpRoots) { 124 EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 125 for (int i=0; i< propNames.length; i++) { 126 props.setProperty (propNames[i],toPath(cpRoots[i])); 127 } 128 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 129 } 130 131 132 private static String toPath (FileObject[] cpRoots) { 133 StringBuffer result = new StringBuffer (); 134 for (int i=0; i<cpRoots.length; i++) { 135 if (i>0) { 136 result.append(':'); } 138 File f = FileUtil.toFile (cpRoots[i]); 139 result.append (f.getAbsolutePath()); 140 } 141 return result.toString(); 142 } 143 144 } 145 | Popular Tags |