KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > MultiDataObjectFilesTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.loaders;
21
22 import junit.textui.TestRunner;
23
24 import org.openide.filesystems.*;
25 import java.io.IOException JavaDoc;
26 import java.util.*;
27 import org.netbeans.junit.*;
28
29 /** Test functionality of FilesSet object returned from MultiFileObject.files()
30  * method.
31  *
32  * @author Petr Hamernik
33  */

34 public class MultiDataObjectFilesTest extends NbTestCase {
35
36     public MultiDataObjectFilesTest(String JavaDoc name) {
37         super(name);
38     }
39
40     public void testFilesSet () throws Exception JavaDoc {
41         DataLoader loader = DataLoader.getLoader(SimpleLoader.class);
42         AddLoaderManuallyHid.addRemoveLoader(loader, true);
43
44         
45         // create directory structur description
46
String JavaDoc[] fsstruct = new String JavaDoc[] {
47             "A.primary", "A.a", "A.b",
48             "B.x0", "B.zx", "B.secondary",
49             "C.a0", "C.a5", "C.a1", "C.a4",
50             "A.primary0", "A.secondary", "A.zx", "A.x0",
51             "C.a2", "C.a3", "C.primary",
52             "B.primary", "B.b", "B.primary0", "B.a"
53         };
54             
55         // clean and create new filesystems
56
TestUtilHid.destroyLocalFileSystem(getName());
57         FileSystem fs = TestUtilHid.createLocalFileSystem(getWorkDir(), fsstruct);
58
59         DataFolder folder = DataFolder.findFolder(fs.getRoot());
60
61         DataObject[] children = folder.getChildren();
62         assertTrue ("DataObjects were not recognized correctly.", children.length == 3);
63         for (int i = 0; i < children.length; i++) {
64             DataObject obj = children[i];
65             Set files = obj.files();
66             
67             Iterator it = files.iterator();
68             FileObject primary = (FileObject) it.next();
69             assertEquals("Primary file is not returned first for "+obj.getName(), primary, obj.getPrimaryFile());
70
71             FileObject last = null;
72             while (it.hasNext()) {
73                 FileObject current = (FileObject) it.next();
74                 if (last != null) {
75                     assertTrue("FileObjects are not alphabetically", last.getNameExt().compareTo(current.getNameExt()) < 0);
76                 }
77                 last = current;
78             }
79         }
80         
81         TestUtilHid.destroyLocalFileSystem(getName());
82     }
83     
84     public static final class SimpleLoader extends MultiFileLoader {
85         public SimpleLoader() {
86             super(SimpleObject.class);
87         }
88         protected String JavaDoc displayName() {
89             return "SimpleLoader";
90         }
91         protected FileObject findPrimaryFile(FileObject fo) {
92             if (!fo.isFolder()) {
93                 return fo.hasExt("primary") ? fo : FileUtil.findBrother(fo, "primary");
94             }
95             return null;
96         }
97         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
98             return new SimpleObject(this, primaryFile);
99         }
100         protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
101             return new FileEntry(obj, primaryFile);
102         }
103         protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) {
104             return new FileEntry(obj, secondaryFile);
105         }
106     }
107     
108     public static final class SimpleObject extends MultiDataObject {
109         public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException {
110             super(fo, l);
111         }
112     }
113
114 }
115
Popular Tags