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