コード例 #1
0
  public static void main(String[] args) throws IOException {
    ConnectionOptions options = new ConnectionOptions();
    options.set(ADDRESS, "unix-box");
    options.set(USERNAME, "demo");
    options.set(PASSWORD, "secret");
    options.set(OPERATING_SYSTEM, UNIX);
    options.set(CONNECTION_TYPE, SFTP);
    OverthereConnection connection = Overthere.getConnection("ssh", options);
    try {
      connection.execute(CmdLine.build("cp", "-r", "/var/log/apt", "/tmp/logs1"));
      OverthereFile logs1 = connection.getFile("/tmp/logs1");
      OverthereFile logs2 = connection.getFile("/tmp/logs2");
      logs2.delete();

      System.err.println("Exists #1: " + logs1.exists());
      System.err.println("Exists #2: " + logs2.exists());

      logs1.renameTo(logs2);
      System.err.println("Exists #1: " + logs1.exists());
      System.err.println("Exists #2: " + logs2.exists());

      logs2.copyTo(logs1);
      System.err.println("Exists #1: " + logs1.exists());
      System.err.println("Exists #2: " + logs2.exists());

      logs1.deleteRecursively();
      logs2.deleteRecursively();
      System.err.println("Exists #1: " + logs1.exists());
      System.err.println("Exists #2: " + logs2.exists());
    } finally {
      connection.close();
    }
  }
コード例 #2
0
 private void deleteConnectionTemporaryDirectory() {
   if (connectionTemporaryDirectory != null) {
     try {
       logger.info("Deleting connection temporary directory {}", connectionTemporaryDirectory);
       connectionTemporaryDirectory.deleteRecursively();
     } catch (RuntimeException exc) {
       logger.warn(
           "Got exception while deleting connection temporary directory {}. Ignoring it.",
           connectionTemporaryDirectory,
           exc);
     }
   }
 }
コード例 #3
0
  @Test
  public void shouldCreatePopulateListAndRemoveTemporaryDirectory() {
    final String prefix = "prefix";
    final String suffix = "suffix";

    OverthereFile tempDir = connection.getTempFile(prefix, suffix);
    assertThat(
        "Expected a non-null return value from HostConnection.getTempFile()",
        tempDir,
        notNullValue());
    assertThat(
        "Expected name of temporary file to start with the prefix",
        tempDir.getName(),
        startsWith(prefix));
    assertThat(
        "Expected name of temporary file to end with the suffix",
        tempDir.getName(),
        endsWith(suffix));
    assertThat("Expected temporary file to not exist yet", tempDir.exists(), equalTo(false));

    tempDir.mkdir();
    assertThat(
        "Expected temporary directory to exist after creating it", tempDir.exists(), equalTo(true));
    assertThat(
        "Expected temporary directory to be a directory", tempDir.isDirectory(), equalTo(true));

    OverthereFile anotherTempDir = connection.getTempFile(prefix, suffix);
    assertThat(
        "Expected temporary directories created with identical prefix and suffix to still be different",
        tempDir.getPath(),
        not(equalTo(anotherTempDir.getPath())));

    OverthereFile nested1 = tempDir.getFile("nested1");
    OverthereFile nested2 = nested1.getFile("nested2");
    OverthereFile nested3 = nested2.getFile("nested3");
    assertThat("Expected deeply nested directory to not exist", nested3.exists(), equalTo(false));
    try {
      nested3.mkdir();
      fail("Expected not to be able to create a deeply nested directory in one go");
    } catch (RuntimeIOException expected1) {
    }
    assertThat(
        "Expected deeply nested directory to still not exist", nested3.exists(), equalTo(false));
    nested3.mkdirs();
    assertThat(
        "Expected deeply nested directory to exist after invoking mkdirs on it",
        nested3.exists(),
        equalTo(true));

    final byte[] contents =
        ("Contents of the temporary file created at "
                + System.currentTimeMillis()
                + "ms since the epoch")
            .getBytes();
    OverthereFile regularFile = tempDir.getFile("somefile.txt");
    OverthereUtils.write(contents, regularFile);

    List<OverthereFile> dirContents = tempDir.listFiles();
    assertThat("Expected directory to contain two entries", dirContents.size(), equalTo(2));
    assertThat(
        "Expected directory to contain parent of deeply nested directory",
        dirContents.contains(nested1),
        equalTo(true));
    assertThat(
        "Expected directory to contain regular file that was just created",
        dirContents.contains(regularFile),
        equalTo(true));

    try {
      nested1.delete();
    } catch (RuntimeIOException expected2) {
    }
    nested1.deleteRecursively();
    assertThat(
        "Expected parent of deeply nested directory to have been removed recursively",
        nested1.exists(),
        equalTo(false));

    regularFile.delete();
    tempDir.delete();
    assertThat(
        "Expected temporary directory to not exist after removing it when it was empty",
        tempDir.exists(),
        equalTo(false));
  }