Source Code Cross Referenced for ValidateLayerConsistencyTest.java in  » IDE-Netbeans » library » org » netbeans » core » projects » 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 » library » org.netbeans.core.projects 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 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.core.projects;
043:
044:        import java.io.ByteArrayOutputStream;
045:        import java.io.BufferedInputStream;
046:        import java.io.File;
047:        import java.io.IOException;
048:        import java.io.InputStream;
049:        import java.net.URL;
050:        import java.text.MessageFormat;
051:        import java.util.ArrayList;
052:        import java.util.Arrays;
053:        import java.util.Collection;
054:        import java.util.Collections;
055:        import java.util.Enumeration;
056:        import java.util.HashMap;
057:        import java.util.HashSet;
058:        import java.util.List;
059:        import java.util.Map;
060:        import java.util.Set;
061:        import java.util.TreeSet;
062:        import java.util.jar.Manifest;
063:        import java.util.logging.Handler;
064:        import java.util.logging.Level;
065:        import java.util.logging.LogRecord;
066:        import java.util.logging.Logger;
067:        import java.util.regex.Matcher;
068:        import java.util.regex.Pattern;
069:        import org.netbeans.core.startup.layers.LayerCacheManager;
070:        import org.netbeans.junit.NbTestCase;
071:        import org.openide.cookies.InstanceCookie;
072:        import org.openide.filesystems.FileObject;
073:        import org.openide.filesystems.FileSystem;
074:        import org.openide.filesystems.FileUtil;
075:        import org.openide.filesystems.MultiFileSystem;
076:        import org.openide.filesystems.Repository;
077:        import org.openide.filesystems.XMLFileSystem;
078:        import org.openide.loaders.DataFolder;
079:        import org.openide.loaders.DataObject;
080:        import org.openide.loaders.DataShadow;
081:        import org.openide.modules.Dependency;
082:        import org.openide.modules.ModuleInfo;
083:        import org.openide.util.Lookup;
084:        import org.openide.util.Mutex;
085:
086:        /** Checks consistency of System File System contents.
087:         *
088:         * @author Jaroslav Tulach
089:         */
090:        public class ValidateLayerConsistencyTest extends NbTestCase {
091:
092:            private ClassLoader contextClassLoader;
093:
094:            public ValidateLayerConsistencyTest(String name) {
095:                super (name);
096:            }
097:
098:            public @Override
099:            void setUp() throws Exception {
100:                clearWorkDir();
101:                Mutex.EVENT.readAccess(new Mutex.Action<Void>() {
102:                    public Void run() {
103:                        contextClassLoader = Thread.currentThread()
104:                                .getContextClassLoader();
105:                        Thread.currentThread().setContextClassLoader(
106:                                Lookup.getDefault().lookup(ClassLoader.class));
107:                        return null;
108:                    }
109:                });
110:            }
111:
112:            public @Override
113:            void tearDown() {
114:                Mutex.EVENT.readAccess(new Mutex.Action<Void>() {
115:                    public Void run() {
116:                        Thread.currentThread().setContextClassLoader(
117:                                contextClassLoader);
118:                        return null;
119:                    }
120:                });
121:            }
122:
123:            protected @Override
124:            boolean runInEQ() {
125:                return true;
126:            }
127:
128:            /* Causes mysterious failure in otherwise OK-looking UI/Runtime/org-netbeans-modules-db-explorer-nodes-RootNode.instance: 
129:            @Override
130:            protected Level logLevel() {
131:                return Level.FINER;
132:            }
133:             */
134:
135:            public void testAreAttributesFine() {
136:                List<String> errors = new ArrayList<String>();
137:
138:                Enumeration<? extends FileObject> files = Repository
139:                        .getDefault().getDefaultFileSystem().getRoot()
140:                        .getChildren(true);
141:                while (files.hasMoreElements()) {
142:                    FileObject fo = files.nextElement();
143:
144:                    // XXX #16761 Removing attr in MFO causes storing special-null value even in unneeded cases.
145:                    // When the issue is fixed remove this hack.
146:                    if ("Windows2/Modes/debugger".equals(fo.getPath()) // NOI18N
147:                            || "Windows2/Modes/explorer".equals(fo.getPath())) { // NOI18N
148:                        continue;
149:                    }
150:
151:                    if ("Keymaps/NetBeans/D-BACK_QUOTE.shadow".equals(fo
152:                            .getPath())
153:                            || "Keymaps/NetBeans55/D-BACK_QUOTE.shadow"
154:                                    .equals(fo.getPath())
155:                            || "Keymaps/Emacs/D-BACK_QUOTE.shadow".equals(fo
156:                                    .getPath())) {
157:                        // #46753
158:                        continue;
159:                    }
160:
161:                    Enumeration<String> attrs = fo.getAttributes();
162:                    while (attrs.hasMoreElements()) {
163:                        String name = attrs.nextElement();
164:
165:                        if (fo.getAttribute(name) == null) {
166:                            errors.add("\n    File " + fo + " attribute name "
167:                                    + name);
168:                        }
169:                    }
170:                }
171:
172:                if (!errors.isEmpty()) {
173:                    fail("Some attributes in files are unreadable" + errors);
174:                }
175:            }
176:
177:            public void testValidShadows() {
178:                // might be better to move into editor/options tests as it is valid only if there are options
179:                List<String> errors = new ArrayList<String>();
180:
181:                FileObject root = Repository.getDefault()
182:                        .getDefaultFileSystem().getRoot();
183:
184:                Enumeration<? extends FileObject> en = root.getChildren(true);
185:                int cnt = 0;
186:                while (en.hasMoreElements()) {
187:                    FileObject fo = en.nextElement();
188:                    cnt++;
189:
190:                    // XXX #16761 Removing attr in MFO causes storing special-null value even in unneeded cases.
191:                    // When the issue is fixed remove this hack.
192:                    if ("Windows2/Modes/debugger".equals(fo.getPath()) // NOI18N
193:                            || "Windows2/Modes/explorer".equals(fo.getPath())) { // NOI18N
194:                        continue;
195:                    }
196:
197:                    if ("Keymaps/NetBeans/D-BACK_QUOTE.shadow".equals(fo
198:                            .getPath())
199:                            || "Keymaps/NetBeans55/D-BACK_QUOTE.shadow"
200:                                    .equals(fo.getPath())
201:                            || "Keymaps/Emacs/D-BACK_QUOTE.shadow".equals(fo
202:                                    .getPath())) {
203:                        // #46753
204:                        continue;
205:                    }
206:
207:                    try {
208:                        DataObject obj = DataObject.find(fo);
209:                        DataShadow ds = obj.getCookie(DataShadow.class);
210:                        if (ds != null) {
211:                            Object o = ds.getOriginal();
212:                            if (o == null) {
213:                                errors
214:                                        .add("\nFile " + fo
215:                                                + " has no original.");
216:                            }
217:                        } else if ("shadow".equals(fo.getExt())) {
218:                            errors.add("\nFile " + fo
219:                                    + " is not a valid DataShadow.");
220:                        }
221:                    } catch (Exception ex) {
222:                        ex.printStackTrace();
223:                        errors.add("\n    File " + fo + " thrown exception "
224:                                + ex);
225:                    }
226:                }
227:
228:                if (!errors.isEmpty()) {
229:                    fail("Some shadow files in NetBeans profile are broken:"
230:                            + errors);
231:                }
232:
233:                if (ValidateLayerConsistencyTest.class.getClassLoader() == ClassLoader
234:                        .getSystemClassLoader()) {
235:                    // do not check the count as this probably means we are running
236:                    // plain Unit test and not inside the IDE mode
237:                    return;
238:                }
239:
240:                if (cnt == 0) {
241:                    fail("No file objects on system file system!");
242:                }
243:            }
244:
245:            public void testContentCanBeRead() {
246:                List<String> errors = new ArrayList<String>();
247:                byte[] buffer = new byte[4096];
248:
249:                Enumeration<? extends FileObject> files = Repository
250:                        .getDefault().getDefaultFileSystem().getRoot()
251:                        .getChildren(true);
252:                while (files.hasMoreElements()) {
253:                    FileObject fo = files.nextElement();
254:
255:                    if (!fo.isData()) {
256:                        continue;
257:                    }
258:                    long size = fo.getSize();
259:
260:                    try {
261:                        long read = 0;
262:                        InputStream is = fo.getInputStream();
263:                        try {
264:                            for (;;) {
265:                                int len = is.read(buffer);
266:                                if (len == -1)
267:                                    break;
268:                                read += len;
269:                            }
270:                        } finally {
271:                            is.close();
272:                        }
273:
274:                        if (size != -1) {
275:                            assertEquals(
276:                                    "The amount of data in stream is the same as the length",
277:                                    size, read);
278:                        }
279:
280:                    } catch (IOException ex) {
281:                        errors
282:                                .add("\n    File " + fo + " cannot be read "
283:                                        + ex);
284:                    }
285:                }
286:
287:                if (!errors.isEmpty()) {
288:                    fail("Some files are unreadable" + errors);
289:                }
290:            }
291:
292:            public void testInstantiateAllInstances() {
293:                List<String> errors = new ArrayList<String>();
294:
295:                Enumeration<? extends FileObject> files = Repository
296:                        .getDefault().getDefaultFileSystem().getRoot()
297:                        .getChildren(true);
298:                while (files.hasMoreElements()) {
299:                    FileObject fo = files.nextElement();
300:
301:                    if (skipFile(fo.getPath())) {
302:                        continue;
303:                    }
304:
305:                    try {
306:                        DataObject obj = DataObject.find(fo);
307:                        InstanceCookie ic = obj.getCookie(InstanceCookie.class);
308:                        if (ic != null) {
309:                            Object o = ic.instanceCreate();
310:                        }
311:                    } catch (Exception ex) {
312:                        ex.printStackTrace();
313:                        errors.add("\n    File " + fo.getPath() + " threw "
314:                                + ex);
315:                    }
316:                }
317:
318:                if (!errors.isEmpty()) {
319:                    fail("Some instances cannot be created " + errors);
320:                }
321:            }
322:
323:            public void testIfOneFileIsDefinedTwiceByDifferentModulesTheyNeedToHaveMutualDependency()
324:                    throws Exception {
325:                ClassLoader l = Lookup.getDefault().lookup(ClassLoader.class);
326:                assertNotNull(
327:                        "In the IDE mode, there always should be a classloader",
328:                        l);
329:
330:                // String -> List<Modules>
331:                Map<String, List<String>> files = new HashMap<String, List<String>>();
332:                class ContentAndAttrs {
333:                    final byte[] contents;
334:                    final Map<String, Object> attrs;
335:                    private final URL layerURL;
336:
337:                    ContentAndAttrs(byte[] contents, Map<String, Object> attrs,
338:                            URL layerURL) {
339:                        this .contents = contents;
340:                        this .attrs = attrs;
341:                        this .layerURL = layerURL;
342:                    }
343:
344:                    public @Override
345:                    String toString() {
346:                        return "ContentAndAttrs[contents="
347:                                + Arrays.toString(contents) + ",attrs=" + attrs
348:                                + ";from=" + layerURL + "]";
349:                    }
350:
351:                    public @Override
352:                    int hashCode() {
353:                        return Arrays.hashCode(contents) ^ attrs.hashCode();
354:                    }
355:
356:                    public @Override
357:                    boolean equals(Object o) {
358:                        if (!(o instanceof  ContentAndAttrs)) {
359:                            return false;
360:                        }
361:                        ContentAndAttrs caa = (ContentAndAttrs) o;
362:                        return Arrays.equals(contents, caa.contents)
363:                                && attrs.equals(caa.attrs);
364:                    }
365:                }
366:                /* < FO path , { content, attributes } > */
367:                Map<String, ContentAndAttrs> contents = new HashMap<String, ContentAndAttrs>();
368:                /* < FO path , < module name, { content, attributes } > > */
369:                Map<String, Map<String, ContentAndAttrs>> differentContents = new HashMap<String, Map<String, ContentAndAttrs>>();
370:
371:                boolean atLeastOne = false;
372:                Enumeration<URL> en = l.getResources("META-INF/MANIFEST.MF");
373:                while (en.hasMoreElements()) {
374:                    URL u = en.nextElement();
375:                    InputStream is = u.openStream();
376:                    Manifest mf;
377:                    try {
378:                        mf = new Manifest(is);
379:                    } finally {
380:                        is.close();
381:                    }
382:                    String module = mf.getMainAttributes().getValue(
383:                            "OpenIDE-Module");
384:                    if (module == null)
385:                        continue;
386:                    String layer = mf.getMainAttributes().getValue(
387:                            "OpenIDE-Module-Layer");
388:                    if (layer == null)
389:                        continue;
390:
391:                    atLeastOne = true;
392:                    URL layerURL = new URL(u, "../" + layer);
393:                    java.net.URLConnection connect = layerURL.openConnection();
394:                    connect.setDefaultUseCaches(false);
395:                    FileSystem fs = new XMLFileSystem(layerURL);
396:
397:                    Enumeration<? extends FileObject> all = fs.getRoot()
398:                            .getChildren(true);
399:                    while (all.hasMoreElements()) {
400:                        FileObject fo = all.nextElement();
401:                        if (!fo.isData())
402:                            continue;
403:
404:                        String path = fo.getPath();
405:                        List<String> list = files.get(path);
406:                        if (list == null) {
407:                            list = new ArrayList<String>();
408:                            files.put(path, list);
409:                            list.add(module);
410:                            contents.put(path, new ContentAndAttrs(
411:                                    getFileContent(fo), getAttributes(fo),
412:                                    layerURL));
413:                        } else {
414:                            ContentAndAttrs contentAttrs = contents.get(path);
415:                            ContentAndAttrs nue = new ContentAndAttrs(
416:                                    getFileContent(fo), getAttributes(fo),
417:                                    layerURL);
418:                            if (!nue.equals(contentAttrs)) {
419:                                //System.err.println("Found differences in " + path + " between " + nue + " and " + contentAttrs);
420:                                Map<String, ContentAndAttrs> diffs = differentContents
421:                                        .get(path);
422:                                if (diffs == null) {
423:                                    diffs = new HashMap<String, ContentAndAttrs>();
424:                                    differentContents.put(path, diffs);
425:                                    diffs.put(list.get(0), contentAttrs);
426:                                }
427:                                diffs.put(module, nue);
428:                                list.add(module);
429:                            }
430:                        }
431:                    }
432:                    // make sure the filesystem closes the stream
433:                    connect.getInputStream().close();
434:                }
435:                contents = null; // Not needed any more
436:
437:                StringBuffer sb = new StringBuffer();
438:                for (Map.Entry<String, List<String>> e : files.entrySet()) {
439:                    List<String> list = e.getValue();
440:                    if (list.size() == 1)
441:                        continue;
442:
443:                    Collection<? extends ModuleInfo> res = Lookup.getDefault()
444:                            .lookupAll(ModuleInfo.class);
445:                    assertFalse("Some modules found", res.isEmpty());
446:
447:                    List<String> list2 = new ArrayList<String>(list);
448:                    for (String name : list) {
449:                        for (ModuleInfo info : res) {
450:                            if (name.equals(info.getCodeName())) {
451:                                // remove dependencies
452:                                for (Dependency d : info.getDependencies()) {
453:                                    list2.remove(d.getName());
454:                                }
455:                            }
456:                        }
457:                    }
458:                    // ok, modules depend on each other
459:                    if (list2.size() <= 1)
460:                        continue;
461:
462:                    sb.append(e.getKey()).append(" is provided by: ").append(
463:                            list).append('\n');
464:                    Map<String, ContentAndAttrs> diffList = differentContents
465:                            .get(e.getKey());
466:                    if (diffList != null) {
467:                        if (list.size() == 2) {
468:                            String module1 = list.get(0);
469:                            String module2 = list.get(1);
470:                            ContentAndAttrs contentAttrs1 = diffList
471:                                    .get(module1);
472:                            ContentAndAttrs contentAttrs2 = diffList
473:                                    .get(module2);
474:                            if (!Arrays.equals(contentAttrs1.contents,
475:                                    contentAttrs2.contents)) {
476:                                sb.append(' ').append(module1).append(
477:                                        ": content = '").append(
478:                                        new String(contentAttrs1.contents))
479:                                        .append('\n');
480:                                sb.append(' ').append(module2).append(
481:                                        ": content = '").append(
482:                                        new String(contentAttrs2.contents))
483:                                        .append('\n');
484:                            }
485:                            if (!contentAttrs1.attrs
486:                                    .equals(contentAttrs2.attrs)) {
487:                                Map<String, Object> attr1 = contentAttrs1.attrs;
488:                                Map<String, Object> attr2 = contentAttrs2.attrs;
489:                                Set<String> keys = new HashSet<String>(attr1
490:                                        .keySet());
491:                                keys.retainAll(attr2.keySet());
492:                                for (String attribute : keys) {
493:                                    Object value1 = attr1.get(attribute);
494:                                    Object value2 = attr2.get(attribute);
495:                                    if (value1 == value2
496:                                            || (value1 != null && value1
497:                                                    .equals(value2))) {
498:                                        // Remove the common attributes so that just the differences show up
499:                                        attr1.remove(attribute);
500:                                        attr2.remove(attribute);
501:                                    }
502:                                }
503:                                sb.append(' ').append(module1).append(
504:                                        ": different attributes = '").append(
505:                                        contentAttrs1.attrs).append('\n');
506:                                sb.append(' ').append(module2).append(
507:                                        ": different attributes = '").append(
508:                                        contentAttrs2.attrs).append('\n');
509:                            }
510:                        } else {
511:                            for (String module : list) {
512:                                ContentAndAttrs contentAttrs = diffList
513:                                        .get(module);
514:                                sb.append(" " + module + ": content = '"
515:                                        + new String(contentAttrs.contents)
516:                                        + "', attributes = "
517:                                        + contentAttrs.attrs + "\n");
518:                            }
519:                        }
520:                    }
521:                }
522:
523:                assertTrue("At least one layer file is usually used",
524:                        atLeastOne);
525:
526:                if (sb.length() > 0) {
527:                    fail("Some modules override their files and do not depend on each other\n"
528:                            + sb);
529:                }
530:            }
531:
532:            public void testNoWarningsFromLayerParsing() throws Exception {
533:                ClassLoader l = Lookup.getDefault().lookup(ClassLoader.class);
534:                assertNotNull(
535:                        "In the IDE mode, there always should be a classloader",
536:                        l);
537:
538:                List<URL> urls = new ArrayList<URL>();
539:                boolean atLeastOne = false;
540:                Enumeration<URL> en = l.getResources("META-INF/MANIFEST.MF");
541:                while (en.hasMoreElements()) {
542:                    URL u = en.nextElement();
543:                    InputStream is = u.openStream();
544:                    Manifest mf;
545:                    try {
546:                        mf = new Manifest(is);
547:                    } finally {
548:                        is.close();
549:                    }
550:                    String module = mf.getMainAttributes().getValue(
551:                            "OpenIDE-Module");
552:                    if (module == null)
553:                        continue;
554:                    String layer = mf.getMainAttributes().getValue(
555:                            "OpenIDE-Module-Layer");
556:                    if (layer == null)
557:                        continue;
558:
559:                    atLeastOne = true;
560:                    URL layerURL = new URL(u, "../" + layer);
561:                    urls.add(layerURL);
562:                }
563:
564:                File cacheDir;
565:                File workDir = getWorkDir();
566:                int i = 0;
567:                do {
568:                    cacheDir = new File(workDir, "layercache" + i);
569:                    i++;
570:                } while (!cacheDir.mkdir());
571:                System.setProperty("netbeans.user", cacheDir.getPath());
572:
573:                LayerCacheManager bcm = LayerCacheManager.manager(true);
574:                Logger err = Logger
575:                        .getLogger("org.netbeans.core.projects.cache");
576:                LayerParseHandler h = new LayerParseHandler();
577:                err.addHandler(h);
578:                ByteArrayOutputStream os = new ByteArrayOutputStream();
579:                bcm.store(bcm.createEmptyFileSystem(), urls, os);
580:                assertEquals("No errors or warnings during layer parsing: "
581:                        + h.errors().toString(), 0, h.errors().size());
582:            }
583:
584:            private static class LayerParseHandler extends Handler {
585:                List<String> errors = new ArrayList<String>();
586:
587:                LayerParseHandler() {
588:                }
589:
590:                public void publish(LogRecord rec) {
591:                    if (Level.WARNING.equals(rec.getLevel())
592:                            || Level.SEVERE.equals(rec.getLevel())) {
593:                        errors.add(MessageFormat.format(rec.getMessage(), rec
594:                                .getParameters()));
595:                    }
596:                }
597:
598:                List<String> errors() {
599:                    return errors;
600:                }
601:
602:                public void flush() {
603:                }
604:
605:                public void close() throws SecurityException {
606:                }
607:            }
608:
609:            public void testFolderOrdering() {
610:                LayerParseHandler h = new LayerParseHandler();
611:                Logger.getLogger("org.openide.filesystems.Ordering")
612:                        .addHandler(h);
613:                Set<List<String>> editorMultiFolders = new HashSet<List<String>>();
614:                Pattern editorFolder = Pattern
615:                        .compile("Editors/(application|text)/([^/]+)(/.+|$)");
616:                final FileSystem sfs = Repository.getDefault()
617:                        .getDefaultFileSystem();
618:                Enumeration<? extends FileObject> files = sfs.getRoot()
619:                        .getChildren(true);
620:                while (files.hasMoreElements()) {
621:                    FileObject fo = files.nextElement();
622:                    if (fo.isFolder()) {
623:                        loadChildren(fo);
624:                        assertNull(
625:                                "OpenIDE-Folder-Order attr should not be used on "
626:                                        + fo, fo
627:                                        .getAttribute("OpenIDE-Folder-Order"));
628:                        assertNull(
629:                                "OpenIDE-Folder-SortMode attr should not be used on "
630:                                        + fo,
631:                                fo.getAttribute("OpenIDE-Folder-SortMode"));
632:                        String path = fo.getPath();
633:                        Matcher m = editorFolder.matcher(path);
634:                        if (m.matches()) {
635:                            List<String> multiPath = new ArrayList<String>(3);
636:                            multiPath.add(path);
637:                            if (m.group(2).endsWith("+xml")) {
638:                                multiPath.add("Editors/" + m.group(1) + "/xml"
639:                                        + m.group(3));
640:                            }
641:                            multiPath.add("Editors" + m.group(3));
642:                            editorMultiFolders.add(multiPath);
643:                        }
644:                    }
645:                }
646:                assertEquals("No warnings relating to folder ordering",
647:                        Collections.emptySet(), new TreeSet<String>(h.errors()));
648:                for (List<String> multiPath : editorMultiFolders) {
649:                    List<FileSystem> layers = new ArrayList<FileSystem>(3);
650:                    for (final String path : multiPath) {
651:                        FileObject folder = sfs.findResource(path);
652:                        if (folder != null) {
653:                            layers.add(new MultiFileSystem(
654:                                    new FileSystem[] { sfs }) {
655:                                protected @Override
656:                                FileObject findResourceOn(FileSystem fs,
657:                                        String res) {
658:                                    FileObject f = fs.findResource(path + '/'
659:                                            + res);
660:                                    return Boolean.TRUE.equals(f
661:                                            .getAttribute("hidden")) ? null : f;
662:                                }
663:                            });
664:                        }
665:                    }
666:                    loadChildren(new MultiFileSystem(layers
667:                            .toArray(new FileSystem[layers.size()])).getRoot());
668:                    assertEquals("No warnings relating to folder ordering in "
669:                            + multiPath, Collections.emptySet(),
670:                            new TreeSet<String>(h.errors()));
671:                }
672:            }
673:
674:            private static void loadChildren(FileObject folder) {
675:                List<FileObject> kids = new ArrayList<FileObject>();
676:                for (DataObject kid : DataFolder.findFolder(folder)
677:                        .getChildren()) {
678:                    kids.add(kid.getPrimaryFile());
679:                }
680:                FileUtil.getOrder(kids, true);
681:            }
682:
683:            private static byte[] getFileContent(FileObject fo)
684:                    throws IOException {
685:                BufferedInputStream in = new BufferedInputStream(fo
686:                        .getInputStream());
687:                int size = (int) fo.getSize();
688:                byte[] content = new byte[size];
689:                int length = 0;
690:                while (length < size) {
691:                    int readLength = in.read(content, length, size - length);
692:                    if (readLength <= 0) {
693:                        throw new IOException("Bad size for " + fo
694:                                + ", size = " + size
695:                                + ", but actual length is " + length);
696:                    }
697:                    length += readLength;
698:                }
699:                return content;
700:            }
701:
702:            private static Map<String, Object> getAttributes(FileObject fo) {
703:                Map<String, Object> attrs = new HashMap<String, Object>();
704:                Enumeration<String> en = fo.getAttributes();
705:                while (en.hasMoreElements()) {
706:                    String attrName = en.nextElement();
707:                    Object attr = fo.getAttribute(attrName);
708:                    attrs.put(attrName, attr);
709:                }
710:                return attrs;
711:            }
712:
713:            private boolean skipFile(String s) {
714:                if (s.startsWith("Templates/")
715:                        && !s.startsWith("Templates/Services")) {
716:                    if (s.endsWith(".shadow") || s.endsWith(".java")) {
717:                        return true;
718:                    }
719:                }
720:
721:                if (s.startsWith("Templates/GUIForms"))
722:                    return true;
723:                if (s.startsWith("Palette/Borders/javax-swing-border-"))
724:                    return true;
725:                if (s.startsWith("Palette/Layouts/javax-swing-BoxLayout"))
726:                    return true;
727:                if (s.startsWith("Templates/Beans/"))
728:                    return true;
729:                if (s
730:                        .startsWith("PaletteUI/org-netbeans-modules-form-palette-CPComponent"))
731:                    return true;
732:                if (s.startsWith("Templates/Ant/CustomTask.java"))
733:                    return true;
734:                if (s.startsWith("Templates/Privileged/Main.shadow"))
735:                    return true;
736:                if (s.startsWith("Templates/Privileged/JFrame.shadow"))
737:                    return true;
738:                if (s.startsWith("Templates/Privileged/Class.shadow"))
739:                    return true;
740:                if (s.startsWith("Templates/Classes"))
741:                    return true;
742:                if (s.startsWith("Templates/JSP_Servlet"))
743:                    return true;
744:                if (s
745:                        .startsWith("EnvironmentProviders/ProfileTypes/Execution/nb-j2ee-deployment.instance"))
746:                    return true;
747:                if (s.startsWith("Shortcuts/D-BACK_QUOTE.shadow"))
748:                    return true;
749:
750:                return false;
751:            }
752:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.