1 19 20 package org.netbeans.modules.java.freeform; 21 22 import java.io.File ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.List ; 28 import org.netbeans.modules.ant.freeform.spi.support.Util; 29 import org.netbeans.spi.java.queries.MultipleRootsUnitTestForSourceQueryImplementation; 30 import org.netbeans.spi.project.AuxiliaryConfiguration; 31 import org.netbeans.spi.project.support.ant.AntProjectHelper; 32 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileStateInvalidException; 35 import org.openide.filesystems.FileUtil; 36 import org.w3c.dom.Element ; 37 38 48 final class TestQuery implements MultipleRootsUnitTestForSourceQueryImplementation { 49 50 private final AntProjectHelper helper; 51 private final PropertyEvaluator eval; 52 private final AuxiliaryConfiguration aux; 53 54 public TestQuery(AntProjectHelper helper, PropertyEvaluator eval, AuxiliaryConfiguration aux) { 55 this.helper = helper; 56 this.eval = eval; 57 this.aux = aux; 58 } 59 60 public URL [] findUnitTests(FileObject source) { 61 URL [][] data = findSourcesAndTests(); 62 URL sourceURL; 63 try { 64 sourceURL = source.getURL(); 65 } catch (FileStateInvalidException e) { 66 return null; 67 } 68 if (Arrays.asList(data[0]).contains(sourceURL)) { 69 return data[1]; 70 } else { 71 return null; 72 } 73 } 74 75 public URL [] findSources(FileObject unitTest) { 76 URL [][] data = findSourcesAndTests(); 77 URL testURL; 78 try { 79 testURL = unitTest.getURL(); 80 } catch (FileStateInvalidException e) { 81 return null; 82 } 83 if (Arrays.asList(data[1]).contains(testURL)) { 84 return data[0]; 85 } else { 86 return null; 87 } 88 } 89 90 94 private URL [][] findSourcesAndTests() { 95 List <URL > sources = new ArrayList <URL >(); 96 List <URL > tests = new ArrayList <URL >(); 97 Element data = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true); 98 if (data != null) { 99 for (Element cu : Util.findSubElements(data)) { 100 assert cu.getLocalName().equals("compilation-unit") : cu; 101 boolean isTests = Util.findElement(cu, "unit-tests", JavaProjectNature.NS_JAVA_2) != null; for (Element pr : Util.findSubElements(cu)) { 103 if (pr.getLocalName().equals("package-root")) { String rawtext = Util.findText(pr); 105 assert rawtext != null; 106 String evaltext = eval.evaluate(rawtext); 107 if (evaltext != null) { 108 (isTests ? tests : sources).add(evalTextToURL(evaltext)); 109 } 110 } 111 } 112 } 113 } 114 return new URL [][] { 115 sources.toArray(new URL [sources.size()]), 116 tests.toArray(new URL [tests.size()]), 117 }; 118 } 119 120 private URL evalTextToURL(String evaltext) { 121 File location = helper.resolveFile(evaltext); 122 URL u; 123 try { 124 u = location.toURI().toURL(); 125 } catch (MalformedURLException e) { 126 throw new AssertionError (e); 127 } 128 if (FileUtil.isArchiveFile(u)) { 129 return FileUtil.getArchiveRoot(u); 130 } else { 131 String us = u.toExternalForm(); 132 if (us.endsWith("/")) { 133 return u; 134 } else { 135 try { 136 return new URL (us + '/'); 137 } catch (MalformedURLException e) { 138 throw new AssertionError (e); 139 } 140 } 141 } 142 } 143 144 } 145 | Popular Tags |