Source Code Cross Referenced for AntProjectHelperTest.java in  » IDE-Netbeans » project.ant » org » netbeans » spi » project » support » ant » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » project.ant » org.netbeans.spi.project.support.ant 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003:         *
0004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005:         *
0006:         * The contents of this file are subject to the terms of either the GNU
0007:         * General Public License Version 2 only ("GPL") or the Common
0008:         * Development and Distribution License("CDDL") (collectively, the
0009:         * "License"). You may not use this file except in compliance with the
0010:         * License. You can obtain a copy of the License at
0011:         * http://www.netbeans.org/cddl-gplv2.html
0012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013:         * specific language governing permissions and limitations under the
0014:         * License.  When distributing the software, include this License Header
0015:         * Notice in each file and include the License file at
0016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
0017:         * particular file as subject to the "Classpath" exception as provided
0018:         * by Sun in the GPL Version 2 section of the License file that
0019:         * accompanied this code. If applicable, add the following below the
0020:         * License Header, with the fields enclosed by brackets [] replaced by
0021:         * your own identifying information:
0022:         * "Portions Copyrighted [year] [name of copyright owner]"
0023:         *
0024:         * Contributor(s):
0025:         *
0026:         * The Original Software is NetBeans. The Initial Developer of the Original
0027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
0028:         * Microsystems, Inc. All Rights Reserved.
0029:         *
0030:         * If you wish your version of this file to be governed by only the CDDL
0031:         * or only the GPL Version 2, indicate your decision by adding
0032:         * "[Contributor] elects to include this software in this distribution
0033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034:         * single choice of license, a recipient has the option to distribute
0035:         * your version of this file under either the CDDL, the GPL Version 2 or
0036:         * to extend the choice of license to its licensees as provided above.
0037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
0038:         * Version 2 license, then the option applies only if the new code is
0039:         * made subject to such option by the copyright holder.
0040:         */
0041:
0042:        package org.netbeans.spi.project.support.ant;
0043:
0044:        import java.io.File;
0045:        import java.io.OutputStream;
0046:        import java.util.Arrays;
0047:        import java.util.Collections;
0048:        import java.util.Map;
0049:        import java.util.Properties;
0050:        import org.netbeans.api.project.Project;
0051:        import org.netbeans.api.project.ProjectManager;
0052:        import org.netbeans.api.project.ProjectUtils;
0053:        import org.netbeans.api.project.TestUtil;
0054:        import org.netbeans.junit.NbTestCase;
0055:        import org.netbeans.modules.project.ant.Util;
0056:        import org.netbeans.spi.project.AuxiliaryConfiguration;
0057:        import org.netbeans.spi.project.CacheDirectoryProvider;
0058:        import org.openide.filesystems.FileLock;
0059:        import org.openide.filesystems.FileObject;
0060:        import org.openide.filesystems.FileUtil;
0061:        import org.openide.modules.InstalledFileLocator;
0062:        import org.openide.util.Mutex;
0063:        import org.openide.util.test.MockChangeListener;
0064:        import org.openide.util.test.MockLookup;
0065:        import org.openide.util.test.MockPropertyChangeListener;
0066:        import org.openide.xml.XMLUtil;
0067:        import org.w3c.dom.Document;
0068:        import org.w3c.dom.Element;
0069:        import org.w3c.dom.Node;
0070:        import org.w3c.dom.NodeList;
0071:
0072:        /* XXX tests needed:
0073:         * - testProjectXmlSavedException
0074:         * try throwing an exception from PXSH.pXS and check that next save is OK
0075:         * need to delete: assert !modifiedMetadataPaths.isEmpty();
0076:         * - testMalformedConfigDataProducesLoadException
0077:         * make a project with broken XML files and check that it does not load
0078:         * - testCleanCheckout
0079:         * make sure loading proceeds naturally with a clean checkout, i.e. no nbproject/private/ dir
0080:         * and that appropriate private.xml and private.properties files are created on demand
0081:         * (this is perhaps already tested adequately by ProjectGeneratorTest)
0082:         * - testVCSFriendliness
0083:         * make various modifications to project/private.xml files and ensure that the
0084:         * number of lines changed in the diff is kept to a minimum
0085:         * - testIsProject
0086:         */
0087:
0088:        /**
0089:         * Test functionality of AntProjectHelper and AntBasedProjectFactorySingleton.
0090:         * @author Jesse Glick
0091:         */
0092:        public class AntProjectHelperTest extends NbTestCase {
0093:
0094:            /**
0095:             * Create test suite.
0096:             * @param name suite name
0097:             */
0098:            public AntProjectHelperTest(String name) {
0099:                super (name);
0100:            }
0101:
0102:            private FileObject scratch;
0103:            private FileObject projdir;
0104:            private ProjectManager pm;
0105:            private Project p;
0106:            private AntProjectHelper h;
0107:            private AntBasedTestUtil.TestListener l;
0108:            private File antJar;
0109:
0110:            protected @Override
0111:            void setUp() throws Exception {
0112:                super .setUp();
0113:                scratch = TestUtil.makeScratchDir(this );
0114:                projdir = scratch.createFolder("proj");
0115:                TestUtil.createFileFromContent(AntProjectHelperTest.class
0116:                        .getResource("data/project.xml"), projdir,
0117:                        "nbproject/project.xml");
0118:                TestUtil.createFileFromContent(AntProjectHelperTest.class
0119:                        .getResource("data/private.xml"), projdir,
0120:                        "nbproject/private/private.xml");
0121:                TestUtil.createFileFromContent(AntProjectHelperTest.class
0122:                        .getResource("data/project.properties"), projdir,
0123:                        "nbproject/project.properties");
0124:                TestUtil.createFileFromContent(AntProjectHelperTest.class
0125:                        .getResource("data/private.properties"), projdir,
0126:                        "nbproject/private/private.properties");
0127:                TestUtil.createFileFromContent(AntProjectHelperTest.class
0128:                        .getResource("data/global.properties"), scratch,
0129:                        "userdir/build.properties");
0130:                antJar = new File(getWorkDir(), "ant/lib/ant.jar");
0131:                MockLookup.setInstances(AntBasedTestUtil
0132:                        .testAntBasedProjectType(), new InstalledFileLocator() {
0133:                    public @Override
0134:                    File locate(String relativePath, String codeNameBase,
0135:                            boolean localized) {
0136:                        if (relativePath.equals("ant/lib/ant.jar")) {
0137:                            return antJar;
0138:                        } else {
0139:                            return null;
0140:                        }
0141:                    }
0142:                });
0143:                pm = ProjectManager.getDefault();
0144:                p = pm.findProject(projdir);
0145:                h = p.getLookup().lookup(AntProjectHelper.class);
0146:                l = new AntBasedTestUtil.TestListener();
0147:            }
0148:
0149:            /**
0150:             * Test that Ant-based projects are at least recognized as such.
0151:             * @throws Exception if anything unexpected happens
0152:             */
0153:            public void testBasicRecognition() throws Exception {
0154:                assertNotNull("recognized the project", p);
0155:                assertEquals("correct project directory", projdir, p
0156:                        .getProjectDirectory());
0157:                assertEquals("found something in project lookup", "hello", p
0158:                        .getLookup().lookup(String.class));
0159:            }
0160:
0161:            /**
0162:             * Test that it is possible to retrieve the main data from project.xml and private.xml.
0163:             * @throws Exception if anything unexpected happens
0164:             */
0165:            public void testGetPrimaryConfigurationData() throws Exception {
0166:                assertNotNull("Had helper in lookup", h);
0167:                Element data = h.getPrimaryConfigurationData(true);
0168:                assertEquals("correct element name", "data", data
0169:                        .getLocalName());
0170:                assertEquals("correct element namespace", "urn:test:shared",
0171:                        data.getNamespaceURI());
0172:                Element stuff = Util.findElement(data, "shared-stuff",
0173:                        "urn:test:shared");
0174:                assertNotNull("had nested stuff in it", stuff);
0175:                data = h.getPrimaryConfigurationData(false);
0176:                assertEquals("correct element name", "data", data
0177:                        .getLocalName());
0178:                assertEquals("correct element namespace", "urn:test:private",
0179:                        data.getNamespaceURI());
0180:                stuff = Util.findElement(data, "private-stuff",
0181:                        "urn:test:private");
0182:                assertNotNull("had nested stuff in it", stuff);
0183:            }
0184:
0185:            /**
0186:             * Test error recovery from malformed project.xml
0187:             * @see "#46048"
0188:             */
0189:            public void testBrokenPrimaryConfigurationData() throws Exception {
0190:                // Make an empty, thus invalid, project.xml:
0191:                TestUtil.createFileFromContent(null, projdir,
0192:                        AntProjectHelper.PROJECT_XML_PATH);
0193:                AntProjectHelper.QUIETLY_SWALLOW_XML_LOAD_ERRORS = true;
0194:                Element data;
0195:                try {
0196:                    data = h.getPrimaryConfigurationData(true);
0197:                } finally {
0198:                    AntProjectHelper.QUIETLY_SWALLOW_XML_LOAD_ERRORS = false;
0199:                }
0200:                assertEquals("correct element name", "data", data
0201:                        .getLocalName());
0202:                assertEquals("correct element namespace", "urn:test:shared",
0203:                        data.getNamespaceURI());
0204:                Element stuff = Util.findElement(data, "shared-stuff",
0205:                        "urn:test:shared");
0206:                assertNull("had no stuff in it", stuff);
0207:                // Make sure a subsequent save proceeds normally too:
0208:                data = XMLUtil.createDocument("whatever", "urn:test:shared",
0209:                        null, null).createElementNS("urn:test:shared", "data");
0210:                data.appendChild(data.getOwnerDocument().createElementNS(
0211:                        "urn:test:shared", "details"));
0212:                h.putPrimaryConfigurationData(data, true);
0213:                pm.saveProject(p);
0214:                Document doc = AntBasedTestUtil.slurpXml(h,
0215:                        AntProjectHelper.PROJECT_XML_PATH);
0216:                Element root = doc.getDocumentElement();
0217:                Element type = Util.findElement(root, "type",
0218:                        AntProjectHelper.PROJECT_NS);
0219:                assertEquals("correct restored type", "test", Util
0220:                        .findText(type));
0221:                Element config = Util.findElement(root, "configuration",
0222:                        AntProjectHelper.PROJECT_NS);
0223:                assertNotNull("have <configuration>", config);
0224:                data = Util.findElement(config, "data", "urn:test:shared");
0225:                assertNotNull("have <data>", data);
0226:                Element details = Util.findElement(data, "details",
0227:                        "urn:test:shared");
0228:                assertNotNull("have <details>", details);
0229:            }
0230:
0231:            /**
0232:             * Test that after retrieving XML config data, you can't mess up other internal stuff.
0233:             * @throws Exception if anything unexpected happens
0234:             */
0235:            public void testImmutabilityOfGottenConfigurationData()
0236:                    throws Exception {
0237:                Element data = h.getPrimaryConfigurationData(true);
0238:                assertNull("no parent for data", data.getParentNode());
0239:                // XXX assure that modifications to data have no effect on a subsequent call
0240:                // XXX get the ownerDocument and assure that the tree cannot be modified using it
0241:            }
0242:
0243:            /**
0244:             * Test that it is possible to load properties from .properties files.
0245:             * @throws Exception if anything unexpected happens
0246:             */
0247:            public void testGetProperties() throws Exception {
0248:                EditableProperties ep = h
0249:                        .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
0250:                assertNotNull("getProperties should never return null", ep);
0251:                assertEquals("three properties defined", 3, ep.size());
0252:                assertEquals("shared.prop correct", "value1", ep
0253:                        .get("shared.prop"));
0254:                assertEquals("overridden.prop correct", "value3", ep
0255:                        .get("overridden.prop"));
0256:                ep = h.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
0257:                assertNotNull("getProperties should never return null", ep);
0258:                assertEquals("four properties defined", 4, ep.size());
0259:                assertEquals("private.prop correct", "value2", ep
0260:                        .get("private.prop"));
0261:                assertEquals("overridden.prop correct", "value4", ep
0262:                        .get("overridden.prop"));
0263:                ep = h.getProperties("bogus/path.properties");
0264:                assertNotNull("getProperties should never return null", ep);
0265:                assertEquals("no properties defined", 0, ep.size());
0266:            }
0267:
0268:            /**
0269:             * Test that Ant properties can be evaluated with proper (recursive) substitutions.
0270:             * @throws Exception if anything unexpected happens
0271:             */
0272:            public void testStandardPropertyEvaluator() throws Exception {
0273:                // Make sure any callbacks happen inside a lock, so changes are not posted asynch:
0274:                ProjectManager.mutex().writeAccess(
0275:                        new Mutex.ExceptionAction<Void>() {
0276:                            public Void run() throws Exception {
0277:                                PropertyEvaluator pev = h
0278:                                        .getStandardPropertyEvaluator();
0279:                                assertEquals("shared.prop correct", "value1",
0280:                                        pev.getProperty("shared.prop"));
0281:                                assertEquals("private.prop correct", "value2",
0282:                                        pev.getProperty("private.prop"));
0283:                                assertEquals("overridden.prop correct",
0284:                                        "value4", pev
0285:                                                .getProperty("overridden.prop"));
0286:                                assertEquals("derived.prop correct",
0287:                                        "value2:value1:${undefined.prop}", pev
0288:                                                .getProperty("derived.prop"));
0289:                                assertEquals("tempdir correct", System
0290:                                        .getProperty("java.io.tmpdir")
0291:                                        + "/foo", pev.getProperty("tempdir"));
0292:                                assertEquals("global.prop correct", "value5",
0293:                                        pev.getProperty("global.prop"));
0294:                                assertEquals("does not have other defs", null,
0295:                                        pev.getProperty("bogus.prop"));
0296:                                Map m = pev.getProperties();
0297:                                assertEquals("shared.prop correct", "value1", m
0298:                                        .get("shared.prop"));
0299:                                assertEquals("private.prop correct", "value2",
0300:                                        m.get("private.prop"));
0301:                                assertEquals("overridden.prop correct",
0302:                                        "value4", m.get("overridden.prop"));
0303:                                assertEquals("derived.prop correct",
0304:                                        "value2:value1:${undefined.prop}", m
0305:                                                .get("derived.prop"));
0306:                                assertEquals("tempdir correct", System
0307:                                        .getProperty("java.io.tmpdir")
0308:                                        + "/foo", m.get("tempdir"));
0309:                                assertEquals("global.prop correct", "value5", m
0310:                                        .get("global.prop"));
0311:                                assertEquals("does not have other defs", null,
0312:                                        m.get("bogus.prop"));
0313:                                assertEquals(
0314:                                        "correct evaluateString",
0315:                                        "value1:value2",
0316:                                        pev
0317:                                                .evaluate("${shared.prop}:${private.prop}"));
0318:                                // #44213: try modifying build.properties.
0319:                                MockPropertyChangeListener l = new MockPropertyChangeListener();
0320:                                pev.addPropertyChangeListener(l);
0321:                                FileObject buildProperties = scratch
0322:                                        .getFileObject("userdir/build.properties");
0323:                                assertNotNull("have build.properties",
0324:                                        buildProperties);
0325:                                FileLock lock = buildProperties.lock();
0326:                                OutputStream os = buildProperties
0327:                                        .getOutputStream(lock);
0328:                                Properties p = new Properties();
0329:                                p.setProperty("global.prop", "value5a");
0330:                                p.setProperty("global.prop.2", "globalvalue2");
0331:                                p.store(os, null);
0332:                                os.close();
0333:                                lock.releaseLock();
0334:                                l.assertEvents("global.prop", "global.prop.2");
0335:                                assertEquals("global.prop is correct",
0336:                                        "value5a", pev
0337:                                                .getProperty("global.prop"));
0338:                                assertEquals("global.prop.2 is correct",
0339:                                        "globalvalue2", pev
0340:                                                .getProperty("global.prop.2"));
0341:                                // #42147: try modifying project.properties and private.properties on disk.
0342:                                FileObject projectProperties = projdir
0343:                                        .getFileObject("nbproject/project.properties");
0344:                                assertNotNull("have project.properties",
0345:                                        projectProperties);
0346:                                lock = projectProperties.lock();
0347:                                os = projectProperties.getOutputStream(lock);
0348:                                p = new Properties();
0349:                                p.setProperty("overridden.prop", "value3a"); // different, but won't matter
0350:                                p.setProperty("shared.prop", "value1a"); // changed
0351:                                p
0352:                                        .setProperty("derived.prop",
0353:                                                "${private.prop}:${shared.prop}:${undefined.prop}"); // same literally
0354:                                p.store(os, null);
0355:                                os.close();
0356:                                lock.releaseLock();
0357:                                l.assertEvents("shared.prop", "derived.prop");
0358:                                assertEquals("shared.prop is correct",
0359:                                        "value1a", pev
0360:                                                .getProperty("shared.prop"));
0361:                                assertEquals("derived.prop correct",
0362:                                        "value2:value1a:${undefined.prop}", pev
0363:                                                .getProperty("derived.prop"));
0364:                                FileObject privateProperties = projdir
0365:                                        .getFileObject("nbproject/private/private.properties");
0366:                                assertNotNull("have private.properties",
0367:                                        privateProperties);
0368:                                lock = privateProperties.lock();
0369:                                os = privateProperties.getOutputStream(lock);
0370:                                p = new Properties();
0371:                                p.setProperty("private.prop", "value2a"); // changed
0372:                                p.setProperty("overridden.prop", "value4"); // same
0373:                                p.setProperty("tempdir",
0374:                                        "${java.io.tmpdir}/foo"); // same
0375:                                p.setProperty("user.properties.file",
0376:                                        "../userdir/build.properties"); // same
0377:                                p.store(os, null);
0378:                                os.close();
0379:                                lock.releaseLock();
0380:                                l.assertEvents("private.prop", "derived.prop");
0381:                                assertEquals("private.prop is correct",
0382:                                        "value2a", pev
0383:                                                .getProperty("private.prop"));
0384:                                assertEquals("derived.prop correct",
0385:                                        "value2a:value1a:${undefined.prop}",
0386:                                        pev.getProperty("derived.prop"));
0387:                                // Try deleting project.properties and make sure its values are cleared.
0388:                                projectProperties.delete();
0389:                                l.assertEvents("shared.prop", "derived.prop");
0390:                                assertEquals("shared.prop is gone", null, pev
0391:                                        .getProperty("shared.prop"));
0392:                                assertEquals("derived.prop is gone", null, pev
0393:                                        .getProperty("derived.prop"));
0394:                                // Now recreate it.
0395:                                projectProperties = projdir.getFileObject(
0396:                                        "nbproject").createData(
0397:                                        "project.properties");
0398:                                lock = projectProperties.lock();
0399:                                os = projectProperties.getOutputStream(lock);
0400:                                p = new Properties();
0401:                                p
0402:                                        .setProperty("derived.prop",
0403:                                                "${private.prop}:${shared.prop}:${undefined.prop.2}"); // restoring w/ changes
0404:                                p.store(os, null);
0405:                                os.close();
0406:                                lock.releaseLock();
0407:                                l.assertEvents("derived.prop");
0408:                                assertEquals(
0409:                                        "derived.prop is back",
0410:                                        "value2a:${shared.prop}:${undefined.prop.2}",
0411:                                        pev.getProperty("derived.prop"));
0412:                                // #44213 cont'd: change user.properties.file and make sure the new definitions are read
0413:                                FileObject buildProperties2 = scratch
0414:                                        .getFileObject("userdir").createData(
0415:                                                "build2.properties");
0416:                                lock = buildProperties2.lock();
0417:                                os = buildProperties2.getOutputStream(lock);
0418:                                p = new Properties();
0419:                                p.setProperty("global.prop", "value5b"); // modified
0420:                                p.setProperty("global.prop.2", "globalvalue2"); // same
0421:                                p.store(os, null);
0422:                                os.close();
0423:                                lock.releaseLock();
0424:                                lock = privateProperties.lock();
0425:                                os = privateProperties.getOutputStream(lock);
0426:                                p = new Properties();
0427:                                p.setProperty("private.prop", "value2a"); // same
0428:                                p.setProperty("overridden.prop", "value4"); // same
0429:                                p.setProperty("tempdir",
0430:                                        "${java.io.tmpdir}/foo"); // same
0431:                                p.setProperty("user.properties.file",
0432:                                        "../userdir/build2.properties"); // changed
0433:                                p.store(os, null);
0434:                                os.close();
0435:                                lock.releaseLock();
0436:                                l.assertEvents("user.properties.file",
0437:                                        "global.prop");
0438:                                assertEquals("user.properties.file is correct",
0439:                                        "../userdir/build2.properties",
0440:                                        pev.getProperty("user.properties.file"));
0441:                                assertEquals("global.prop is correct",
0442:                                        "value5b", pev
0443:                                                .getProperty("global.prop"));
0444:                                lock = buildProperties2.lock();
0445:                                os = buildProperties2.getOutputStream(lock);
0446:                                p = new Properties();
0447:                                p.setProperty("global.prop", "value5b"); // same
0448:                                // no global.prop.2
0449:                                p.store(os, null);
0450:                                os.close();
0451:                                lock.releaseLock();
0452:                                l.assertEvents("global.prop.2");
0453:                                assertEquals("global.prop.2 is gone", null, pev
0454:                                        .getProperty("global.prop.2"));
0455:                                // XXX try eval when user.properties.file is not defined (tricky, need to preset netbeans.user)
0456:                                return null;
0457:                            }
0458:                        });
0459:            }
0460:
0461:            /**
0462:             * Test that resolving file names relative to the project basedir works.
0463:             * @throws Exception if anything unexpected happens
0464:             */
0465:            public void testResolveFile() throws Exception {
0466:                // XXX could also be moved to PropertyUtilsTest
0467:                File scratchF = FileUtil.toFile(scratch);
0468:                assertNotNull("scratch directory exists on disk", scratchF);
0469:                File projdirF = FileUtil.toFile(projdir);
0470:                assertNotNull("project directory exists on disk", projdirF);
0471:                assertEquals(". resolves to project basedir", projdirF, h
0472:                        .resolveFile("."));
0473:                assertEquals(". resolves to project basedir", projdir, h
0474:                        .resolveFileObject("."));
0475:                assertEquals("simple relative path resolves", new File(
0476:                        projdirF, "foo"), h.resolveFile("foo"));
0477:                assertEquals(
0478:                        "simple relative path resolves (but there is no such file object)",
0479:                        null, h.resolveFileObject("foo"));
0480:                assertEquals("Unix-style ./ resolves",
0481:                        new File(projdirF, "foo"), h.resolveFile("./foo"));
0482:                assertEquals("DOS-style .\\ resolves",
0483:                        new File(projdirF, "foo"), h.resolveFile(".\\foo"));
0484:                assertEquals(
0485:                        "Unix-style ./ resolves (but there is no such file object)",
0486:                        null, h.resolveFileObject("./foo"));
0487:                assertEquals(
0488:                        "DOS-style ./ resolves (but there is no such file object)",
0489:                        null, h.resolveFileObject(".\\foo"));
0490:                assertEquals(".. resolves up a dir", scratchF, h
0491:                        .resolveFile(".."));
0492:                assertEquals(".. resolves up a dir", scratch, h
0493:                        .resolveFileObject(".."));
0494:                assertEquals("Unix-style ../ resolves up and down", projdirF, h
0495:                        .resolveFile("../proj"));
0496:                assertEquals("DOS-style ..\\ resolves up and down", projdirF, h
0497:                        .resolveFile("..\\proj"));
0498:                assertEquals("Unix-style ../ resolves up and down", projdir, h
0499:                        .resolveFileObject("../proj"));
0500:                assertEquals("DOS-style ..\\ resolves up and down", projdir, h
0501:                        .resolveFileObject("..\\proj"));
0502:                assertEquals("absolute path is left alone", scratchF, h
0503:                        .resolveFile(scratchF.getAbsolutePath()));
0504:                assertEquals("absolute path is left alone", scratch, h
0505:                        .resolveFileObject(scratchF.getAbsolutePath()));
0506:                File somethingElseF = new File(scratchF.getParentFile(),
0507:                        "nonexistent-file-path");
0508:                assertEquals("absolute (nonexistent) path is left alone",
0509:                        somethingElseF, h.resolveFile(somethingElseF
0510:                                .getAbsolutePath()));
0511:                assertEquals("absolute (nonexistent) path has no file object",
0512:                        null, h.resolveFileObject(somethingElseF
0513:                                .getAbsolutePath()));
0514:            }
0515:
0516:            /**
0517:             * Test that resolving file paths (for example, classpaths) relative to the project basedir works.
0518:             * Note that Ant permits any kind of path separator;
0519:             * see {@link PropertyUtils#tokenizePath} for details of the tokenization.
0520:             * @throws Exception if anything unexpected happens
0521:             */
0522:            public void testResolvePath() throws Exception {
0523:                // XXX could also be moved to PropertyUtilsTest
0524:                File scratchF = FileUtil.toFile(scratch);
0525:                assertNotNull("scratch dir exists on disk", scratchF);
0526:                String scratchS = scratchF.getAbsolutePath();
0527:                File projdirF = FileUtil.toFile(projdir);
0528:                assertNotNull("project dir exists on disk", projdirF);
0529:                String projdirS = projdirF.getAbsolutePath();
0530:                assertEquals("empty path doesn't need to resolve", "", h
0531:                        .resolvePath(""));
0532:                assertEquals(". resolves", projdirS, h.resolvePath("."));
0533:                assertEquals(".. resolves", scratchS, h.resolvePath(".."));
0534:                assertEquals("Unix-style ../ resolves", projdirS, h
0535:                        .resolvePath("../proj"));
0536:                assertEquals("DOS-style ..\\ resolves", projdirS, h
0537:                        .resolvePath("..\\proj"));
0538:                String longpath = projdirS + File.pathSeparator + scratchS
0539:                        + File.pathSeparator + projdirS + File.pathSeparator
0540:                        + projdirS;
0541:                assertEquals("mixed Unix-style path resolves", longpath, h
0542:                        .resolvePath(".:..:../proj:..\\proj"));
0543:                assertEquals("mixed DOS-style path resolves", longpath, h
0544:                        .resolvePath(".;..;../proj;..\\proj"));
0545:                assertEquals("absolute path resolves to itself", scratchS, h
0546:                        .resolvePath(scratchS));
0547:                // XXX check use of Unix symlinks - don't want them canonicalized
0548:                // details of tokenization semantics left to PropertyUtilsTest.testTokenizePath
0549:            }
0550:
0551:            /**
0552:             * Test that storing changes to .properties files works.
0553:             * @throws Exception if anything unexpected happens
0554:             */
0555:            public void testPutProperties() throws Exception {
0556:                h.addAntProjectListener(l);
0557:                EditableProperties ep = h
0558:                        .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
0559:                assertNotNull("getProperties should never return null", ep);
0560:                assertEquals("three properties defined", 3, ep.size());
0561:                ep.put("testprop", "testval");
0562:                assertTrue("uncommitted changes do not modify project", !pm
0563:                        .isModified(p));
0564:                assertEquals(
0565:                        "uncommitted changes not yet in project properties",
0566:                        null, h.getStandardPropertyEvaluator().getProperty(
0567:                                "testprop"));
0568:                assertEquals("uncommitted changes fire no events", 0, l
0569:                        .events().length);
0570:                h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
0571:                assertTrue("committed changes do modify project", pm
0572:                        .isModified(p));
0573:                AntProjectEvent[] evs = l.events();
0574:                assertEquals("putProperties fires one event", 1, evs.length);
0575:                assertEquals("correct helper", h, evs[0].getHelper());
0576:                assertEquals("correct path",
0577:                        AntProjectHelper.PROJECT_PROPERTIES_PATH, evs[0]
0578:                                .getPath());
0579:                assertTrue("expected change", evs[0].isExpected());
0580:                assertEquals("committed changes are in project properties",
0581:                        "testval", h.getStandardPropertyEvaluator()
0582:                                .getProperty("testprop"));
0583:                Properties props = AntBasedTestUtil.slurpProperties(h,
0584:                        AntProjectHelper.PROJECT_PROPERTIES_PATH);
0585:                assertNotNull("project.properties already exists", props);
0586:                assertEquals(
0587:                        "project.properties does not yet contain testprop",
0588:                        null, props.getProperty("testprop"));
0589:                pm.saveProject(p);
0590:                assertTrue("project is now saved", !pm.isModified(p));
0591:                assertEquals("saving changes fires no new events", 0, l
0592:                        .events().length);
0593:                assertEquals(
0594:                        "committed & saved changes are in project properties",
0595:                        "testval", h.getStandardPropertyEvaluator()
0596:                                .getProperty("testprop"));
0597:                props = AntBasedTestUtil.slurpProperties(h,
0598:                        AntProjectHelper.PROJECT_PROPERTIES_PATH);
0599:                assertNotNull("project.properties still exists", props);
0600:                assertEquals("project.properties now contains testprop",
0601:                        "testval", props.getProperty("testprop"));
0602:                // #42147: changes made on disk should fire changes to AntProjectListener, not just to the PropertyEvaluator
0603:                ProjectManager.mutex().writeAccess(
0604:                        new Mutex.ExceptionAction<Void>() {
0605:                            public Void run() throws Exception {
0606:                                TestUtil
0607:                                        .createFileFromContent(
0608:                                                null,
0609:                                                h.getProjectDirectory(),
0610:                                                AntProjectHelper.PROJECT_PROPERTIES_PATH);
0611:                                return null;
0612:                            }
0613:                        });
0614:                evs = l.events();
0615:                assertEquals("touching project.properties fires one event", 1,
0616:                        evs.length);
0617:                assertEquals("correct helper", h, evs[0].getHelper());
0618:                assertEquals("correct path",
0619:                        AntProjectHelper.PROJECT_PROPERTIES_PATH, evs[0]
0620:                                .getPath());
0621:                assertFalse("unexpected change", evs[0].isExpected());
0622:                assertEquals(
0623:                        "empty file now",
0624:                        Collections.EMPTY_MAP,
0625:                        h
0626:                                .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH));
0627:                // XXX try creating and deleting property files
0628:                // XXX try modifying several property files and saving in a batch
0629:                // XXX try storing unmodified properties and see what happens
0630:                // XXX try storing a fresh EditableProperties object not returned from getProperties
0631:            }
0632:
0633:            /**
0634:             * Test that writing changes to project.xml and private.xml works.
0635:             * @throws Exception if anything unexpected happens
0636:             */
0637:            public void testPutPrimaryConfigurationData() throws Exception {
0638:                h.addAntProjectListener(l);
0639:                Element data = h.getPrimaryConfigurationData(true);
0640:                assertNotNull("<shared-stuff/> is there to start", Util
0641:                        .findElement(data, "shared-stuff", "urn:test:shared"));
0642:                assertTrue("project is not initially modified", !pm
0643:                        .isModified(p));
0644:                assertEquals("gPCD fires no events", 0, l.events().length);
0645:                assertNotNull("config data has an owner document", data
0646:                        .getOwnerDocument());
0647:                Element nue = data.getOwnerDocument().createElementNS(
0648:                        "urn:test:shared", "misc");
0649:                data.appendChild(nue);
0650:                assertTrue("project is not modified after uncommitted change",
0651:                        !pm.isModified(p));
0652:                assertEquals("no events fired after uncommitted change", 0, l
0653:                        .events().length);
0654:                assertEquals(
0655:                        "after uncommitted change gPCD does not yet have new <misc/>",
0656:                        null, Util.findElement(h
0657:                                .getPrimaryConfigurationData(true), "misc",
0658:                                "urn:test:shared"));
0659:                h.putPrimaryConfigurationData(data, true);
0660:                assertTrue("project is modified after committed change", pm
0661:                        .isModified(p));
0662:                AntProjectEvent[] evs = l.events();
0663:                assertEquals("putPCD fires one event", 1, evs.length);
0664:                assertEquals("correct helper", h, evs[0].getHelper());
0665:                assertEquals("correct path", AntProjectHelper.PROJECT_XML_PATH,
0666:                        evs[0].getPath());
0667:                assertTrue("expected change", evs[0].isExpected());
0668:                nue = Util.findElement(h.getPrimaryConfigurationData(true),
0669:                        "misc", "urn:test:shared");
0670:                assertNotNull("after committed change gPCD has new <misc/>",
0671:                        nue);
0672:                assertEquals("new element name is correct", "misc", nue
0673:                        .getLocalName());
0674:                assertEquals("new element namespace is correct",
0675:                        "urn:test:shared", nue.getNamespaceURI());
0676:                Document doc = AntBasedTestUtil.slurpXml(h,
0677:                        AntProjectHelper.PROJECT_XML_PATH);
0678:                Element configuration = Util.findElement(doc
0679:                        .getDocumentElement(), "configuration",
0680:                        AntProjectHelper.PROJECT_NS);
0681:                assertNotNull("still has <configuration> on disk",
0682:                        configuration);
0683:                data = Util.findElement(configuration, "data",
0684:                        "urn:test:shared");
0685:                assertNotNull("still has <data> on disk", data);
0686:                nue = Util.findElement(data, "misc", "urn:test:shared");
0687:                assertEquals("<misc/> not yet on disk", null, nue);
0688:                pm.saveProject(p);
0689:                assertTrue("project is not modified after save", !pm
0690:                        .isModified(p));
0691:                assertEquals("saving changes fires no new events", 0, l
0692:                        .events().length);
0693:                nue = Util.findElement(h.getPrimaryConfigurationData(true),
0694:                        "misc", "urn:test:shared");
0695:                assertNotNull("after save gPCD still has new <misc/>", nue);
0696:                doc = AntBasedTestUtil.slurpXml(h,
0697:                        AntProjectHelper.PROJECT_XML_PATH);
0698:                configuration = Util.findElement(doc.getDocumentElement(),
0699:                        "configuration", AntProjectHelper.PROJECT_NS);
0700:                assertNotNull("still has <configuration> on disk",
0701:                        configuration);
0702:                data = Util.findElement(configuration, "data",
0703:                        "urn:test:shared");
0704:                assertNotNull("still has <data> on disk", data);
0705:                nue = Util.findElement(data, "misc", "urn:test:shared");
0706:                assertNotNull("<misc/> now on disk", nue);
0707:                // #42147: changes made on disk should result in firing of an AntProjectEvent
0708:                ProjectManager.mutex().writeAccess(
0709:                        new Mutex.ExceptionAction<Void>() {
0710:                            public Void run() throws Exception {
0711:                                TestUtil
0712:                                        .createFileFromContent(
0713:                                                AntProjectHelperTest.class
0714:                                                        .getResource("data/project-modified.xml"),
0715:                                                projdir,
0716:                                                AntProjectHelper.PROJECT_XML_PATH);
0717:                                return null;
0718:                            }
0719:                        });
0720:                evs = l.events();
0721:                assertEquals("writing project.xml on disk fires one event", 1,
0722:                        evs.length);
0723:                assertEquals("correct helper", h, evs[0].getHelper());
0724:                assertEquals("correct path", AntProjectHelper.PROJECT_XML_PATH,
0725:                        evs[0].getPath());
0726:                assertFalse("unexpected change", evs[0].isExpected());
0727:                assertEquals("correct new display name", "Some New Name",
0728:                        ProjectUtils.getInformation(p).getDisplayName());
0729:                data = h.getPrimaryConfigurationData(true);
0730:                Element stuff = Util.findElement(data, "other-shared-stuff",
0731:                        "urn:test:shared");
0732:                assertNotNull("have <other-shared-stuff/> now", stuff);
0733:                AuxiliaryConfiguration aux = p.getLookup().lookup(
0734:                        AuxiliaryConfiguration.class);
0735:                data = aux.getConfigurationFragment("data",
0736:                        "urn:test:shared-aux", true);
0737:                assertNotNull("have aux <data>", data);
0738:                stuff = Util.findElement(data, "other-aux-shared-stuff",
0739:                        "urn:test:shared-aux");
0740:                assertNotNull("have <other-aux-shared-stuff/> now", stuff);
0741:                // XXX try private.xml too
0742:                // XXX try modifying both XML files, or different parts of the same, and saving in a batch
0743:                // XXX try storing unmodified XML fragments and see what happens
0744:                // XXX try storing a fresh Element not returned from getPrimaryConfigurationData
0745:            }
0746:
0747:            /**
0748:             * Test that it is possible for external code to store custom data in project.xml and private.xml.
0749:             * @throws Exception if anything unexpected happens
0750:             */
0751:            public void testExtensibleMetadataProviderImpl() throws Exception {
0752:                AuxiliaryConfiguration aux = p.getLookup().lookup(
0753:                        AuxiliaryConfiguration.class);
0754:                assertNotNull("AuxiliaryConfiguration present", aux);
0755:                CacheDirectoryProvider cdp = p.getLookup().lookup(
0756:                        CacheDirectoryProvider.class);
0757:                assertNotNull("CacheDirectoryProvider present", cdp);
0758:                // Check cache dir.
0759:                FileObject cache = cdp.getCacheDirectory();
0760:                assertNotNull("has a cache dir", cache);
0761:                assertTrue("cache dir is a folder", cache.isFolder());
0762:                assertEquals("cache dir is empty", Collections.EMPTY_LIST,
0763:                        Arrays.asList(cache.getChildren()));
0764:                cache.createData("foo");
0765:                cache = cdp.getCacheDirectory();
0766:                assertEquals("cache contents still there", 1, cache
0767:                        .getChildren().length);
0768:                // Check read of shared data.
0769:                h.addAntProjectListener(l);
0770:                Element data = aux.getConfigurationFragment("data",
0771:                        "urn:test:shared-aux", true);
0772:                assertNotNull("found shared <data>", data);
0773:                assertEquals("correct name", "data", data.getLocalName());
0774:                assertEquals("correct namespace", "urn:test:shared-aux", data
0775:                        .getNamespaceURI());
0776:                Element stuff = Util.findElement(data, "aux-shared-stuff",
0777:                        "urn:test:shared-aux");
0778:                assertNotNull("found <aux-shared-stuff/>", stuff);
0779:                assertEquals("gCF fires no changes", 0, l.events().length);
0780:                // Check write of shared data.
0781:                stuff.setAttribute("attr", "val");
0782:                assertFalse("project not modified by local change", pm
0783:                        .isModified(p));
0784:                aux.putConfigurationFragment(data, true);
0785:                assertTrue("now project is modified", pm.isModified(p));
0786:                AntProjectEvent[] evs = l.events();
0787:                assertEquals("pCF fires one event", 1, evs.length);
0788:                assertEquals("correct helper", h, evs[0].getHelper());
0789:                assertEquals("correct path", AntProjectHelper.PROJECT_XML_PATH,
0790:                        evs[0].getPath());
0791:                assertTrue("expected change", evs[0].isExpected());
0792:                pm.saveProject(p);
0793:                assertEquals("saving project fires no new changes", 0, l
0794:                        .events().length);
0795:                Document doc = AntBasedTestUtil.slurpXml(h,
0796:                        AntProjectHelper.PROJECT_XML_PATH);
0797:                Element config = Util.findElement(doc.getDocumentElement(),
0798:                        "configuration", AntProjectHelper.PROJECT_NS);
0799:                assertNotNull("<configuration> still exists", config);
0800:                data = Util.findElement(config, "data", "urn:test:shared-aux");
0801:                assertNotNull("<data> still exists", data);
0802:                stuff = Util.findElement(data, "aux-shared-stuff",
0803:                        "urn:test:shared-aux");
0804:                assertNotNull("still have <aux-shared-stuff/>", stuff);
0805:                assertEquals("attr written correctly", "val", stuff
0806:                        .getAttribute("attr"));
0807:                // Check read of private data.
0808:                data = aux.getConfigurationFragment("data",
0809:                        "urn:test:private-aux", false);
0810:                assertNotNull("found shared <data>", data);
0811:                assertEquals("correct name", "data", data.getLocalName());
0812:                assertEquals("correct namespace", "urn:test:private-aux", data
0813:                        .getNamespaceURI());
0814:                stuff = Util.findElement(data, "aux-private-stuff",
0815:                        "urn:test:private-aux");
0816:                assertNotNull("found <aux-private-stuff/>", stuff);
0817:                assertEquals("gCF fires no changes", 0, l.events().length);
0818:                // Check write of private data.
0819:                stuff.setAttribute("attr", "val");
0820:                assertFalse("project not modified by local change", pm
0821:                        .isModified(p));
0822:                aux.putConfigurationFragment(data, false);
0823:                assertTrue("now project is modified", pm.isModified(p));
0824:                evs = l.events();
0825:                assertEquals("pCF fires one event", 1, evs.length);
0826:                assertEquals("correct helper", h, evs[0].getHelper());
0827:                assertEquals("correct path", AntProjectHelper.PRIVATE_XML_PATH,
0828:                        evs[0].getPath());
0829:                assertTrue("expected change", evs[0].isExpected());
0830:                pm.saveProject(p);
0831:                assertEquals("saving project fires no new changes", 0, l
0832:                        .events().length);
0833:                doc = AntBasedTestUtil.slurpXml(h,
0834:                        AntProjectHelper.PRIVATE_XML_PATH);
0835:                config = doc.getDocumentElement();
0836:                data = Util.findElement(config, "data", "urn:test:private-aux");
0837:                assertNotNull("<data> still exists", data);
0838:                stuff = Util.findElement(data, "aux-private-stuff",
0839:                        "urn:test:private-aux");
0840:                assertNotNull("still have <aux-private-stuff/>", stuff);
0841:                assertEquals("attr written correctly", "val", stuff
0842:                        .getAttribute("attr"));
0843:                // Check that missing fragments are not returned.
0844:                Element bogus = aux.getConfigurationFragment("doesn't exist",
0845:                        "bogus", true);
0846:                assertNull("no such fragment - wrong name/ns", bogus);
0847:                bogus = aux.getConfigurationFragment("data", "bogus", true);
0848:                assertNull("no such fragment - wrong ns", bogus);
0849:                bogus = aux.getConfigurationFragment("doesn't exist",
0850:                        "urn:test:shared-aux", true);
0851:                assertNull("no such fragment - wrong name", bogus);
0852:                bogus = aux.getConfigurationFragment("data",
0853:                        "urn:test:shared-aux", false);
0854:                assertNull("no such fragment - wrong file", bogus);
0855:                // Try adding a new fragment.
0856:                Document temp = XMLUtil.createDocument("whatever", null, null,
0857:                        null);
0858:                data = temp.createElementNS("urn:test:whatever", "hello");
0859:                data.appendChild(temp.createTextNode("stuff"));
0860:                assertFalse("project currently unmodified", pm.isModified(p));
0861:                aux.putConfigurationFragment(data, true);
0862:                assertTrue("adding frag modified project", pm.isModified(p));
0863:                evs = l.events();
0864:                assertEquals("pCF fires one event", 1, evs.length);
0865:                assertEquals("correct path", AntProjectHelper.PROJECT_XML_PATH,
0866:                        evs[0].getPath());
0867:                pm.saveProject(p);
0868:                assertEquals("saving project fires no new changes", 0, l
0869:                        .events().length);
0870:                data = aux.getConfigurationFragment("hello",
0871:                        "urn:test:whatever", true);
0872:                assertNotNull("can retrieve new frag", data);
0873:                doc = AntBasedTestUtil.slurpXml(h,
0874:                        AntProjectHelper.PROJECT_XML_PATH);
0875:                config = Util.findElement(doc.getDocumentElement(),
0876:                        "configuration", AntProjectHelper.PROJECT_NS);
0877:                assertNotNull("<configuration> still exists", config);
0878:                data = Util.findElement(config, "hello", "urn:test:whatever");
0879:                assertNotNull("<hello> still exists", data);
0880:                assertEquals("correct nested contents too", "stuff", Util
0881:                        .findText(data));
0882:                // Try removing a fragment.
0883:                assertFalse("project is unmodified", pm.isModified(p));
0884:                assertTrue("can remove new frag", aux
0885:                        .removeConfigurationFragment("hello",
0886:                                "urn:test:whatever", true));
0887:                assertTrue("project is now modified", pm.isModified(p));
0888:                assertNull("now frag is gone", aux.getConfigurationFragment(
0889:                        "hello", "urn:test:whatever", true));
0890:                pm.saveProject(p);
0891:                doc = AntBasedTestUtil.slurpXml(h,
0892:                        AntProjectHelper.PROJECT_XML_PATH);
0893:                config = Util.findElement(doc.getDocumentElement(),
0894:                        "configuration", AntProjectHelper.PROJECT_NS);
0895:                assertNotNull("<configuration> still exists", config);
0896:                data = Util.findElement(config, "hello", "urn:test:whatever");
0897:                assertNull("now <hello> is gone", data);
0898:                assertFalse("cannot remove a frag that is not there", aux
0899:                        .removeConfigurationFragment("hello",
0900:                                "urn:test:whatever", true));
0901:                assertFalse(
0902:                        "trying to remove a nonexistent frag does not modify project",
0903:                        pm.isModified(p));
0904:
0905:                // check that created elements are ordered
0906:                data = temp.createElementNS("namespace", "ccc");
0907:                aux.putConfigurationFragment(data, true);
0908:                data = temp.createElementNS("namespace", "bbb");
0909:                aux.putConfigurationFragment(data, true);
0910:                data = temp.createElementNS("namespace", "aaa");
0911:                aux.putConfigurationFragment(data, true);
0912:                data = temp.createElementNS("namespace-1", "bbb");
0913:                aux.putConfigurationFragment(data, true);
0914:                data = temp.createElementNS("name-sp", "bbb");
0915:                aux.putConfigurationFragment(data, true);
0916:                data = temp.createElementNS("namespace", "aaaa");
0917:                aux.putConfigurationFragment(data, true);
0918:                pm.saveProject(p);
0919:                doc = AntBasedTestUtil.slurpXml(h,
0920:                        AntProjectHelper.PROJECT_XML_PATH);
0921:                config = Util.findElement(doc.getDocumentElement(),
0922:                        "configuration", AntProjectHelper.PROJECT_NS);
0923:                String[] names = new String[] { "aaa-namespace",
0924:                        "aaaa-namespace", "bbb-name-sp", "bbb-namespace",
0925:                        "bbb-namespace-1", "ccc-namespace",
0926:                        "data-urn:test:shared", "data-urn:test:shared-aux" };
0927:                int count = 0;
0928:                NodeList list = config.getChildNodes();
0929:                for (int i = 0; i < list.getLength(); i++) {
0930:                    Node n = list.item(i);
0931:                    if (n.getNodeType() != Node.ELEMENT_NODE) {
0932:                        continue;
0933:                    }
0934:                    assertEquals(names[count], n.getNodeName() + "-"
0935:                            + n.getNamespaceURI());
0936:                    count++;
0937:                }
0938:                assertEquals("Elements count does not match", names.length,
0939:                        count);
0940:
0941:                // XXX check that it cannot be used to load or store primary configuration data
0942:                // or other general fixed metadata
0943:                // XXX try overwriting data
0944:            }
0945:
0946:            public void test68872() throws Exception {
0947:                AuxiliaryConfiguration aux = p.getLookup().lookup(
0948:                        AuxiliaryConfiguration.class);
0949:                assertNotNull("AuxiliaryConfiguration present", aux);
0950:
0951:                Element data = aux.getConfigurationFragment("data",
0952:                        "urn:test:private-aux", false);
0953:                Element stuff = Util.findElement(data, "aux-private-stuff",
0954:                        "urn:test:private-aux");
0955:                assertNotNull("found <aux-private-stuff/>", stuff);
0956:                // Check write of private data.
0957:                stuff.setAttribute("attr", "val");
0958:                aux.putConfigurationFragment(data, false);
0959:                assertTrue("now project is modified", pm.isModified(p));
0960:
0961:                FileObject privateXMLFO = p.getProjectDirectory()
0962:                        .getFileObject("nbproject/private/private.xml");
0963:
0964:                assertNotNull(privateXMLFO);
0965:
0966:                File privateXML = FileUtil.toFile(privateXMLFO);
0967:
0968:                privateXML.delete();
0969:
0970:                privateXMLFO.refresh();
0971:                privateXMLFO.getParent().refresh();
0972:
0973:                pm.saveProject(p);
0974:
0975:                //the file should be renewed with new data:
0976:                assertTrue(privateXML.exists());
0977:
0978:                //check the data are written:
0979:                Document doc = AntBasedTestUtil.slurpXml(h,
0980:                        AntProjectHelper.PRIVATE_XML_PATH);
0981:                Element config = doc.getDocumentElement();
0982:                data = Util.findElement(config, "data", "urn:test:private-aux");
0983:                assertNotNull("<data> still exists", data);
0984:                stuff = Util.findElement(data, "aux-private-stuff",
0985:                        "urn:test:private-aux");
0986:                assertNotNull("still have <aux-private-stuff/>", stuff);
0987:                assertEquals("attr written correctly", "val", stuff
0988:                        .getAttribute("attr"));
0989:
0990:                //check that on-disk changes are not ignored if the project is saved:
0991:                privateXML.delete();
0992:
0993:                privateXMLFO.refresh();
0994:                privateXMLFO.getParent().refresh();
0995:
0996:                assertNull(aux.getConfigurationFragment("data",
0997:                        "urn:test:private-aux", false));
0998:            }
0999:
1000:            public void testCreatePropertyProvider() throws Exception {
1001:                PropertyProvider pp = h
1002:                        .getPropertyProvider(AntProjectHelper.PROJECT_PROPERTIES_PATH);
1003:                Map<String, String> defs = pp.getProperties();
1004:                assertEquals("correct number of defs", 3, defs.size());
1005:                assertEquals("correct value", "value1", defs.get("shared.prop"));
1006:                // Test changes.
1007:                MockChangeListener mcl = new MockChangeListener();
1008:                pp.addChangeListener(mcl);
1009:                EditableProperties ep = h
1010:                        .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
1011:                ep.setProperty("foo", "bar");
1012:                mcl.msg("no events from uncommitted changes").assertNoEvents();
1013:                h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
1014:                mcl.msg("got a change from setting a property").assertEvent();
1015:                defs = pp.getProperties();
1016:                assertEquals("correct new size", 4, defs.size());
1017:                assertEquals("correct new value", "bar", defs.get("foo"));
1018:                // No-op changes.
1019:                ep = ep.cloneProperties();
1020:                ep.setProperty("foo", "bar2");
1021:                ep.setProperty("foo", "bar");
1022:                h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
1023:                mcl.msg("no events from no-op changes").assertNoEvents();
1024:                // Deleting a property file.
1025:                h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, null);
1026:                mcl.msg("got a change from removing a property file")
1027:                        .assertEvent();
1028:                assertEquals("now have no definitions", Collections.EMPTY_MAP,
1029:                        pp.getProperties());
1030:                // Start off with no file, then create it.
1031:                String path = "foo.properties";
1032:                pp = h.getPropertyProvider(path);
1033:                pp.addChangeListener(mcl);
1034:                assertEquals("no defs initially", Collections.EMPTY_MAP, pp
1035:                        .getProperties());
1036:                assertNull("no file made yet", h.getProjectDirectory()
1037:                        .getFileObject(path));
1038:                ep = new EditableProperties();
1039:                ep.setProperty("one", "1");
1040:                ep.setProperty("two", "2");
1041:                h.putProperties(path, ep);
1042:                mcl.msg("making the file fired a change").assertEvent();
1043:                defs = pp.getProperties();
1044:                assertEquals("two defs", 2, defs.size());
1045:                assertEquals("right value #1", "1", defs.get("one"));
1046:                assertEquals("right value #2", "2", defs.get("two"));
1047:                assertNull("no file yet saved to disk", h.getProjectDirectory()
1048:                        .getFileObject(path));
1049:                ep.setProperty("three", "3");
1050:                mcl.msg("no events from uncomm. change").assertNoEvents();
1051:                h.putProperties(path, ep);
1052:                mcl.msg("now have changed new file").assertEvent();
1053:                defs = pp.getProperties();
1054:                assertEquals("three defs", 3, defs.size());
1055:                // XXX test that saving the project fires no additional changes
1056:                // XXX test changes fired if file modified (or created or removed) on disk
1057:            }
1058:
1059:            /* XXX unable to simulate #50198 in a test environment:
1060:            public void testMultithreadedAccessToXmlData() throws Exception {
1061:                class R implements Runnable {
1062:                    private final int x;
1063:                    public R(int x) {
1064:                        this.x = x;
1065:                    }
1066:                    public void run() {
1067:                        System.out.println("starting #" + x);
1068:                        for (int i = 0; i < 1000; i++) {
1069:                            Element data = h.getPrimaryConfigurationData(true);
1070:                            Util.findSubElements(data);
1071:                            if (i % 100 == 0) {
1072:                                System.out.println("in the middle of #" + x);
1073:                                try {
1074:                                    Thread.sleep((long) (Math.random() * 25));
1075:                                } catch (InterruptedException e) {
1076:                                    e.printStackTrace();
1077:                                }
1078:                            }
1079:                        }
1080:                        System.out.println("finishing #" + x);
1081:                    }
1082:                }
1083:                Thread t1 = new Thread(new R(1));
1084:                Thread t2 = new Thread(new R(2));
1085:                t1.start();
1086:                t2.start();
1087:                t1.join();
1088:                t2.join();
1089:            }
1090:             */
1091:
1092:            public void testStockPropertyPreprovider() throws Exception {
1093:                Map<String, String> props = h.getStockPropertyPreprovider()
1094:                        .getProperties();
1095:                assertEquals(FileUtil.toFile(projdir).getAbsolutePath(), props
1096:                        .get("basedir"));
1097:                assertEquals(new File(getWorkDir(), "ant").getAbsolutePath(),
1098:                        props.get("ant.home"));
1099:                assertEquals(antJar.getAbsolutePath(), props
1100:                        .get("ant.core.lib"));
1101:            }
1102:
1103:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.