1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.io.File ; 23 import java.net.URI ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import org.netbeans.api.project.ant.AntArtifact; 27 import org.netbeans.api.project.ant.AntArtifactQuery; 28 import org.netbeans.modules.ant.freeform.spi.support.Util; 29 import org.netbeans.spi.project.support.ant.AntProjectHelper; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Element ; 32 33 37 public class ArtifactProviderTest extends TestBase { 38 39 public ArtifactProviderTest(String name) { 40 super(name); 41 } 42 43 public void testBuildArtifact() throws Exception { 44 File mainJar = simple.helper().resolveFile("build/simple-app.jar"); 45 AntArtifact aa = AntArtifactQuery.findArtifactFromFile(mainJar); 46 assertNotNull("have artifact for " + mainJar, aa); 47 verifyArtifact(aa); 48 aa = AntArtifactQuery.findArtifactByID(simple, "jar"); 50 assertNotNull("found artifact by ID", aa); 51 verifyArtifact(aa); 52 AntArtifact[] aas = AntArtifactQuery.findArtifactsByType(simple, "jar"); 53 assertEquals("found one 'jar' artifact", 1, aas.length); 54 verifyArtifact(aas[0]); 55 } 56 57 private void verifyArtifact(AntArtifact aa) { 58 assertEquals("right project", simple, aa.getProject()); 59 assertEquals("right location", URI.create("build/simple-app.jar"), aa.getArtifactLocations()[0]); 60 assertEquals("right target", "jar", aa.getTargetName()); 61 assertEquals("right clean target", "clean", aa.getCleanTargetName()); 62 assertEquals("right ID", "jar", aa.getID()); 64 assertEquals("right type", "jar", aa.getType()); 65 assertEquals("right script", simple.helper().resolveFile("build.xml"), aa.getScriptLocation()); 66 } 67 68 public void testGetBuildArtifacts() throws Exception { 69 AntProjectHelper helper = simple.helper(); 70 List <Export> exports = new ArrayList <Export>(); 71 Export e = new Export(); 72 e.type = "jar"; 73 e.location = "path/smth.jar"; 74 e.script = "someScript"; 75 e.buildTarget = "build_target"; 76 exports.add(e); 77 putExports(helper, exports); 78 AntArtifact[] aa = AntArtifactQuery.findArtifactsByType(simple, "jar"); 79 assertNotNull("some artifact found", aa); 80 assertEquals("one artifact found", 1, aa.length); 81 82 e = new Export(); 83 e.type = "jar"; 84 e.location = "path/smth.jar"; 85 e.script = "someScript"; 86 e.buildTarget = "build_target2"; 87 exports.add(e); 88 putExports(helper, exports); 89 aa = AntArtifactQuery.findArtifactsByType(simple, "jar"); 90 assertNotNull("some artifact found", aa); 91 assertEquals("two artifacts found", 2, aa.length); 92 93 e = new Export(); 95 e.type = "jar"; 96 e.location = "path/smth2.jar"; 97 e.script = "someScript"; 98 e.buildTarget = "build_target2"; 99 exports.add(e); 100 putExports(helper, exports); 101 aa = AntArtifactQuery.findArtifactsByType(simple, "jar"); 102 assertNotNull("some artifact found", aa); 103 assertEquals("two artifacts found", 2, aa.length); 104 105 exports.remove(0); 106 putExports(helper, exports); 107 aa = AntArtifactQuery.findArtifactsByType(simple, "jar"); 108 assertNotNull("some artifact found", aa); 109 assertEquals("one artifact found", 1, aa.length); 110 assertEquals("the artifact has two locations", 2, aa[0].getArtifactLocations().length); 111 } 113 114 private static void putExports(AntProjectHelper helper, List <Export> exports) { 115 Element data = Util.getPrimaryConfigurationData(helper); 117 Document doc = data.getOwnerDocument(); 118 for (Element exportEl : Util.findSubElements(data)) { 119 if (!exportEl.getLocalName().equals("export")) { continue; 121 } 122 data.removeChild(exportEl); 123 } 124 for (Export export : exports) { 125 Element exportEl = doc.createElementNS(FreeformProjectType.NS_GENERAL, "export"); Element el; 127 el = doc.createElementNS(FreeformProjectType.NS_GENERAL, "type"); el.appendChild(doc.createTextNode(export.type)); exportEl.appendChild(el); 130 el = doc.createElementNS(FreeformProjectType.NS_GENERAL, "location"); el.appendChild(doc.createTextNode(export.location)); exportEl.appendChild(el); 133 if (export.script != null) { 134 el = doc.createElementNS(FreeformProjectType.NS_GENERAL, "script"); el.appendChild(doc.createTextNode(export.script)); exportEl.appendChild(el); 137 } 138 el = doc.createElementNS(FreeformProjectType.NS_GENERAL, "build-target"); el.appendChild(doc.createTextNode(export.buildTarget)); exportEl.appendChild(el); 141 if (export.cleanTarget != null) { 142 el = doc.createElementNS(FreeformProjectType.NS_GENERAL, "clean-target"); el.appendChild(doc.createTextNode(export.cleanTarget)); exportEl.appendChild(el); 145 } 146 Element later = Util.findElement(data, "view", FreeformProjectType.NS_GENERAL); 147 if (later == null) { 148 later = Util.findElement(data, "subprojects", FreeformProjectType.NS_GENERAL); 149 } 150 data.insertBefore(exportEl, later); 151 } 152 Util.putPrimaryConfigurationData(helper, data); 153 } 154 155 private static final class Export { 156 public String type; 157 public String location; 158 public String script; public String buildTarget; 160 public String cleanTarget; } 162 163 } 164 | Popular Tags |