Introduce multi-workspaces JCR file system.
[lgpl/argeo-commons.git] / org.argeo.jcr / ext / test / org / argeo / jcr / fs / JcrFileSystemTest.java
index 2ad75a2aa2fceee280106182428e1427f988a5ca..886efa3675450c7c14faf7c1fc1346bdcc7a4078 100644 (file)
@@ -13,11 +13,14 @@ import java.util.Arrays;
 import java.util.Map;
 
 import javax.jcr.Property;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.jackrabbit.core.RepositoryImpl;
 import org.argeo.jackrabbit.fs.JackrabbitMemoryFsProvider;
 
 import junit.framework.TestCase;
@@ -25,6 +28,38 @@ import junit.framework.TestCase;
 public class JcrFileSystemTest extends TestCase {
        private final static Log log = LogFactory.getLog(JcrFileSystemTest.class);
 
+       public void testMounts() throws Exception {
+               JackrabbitMemoryFsProvider fsProvider = new JackrabbitMemoryFsProvider() {
+
+                       @Override
+                       protected void postRepositoryCreation(RepositoryImpl repositoryImpl) throws RepositoryException {
+                               // create workspace
+                               Session session = login();
+                               session.getWorkspace().createWorkspace("test");
+                       }
+
+               };
+
+               Path rootPath = fsProvider.getPath(new URI("jcr+memory:/"));
+               log.debug("Got root " + rootPath);
+
+               Path testMount = fsProvider.getPath(new URI("jcr+memory:/test"));
+               log.debug("Test path");
+               assertEquals(rootPath, testMount.getParent());
+               assertEquals(testMount.getFileName(), rootPath.relativize(testMount));
+
+               Path testPath = testMount.resolve("test.txt");
+               log.debug("Create file " + testPath);
+               Files.createFile(testPath);
+               BasicFileAttributes bfa = Files.readAttributes(testPath, BasicFileAttributes.class);
+               FileTime ft = bfa.creationTime();
+               assertNotNull(ft);
+               assertTrue(bfa.isRegularFile());
+               log.debug("Created " + testPath + " (" + ft + ")");
+               Files.delete(testPath);
+               log.debug("Deleted " + testPath);
+       }
+
        public void testSimple() throws Exception {
                FileSystemProvider fsProvider = new JackrabbitMemoryFsProvider();
 
@@ -66,6 +101,9 @@ public class JcrFileSystemTest extends TestCase {
                log.debug("Created sub directories " + subsubdir);
                Path copiedFile = testDir.resolve("copiedFile.txt");
                log.debug("Resolved " + copiedFile);
+               Path relativeCopiedFile = testDir.relativize(copiedFile);
+               assertEquals(copiedFile.getFileName().toString(), relativeCopiedFile.toString());
+               log.debug("Relative copied file " + relativeCopiedFile);
                try (OutputStream out = Files.newOutputStream(copiedFile); InputStream in = Files.newInputStream(testPath)) {
                        IOUtils.copy(in, out);
                }
@@ -90,7 +128,7 @@ public class JcrFileSystemTest extends TestCase {
                log.debug("Listed " + testDir);
                // Generic attributes
                Map<String, Object> attrs = Files.readAttributes(copiedFile, "*");
-               assertEquals(5, attrs.size());
+               assertEquals(3, attrs.size());
                log.debug("Read attributes of " + copiedFile + ": " + attrs.keySet());
                // Direct node access
                NodeFileAttributes nfa = Files.readAttributes(copiedFile, NodeFileAttributes.class);
@@ -100,7 +138,27 @@ public class JcrFileSystemTest extends TestCase {
                Files.setAttribute(copiedFile, Property.JCR_LANGUAGE, "fr");
                log.debug("Set language");
                attrs = Files.readAttributes(copiedFile, "*");
-               assertEquals(6, attrs.size());
+               assertEquals(4, attrs.size());
                log.debug("Read attributes of " + copiedFile + ": " + attrs.keySet());
        }
+
+       public void testIllegalCharacters() throws Exception {
+               FileSystemProvider fsProvider = new JackrabbitMemoryFsProvider();
+               String fileName = "tüßçt[1].txt";
+               String pathStr = "/testDir/" + fileName;
+               Path testDir = fsProvider.getPath(new URI("jcr+memory:/testDir"));
+               Files.createDirectory(testDir);
+               Path testPath = testDir.resolve(fileName);
+               assertEquals(pathStr, testPath.toString());
+               Files.createFile(testPath);
+               DirectoryStream<Path> files = Files.newDirectoryStream(testDir);
+               Path listedPath = files.iterator().next();
+               assertEquals(pathStr, listedPath.toString());
+
+               String dirName = "*[~WeirdDir~]*";
+               Path subDir = testDir.resolve(dirName);
+               Files.createDirectory(subDir);
+               subDir = testDir.resolve(dirName);
+               assertEquals(dirName, subDir.getFileName().toString());
+       }
 }