1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.util.Arrays ; 25 import java.util.Collections ; 26 import java.util.HashSet ; 27 import java.util.Set ; 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 ; 35 import org.w3c.dom.NodeList ; 36 37 41 public class FreeformEvaluatorTest extends TestBase { 42 43 public FreeformEvaluatorTest(String name) { 44 super(name); 45 } 46 47 public void testPropertyEvaluation() throws Exception { 48 PropertyEvaluator eval = simple.evaluator(); 49 assertEquals("right src.dir", "src", eval.getProperty("src.dir")); 50 } 51 52 public void testPropertyEvaluationChanges() throws Exception { 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 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 os = buildProperties.getOutputStream(); 70 try { 71 p.store(os); 72 } finally { 73 ProjectManager.mutex().readAccess(new Mutex.ExceptionAction<Void >() { 75 public Void run() throws Exception { 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 { 87 FreeformProject simple2 = copyProject(simple); 89 EditableProperties p = new EditableProperties(); 93 p.setProperty("build.properties", "build.properties"); 94 FileObject locProperties = simple2.getProjectDirectory().createData("loc.properties"); 95 OutputStream os = locProperties.getOutputStream(); 96 try { 97 p.store(os); 98 } finally { 99 os.close(); 100 } 101 Element data = simple2.getPrimaryConfigurationData(); 102 NodeList propertiesNL = data.getElementsByTagNameNS(FreeformProjectType.NS_GENERAL, "properties"); 103 assertEquals("one <properties>", 1, propertiesNL.getLength()); 104 Element properties = (Element ) propertiesNL.item(0); 105 properties.removeChild(properties.getFirstChild()); 106 Element 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 PropertyEvaluator eval = simple2.evaluator(); 113 TestPCL l = new TestPCL(); 114 eval.addPropertyChangeListener(l); 115 assertEquals("right src.dir", "src", eval.getProperty("src.dir")); 116 p = new EditableProperties(); 118 FileObject buildProperties = simple2.getProjectDirectory().getFileObject("build.properties"); 119 assertNotNull("have build.properties", buildProperties); 120 InputStream 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 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 final OutputStream _os = os; 146 ProjectManager.mutex().readAccess(new Mutex.ExceptionAction<Void >() { 147 public Void run() throws Exception { 148 _os.close(); 149 return null; 150 } 151 }); 152 } 153 Set <String > exact = new HashSet <String >(Arrays.asList("src.dir", "build.properties")); 155 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 |