1 19 20 package org.netbeans.modules.java.j2seproject; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.Collection ; 29 import java.util.Collections ; 30 import java.util.HashSet ; 31 import java.util.List ; 32 import java.util.Locale ; 33 import java.util.Properties ; 34 import java.util.Set ; 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 48 public class J2SEConfigurationProviderTest extends NbTestCase { 49 50 public J2SEConfigurationProviderTest(String 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 { 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 { 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 { 77 TestListener l = new TestListener(); 78 pcp.addPropertyChangeListener(l); 79 Properties p = new Properties (); 80 p.setProperty("$label", "Debug"); 81 write(p, d, "nbproject/configs/debug.properties"); 82 p = new Properties (); 83 p.setProperty("$label", "Release"); 84 write(p, d, "nbproject/configs/release.properties"); 85 write(new Properties (), d, "nbproject/configs/misc.properties"); 86 assertEquals(Collections.singleton(ProjectConfigurationProvider.PROP_CONFIGURATIONS), l.events()); 87 List <ProjectConfiguration> configs = new ArrayList <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 <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 <ProjectConfiguration>(getConfigurations(pcp)); 101 assertEquals(1, configs.size()); 102 write(new Properties (), d, "nbproject/configs/misc.properties"); 103 assertEquals(Collections.singleton(ProjectConfigurationProvider.PROP_CONFIGURATIONS), l.events()); 104 configs = new ArrayList <ProjectConfiguration>(getConfigurations(pcp)); 105 assertEquals(2, configs.size()); 106 } 107 108 public void testActiveConfiguration() throws Exception { 109 write(new Properties (), d, "nbproject/configs/debug.properties"); 110 write(new Properties (), 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 <ProjectConfiguration> configs = new ArrayList <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 x) {} 132 assertEquals(Collections.emptySet(), l.events()); 133 try { 134 setActiveConfiguration(pcp, new ProjectConfiguration() { 135 public String getDisplayName() { 136 return "bogus"; 137 } 138 }); 139 fail(); 140 } catch (IllegalArgumentException x) { 141 } catch (ClassCastException x) { 143 } 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 { 154 PropertyEvaluator eval = p.evaluator(); 155 TestListener l = new TestListener(); 156 eval.addPropertyChangeListener(l); 157 Properties p = new Properties (); 158 p.setProperty("debug", "true"); 159 write(p, d, "nbproject/configs/debug.properties"); 160 p = new Properties (); 161 p.setProperty("debug", "false"); 162 write(p, d, "nbproject/configs/release.properties"); 163 p = new Properties (); 164 p.setProperty("more", "stuff"); 165 write(p, d, "nbproject/private/configs/release.properties"); 166 List <ProjectConfiguration> configs = new ArrayList <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 <String >(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 <String >(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 <String >(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 } 191 192 public void testInitialListening() throws Exception { ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void >() { 194 public Void run() throws Exception { 195 TestListener l = new TestListener(); 196 pcp.addPropertyChangeListener(l); 197 Properties props = new Properties (); 198 props.setProperty("$label", "X"); 199 write(props, d, "nbproject/configs/x.properties"); 200 props = new Properties (); 201 props.setProperty("config", "x"); 202 write(props, d, "nbproject/private/config.properties"); 203 assertEquals(new HashSet <String >(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 p, FileObject d, String path) throws IOException { 213 FileObject f = FileUtil.createData(d, path); 214 OutputStream os = f.getOutputStream(); 215 p.store(os, null); 216 os.close(); 217 } 218 219 private static final class TestListener implements PropertyChangeListener { 220 private Set <String > events = new HashSet <String >(); 221 public void propertyChange(PropertyChangeEvent evt) { 222 events.add(evt.getPropertyName()); 223 } 224 public Set <String > events() { 225 Set <String > copy = events; 226 events = new HashSet <String >(); 227 return copy; 228 } 229 } 230 231 private static Collection <? extends ProjectConfiguration> getConfigurations(ProjectConfigurationProvider<?> pcp) { 232 return pcp.getConfigurations(); 233 } 234 235 @SuppressWarnings ("unchecked") 236 private static void setActiveConfiguration(ProjectConfigurationProvider<?> pcp, ProjectConfiguration pc) throws IOException { 237 ProjectConfigurationProvider _pcp = pcp; 238 _pcp.setActiveConfiguration(pc); 239 } 240 241 } 242 | Popular Tags |