Java Code Examples for com.googlecode.totallylazy.Files

The following examples show how to use com.googlecode.totallylazy.Files. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: totallylazy   Source File: ZipTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void canZipAndUnzip() throws Exception {
    File playground = emptyVMDirectory("totallylazy");
    File a = file(playground, "a.txt");
    File b = file(playground, "folder/b.txt");
    File zipFile = temporaryFile();

    Zip.zip(playground, zipFile);

    Files.deleteFiles(playground);
    assertThat(a.exists(), is(false));
    assertThat(b.exists(), is(false));

    Zip.unzip(zipFile, playground);

    assertThat(a.exists(), is(true));
    assertThat(b.exists(), is(true));

    Files.deleteFiles(playground);
    zipFile.delete();
}
 
Example 2
Source Project: totallylazy   Source File: ZipTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void preservesModifiedDate() throws Exception {
    File playground = emptyVMDirectory("totallylazy");
    File a = file(playground, "a.txt");
    Date date = date(2001, 1, 10);
    a.setLastModified(date.getTime());
    File zipFile = temporaryFile();

    Zip.zip(playground, zipFile);
    Files.deleteFiles(playground);
    Zip.unzip(zipFile, playground);

    assertThat(a.exists(), is(true));
    assertThat(new Date(a.lastModified()), is(date));

    Files.deleteFiles(playground);
    zipFile.delete();
}
 
Example 3
Source Project: java-repl   Source File: WebConsoleResource.java    License: Apache License 2.0 6 votes vote down vote up
@POST
@Hidden
@Path("snap")
@Produces(MediaType.APPLICATION_JSON)
public Response snap(@FormParam("id") String id) {
    Option<WebConsoleClientHandler> clientHandler = agent.client(id);

    if (!clientHandler.isEmpty()) {
        String snapId = UUID.randomUUID().toString();

        Files.write(clientHandler.get().history().toString("\n").getBytes(), snapFile(snapId));

        return ok()
                .entity(emptyMap(String.class, Object.class)
                        .insert("snap", snapId)
                        .insert("uri", snapUri(snapId).toString())
                );

    } else {
        return response(BAD_REQUEST);
    }
}
 
Example 4
Source Project: yatspec   Source File: FileRenderer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void complete(File yatspecOutputDir, Result result) throws Exception {
    final File output = outputFile(yatspecOutputDir, result);
    output.delete();
    output.getParentFile().mkdirs();
    String content = render(result);
    Files.write(content.getBytes("UTF-8"), output);
    System.out.println("Yatspec output:\n" + output);
}
 
Example 5
Source Project: totallylazy   Source File: TreeVisualiser.java    License: Apache License 2.0 5 votes vote down vote up
private void render(TreeMap<?, ?> map) {
    final File file = new File(Files.temporaryDirectory(), getClass().getSimpleName() + ".html");
    Files.write(bytes("<html><head><style>" +
            ".tree { border: 1px solid grey; padding: 0 1px; } " +
            ".key { text-align: center; } " +
            ".tree, .left, .right { display: table-cell; }" +
            "</style></head><body>" + new TreeMapRenderer().render(map) + "</body></html>"), file);
    System.out.println("tree = " + file);
}
 
Example 6
Source Project: java-repl   Source File: ConsoleHistory.java    License: Apache License 2.0 5 votes vote down vote up
public boolean save() {
    if (file.isEmpty())
        return false;

    try {
        Files.write(history.toString("\n").getBytes(), file.get());
        return true;
    } catch (Exception e) {
        return false;
    }
}
 
Example 7
Source Project: java-repl   Source File: ConsoleHistoryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void savesAndLoadsMultilineHistory() {
    File historyFile = Files.temporaryFile();
    ConsoleHistory history = emptyHistory(never(), some(historyFile))
            .add("multiline\nexpression")
            .add("single line expression")
            .add("another\nmultiline\nexpression");

    history.save();

    assertThat(historyFromFile(never(), some(historyFile)).items(), is(history.items().map(replaceAll("\n", " "))));
}
 
Example 8
Source Project: totallylazy   Source File: FileSource.java    License: Apache License 2.0 4 votes vote down vote up
public static FileSource fileSource(File folder) {
    return fileSource(folder, Files.recursiveFilesDirectoriesFirst(folder));
}
 
Example 9
Source Project: totallylazy   Source File: FileSource.java    License: Apache License 2.0 4 votes vote down vote up
public static Function1<File, Pair<String, File>> relativeTo(final File folder) {
    return file -> Pair.pair(Files.relativePath(folder, file), file);
}