示例#1
0
  public void testGetParentFile() throws IOException {
    File file = fs.getFile("file.txt");
    assertNull(file.getParentFile());

    file = fs.getFile("/parentdir/file.txt");
    File parentDir = file.getParentFile();
    assertTrue(parentDir instanceof GridFile);
    assertEquals(parentDir.getPath(), "/parentdir");
  }
示例#2
0
 public void testEquals() throws Exception {
   assertFalse(fs.getFile("").equals(null));
   assertTrue(fs.getFile("").equals(fs.getFile("")));
   assertTrue(fs.getFile("").equals(fs.getFile("/")));
   assertTrue(fs.getFile("foo.txt").equals(fs.getFile("foo.txt")));
   assertTrue(fs.getFile("foo.txt").equals(fs.getFile("/foo.txt")));
   assertFalse(fs.getFile("foo.txt").equals(fs.getFile("FOO.TXT")));
   assertFalse(fs.getFile("/foo.txt").equals(new File("/foo.txt")));
 }
示例#3
0
  public void testGetParent() throws IOException {
    File file = fs.getFile("file.txt");
    assertEquals(file.getParent(), null);

    file = fs.getFile("/parentdir/file.txt");
    assertEquals(file.getParent(), "/parentdir");

    file = fs.getFile("/parentdir/subdir/file.txt");
    assertEquals(file.getParent(), "/parentdir/subdir");
  }
示例#4
0
  public void testGetFile() throws Exception {
    assertEquals(fs.getFile("file.txt").getPath(), "file.txt");
    assertEquals(fs.getFile("/file.txt").getPath(), "/file.txt");
    assertEquals(fs.getFile("myDir/file.txt").getPath(), "myDir/file.txt");
    assertEquals(fs.getFile("/myDir/file.txt").getPath(), "/myDir/file.txt");

    assertEquals(fs.getFile("myDir", "file.txt").getPath(), "myDir/file.txt");
    assertEquals(fs.getFile("/myDir", "file.txt").getPath(), "/myDir/file.txt");

    File dir = fs.getFile("/myDir");
    assertEquals(fs.getFile(dir, "file.txt").getPath(), "/myDir/file.txt");

    dir = fs.getFile("myDir");
    assertEquals(fs.getFile(dir, "file.txt").getPath(), "myDir/file.txt");
  }
示例#5
0
  public void testRun() throws Exception {
    String wsName = "test";
    DefaultCacheManager dcm = new DefaultCacheManager();
    Cache<String, Metadata> metaCache = dcm.getCache("meta");
    Cache<String, byte[]> dataCache = dcm.getCache("data");

    GridFilesystem gfs = new GridFilesystem(dataCache, metaCache);

    BuildContext bcontext =
        DirectoryBuilder.newDirectoryInstance(metaCache, dataCache, metaCache, wsName);
    bcontext.chunkSize(1024 * 1024);
    Directory directory = bcontext.create();
    Central central = CentralConfig.oldFromDir(directory).build();

    central.newIndexer().index(IndexJobs.create("/bleujin", 10));

    central.newSearcher().createRequest("").find().debugPrint();

    OutputStream output = gfs.getOutput("/test.data");
    IOUtil.copyNClose(new StringInputStream("hello bleujin"), output);

    Debug.line(IOUtil.toStringWithClose(gfs.getInput("/test.data")));

    central.newSearcher().createRequest("").find().debugPrint();

    File root = gfs.getFile("/");
    viewFile(root);

    dcm.stop();
  }
示例#6
0
 @Override
 public String[] getChildrenNames(ITransaction transaction, String uri) throws WebdavException {
   uri = normalizeURI(uri);
   log.tracef("GridStore.getChildrenNames(%s)", uri);
   File file = fs.getFile(root, uri);
   try {
     String[] childrenNames = null;
     if (file.isDirectory()) {
       File[] children = file.listFiles();
       if (children == null) throw new WebdavException("IO error while listing files for " + file);
       List<String> childList = new ArrayList<String>();
       for (int i = 0; i < children.length; i++) {
         String name = children[i].getName();
         childList.add(name);
         log.trace("Child " + i + ": " + name);
       }
       childrenNames = new String[childList.size()];
       childrenNames = childList.toArray(childrenNames);
     }
     return childrenNames;
   } catch (Exception e) {
     log.error("GridStore.getChildrenNames(" + uri + ") failed", e);
     throw new WebdavException(e);
   }
 }
示例#7
0
 @Override
 public long getResourceLength(ITransaction transaction, String uri) throws WebdavException {
   uri = normalizeURI(uri);
   log.tracef("GridStore.getResourceLength(%s)", uri);
   File file = fs.getFile(root, uri);
   return file.length();
 }
示例#8
0
 @Override
 public void removeObject(ITransaction transaction, String uri) throws WebdavException {
   uri = normalizeURI(uri);
   File file = fs.getFile(root, uri);
   boolean success = file.delete();
   log.tracef("GridStore.removeObject(%s)=%s", uri, success);
   if (!success) throw new WebdavException("cannot delete object: " + uri);
 }
示例#9
0
 @Override
 public void createFolder(ITransaction transaction, String uri) throws WebdavException {
   uri = normalizeURI(uri);
   log.tracef("GridStore.createFolder(%s)", uri);
   File file = fs.getFile(root, uri);
   if (!file.mkdir()) {
     throw new WebdavException("cannot create folder: " + uri);
   }
 }
示例#10
0
  public void testList() throws Exception {
    assertNull(fs.getFile("nonExistentDir").list());
    assertEquals(createDir("/emptyDir").list().length, 0);

    File dir = createDirWithFiles();
    String[] filenames = dir.list();
    assertEquals(
        asSet(filenames),
        asSet("foo1.txt", "foo2.txt", "bar1.txt", "bar2.txt", "fooDir", "barDir"));
  }
示例#11
0
 public void testGetName() throws IOException {
   assertEquals(fs.getFile("").getName(), "");
   assertEquals(fs.getFile("/").getName(), "");
   assertEquals(fs.getFile("file.txt").getName(), "file.txt");
   assertEquals(fs.getFile("/file.txt").getName(), "file.txt");
   assertEquals(fs.getFile("/dir/file.txt").getName(), "file.txt");
   assertEquals(fs.getFile("/dir/subdir/file.txt").getName(), "file.txt");
   assertEquals(fs.getFile("dir/subdir/file.txt").getName(), "file.txt");
 }
示例#12
0
  public void testRootDir() throws Exception {
    File rootDir = fs.getFile("/");
    assertTrue(rootDir.exists());
    assertTrue(rootDir.isDirectory());

    createFile("/foo.txt");
    String[] filenames = rootDir.list();
    assertNotNull(filenames);
    assertEquals(filenames.length, 1);
    assertEquals(filenames[0], "foo.txt");
  }
示例#13
0
 public void testMultiClose() throws Exception {
   String filePath = "test_close.dat";
   OutputStream out = fs.getOutput(filePath);
   try {
     out.write(1);
   } finally {
     out.close();
     out.close();
   }
   File f = fs.getFile(filePath);
   assertEquals(f.length(), 1);
 }
示例#14
0
  public void testDeleteRemovesAllChunks() throws Exception {
    assertEquals(numberOfChunksInCache(), 0);
    assertEquals(numberOfMetadataEntries(), 0);

    writeToFile("delete.txt", "delete me", 100);

    GridFile file = (GridFile) fs.getFile("delete.txt");
    boolean deleted = file.delete();
    assertTrue(deleted);
    assertFalse(file.exists());
    assertEquals(numberOfChunksInCache(), 0);
    assertEquals(numberOfMetadataEntries(), 0);
  }
示例#15
0
 @Override
 public void createResource(ITransaction transaction, String uri) throws WebdavException {
   uri = normalizeURI(uri);
   log.tracef("GridStore.createResource(%s)", uri);
   File file = fs.getFile(root, uri);
   try {
     if (!file.createNewFile()) {
       throw new WebdavException("cannot create file: " + uri);
     }
   } catch (IOException e) {
     log.error("GridStore.createResource(" + uri + ") failed", e);
     throw new WebdavException(e);
   }
 }
示例#16
0
  public void testWriteAcrossMultipleChunksWithNonDefaultChunkSizeAfterFileIsExplicitlyCreated()
      throws Exception {
    GridFile file = (GridFile) fs.getFile("multipleChunks.txt", 20); // chunkSize = 20
    file.createNewFile();

    writeToFile(
        "multipleChunks.txt",
        "This text spans multiple chunks, because each chunk is only 20 bytes long.",
        10); // chunkSize = 10 (but it is ignored, because the file was already created with
    // chunkSize = 20

    String text = getContents("multipleChunks.txt");
    assertEquals(
        text, "This text spans multiple chunks, because each chunk is only 20 bytes long.");
  }
示例#17
0
  public void testListFiles() throws Exception {
    assertNull(fs.getFile("nonExistentDir").listFiles());
    assertEquals(createDir("/emptyDir").listFiles().length, 0);

    File dir = createDirWithFiles();
    File[] files = dir.listFiles();
    assertEquals(
        asSet(getPaths(files)),
        asSet(
            "/myDir/foo1.txt",
            "/myDir/foo2.txt",
            "/myDir/fooDir",
            "/myDir/bar1.txt",
            "/myDir/bar2.txt",
            "/myDir/barDir"));
  }
示例#18
0
  @Override
  public InputStream getResourceContent(ITransaction transaction, String uri)
      throws WebdavException {
    uri = normalizeURI(uri);
    log.tracef("GridStore.getResourceContent(%s)", uri);
    File file = fs.getFile(root, uri);

    InputStream in;
    try {
      // in=new BufferedInputStream(fs.getInput(file));
      in = fs.getInput(file);
    } catch (IOException e) {
      log.error("GridStore.getResourceContent(" + uri + ") failed");
      throw new WebdavException(e);
    }
    return in;
  }
示例#19
0
  @Override
  public StoredObject getStoredObject(ITransaction transaction, String uri) {
    uri = normalizeURI(uri);
    log.tracef("GridStore.getStoredObject(%s)", uri);
    StoredObject so = null;

    File file = fs.getFile(root, uri);
    if (file.exists()) {
      so = new StoredObject();
      so.setFolder(file.isDirectory());
      so.setLastModified(new Date(file.lastModified()));
      so.setCreationDate(new Date(file.lastModified()));
      so.setResourceLength(getResourceLength(transaction, uri));
    }

    return so;
  }
示例#20
0
  public GridStore(File root) {
    data = CacheManagerHolder.cacheContainer.getCache(CacheManagerHolder.dataCacheName);
    metadata = CacheManagerHolder.cacheContainer.getCache(CacheManagerHolder.metadataCacheName);

    try {
      data.start();
      metadata.start();
    } catch (Exception e) {
      throw new RuntimeException("creation of cluster failed", e);
    }

    fs = new GridFilesystem(data, metadata);

    this.root = fs.getFile(root.getPath());
    if (!this.root.mkdirs())
      throw new WebdavException(
          "root path: " + root.getAbsolutePath() + " does not exist and could not be created");
  }
示例#21
0
  public void testLastModified() throws Exception {
    assertEquals(fs.getFile("nonExistentFile.txt").lastModified(), 0);

    long time1 = System.currentTimeMillis();
    File file = createFile("file.txt");
    long time2 = System.currentTimeMillis();

    assertTrue(time1 <= file.lastModified());
    assertTrue(file.lastModified() <= time2);

    Thread.sleep(100);

    time1 = System.currentTimeMillis();
    writeToFile(file.getPath(), "foo");
    time2 = System.currentTimeMillis();

    assertTrue(time1 <= file.lastModified());
    assertTrue(file.lastModified() <= time2);
  }
示例#22
0
  public void testWriteAfterClose() throws Exception {
    String filePath = "test_write_to_closed.dat";
    OutputStream out = fs.getOutput(filePath);

    try {
      out.write(1);
    } finally {
      out.close();
    }
    IOException e = null;
    try {
      out.write(2);
    } catch (IOException ex) {
      e = ex;
    }
    assertNotNull(e);
    File f = fs.getFile(filePath);
    assertEquals(f.length(), 1);
  }
示例#23
0
  public void testLeadingSeparatorIsOptional() throws IOException {
    File gridFile = fs.getFile("file.txt");
    assert gridFile.createNewFile();

    assertTrue(fs.getFile("file.txt").exists());
    assertTrue(fs.getFile("/file.txt").exists());

    File dir = fs.getFile("dir");
    boolean dirCreated = dir.mkdir();
    assertTrue(dirCreated);

    assertTrue(fs.getFile("dir").exists());
    assertTrue(fs.getFile("/dir").exists());
  }
示例#24
0
  @Override
  public long setResourceContent(
      ITransaction transaction,
      String uri,
      InputStream is,
      String contentType,
      String characterEncoding)
      throws WebdavException {
    uri = normalizeURI(uri);
    log.tracef("GridStore.setResourceContent(%s)", uri);
    File file = fs.getFile(root, uri);
    try {
      OutputStream os = fs.getOutput((GridFile) file);
      // OutputStream os=new BufferedOutputStream(fs.getOutput((GridFile)file), BUF_SIZE);
      try {
        int read;
        byte[] copyBuffer = new byte[BUF_SIZE];

        while ((read = is.read(copyBuffer, 0, copyBuffer.length)) != -1) {
          os.write(copyBuffer, 0, read);
        }
      } finally {
        Util.close(is);
        Util.close(os);
      }
    } catch (IOException e) {
      log.error("GridStore.setResourceContent(" + uri + ") failed", e);
      throw new WebdavException(e);
    }
    long length = -1;

    try {
      length = file.length();
    } catch (SecurityException e) {
      log.error("GridStore.setResourceContent(" + uri + ") failed" + "\nCan't get file.length");
    }
    return length;
  }
示例#25
0
 public void testNonExistentFileIsNeitherFileNorDirectory() throws IOException {
   File file = fs.getFile("nonExistentFile.txt");
   assertFalse(file.exists());
   assertFalse(file.isFile());
   assertFalse(file.isDirectory());
 }
示例#26
0
 @Test(expectedExceptions = IOException.class)
 public void testCreateNewFileInNonExistentDir() throws IOException {
   File file = fs.getFile("nonExistent/file.txt");
   file.createNewFile();
 }
示例#27
0
 private File createFile(String pathname) throws IOException {
   File file = fs.getFile(pathname);
   assert file.createNewFile();
   return file;
 }
示例#28
0
 public void testCreateNewFile() throws IOException {
   File file = fs.getFile("file.txt");
   assertTrue(file.createNewFile()); // file should be created successfully
   assertFalse(file.createNewFile()); // file should not be created, because it already exists
 }
示例#29
0
 @Test(expectedExceptions = UnsupportedOperationException.class)
 public void testGetUsableSpace() {
   fs.getFile("nonsuch.txt").getUsableSpace();
 }
示例#30
0
 @Test(expectedExceptions = UnsupportedOperationException.class)
 public void testSetExecutable2() {
   fs.getFile("nonsuch.txt").setExecutable(true, true);
 }