001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.api.project.ant;
043:
044: import org.netbeans.spi.project.support.ant.*;
045: import org.netbeans.spi.project.AuxiliaryConfiguration;
046: import org.netbeans.spi.project.ant.AntBuildExtenderImplementation;
047: import java.util.Collections;
048: import java.util.List;
049: import org.netbeans.api.project.Project;
050: import org.netbeans.api.project.ProjectManager;
051: import org.netbeans.api.project.TestUtil;
052: import org.netbeans.junit.NbTestCase;
053: import org.netbeans.modules.project.ant.AntBuildExtenderAccessor;
054: import org.openide.filesystems.FileObject;
055: import org.w3c.dom.Element;
056: import org.w3c.dom.NodeList;
057:
058: /**
059: * Test functionality of AntBuildExtender.
060: * @author mkleint
061: */
062: public class AntBuildExtenderTest extends NbTestCase {
063:
064: public AntBuildExtenderTest(String name) {
065: super (name);
066: }
067:
068: private FileObject scratch;
069: private FileObject projdir;
070: private FileObject extension1;
071: private ProjectManager pm;
072: private Project p;
073: private AntProjectHelper h;
074: private GeneratedFilesHelper gfh;
075: private ExtImpl extenderImpl;
076:
077: protected void setUp() throws Exception {
078: super .setUp();
079: scratch = TestUtil.makeScratchDir(this );
080: projdir = scratch.createFolder("proj");
081: TestUtil.createFileFromContent(GeneratedFilesHelperTest.class
082: .getResource("data/project.xml"), projdir,
083: "nbproject/project.xml");
084: extension1 = TestUtil.createFileFromContent(
085: GeneratedFilesHelperTest.class
086: .getResource("data/extension1.xml"), projdir,
087: "nbproject/extension1.xml");
088: extenderImpl = new ExtImpl();
089: TestUtil.setLookup(new Object[] { AntBasedTestUtil
090: .testAntBasedProjectType(extenderImpl), });
091: pm = ProjectManager.getDefault();
092: p = pm.findProject(projdir);
093: extenderImpl.project = p;
094: h = p.getLookup().lookup(AntProjectHelper.class);
095: gfh = p.getLookup().lookup(GeneratedFilesHelper.class);
096: assertNotNull(gfh);
097: }
098:
099: public void testGetExtendableTargets() {
100: AntBuildExtender instance = p.getLookup().lookup(
101: AntBuildExtender.class);
102:
103: List<String> result = instance.getExtensibleTargets();
104:
105: assertEquals(1, result.size());
106: assertEquals("all", result.get(0));
107: }
108:
109: public void testAddExtension() {
110: AntBuildExtender instance = p.getLookup().lookup(
111: AntBuildExtender.class);
112: instance.addExtension("milos", extension1);
113: Element el = p.getLookup().lookup(AuxiliaryConfiguration.class)
114: .getConfigurationFragment(
115: AntBuildExtenderAccessor.ELEMENT_ROOT,
116: AntBuildExtenderAccessor.AUX_NAMESPACE, true);
117: assertNotNull(el);
118: NodeList nl = el
119: .getElementsByTagName(AntBuildExtenderAccessor.ELEMENT_EXTENSION);
120: assertEquals(1, nl.getLength());
121: Element extens = (Element) nl.item(0);
122: assertEquals("milos", extens
123: .getAttribute(AntBuildExtenderAccessor.ATTR_ID));
124: assertEquals("extension1.xml", extens
125: .getAttribute(AntBuildExtenderAccessor.ATTR_FILE));
126: }
127:
128: public void testRemoveExtension() {
129: AntBuildExtender instance = p.getLookup().lookup(
130: AntBuildExtender.class);
131: testAddExtension();
132: extenderImpl.oldElement = p.getLookup().lookup(
133: AuxiliaryConfiguration.class).getConfigurationFragment(
134: AntBuildExtenderAccessor.ELEMENT_ROOT,
135: AntBuildExtenderAccessor.AUX_NAMESPACE, true);
136: instance.removeExtension("milos");
137: Element el = p.getLookup().lookup(AuxiliaryConfiguration.class)
138: .getConfigurationFragment(
139: AntBuildExtenderAccessor.ELEMENT_ROOT,
140: AntBuildExtenderAccessor.AUX_NAMESPACE, true);
141:
142: assertNotNull(el);
143: NodeList nl = el
144: .getElementsByTagName(AntBuildExtenderAccessor.ELEMENT_EXTENSION);
145: assertEquals(0, nl.getLength());
146: }
147:
148: public void testGetExtension() {
149: AntBuildExtender instance = p.getLookup().lookup(
150: AntBuildExtender.class);
151: testAddExtension();
152: AntBuildExtender.Extension ext = instance.getExtension("milos");
153: assertNotNull(ext);
154: }
155:
156: private class ExtImpl implements AntBuildExtenderImplementation {
157: Project project;
158: Element newElement;
159: Element oldElement;
160: List<String> targets = Collections.singletonList("all");
161:
162: public List<String> getExtensibleTargets() {
163: return targets;
164: }
165:
166: public void updateBuildExtensionMetadata(Element element) {
167: newElement = element;
168: }
169:
170: public Element getBuildExtensionMetadata() {
171: return oldElement;
172: }
173:
174: public Project getOwningProject() {
175: return project;
176: }
177:
178: }
179:
180: }
|