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.apache.tools.ant.module.xml;
043:
044: import java.io.File;
045: import java.io.OutputStream;
046: import javax.swing.event.ChangeEvent;
047: import javax.swing.event.ChangeListener;
048: import org.apache.tools.ant.module.api.AntProjectCookie;
049: import org.apache.tools.ant.module.loader.AntProjectDataLoader;
050: import org.apache.tools.ant.module.loader.AntProjectDataObject;
051: import org.netbeans.junit.MockServices;
052: import org.netbeans.junit.NbTestCase;
053: import org.openide.filesystems.FileLock;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.loaders.DataObject;
057: import org.w3c.dom.Document;
058:
059: // XXX testBasicParsing
060: // XXX testMinimumChangesFired
061:
062: /**
063: * Test {@link AntProjectSupport} parsing functionality.
064: * @author Jesse Glick
065: */
066: public class AntProjectSupportTest extends NbTestCase {
067:
068: public AntProjectSupportTest(String name) {
069: super (name);
070: }
071:
072: private FileObject scratch;
073:
074: @Override
075: protected void setUp() throws Exception {
076: super .setUp();
077: clearWorkDir();
078: File scratchF = getWorkDir();
079: scratch = FileUtil.toFileObject(scratchF);
080: assertNotNull("FO for " + scratchF, scratch);
081: MockServices.setServices(AntProjectDataLoader.class);
082: }
083:
084: public void testInitiallyInvalidScript() throws Exception {
085: FileObject fo = scratch.createData("build.xml");
086: assertEquals("it is an APDO", AntProjectDataObject.class,
087: DataObject.find(fo).getClass());
088: AntProjectCookie apc = new AntProjectSupport(fo);
089: TestCL l = new TestCL();
090: apc.addChangeListener(l);
091: assertNull("invalid", apc.getDocument());
092: assertNotNull("invalid", apc.getParseException());
093: FileLock lock = fo.lock();
094: try {
095: OutputStream os = fo.getOutputStream(lock);
096: try {
097: os
098: .write("<project default='x'><target name='x'/></project>"
099: .getBytes("UTF-8"));
100: } finally {
101: os.close();
102: }
103: } finally {
104: lock.releaseLock();
105: }
106: assertTrue("got a change", l.expect(5000));
107: Thread.sleep(1000); // XXX why??
108: assertEquals("now valid (no exc)", null, apc
109: .getParseException());
110: Document doc = apc.getDocument();
111: assertNotNull("now valid (have doc)", doc);
112: assertEquals("one target", 1, doc
113: .getElementsByTagName("target").getLength());
114: }
115:
116: /**
117: * Change listener that can be polled.
118: * Handles asynchronous changes.
119: */
120: private static final class TestCL implements ChangeListener {
121:
122: private boolean fired;
123:
124: public TestCL() {
125: }
126:
127: public synchronized void stateChanged(ChangeEvent e) {
128: fired = true;
129: notify();
130: }
131:
132: /**
133: * Check whether a change has occurred by now (do not block).
134: * Also resets the flag so the next call will expect a new change.
135: * @return true if a change has occurred
136: */
137: public synchronized boolean expect() {
138: boolean f = fired;
139: fired = false;
140: return f;
141: }
142:
143: /**
144: * Check whether a change has occurred by now or occurs within some time.
145: * Also resets the flag so the next call will expect a new change.
146: * @param timeout a maximum amount of time to wait, in milliseconds
147: * @return true if a change has occurred
148: */
149: public synchronized boolean expect(long timeout)
150: throws InterruptedException {
151: if (!fired) {
152: wait(timeout);
153: }
154: return expect();
155: }
156:
157: }
158:
159: }
|