samples/unionfs/test/org/apidesign/unionfs/UnionFSTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:56:56 +0200
changeset 135 6a66df28018c
permissions -rw-r--r--
Sample code to work with filesystem layers
jtulach@135
     1
package org.apidesign.unionfs;
jtulach@135
     2
jtulach@135
     3
import java.util.Arrays;
jtulach@135
     4
import java.util.HashSet;
jtulach@135
     5
import java.util.Set;
jtulach@135
     6
import org.junit.After;
jtulach@135
     7
import org.junit.AfterClass;
jtulach@135
     8
import org.junit.Before;
jtulach@135
     9
import org.junit.BeforeClass;
jtulach@135
    10
import org.junit.Test;
jtulach@135
    11
import org.openide.filesystems.FileObject;
jtulach@135
    12
import static org.junit.Assert.*;
jtulach@135
    13
import org.openide.filesystems.FileSystem;
jtulach@135
    14
import org.xml.sax.SAXException;
jtulach@135
    15
jtulach@135
    16
public class UnionFSTest {
jtulach@135
    17
jtulach@135
    18
    public UnionFSTest() {
jtulach@135
    19
    }
jtulach@135
    20
jtulach@135
    21
    @BeforeClass
jtulach@135
    22
    public static void setUpClass() throws Exception {
jtulach@135
    23
    }
jtulach@135
    24
jtulach@135
    25
    @AfterClass
jtulach@135
    26
    public static void tearDownClass() throws Exception {
jtulach@135
    27
    }
jtulach@135
    28
jtulach@135
    29
    @Before
jtulach@135
    30
    public void setUp() {
jtulach@135
    31
    }
jtulach@135
    32
jtulach@135
    33
    @After
jtulach@135
    34
    public void tearDown() {
jtulach@135
    35
    }
jtulach@135
    36
jtulach@135
    37
    @Test
jtulach@135
    38
    public void union() throws SAXException {
jtulach@135
    39
        FileSystem one = UnionFS.fromResource(UnionFSTest.class.getResource("fs-one.xml"));
jtulach@135
    40
        assertChildren("There is just one file in the folder", one, "Menu", "Open.instance");
jtulach@135
    41
        
jtulach@135
    42
        FileSystem two = UnionFS.fromResource(UnionFSTest.class.getResource("fs-two.xml"));
jtulach@135
    43
        assertChildren("There is just one file in the other folder", two, "Menu", "Close.instance");
jtulach@135
    44
        
jtulach@135
    45
        FileSystem union = UnionFS.union(one, two);
jtulach@135
    46
        assertChildren("There both in the union", union, "Menu", "Open.instance", "Close.instance");
jtulach@135
    47
    }
jtulach@135
    48
jtulach@135
    49
    private static void assertChildren(String msg, FileSystem fs, String folder, String... expect) {
jtulach@135
    50
        Set<String> names = new HashSet<String>();
jtulach@135
    51
        names.addAll(Arrays.asList(expect));
jtulach@135
    52
        
jtulach@135
    53
        FileObject fo = fs.getRoot().getFileObject(folder);
jtulach@135
    54
        assertNotNull(msg + " folder " + folder + " has to exist", fo);
jtulach@135
    55
        
jtulach@135
    56
        for (FileObject ch : fo.getChildren()) {
jtulach@135
    57
            names.remove(ch.getNameExt());
jtulach@135
    58
        }
jtulach@135
    59
jtulach@135
    60
        if (!names.isEmpty()) {
jtulach@135
    61
            fail(msg + " - expected files not found " + names);
jtulach@135
    62
        }
jtulach@135
    63
    }
jtulach@135
    64
jtulach@135
    65
}