001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project;
043:
044: import java.io.BufferedReader;
045: import java.io.File;
046: import java.io.FileInputStream;
047: import java.io.FileNotFoundException;
048: import java.io.FileOutputStream;
049: import java.io.FileReader;
050: import java.io.IOException;
051: import java.io.InputStream;
052: import java.io.InputStreamReader;
053: import java.io.OutputStream;
054: import java.io.PrintWriter;
055: import java.util.ArrayList;
056: import java.util.Arrays;
057: import java.util.Collections;
058: import java.util.HashMap;
059: import java.util.List;
060: import java.util.Map;
061: import java.util.Set;
062: import java.util.SortedSet;
063: import junit.framework.TestCase;
064: import org.netbeans.api.project.ProjectInformation;
065: import org.netbeans.api.project.ProjectUtils;
066: import org.netbeans.modules.apisupport.project.CreatedModifiedFiles.Operation;
067: import org.netbeans.modules.apisupport.project.layers.LayerTestBase;
068: import org.netbeans.modules.apisupport.project.ui.customizer.ModuleDependency;
069: import org.netbeans.modules.apisupport.project.universe.LocalizedBundleInfo;
070: import org.netbeans.spi.project.support.ant.EditableProperties;
071: import org.openide.filesystems.FileLock;
072: import org.openide.filesystems.FileObject;
073: import org.openide.filesystems.FileUtil;
074: import org.openide.filesystems.test.TestFileUtils;
075: import org.openide.modules.SpecificationVersion;
076:
077: /**
078: * Tests {@link CreatedModifiedFiles}.
079: * @author Martin Krauskopf
080: */
081: public class CreatedModifiedFilesTest extends LayerTestBase {
082:
083: private static final String[] HTML_CONTENT = { "<html>",
084: "i am some ${file}", "</html>" };
085:
086: private static final Map<String, String> TOKENS_MAP = new HashMap<String, String>();
087:
088: static {
089: TOKENS_MAP.put("file", "template");
090: }
091:
092: private static final String[] HTML_CONTENT_TOKENIZED = { "<html>",
093: "i am some template", "</html>" };
094:
095: public CreatedModifiedFilesTest(String name) {
096: super (name);
097: }
098:
099: protected @Override
100: void setUp() throws Exception {
101: super .setUp();
102: TestBase.initializeBuildProperties(getWorkDir(), getDataDir());
103: }
104:
105: public void testCreatedModifiedFiles() throws Exception {
106: NbModuleProject project = TestBase.generateStandaloneModule(
107: getWorkDir(), "module1");
108: CreatedModifiedFiles cmf = new CreatedModifiedFiles(project);
109: cmf.add(cmf.bundleKeyDefaultBundle(LocalizedBundleInfo.NAME,
110: "Much Better Name"));
111: cmf.add(cmf.bundleKey("src/custom.properties", "some.property",
112: "some value"));
113: cmf.add(cmf.addLoaderSection("org/example/module1/MyExtLoader",
114: null));
115: cmf.add(cmf.createFile(
116: "src/org/example/module1/resources/template.html",
117: createFile(HTML_CONTENT)));
118: cmf.add(cmf.addLookupRegistration(
119: "org.example.spi.somemodule.ProvideMe",
120: "org.example.module1.ProvideMeImpl", false));
121:
122: assertRelativePaths(
123: new String[] {
124: "src/META-INF/services/org.example.spi.somemodule.ProvideMe",
125: "src/custom.properties",
126: "src/org/example/module1/resources/template.html" },
127: cmf.getCreatedPaths());
128: assertRelativePaths(
129: new String[] { "manifest.mf",
130: "src/org/example/module1/resources/Bundle.properties" },
131: cmf.getModifiedPaths());
132:
133: cmf.run();
134: }
135:
136: public void testBundleKeyDefaultBundle() throws Exception {
137: NbModuleProject project = TestBase.generateStandaloneModule(
138: getWorkDir(), "module1");
139: ProjectInformation pi = ProjectUtils.getInformation(project);
140: assertEquals("display name before from bundle",
141: "Testing Module", pi.getDisplayName());
142: assertEquals("display name before from project",
143: "Testing Module", project.getBundleInfo()
144: .getDisplayName());
145:
146: CreatedModifiedFiles cmf = new CreatedModifiedFiles(project);
147: Operation op = cmf.bundleKeyDefaultBundle(
148: LocalizedBundleInfo.NAME, "Much Better Name");
149: assertRelativePath(
150: "src/org/example/module1/resources/Bundle.properties",
151: op.getModifiedPaths());
152: op.run();
153:
154: pi = ProjectUtils.getInformation(project);
155: assertEquals("display name after from bundle",
156: "Much Better Name", pi.getDisplayName());
157: assertEquals("display name after from project",
158: "Much Better Name", project.getBundleInfo()
159: .getDisplayName());
160: }
161:
162: public void testBundleKey() throws Exception {
163: NbModuleProject project = TestBase.generateStandaloneModule(
164: getWorkDir(), "module1");
165:
166: CreatedModifiedFiles cmf = new CreatedModifiedFiles(project);
167: Operation op = cmf.bundleKey("src/custom.properties",
168: "some.property", "some value");
169:
170: assertRelativePath("src/custom.properties", op
171: .getCreatedPaths());
172:
173: cmf.add(op);
174: cmf.run();
175:
176: EditableProperties ep = Util.loadProperties(FileUtil
177: .toFileObject(TestBase.file(getWorkDir(),
178: "module1/src/custom.properties")));
179: assertEquals("property created", "some value", ep
180: .getProperty("some.property"));
181: }
182:
183: public void testAddLoaderSection() throws Exception {
184: NbModuleProject project = TestBase.generateStandaloneModule(
185: getWorkDir(), "module1");
186:
187: CreatedModifiedFiles cmf = new CreatedModifiedFiles(project);
188: Operation op = cmf.addLoaderSection(
189: "org/example/module1/MyExtLoader", null);
190:
191: assertRelativePath("manifest.mf", op.getModifiedPaths());
192:
193: op.run();
194:
195: EditableManifest em = Util.loadManifest(FileUtil
196: .toFileObject(TestBase.file(getWorkDir(),
197: "module1/manifest.mf")));
198: assertEquals("loader section was added", "Loader", em
199: .getAttribute("OpenIDE-Module-Class",
200: "org/example/module1/MyExtLoader.class"));
201: }
202:
203: public void testAddLookupRegistration() throws Exception {
204: NbModuleProject project = TestBase.generateStandaloneModule(
205: getWorkDir(), "module1");
206:
207: CreatedModifiedFiles cmf = new CreatedModifiedFiles(project);
208: cmf.add(cmf.addLookupRegistration(
209: "org.example.spi.somemodule.ProvideMe",
210: "org.example.module1.ProvideMeImpl1", false));
211: cmf.add(cmf.addLookupRegistration(
212: "org.example.spi.somemodule.ProvideMe",
213: "org.example.module1.ProvideMeImpl2", false));
214: cmf.add(cmf.addLookupRegistration(
215: "org.example.spi.somemodule.ProvideMe",
216: "org.example.module1.ProvideMeImpl1", true));
217:
218: String[] paths = {
219: "src/META-INF/services/org.example.spi.somemodule.ProvideMe",
220: "test/unit/src/META-INF/services/org.example.spi.somemodule.ProvideMe", };
221: assertRelativePaths(paths, cmf.getCreatedPaths());
222:
223: cmf.run();
224:
225: FileObject registry = project.getProjectDirectory()
226: .getFileObject(paths[0]);
227: assertNotNull(registry);
228: InputStream is = registry.getInputStream();
229: try {
230: BufferedReader r = new BufferedReader(
231: new InputStreamReader(is, "UTF-8"));
232: assertEquals("org.example.module1.ProvideMeImpl1", r
233: .readLine());
234: assertEquals("org.example.module1.ProvideMeImpl2", r
235: .readLine());
236: assertEquals(null, r.readLine());
237: } finally {
238: is.close();
239: }
240: registry = project.getProjectDirectory()
241: .getFileObject(paths[1]);
242: assertNotNull(registry);
243: is = registry.getInputStream();
244: try {
245: BufferedReader r = new BufferedReader(
246: new InputStreamReader(is, "UTF-8"));
247: assertEquals("org.example.module1.ProvideMeImpl1", r
248: .readLine());
249: assertEquals(null, r.readLine());
250: } finally {
251: is.close();
252: }
253: }
254:
255: public void testCreateFile() throws Exception {
256: NbModuleProject project = TestBase.generateStandaloneModule(
257: getWorkDir(), "module1");
258:
259: CreatedModifiedFiles cmf = new CreatedModifiedFiles(project);
260:
261: String templatePath = "src/org/example/module1/resources/template.html";
262: Operation op = cmf.createFile(templatePath,
263: createFile(HTML_CONTENT));
264:
265: assertRelativePath(templatePath, op.getCreatedPaths());
266:
267: cmf.add(op);
268: cmf.run();
269:
270: assertFileContent(HTML_CONTENT, new File(getWorkDir(),
271: "module1/" + templatePath));
272: }
273:
274: public void testCreateBinaryFile() throws Exception {
275: NbModuleProject project = TestBase.generateStandaloneModule(
276: getWorkDir(), "module1");
277:
278: CreatedModifiedFiles cmf = new CreatedModifiedFiles(project);
279:
280: String templatePath = "src/org/example/module1/resources/binarytemplate.zip";
281:
282: FileObject binaryFile = createBinaryFile(HTML_CONTENT);
283:
284: Operation op = cmf.createFile(templatePath, binaryFile);
285:
286: assertRelativePath(templatePath, op.getCreatedPaths());
287:
288: cmf.add(op);
289: cmf.run();
290:
291: assertFileContent(binaryFile, new File(getWorkDir(), "module1/"
292: + templatePath));
293: }
294:
295: public void testCreateFileWithSubstitutions() throws Exception {
296: NbModuleProject project = TestBase.generateStandaloneModule(
297: getWorkDir(), "module1");
298:
299: CreatedModifiedFiles cmf = new CreatedModifiedFiles(project);
300:
301: String templatePath = "src/org/example/module1/resources/template.html";
302: Operation op = cmf.createFileWithSubstitutions(templatePath,
303: createFile(HTML_CONTENT), TOKENS_MAP);
304:
305: assertRelativePath(templatePath, op.getCreatedPaths());
306:
307: cmf.add(op);
308: cmf.run();
309:
310: assertFileContent(HTML_CONTENT_TOKENIZED, new File(
311: getWorkDir(), "module1/" + templatePath));
312: }
313:
314: public void testAddModuleDependency() throws Exception {
315: NbModuleProject project = TestBase.generateStandaloneModule(
316: getWorkDir(), "module1");
317:
318: CreatedModifiedFiles cmf = new CreatedModifiedFiles(project);
319:
320: Operation op = cmf.addModuleDependency(
321: "org.apache.tools.ant.module", "3",
322: new SpecificationVersion("3.9"), true);
323:
324: assertRelativePath("nbproject/project.xml", op
325: .getModifiedPaths());
326:
327: cmf.add(op);
328: cmf.run();
329:
330: ProjectXMLManager pxm = new ProjectXMLManager(project);
331: Set<ModuleDependency> deps = pxm.getDirectDependencies();
332: assertEquals("one dependency", 1, deps.size());
333: ModuleDependency antDep = deps.iterator().next();
334: assertEquals("cnb", "org.apache.tools.ant.module", antDep
335: .getModuleEntry().getCodeNameBase());
336: assertEquals("release version", "3", antDep.getReleaseVersion());
337: assertEquals("specification version", "3.9", antDep
338: .getSpecificationVersion());
339: assertTrue("compile dependeny", antDep.hasCompileDependency());
340: assertFalse("implementation dependeny", antDep
341: .hasImplementationDepedendency());
342: }
343:
344: public void testTheSameModuleDependencyTwice() throws Exception {
345: NbModuleProject project = TestBase.generateStandaloneModule(
346: getWorkDir(), "module1");
347:
348: CreatedModifiedFiles cmf = new CreatedModifiedFiles(project);
349:
350: Operation op = cmf.addModuleDependency(
351: "org.apache.tools.ant.module", null, null, false);
352:
353: assertRelativePath("nbproject/project.xml", op
354: .getModifiedPaths());
355:
356: cmf.add(op);
357: cmf.add(op);
358: cmf.run();
359:
360: ProjectXMLManager pxm = new ProjectXMLManager(project);
361: Set deps = pxm.getDirectDependencies();
362: assertEquals("one dependency", 1, deps.size());
363: ModuleDependency antDep = (ModuleDependency) deps.toArray()[0];
364: assertEquals("cnb", "org.apache.tools.ant.module", antDep
365: .getModuleEntry().getCodeNameBase());
366: }
367:
368: public void testCreateLayerEntry() throws Exception {
369: NbModuleProject project = TestBase.generateStandaloneModule(
370: getWorkDir(), "module1");
371:
372: CreatedModifiedFiles cmf = new CreatedModifiedFiles(project);
373: Operation layerOp = cmf
374: .createLayerEntry(
375: "Menu/Tools/org-example-module1-BeepAction.instance",
376: null, null, null, Collections
377: .<String, Object> singletonMap(
378: "position", 400));
379: layerOp.run();
380:
381: layerOp = cmf.createLayerEntry(
382: "Services/org-example-module1-Module1UI.settings",
383: null, null, null, null);
384: cmf.add(layerOp);
385: assertRelativePath(
386: "src/org/example/module1/resources/layer.xml", layerOp
387: .getModifiedPaths());
388:
389: layerOp = cmf.createLayerEntry(
390: "Menu/Tools/org-example-module1-BlareAction.instance",
391: null, null, null, null);
392: cmf.add(layerOp);
393:
394: layerOp = cmf
395: .createLayerEntry(
396: "Menu/Tools/org-example-module1-DrumAction.instance",
397: null, null, null, Collections
398: .<String, Object> singletonMap(
399: "position", 405));
400: cmf.add(layerOp);
401:
402: layerOp = cmf.orderLayerEntry("Menu/Tools",
403: "org-example-module1-BeepAction.instance",
404: "org-example-module1-BlareAction.instance",
405: "org-example-module1-DrumAction.instance");
406: cmf.add(layerOp);
407:
408: layerOp = cmf.createLayerEntry(
409: "Services/org-example-module1-Other.settings",
410: createFile(HTML_CONTENT), null, null, null);
411: cmf.add(layerOp);
412:
413: layerOp = cmf.orderLayerEntry("Services", null,
414: "org-example-module1-Other.settings",
415: "org-example-module1-Module1UI.settings");
416: cmf.add(layerOp);
417:
418: layerOp = cmf.createLayerEntry(
419: "Services/org-example-module1-Tokenized.settings",
420: createFile(HTML_CONTENT), TOKENS_MAP, null, null);
421: cmf.add(layerOp);
422:
423: layerOp = cmf
424: .createLayerEntry(
425: "Services/org-example-module1-LocalizedAndTokened.settings",
426: createFile(HTML_CONTENT), TOKENS_MAP,
427: "Some Settings", null);
428: cmf.add(layerOp);
429:
430: assertRelativePaths(new String[] {
431: "src/org/example/module1/resources/Bundle.properties",
432: "src/org/example/module1/resources/layer.xml" }, cmf
433: .getModifiedPaths());
434: assertRelativePaths(
435: new String[] {
436: "src/org/example/module1/resources/org-example-module1-LocalizedAndTokenedSettings.xml",
437: "src/org/example/module1/resources/org-example-module1-OtherSettings.xml",
438: "src/org/example/module1/resources/org-example-module1-TokenizedSettings.xml" },
439: cmf.getCreatedPaths());
440: cmf.run();
441:
442: assertFileContent(
443: HTML_CONTENT_TOKENIZED,
444: new File(
445: getWorkDir(),
446: "module1/src/org/example/module1/resources/org-example-module1-TokenizedSettings.xml"));
447:
448: // check layer content
449: String[] supposedContent = {
450: "<filesystem>",
451: "<folder name=\"Menu\">",
452: "<folder name=\"Tools\">",
453: "<file name=\"org-example-module1-BeepAction.instance\">",
454: "<attr name=\"position\" intvalue=\"400\"/>",
455: "</file>",
456: "<file name=\"org-example-module1-BlareAction.instance\">",
457: "<attr name=\"position\" intvalue=\"402\"/>",
458: "</file>",
459: "<file name=\"org-example-module1-DrumAction.instance\">",
460: "<attr name=\"position\" intvalue=\"405\"/>",
461: "</file>",
462: "</folder>",
463: "</folder>",
464: "<folder name=\"Services\">",
465: "<attr name=\"org-example-module1-Other.settings/org-example-module1-Module1UI.settings\" boolvalue=\"true\"/>",
466: "<file name=\"org-example-module1-LocalizedAndTokened.settings\" url=\"org-example-module1-LocalizedAndTokenedSettings.xml\">",
467: "<attr name=\"SystemFileSystem.localizingBundle\" stringvalue=\"org.example.module1.resources.Bundle\"/>",
468: "</file>",
469: "<file name=\"org-example-module1-Module1UI.settings\"/>",
470: "<file name=\"org-example-module1-Other.settings\" url=\"org-example-module1-OtherSettings.xml\"/>",
471: "<file name=\"org-example-module1-Tokenized.settings\" url=\"org-example-module1-TokenizedSettings.xml\"/>",
472: "</folder>", "</filesystem>" };
473: assertLayerContent(supposedContent, new File(getWorkDir(),
474: "module1/src/org/example/module1/resources/layer.xml"));
475:
476: // check bundle content
477: EditableProperties ep = Util
478: .loadProperties(FileUtil
479: .toFileObject(TestBase
480: .file(getWorkDir(),
481: "module1/src/org/example/module1/resources/Bundle.properties")));
482: assertEquals(
483: "localized name property",
484: "Some Settings",
485: ep
486: .getProperty("Services/org-example-module1-LocalizedAndTokened.settings"));
487: assertEquals("module name", "Testing Module", ep
488: .getProperty("OpenIDE-Module-Name"));
489: }
490:
491: public void testCreateLayerAttribute() throws Exception {
492: NbModuleProject project = TestBase.generateStandaloneModule(
493: getWorkDir(), "module1");
494: CreatedModifiedFiles cmf = new CreatedModifiedFiles(project);
495: String fqClassName = "org.example.module1.BeepAction";
496: String dashedFqClassName = fqClassName.replace('.', '-');
497: String layerPath = "Actions/Tools/" + dashedFqClassName
498: + ".instance";
499:
500: Operation op = cmf.createLayerEntry(layerPath, null, null,
501: null, null);
502: cmf.add(op);
503:
504: op = cmf.createLayerAttribute(layerPath, "instanceClass",
505: fqClassName);
506: assertRelativePath(
507: "src/org/example/module1/resources/layer.xml", op
508: .getModifiedPaths());
509:
510: cmf.add(op);
511: cmf.run();
512:
513: String[] supposedContent = {
514: "<filesystem>",
515: "<folder name=\"Actions\">",
516: "<folder name=\"Tools\">",
517: "<file name=\"org-example-module1-BeepAction.instance\">",
518: "<attr name=\"instanceClass\" stringvalue=\"org.example.module1.BeepAction\"/>",
519: "</file>", "</folder>", "</folder>", "</filesystem>" };
520: assertLayerContent(supposedContent, new File(getWorkDir(),
521: "module1/src/org/example/module1/resources/layer.xml"));
522: }
523:
524: /** @see "#64273" */
525: public void testCreateLayerEntryWithoutLocalizingBundle()
526: throws Exception {
527: NbModuleProject project = TestBase.generateStandaloneModule(
528: getWorkDir(), "module");
529: project.getProjectDirectory().getFileObject(
530: "src/org/example/module/resources/Bundle.properties")
531: .delete();
532: FileObject mf = project.getProjectDirectory().getFileObject(
533: "manifest.mf");
534: EditableManifest m;
535: InputStream is = mf.getInputStream();
536: try {
537: m = new EditableManifest(is);
538: } finally {
539: is.close();
540: }
541: m.removeAttribute("OpenIDE-Module-Localizing-Bundle", null);
542: FileLock lock = mf.lock();
543: try {
544: OutputStream os = mf.getOutputStream(lock);
545: try {
546: m.write(os);
547: } finally {
548: os.close();
549: }
550: } finally {
551: lock.releaseLock();
552: }
553: CreatedModifiedFiles cmf = new CreatedModifiedFiles(project);
554: Operation op = cmf
555: .createLayerEntry("f", null, null, "F!", null);
556: cmf.add(op);
557: cmf.run();
558: String[] supposedContent = new String[] { "<filesystem>",
559: "<file name=\"f\"/>", "</filesystem>" };
560: assertLayerContent(supposedContent, new File(getWorkDir(),
561: "module/src/org/example/module/resources/layer.xml"));
562: }
563:
564: public void testLayerEntryOverlappingFilenames() throws Exception { // #85138
565: NbModuleProject project = TestBase.generateStandaloneModule(
566: getWorkDir(), "module");
567: FileObject content = FileUtil.createMemoryFileSystem()
568: .getRoot().createData("x");
569: CreatedModifiedFiles cmf = new CreatedModifiedFiles(project);
570: Operation op = cmf.createLayerEntry("file", content, null,
571: null, null);
572: cmf.add(op);
573: assertEquals("[src/org/example/module/resources/layer.xml]",
574: Arrays.toString(op.getModifiedPaths()));
575: assertEquals("[src/org/example/module/resources/file]", Arrays
576: .toString(op.getCreatedPaths()));
577: assertEquals("[]", Arrays.toString(op.getInvalidPaths()));
578: cmf.run();
579: assertEquals("[src/org/example/module/resources/layer.xml]",
580: Arrays.toString(cmf.getModifiedPaths()));
581: assertEquals("[src/org/example/module/resources/file]", Arrays
582: .toString(cmf.getCreatedPaths()));
583: assertEquals("[]", Arrays.toString(cmf.getInvalidPaths()));
584: assertNotNull(project.getProjectDirectory().getFileObject(
585: "src/org/example/module/resources/file"));
586: // #1: cannot add the same layer path twice.
587: cmf = new CreatedModifiedFiles(project);
588: op = cmf.createLayerEntry("file", content, null, null, null);
589: cmf.add(op);
590: assertEquals("[]", Arrays.toString(op.getModifiedPaths()));
591: assertEquals("[]", Arrays.toString(op.getCreatedPaths()));
592: assertEquals("[file]", Arrays.toString(op.getInvalidPaths()));
593: // #2: if files of the same basename are added twice, uniquify external file.
594: cmf = new CreatedModifiedFiles(project);
595: op = cmf
596: .createLayerEntry("dir/file", content, null, null, null);
597: cmf.add(op);
598: assertEquals("[src/org/example/module/resources/layer.xml]",
599: Arrays.toString(op.getModifiedPaths()));
600: assertEquals("[src/org/example/module/resources/file_1]",
601: Arrays.toString(op.getCreatedPaths()));
602: assertEquals("[]", Arrays.toString(op.getInvalidPaths()));
603: cmf.run();
604: assertEquals("[src/org/example/module/resources/layer.xml]",
605: Arrays.toString(cmf.getModifiedPaths()));
606: assertEquals("[src/org/example/module/resources/file_1]",
607: Arrays.toString(cmf.getCreatedPaths()));
608: assertEquals("[]", Arrays.toString(cmf.getInvalidPaths()));
609: assertNotNull(project.getProjectDirectory().getFileObject(
610: "src/org/example/module/resources/file_1"));
611: }
612:
613: public static void assertRelativePath(String expectedPath,
614: String[] paths) {
615: TestCase.assertEquals("one path", 1, paths.length);
616: TestCase.assertEquals("created, modified paths", expectedPath,
617: paths[0]);
618: }
619:
620: public static void assertRelativePath(String expectedPath,
621: SortedSet<String> paths) {
622: String[] s = new String[paths.size()];
623: assertRelativePath(expectedPath, paths.toArray(s));
624: }
625:
626: public static void assertRelativePaths(String[] expectedPaths,
627: String[] paths) {
628: TestCase.assertEquals("created, modified paths", Arrays
629: .asList(expectedPaths), Arrays.asList(paths));
630: }
631:
632: private FileObject createFile(String[] content) throws IOException {
633: File myTemplate = new File(getWorkDir(), "myTemplate.html");
634: OutputStream myTemplateOS = new FileOutputStream(myTemplate);
635: PrintWriter pw = new PrintWriter(myTemplateOS);
636: try {
637: for (String line : content) {
638: pw.println(line);
639: }
640: } finally {
641: pw.close();
642: }
643: return FileUtil.toFileObject(myTemplate);
644: }
645:
646: private FileObject createBinaryFile(String[] content)
647: throws IOException {
648: StringBuilder b = new StringBuilder();
649: for (String section : content) {
650: b.append(section);
651: }
652: return TestFileUtils.writeZipFile(FileUtil
653: .toFileObject(getWorkDir()), "myTemplate.zip",
654: "a/b/c/d.txt:" + b);
655: }
656:
657: private void assertFileContent(String[] content, File file)
658: throws IOException {
659: assertTrue("file exist and is a regular file", file.isFile());
660: BufferedReader br = new BufferedReader(new FileReader(file));
661: try {
662: for (int i = 0; i < content.length; i++) {
663: assertEquals("file content", content[i], br.readLine());
664: }
665: assertNull(br.readLine());
666: } finally {
667: br.close();
668: }
669: }
670:
671: private void assertFileContent(FileObject f1, File f2)
672: throws IOException {
673: InputStream is = f1.getInputStream();
674: InputStream is2 = new FileInputStream(f2);
675:
676: try {
677: byte[] content = new byte[is.available()];
678: is.read(content);
679:
680: byte[] content2 = new byte[is2.available()];
681: is2.read(content2);
682:
683: assertEquals(content.length, content2.length);
684: for (int i = 0; i < content.length; i++) {
685: assertEquals("file content", content[i], content2[i]);
686: }
687: } finally {
688: is.close();
689: is2.close();
690: }
691: }
692:
693: public static void assertLayerContent(
694: final String[] supposedContent, final File layerF)
695: throws IOException, FileNotFoundException {
696: BufferedReader reader = new BufferedReader(new FileReader(
697: layerF));
698: List<String> actualContent = new ArrayList<String>();
699: boolean fsElementReached = false;
700: String line;
701:
702: try {
703: while ((line = reader.readLine()) != null) {
704: line = line.trim();
705: if (!fsElementReached
706: && line.equals(supposedContent[0])) {
707: fsElementReached = true;
708: actualContent.add(line);
709: continue;
710: }
711: if (fsElementReached) {
712: actualContent.add(line);
713: }
714: }
715: } finally {
716: reader.close();
717: }
718:
719: assertEquals("content of layer", Arrays.asList(supposedContent)
720: .toString(), actualContent.toString());
721: }
722:
723: }
|