⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filemanagertest.java

📁 pebble-blog 博客源码博客源码博客源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  /**   * Tests that a file can't be renamed when the new file is outside of   * the root.   */  public void testRenameFileThrowsExceptionWhenNewFileOutsideOfRoot() throws Exception {    try {      assertNull(fileManager.renameFile("/", "afile.txt", "../afile.txt"));      fail("Renaming a file outside of the root isn't allowed");    } catch (IllegalFileAccessException ifae) {    }  }  /**   * Tests that a file can be renamed.   */  public void testRenameFile() throws Exception {    File file = fileManager.getFile("/afile.txt");    BufferedWriter writer = new BufferedWriter(new FileWriter(file));    writer.write("Testing...");    writer.flush();    writer.close();    long length = file.length();    File newFile = fileManager.renameFile("/", "afile.txt", "anewfile.txt");    assertNotNull(newFile);    assertFalse(file.exists());    assertTrue(newFile.exists());    assertEquals(length, newFile.length());    // and clean up    newFile.delete();  }  /**   * Tests that a file can't be deleted outside of the root.   */  public void testDeleteFileThrowsExceptionWhenNewFileOutsideOfRoot() throws Exception {    try {      fileManager.deleteFile("/", "../afile.txt");      fail("Deleting a file outside of the root isn't allowed");    } catch (IllegalFileAccessException ifae) {    }  }  /**   * Tests that a file can be loaded.   */  public void testLoadFile() throws Exception {    File file = fileManager.getFile("/afile.txt");    BufferedWriter writer = new BufferedWriter(new FileWriter(file));    writer.write("First line");    writer.newLine();    writer.write("Second line");    writer.flush();    writer.close();    StringBuffer expectedContent = new StringBuffer();    expectedContent.append("First line");    expectedContent.append(System.getProperty("line.separator"));    expectedContent.append("Second line");    String content = fileManager.loadFile("/", "afile.txt");    assertEquals(expectedContent.toString(), content);    // and clean up    file.delete();  }  /**   * Tests that a file can't be loaded from outside of the root.   */  public void testLoadFileThrowsExceptionWhenNewFileOutsideOfRoot() throws Exception {    try {      fileManager.loadFile("/", "../afile.txt");      fail("Loading a file outside of the root isn't allowed");    } catch (IllegalFileAccessException ifae) {    }  }  /**   * Tests that a file can be saved.   */  public void testSaveFile() throws Exception {    StringBuffer content = new StringBuffer();    content.append("First line");    content.append(System.getProperty("line.separator"));    content.append("Second line");    fileManager.saveFile("/", "afile.txt", content.toString());    assertEquals(content.toString(), fileManager.loadFile("/", "afile.txt"));    // and clean up    File file = fileManager.getFile("/afile.txt");    file.delete();  }  /**   * Tests that a file can't be saved outside of the root.   */  public void testSaveFileThrowsExceptionWhenNewFileOutsideOfRoot() throws Exception {    try {      fileManager.saveFile("/", "../afile.txt", "some content");      fail("Saving a file outside of the root isn't allowed");    } catch (IllegalFileAccessException ifae) {    }  }  /**   * Tests that files can be accessed.   */  public void testGetFiles() throws Exception {    // create some files and directories    fileManager.createDirectory("/", "a");    fileManager.createDirectory("/", "z");    fileManager.saveFile("/", "y.txt", "Some content");    fileManager.saveFile("/", "b.txt", "Some content");    List files = fileManager.getFiles("/");    assertEquals(4, files.size());    // the files should be in this order    // - a, z, b.txt, y.txt (directories followed by files, both alphabetically)    FileMetaData file = (FileMetaData)files.get(0);    assertEquals("a", file.getName());    assertEquals("/", file.getPath());    assertTrue(file.isDirectory());    file = (FileMetaData)files.get(1);    assertEquals("z", file.getName());    assertEquals("/", file.getPath());    assertTrue(file.isDirectory());    file = (FileMetaData)files.get(2);    assertEquals("b.txt", file.getName());    assertEquals("/", file.getPath());    assertFalse(file.isDirectory());    file = (FileMetaData)files.get(3);    assertEquals("y.txt", file.getName());    assertEquals("/", file.getPath());    assertFalse(file.isDirectory());    // and clean up    fileManager.deleteFile("/", "a");    fileManager.deleteFile("/", "z");    fileManager.deleteFile("/", "y.txt");    fileManager.deleteFile("/", "b.txt");  }  /**   * Tests that files can be accessed recursively.   */  public void testGetFilesRecursively() throws Exception {    // create some files and directories    fileManager.createDirectory("/", "a");    fileManager.saveFile("/a", "a.txt", "Some content");    fileManager.saveFile("/", "b.txt", "Some content");    List files = fileManager.getFiles("/", true);    assertEquals(3, files.size());    // the files should be in this order    // - a, a/a.txt, b.txt (directories followed by files, alphabetically and recursively)    FileMetaData file = (FileMetaData)files.get(0);    assertEquals("a", file.getName());    assertEquals("/", file.getPath());    assertTrue(file.isDirectory());    file = (FileMetaData)files.get(1);    assertEquals("a.txt", file.getName());    assertEquals("/a", file.getPath());    assertFalse(file.isDirectory());    file = (FileMetaData)files.get(2);    assertEquals("b.txt", file.getName());    assertEquals("/", file.getPath());    assertFalse(file.isDirectory());    // and clean up    fileManager.deleteFile("/a", "a.txt");    fileManager.deleteFile("/", "a");    fileManager.deleteFile("/", "b.txt");  }  /**   * Tests that files can be accessed when directory is empty.   */  public void testGetFilesFromEmptyDirectory() throws Exception {    List files = fileManager.getFiles("/");    assertEquals(0, files.size());  }  /**   * Tests that files can be accessed from a non-existent directory.   */  public void testGetFilesFromNonExistentDirectory() throws Exception {    // the theme path "/some/path" doesn't exist    Theme theme = new Theme(blog, "custom", "/some/path");    blog.setEditableTheme(theme);    fileManager = new FileManager(blog, FileMetaData.THEME_FILE);    List files = fileManager.getFiles("/");    assertEquals(0, files.size());  }  /**   * Tests that files can't be accessed outside of the root.   */  public void testGetFilesThrowsExceptionWhenNewFileOutsideOfRoot() throws Exception {    try {      fileManager.getFiles("../");      fail("Accessing files outside of the root isn't allowed");    } catch (IllegalFileAccessException ifae) {    }  }  /**   * Tests that the URLs for blog files are set correctly.   */  public void testUrlForBlogFile() throws Exception {    fileManager = new FileManager(blog, FileMetaData.BLOG_FILE);    fileManager.saveFile("/", "a.txt", "Some content");    List files = fileManager.getFiles("/");    FileMetaData file = (FileMetaData)files.get(0);    assertEquals("files/a.txt", file.getUrl());    // and clean up    fileManager.deleteFile("/", "a.txt");  }  /**   * Tests that the URLs for blog images are set correctly.   */  public void testUrlForBlogImage() throws Exception {    fileManager = new FileManager(blog, FileMetaData.BLOG_IMAGE);    fileManager.saveFile("/", "a.txt", "Some content");    List files = fileManager.getFiles("/");    FileMetaData file = (FileMetaData)files.get(0);    assertEquals("images/a.txt", file.getUrl());    // and clean up    fileManager.deleteFile("/", "a.txt");  }  /**   * Tests that the URLs for theme files are set correctly.   */  public void testUrlForThemeFile() throws Exception {    Theme theme = new Theme(blog, "theme", blog.getRoot());    File themeDirectory = new File(blog.getThemeDirectory());    themeDirectory.mkdir();    blog.setEditableTheme(theme);    fileManager = new FileManager(blog, FileMetaData.THEME_FILE);    fileManager.saveFile("/", "a.txt", "Some content");    List files = fileManager.getFiles("/");    FileMetaData file = (FileMetaData)files.get(0);    assertEquals("theme/a.txt", file.getUrl());    // and clean up    fileManager.deleteFile("/", "a.txt");    themeDirectory.delete();  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -