KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > J2SEConfigurationProviderTest


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.java.j2seproject;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Locale JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.Set JavaDoc;
35 import org.netbeans.api.project.ProjectManager;
36 import org.netbeans.junit.NbTestCase;
37 import org.netbeans.spi.project.ProjectConfiguration;
38 import org.netbeans.spi.project.ProjectConfigurationProvider;
39 import org.netbeans.spi.project.support.ant.EditableProperties;
40 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.util.Mutex;
44
45 /**
46  * @author Jesse Glick
47  */

48 public class J2SEConfigurationProviderTest extends NbTestCase {
49
50     public J2SEConfigurationProviderTest(String JavaDoc name) {
51         super(name);
52     }
53
54     private FileObject d;
55     private J2SEProject p;
56     private ProjectConfigurationProvider pcp;
57
58     protected void setUp() throws Exception JavaDoc {
59         super.setUp();
60         clearWorkDir();
61         d = J2SEProjectGenerator.createProject(getWorkDir(), "test", null, null).getProjectDirectory();
62         p = (J2SEProject) ProjectManager.getDefault().findProject(d);
63         pcp = p.getLookup().lookup(ProjectConfigurationProvider.class);
64         assertNotNull(pcp);
65         Locale.setDefault(Locale.US);
66     }
67
68     public void testInitialState() throws Exception JavaDoc {
69         assertEquals(1, pcp.getConfigurations().size());
70         assertNotNull(pcp.getActiveConfiguration());
71         assertEquals(pcp.getActiveConfiguration(), pcp.getConfigurations().iterator().next());
72         assertEquals("<default>", pcp.getActiveConfiguration().getDisplayName());
73         assertTrue(pcp.hasCustomizer());
74     }
75
76     public void testConfigurations() throws Exception JavaDoc {
77         TestListener l = new TestListener();
78         pcp.addPropertyChangeListener(l);
79         Properties JavaDoc p = new Properties JavaDoc();
80         p.setProperty("$label", "Debug");
81         write(p, d, "nbproject/configs/debug.properties");
82         p = new Properties JavaDoc();
83         p.setProperty("$label", "Release");
84         write(p, d, "nbproject/configs/release.properties");
85         write(new Properties JavaDoc(), d, "nbproject/configs/misc.properties");
86         assertEquals(Collections.singleton(ProjectConfigurationProvider.PROP_CONFIGURATIONS), l.events());
87         List JavaDoc<ProjectConfiguration> configs = new ArrayList JavaDoc<ProjectConfiguration>(getConfigurations(pcp));
88         assertEquals(4, configs.size());
89         assertEquals("<default>", configs.get(0).getDisplayName());
90         assertEquals("Debug", configs.get(1).getDisplayName());
91         assertEquals("misc", configs.get(2).getDisplayName());
92         assertEquals("Release", configs.get(3).getDisplayName());
93         assertEquals(Collections.emptySet(), l.events());
94         d.getFileObject("nbproject/configs/debug.properties").delete();
95         assertEquals(Collections.singleton(ProjectConfigurationProvider.PROP_CONFIGURATIONS), l.events());
96         configs = new ArrayList JavaDoc<ProjectConfiguration>(getConfigurations(pcp));
97         assertEquals(3, configs.size());
98         d.getFileObject("nbproject/configs").delete();
99         assertEquals(Collections.singleton(ProjectConfigurationProvider.PROP_CONFIGURATIONS), l.events());
100         configs = new ArrayList JavaDoc<ProjectConfiguration>(getConfigurations(pcp));
101         assertEquals(1, configs.size());
102         write(new Properties JavaDoc(), d, "nbproject/configs/misc.properties");
103         assertEquals(Collections.singleton(ProjectConfigurationProvider.PROP_CONFIGURATIONS), l.events());
104         configs = new ArrayList JavaDoc<ProjectConfiguration>(getConfigurations(pcp));
105         assertEquals(2, configs.size());
106     }
107
108     public void testActiveConfiguration() throws Exception JavaDoc {
109         write(new Properties JavaDoc(), d, "nbproject/configs/debug.properties");
110         write(new Properties JavaDoc(), d, "nbproject/configs/release.properties");
111         TestListener l = new TestListener();
112         pcp.addPropertyChangeListener(l);
113         ProjectConfiguration def = pcp.getActiveConfiguration();
114         assertEquals("<default>", def.getDisplayName());
115         List JavaDoc<ProjectConfiguration> configs = new ArrayList JavaDoc<ProjectConfiguration>(getConfigurations(pcp));
116         assertEquals(3, configs.size());
117         ProjectConfiguration c = configs.get(2);
118         assertEquals("release", c.getDisplayName());
119         setActiveConfiguration(pcp, c);
120         assertEquals(c, pcp.getActiveConfiguration());
121         assertEquals(Collections.singleton(ProjectConfigurationProvider.PROP_CONFIGURATION_ACTIVE), l.events());
122         setActiveConfiguration(pcp, c);
123         assertEquals(c, pcp.getActiveConfiguration());
124         assertEquals(Collections.emptySet(), l.events());
125         setActiveConfiguration(pcp, def);
126         assertEquals(def, pcp.getActiveConfiguration());
127         assertEquals(Collections.singleton(ProjectConfigurationProvider.PROP_CONFIGURATION_ACTIVE), l.events());
128         try {
129             setActiveConfiguration(pcp, null);
130             fail();
131         } catch (IllegalArgumentException JavaDoc x) {/*OK*/}
132         assertEquals(Collections.emptySet(), l.events());
133         try {
134             setActiveConfiguration(pcp, new ProjectConfiguration() {
135                 public String JavaDoc getDisplayName() {
136                     return "bogus";
137                 }
138             });
139             fail();
140         } catch (IllegalArgumentException JavaDoc x) {
141             // OK, not in original set
142
} catch (ClassCastException JavaDoc x) {
143             // also OK, not of correct type
144
}
145         assertEquals(Collections.emptySet(), l.events());
146         EditableProperties ep = new EditableProperties();
147         ep.setProperty("config", "debug");
148         p.getUpdateHelper().putProperties("nbproject/private/config.properties", ep);
149         assertEquals("debug", pcp.getActiveConfiguration().getDisplayName());
150         assertEquals(Collections.singleton(ProjectConfigurationProvider.PROP_CONFIGURATION_ACTIVE), l.events());
151     }
152
153     public void testEvaluator() throws Exception JavaDoc {
154         PropertyEvaluator eval = p.evaluator();
155         TestListener l = new TestListener();
156         eval.addPropertyChangeListener(l);
157         Properties JavaDoc p = new Properties JavaDoc();
158         p.setProperty("debug", "true");
159         write(p, d, "nbproject/configs/debug.properties");
160         p = new Properties JavaDoc();
161         p.setProperty("debug", "false");
162         write(p, d, "nbproject/configs/release.properties");
163         p = new Properties JavaDoc();
164         p.setProperty("more", "stuff");
165         write(p, d, "nbproject/private/configs/release.properties");
166         List JavaDoc<ProjectConfiguration> configs = new ArrayList JavaDoc<ProjectConfiguration>(getConfigurations(pcp));
167         assertEquals(3, configs.size());
168         ProjectConfiguration c = configs.get(1);
169         assertEquals("debug", c.getDisplayName());
170         setActiveConfiguration(pcp, c);
171         assertEquals(new HashSet JavaDoc<String JavaDoc>(Arrays.asList("config", "debug")), l.events());
172         assertEquals("debug", eval.getProperty("config"));
173         assertEquals("true", eval.getProperty("debug"));
174         assertEquals(null, eval.getProperty("more"));
175         c = configs.get(2);
176         assertEquals("release", c.getDisplayName());
177         setActiveConfiguration(pcp, c);
178         assertEquals(new HashSet JavaDoc<String JavaDoc>(Arrays.asList("config", "debug", "more")), l.events());
179         assertEquals("release", eval.getProperty("config"));
180         assertEquals("false", eval.getProperty("debug"));
181         assertEquals("stuff", eval.getProperty("more"));
182         c = configs.get(0);
183         assertEquals("<default>", c.getDisplayName());
184         setActiveConfiguration(pcp, c);
185         assertEquals(new HashSet JavaDoc<String JavaDoc>(Arrays.asList("config", "debug", "more")), l.events());
186         assertEquals(null, eval.getProperty("config"));
187         assertEquals(null, eval.getProperty("debug"));
188         assertEquals(null, eval.getProperty("more"));
189         // XXX test nbproject/private/configs/*.properties
190
}
191
192     public void testInitialListening() throws Exception JavaDoc { // #84781
193
ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void JavaDoc>() {
194             public Void JavaDoc run() throws Exception JavaDoc {
195                 TestListener l = new TestListener();
196                 pcp.addPropertyChangeListener(l);
197                 Properties JavaDoc props = new Properties JavaDoc();
198                 props.setProperty("$label", "X");
199                 write(props, d, "nbproject/configs/x.properties");
200                 props = new Properties JavaDoc();
201                 props.setProperty("config", "x");
202                 write(props, d, "nbproject/private/config.properties");
203                 assertEquals(new HashSet JavaDoc<String JavaDoc>(Arrays.asList(ProjectConfigurationProvider.PROP_CONFIGURATIONS, ProjectConfigurationProvider.PROP_CONFIGURATION_ACTIVE)),
204                         l.events());
205                 assertEquals(2, pcp.getConfigurations().size());
206                 assertEquals("X", pcp.getActiveConfiguration().getDisplayName());
207                 return null;
208             }
209         });
210     }
211
212     private void write(Properties JavaDoc p, FileObject d, String JavaDoc path) throws IOException JavaDoc {
213         FileObject f = FileUtil.createData(d, path);
214         OutputStream JavaDoc os = f.getOutputStream();
215         p.store(os, null);
216         os.close();
217     }
218
219     private static final class TestListener implements PropertyChangeListener JavaDoc {
220         private Set JavaDoc<String JavaDoc> events = new HashSet JavaDoc<String JavaDoc>();
221         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
222             events.add(evt.getPropertyName());
223         }
224         public Set JavaDoc<String JavaDoc> events() {
225             Set JavaDoc<String JavaDoc> copy = events;
226             events = new HashSet JavaDoc<String JavaDoc>();
227             return copy;
228         }
229     }
230
231     private static Collection JavaDoc<? extends ProjectConfiguration> getConfigurations(ProjectConfigurationProvider<?> pcp) {
232         return pcp.getConfigurations();
233     }
234
235     @SuppressWarnings JavaDoc("unchecked")
236     private static void setActiveConfiguration(ProjectConfigurationProvider<?> pcp, ProjectConfiguration pc) throws IOException JavaDoc {
237         ProjectConfigurationProvider _pcp = pcp;
238         _pcp.setActiveConfiguration(pc);
239     }
240
241 }
242
Popular Tags