KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > xml > AntProjectSupportTest


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.apache.tools.ant.module.xml;
21
22 import java.io.File JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import javax.swing.event.ChangeEvent JavaDoc;
25 import javax.swing.event.ChangeListener JavaDoc;
26 import org.apache.tools.ant.module.api.AntProjectCookie;
27 import org.apache.tools.ant.module.loader.AntProjectDataLoader;
28 import org.apache.tools.ant.module.loader.AntProjectDataObject;
29 import org.netbeans.junit.MockServices;
30 import org.netbeans.junit.NbTestCase;
31 import org.openide.filesystems.FileLock;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.loaders.DataObject;
35 import org.w3c.dom.Document JavaDoc;
36
37 // XXX testBasicParsing
38
// XXX testMinimumChangesFired
39

40 /**
41  * Test {@link AntProjectSupport} parsing functionality.
42  * @author Jesse Glick
43  */

44 public class AntProjectSupportTest extends NbTestCase {
45     
46     public AntProjectSupportTest(String JavaDoc name) {
47         super(name);
48     }
49     
50     private FileObject scratch;
51     
52     @Override JavaDoc
53     protected void setUp() throws Exception JavaDoc {
54         super.setUp();
55         clearWorkDir();
56         File JavaDoc scratchF = getWorkDir();
57         scratch = FileUtil.toFileObject(scratchF);
58         assertNotNull("FO for " + scratchF, scratch);
59         MockServices.setServices(AntProjectDataLoader.class);
60     }
61     
62     public void testInitiallyInvalidScript() throws Exception JavaDoc {
63         FileObject fo = scratch.createData("build.xml");
64         assertEquals("it is an APDO", AntProjectDataObject.class, DataObject.find(fo).getClass());
65         AntProjectCookie apc = new AntProjectSupport(fo);
66         TestCL l = new TestCL();
67         apc.addChangeListener(l);
68         assertNull("invalid", apc.getDocument());
69         assertNotNull("invalid", apc.getParseException());
70         FileLock lock = fo.lock();
71         try {
72             OutputStream JavaDoc os = fo.getOutputStream(lock);
73             try {
74                 os.write("<project default='x'><target name='x'/></project>".getBytes("UTF-8"));
75             } finally {
76                 os.close();
77             }
78         } finally {
79             lock.releaseLock();
80         }
81         assertTrue("got a change", l.expect(5000));
82         Thread.sleep(1000); // XXX why??
83
assertEquals("now valid (no exc)", null, apc.getParseException());
84         Document JavaDoc doc = apc.getDocument();
85         assertNotNull("now valid (have doc)", doc);
86         assertEquals("one target", 1, doc.getElementsByTagName("target").getLength());
87     }
88
89     /**
90      * Change listener that can be polled.
91      * Handles asynchronous changes.
92      */

93     private static final class TestCL implements ChangeListener JavaDoc {
94         
95         private boolean fired;
96         
97         public TestCL() {}
98         
99         public synchronized void stateChanged(ChangeEvent JavaDoc e) {
100             fired = true;
101             notify();
102         }
103         
104         /**
105          * Check whether a change has occurred by now (do not block).
106          * Also resets the flag so the next call will expect a new change.
107          * @return true if a change has occurred
108          */

109         public synchronized boolean expect() {
110             boolean f = fired;
111             fired = false;
112             return f;
113         }
114         
115         /**
116          * Check whether a change has occurred by now or occurs within some time.
117          * Also resets the flag so the next call will expect a new change.
118          * @param timeout a maximum amount of time to wait, in milliseconds
119          * @return true if a change has occurred
120          */

121         public synchronized boolean expect(long timeout) throws InterruptedException JavaDoc {
122             if (!fired) {
123                 wait(timeout);
124             }
125             return expect();
126         }
127         
128     }
129     
130 }
131
Popular Tags