KickJava   Java API By Example, From Geeks To Geeks.

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


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.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28 import org.netbeans.api.project.ProjectManager;
29 import org.netbeans.spi.project.support.ant.EditableProperties;
30 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
31 import org.openide.filesystems.FileLock;
32 import org.openide.filesystems.FileObject;
33 import org.openide.util.Mutex;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36
37 /**
38  * Test property evaluation.
39  * @author Jesse Glick
40  */

41 public class FreeformEvaluatorTest extends TestBase {
42     
43     public FreeformEvaluatorTest(String JavaDoc name) {
44         super(name);
45     }
46     
47     public void testPropertyEvaluation() throws Exception JavaDoc {
48         PropertyEvaluator eval = simple.evaluator();
49         assertEquals("right src.dir", "src", eval.getProperty("src.dir"));
50     }
51     
52     public void testPropertyEvaluationChanges() throws Exception JavaDoc {
53         FreeformProject simple2 = copyProject(simple);
54         PropertyEvaluator eval = simple2.evaluator();
55         assertEquals("right src.dir", "src", eval.getProperty("src.dir"));
56         EditableProperties p = new EditableProperties();
57         FileObject buildProperties = simple2.getProjectDirectory().getFileObject("build.properties");
58         assertNotNull("have build.properties", buildProperties);
59         InputStream JavaDoc is = buildProperties.getInputStream();
60         try {
61             p.load(is);
62         } finally {
63             is.close();
64         }
65         assertEquals("right original value", "src", p.getProperty("src.dir"));
66         p.setProperty("src.dir", "somethingnew");
67         TestPCL l = new TestPCL();
68         eval.addPropertyChangeListener(l);
69         final OutputStream JavaDoc os = buildProperties.getOutputStream();
70         try {
71             p.store(os);
72         } finally {
73             // close file under ProjectManager.readAccess so that events are fired synchronously
74
ProjectManager.mutex().readAccess(new Mutex.ExceptionAction<Void JavaDoc>() {
75                 public Void JavaDoc run() throws Exception JavaDoc {
76                     os.close();
77                     return null;
78                 }
79             });
80         }
81         assertEquals("got a change from properties file in src.dir", Collections.singleton("src.dir"), l.changed);
82         l.reset();
83         assertEquals("new value of src.dir", "somethingnew", eval.getProperty("src.dir"));
84     }
85     
86     public void testChangesInPropertyFileLocation() throws Exception JavaDoc {
87         // #48230: if some change in earlier properties causes location of a property file to change, reread it
88
FreeformProject simple2 = copyProject(simple);
89         // Replace <property name="build.properties">build.properties</property>
90
// with <property-file>loc.properties</property-file> so that we can change it
91
// without triggering a project.xml change (which always fires changes, so it is cheating).
92
EditableProperties p = new EditableProperties();
93         p.setProperty("build.properties", "build.properties");
94         FileObject locProperties = simple2.getProjectDirectory().createData("loc.properties");
95         OutputStream JavaDoc os = locProperties.getOutputStream();
96         try {
97             p.store(os);
98         } finally {
99             os.close();
100         }
101         Element JavaDoc data = simple2.getPrimaryConfigurationData();
102         NodeList JavaDoc propertiesNL = data.getElementsByTagNameNS(FreeformProjectType.NS_GENERAL, "properties");
103         assertEquals("one <properties>", 1, propertiesNL.getLength());
104         Element JavaDoc properties = (Element JavaDoc) propertiesNL.item(0);
105         properties.removeChild(properties.getFirstChild());
106         Element JavaDoc propertyFile = properties.getOwnerDocument().createElementNS(FreeformProjectType.NS_GENERAL, "property-file");
107         propertyFile.appendChild(properties.getOwnerDocument().createTextNode("loc.properties"));
108         properties.insertBefore(propertyFile, properties.getFirstChild());
109         simple2.putPrimaryConfigurationData(data);
110         ProjectManager.getDefault().saveProject(simple2);
111         // Now check baseline evaluation.
112
PropertyEvaluator eval = simple2.evaluator();
113         TestPCL l = new TestPCL();
114         eval.addPropertyChangeListener(l);
115         assertEquals("right src.dir", "src", eval.getProperty("src.dir"));
116         // Make a build2.properties with a slight change.
117
p = new EditableProperties();
118         FileObject buildProperties = simple2.getProjectDirectory().getFileObject("build.properties");
119         assertNotNull("have build.properties", buildProperties);
120         InputStream JavaDoc is = buildProperties.getInputStream();
121         try {
122             p.load(is);
123         } finally {
124             is.close();
125         }
126         assertEquals("right original value", "src", p.getProperty("src.dir"));
127         p.setProperty("src.dir", "somethingnew");
128         FileObject buildProperties2 = simple2.getProjectDirectory().createData("build2.properties");
129         os = buildProperties2.getOutputStream();
130         try {
131             p.store(os);
132         } finally {
133             os.close();
134         }
135         assertEquals("No changes fired yet", Collections.EMPTY_SET, l.changed);
136         // Tell loc.properties to point to it.
137
p = new EditableProperties();
138         p.setProperty("build.properties", "build2.properties");
139         locProperties = simple2.getProjectDirectory().getFileObject("loc.properties");
140         os = locProperties.getOutputStream();
141         try {
142             p.store(os);
143         } finally {
144             // close file under ProjectManager.readAccess so that events are fired synchronously
145
final OutputStream JavaDoc _os = os;
146             ProjectManager.mutex().readAccess(new Mutex.ExceptionAction<Void JavaDoc>() {
147                 public Void JavaDoc run() throws Exception JavaDoc {
148                     _os.close();
149                     return null;
150                 }
151             });
152         }
153         // Check that the change took.
154
Set JavaDoc<String JavaDoc> exact = new HashSet JavaDoc<String JavaDoc>(Arrays.asList("src.dir", "build.properties"));
155         // OK to just return null for the property name instead.
156
assertTrue("got a change from properties file in src.dir: " + l.changed, l.changed.contains(null) || l.changed.equals(exact));
157         l.reset();
158         assertEquals("new value of src.dir", "somethingnew", eval.getProperty("src.dir"));
159     }
160     
161 }
162
Popular Tags