KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > ArtifactProviderTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.ant.freeform;
21
22 import java.io.File JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
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 JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32
33 /**
34  * Test {@link ArtifactProvider}.
35  * @author Jesse Glick
36  */

37 public class ArtifactProviderTest extends TestBase {
38     
39     public ArtifactProviderTest(String JavaDoc name) {
40         super(name);
41     }
42     
43     public void testBuildArtifact() throws Exception JavaDoc {
44         File JavaDoc 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         // Could be different instances returned each time, so check each one:
49
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         // ID should be target name if that does not cause a conflict
63
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 JavaDoc {
69         AntProjectHelper helper = simple.helper();
70         List JavaDoc<Export> exports = new ArrayList JavaDoc<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         // one type/target/script produces two outputs -> no AA
94
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         // XXX test ordering perhaps? not critical
112
}
113
114     private static void putExports(AntProjectHelper helper, List JavaDoc<Export> exports) {
115         //assert ProjectManager.mutex().isWriteAccess();
116
Element JavaDoc data = Util.getPrimaryConfigurationData(helper);
117         Document JavaDoc doc = data.getOwnerDocument();
118         for (Element JavaDoc exportEl : Util.findSubElements(data)) {
119             if (!exportEl.getLocalName().equals("export")) { // NOI18N
120
continue;
121             }
122             data.removeChild(exportEl);
123         }
124         for (Export export : exports) {
125             Element JavaDoc exportEl = doc.createElementNS(FreeformProjectType.NS_GENERAL, "export"); // NOI18N
126
Element JavaDoc el;
127             el = doc.createElementNS(FreeformProjectType.NS_GENERAL, "type"); // NOI18N
128
el.appendChild(doc.createTextNode(export.type)); // NOI18N
129
exportEl.appendChild(el);
130             el = doc.createElementNS(FreeformProjectType.NS_GENERAL, "location"); // NOI18N
131
el.appendChild(doc.createTextNode(export.location)); // NOI18N
132
exportEl.appendChild(el);
133             if (export.script != null) {
134                 el = doc.createElementNS(FreeformProjectType.NS_GENERAL, "script"); // NOI18N
135
el.appendChild(doc.createTextNode(export.script)); // NOI18N
136
exportEl.appendChild(el);
137             }
138             el = doc.createElementNS(FreeformProjectType.NS_GENERAL, "build-target"); // NOI18N
139
el.appendChild(doc.createTextNode(export.buildTarget)); // NOI18N
140
exportEl.appendChild(el);
141             if (export.cleanTarget != null) {
142                 el = doc.createElementNS(FreeformProjectType.NS_GENERAL, "clean-target"); // NOI18N
143
el.appendChild(doc.createTextNode(export.cleanTarget)); // NOI18N
144
exportEl.appendChild(el);
145             }
146             Element JavaDoc 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 JavaDoc type;
157         public String JavaDoc location;
158         public String JavaDoc script; // optional
159
public String JavaDoc buildTarget;
160         public String JavaDoc cleanTarget; // optional
161
}
162     
163 }
164
Popular Tags