Example usage for org.apache.commons.io FileUtils touch

List of usage examples for org.apache.commons.io FileUtils touch

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils touch.

Prototype

public static void touch(File file) throws IOException 

Source Link

Document

Implements the same behaviour as the "touch" utility on Unix.

Usage

From source file:org.tonguetied.datatransfer.ExportServiceTest.java

public final void testCreateArchiveWithEmptyFiles() throws Exception {
    archiveTestDirectory = new File(USER_DIR, "testCreateArchiveWithEmptyFiles");
    FileUtils.forceMkdir(archiveTestDirectory);
    FileUtils.touch(new File(archiveTestDirectory, "temp"));
    assertEquals(1, archiveTestDirectory.listFiles().length);
    assertTrue(archiveTestDirectory.isDirectory());
    dataService.createArchive(archiveTestDirectory);
    assertEquals(2, archiveTestDirectory.listFiles().length);
    // examine zip file
    File[] files = archiveTestDirectory.listFiles(new FileExtensionFilter(".zip"));
    assertEquals(1, files.length);/*from   w  w w  . ja v  a 2s .c  o  m*/
    ZipInputStream zis = null;
    try {
        zis = new ZipInputStream(new FileInputStream(files[0]));
        ZipEntry zipEntry = zis.getNextEntry();
        assertEquals("temp", zipEntry.getName());
        zis.closeEntry();
    } finally {
        IOUtils.closeQuietly(zis);
    }
}

From source file:org.wisdom.maven.mojos.CoffeeScriptCompilerMojoTest.java

@Test
public void testWatching() throws MojoFailureException, MojoExecutionException, IOException, WatchingException,
        InterruptedException {//from   w w w.  ja  v  a 2 s.c om
    cleanup();

    // Copy script to script2 (do not modify script as it is used by other tests).
    final File originalInternalScript = new File(FAKE_PROJECT,
            "src/main/resources/assets/coffee/script.coffee");
    final File newInternalScript = new File(FAKE_PROJECT, "src/main/resources/assets/coffee/script2.coffee");
    final File originalExternalScript = new File(FAKE_PROJECT, "src/main/assets/script.coffee");

    String originalScriptContent = FileUtils.readFileToString(originalInternalScript);
    FileUtils.copyFile(originalInternalScript, newInternalScript);

    mojo.execute();

    File script = new File(FAKE_PROJECT_TARGET, "classes/assets/coffee/script2.js");
    File map = new File(FAKE_PROJECT_TARGET, "classes/assets/coffee/script2.js.map");
    assertThat(script).isFile();
    assertThat(map).isFile();
    assertThat(FileUtils.readFileToString(script)).contains("square = function(x) {")
            .contains("return \"Filling the \" + container + \" with \" + liquid + \"...\";");

    File ext = new File(FAKE_PROJECT_TARGET, "wisdom/assets/script.js");
    assertThat(ext).isFile();
    assertThat(FileUtils.readFileToString(ext)).contains("square = function(x) {")
            .contains("return \"Filling the \" + container + \" with \" + liquid + \"...\";");

    // Delete script 2
    newInternalScript.delete();
    mojo.fileDeleted(newInternalScript);

    assertThat(new File(FAKE_PROJECT_TARGET, "classes/assets/coffee/script2.js")).doesNotExist();
    assertThat(new File(FAKE_PROJECT_TARGET, "classes/assets/coffee/script2.js.map")).doesNotExist();

    // Recreate the file with another name (same content)
    File newFile = new File(FAKE_PROJECT, "src/main/resources/assets/script3.coffee");
    FileUtils.write(newFile, originalScriptContent);
    mojo.fileCreated(newFile);
    File script3 = new File(FAKE_PROJECT_TARGET, "classes/assets/script3.js");
    assertThat(FileUtils.readFileToString(script3)).contains("square = function(x) {")
            .contains("return \"Filling the \" + container + \" with \" + liquid + \"...\";");

    // Update link
    long originalLastModified = ext.lastModified();
    FileUtils.touch(originalExternalScript);
    mojo.fileUpdated(originalExternalScript);
    // The file should have been updated
    assertThat(ext.lastModified()).isGreaterThanOrEqualTo(originalLastModified);
}

From source file:org.wisdom.maven.mojos.LessCompilerMojoTest.java

@Test
public void testWatching() throws MojoFailureException, MojoExecutionException, IOException, WatchingException,
        InterruptedException {/*  w  w  w .  j  a  v a  2 s . com*/
    cleanup();

    // Copy style to style2 (do not modify script as it is used by other tests).
    final File originalInternalStyle = new File(FAKE_PROJECT, "src/main/resources/assets/less/style.less");
    final File newInternalStyle = new File(FAKE_PROJECT, "src/main/resources/assets/less/style2.less");
    final File originalExternalStyle = new File(FAKE_PROJECT, "src/main/assets/style.less");

    String originalStyleContent = FileUtils.readFileToString(originalInternalStyle);
    FileUtils.copyFile(originalInternalStyle, newInternalStyle);

    mojo.execute();

    File style = new File(FAKE_PROJECT_TARGET, "classes/assets/less/style2.css");
    assertThat(style).isFile();
    assertThat(FileUtils.readFileToString(style)).contains("-webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);")
            .contains("box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);").contains("border-color: #fdcdea;")
            .contains("color: #fe33ac;");

    File ext = new File(FAKE_PROJECT_TARGET, "wisdom/assets/style.css");
    assertThat(ext).isFile();
    assertThat(FileUtils.readFileToString(style)).contains("-webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);")
            .contains("box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);").contains("border-color: #fdcdea;")
            .contains("color: #fe33ac;");

    // Delete script 2
    newInternalStyle.delete();
    mojo.fileDeleted(newInternalStyle);

    assertThat(new File(FAKE_PROJECT_TARGET, "classes/assets/less/style2.css").isFile()).isFalse();

    // Recreate the file with another name (same content)
    File newFile = new File(FAKE_PROJECT, "src/main/resources/assets/style3.less");
    FileUtils.write(newFile, originalStyleContent);
    mojo.fileCreated(newFile);
    File style3 = new File(FAKE_PROJECT_TARGET, "classes/assets/style3.css");
    assertThat(FileUtils.readFileToString(style3)).contains("-webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);")
            .contains("box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);").contains("border-color: #fdcdea;")
            .contains("color: #fe33ac;");

    // Update link
    long originalLastModified = ext.lastModified();
    FileUtils.touch(originalExternalStyle);
    mojo.fileUpdated(originalExternalStyle);
    // The file should have been updated
    assertThat(ext.lastModified()).isGreaterThanOrEqualTo(originalLastModified);
}

From source file:org.wisdom.maven.mojos.TypeScriptCompilerMojoTest.java

@Test
public void testWatching() throws MojoFailureException, MojoExecutionException, IOException, WatchingException,
        InterruptedException {/*from   ww w. j  ava 2s .  c o m*/
    cleanup();

    // Copy animal to animal (do not modify var as it is used by other tests).
    final File originalAnimal = new File(FAKE_PROJECT, "src/main/resources/assets/ts/Animal.ts");
    final File newAnimal = new File(FAKE_PROJECT, "src/main/resources/assets/ts/Animal2.ts");
    final File originalRT = new File(FAKE_PROJECT, "src/main/assets/assets/ts/raytracer.ts");
    String originalAnimalContent = FileUtils.readFileToString(originalAnimal);
    String newContent = originalAnimalContent.replace("Animal", "Animal2").replace("Snake", "Snake2")
            .replace("Horse", "Horse2").replace("tom", "tom2").replace("sam", "sam2");
    FileUtils.write(newAnimal, newContent);

    mojo.execute();

    final File anim = new File(FAKE_PROJECT_TARGET, "classes/assets/ts/Animal2.js");
    assertThat(anim).isFile();
    String content = FileUtils.readFileToString(anim);
    assertThat(content).contains("var sam2 = new Snake2(\"Sammy the Python\");");

    final File rt = new File(FAKE_PROJECT_TARGET, "wisdom/assets/ts/raytracer.js");
    assertThat(rt).isFile();
    content = FileUtils.readFileToString(rt);
    assertThat(content).contains("new Plane(new Vector(0.0, 1.0, 0.0), 0.0, Surfaces.checkerboard),");

    // Delete new animal
    newAnimal.delete();
    mojo.fileDeleted(newAnimal);

    assertThat(anim.isFile()).isFalse();
    // Check that the .d.ts and the .js.map are deleted too
    assertThat(new File(FAKE_PROJECT_TARGET, "classes/ts/Animal2.d.ts")).doesNotExist();
    assertThat(new File(FAKE_PROJECT_TARGET, "classes/ts/Animal2.js.map")).doesNotExist();

    // Recreate the file with another name (same content)
    File newFile = new File(FAKE_PROJECT, "src/main/resources/Animal3.ts");
    newContent = originalAnimalContent.replace("Animal", "Animal3").replace("Snake", "Snake3")
            .replace("Horse", "Horse3").replace("tom", "tom3").replace("sam", "sam3");
    FileUtils.write(newFile, newContent);
    mojo.fileCreated(newFile);
    File var3 = new File(FAKE_PROJECT_TARGET, "classes/Animal3.js");
    assertThat(var3).isFile();
    content = FileUtils.readFileToString(var3);
    assertThat(content).contains("var sam3 = new Snake3(\"Sammy the Python\");");

    // Update link
    long originalLastModified = rt.lastModified();
    FileUtils.touch(originalRT);
    mojo.fileUpdated(originalRT);
    // The file should have been updated
    assertThat(rt.lastModified()).isGreaterThanOrEqualTo(originalLastModified);
}

From source file:org.wisdom.maven.pipeline.PipelineTest.java

@Test
public void testAdditionUpdateAndDeleteOfFile() throws IOException {
    File txt = new File(SOURCES, "touch.txt");
    txt.createNewFile();/*from   www. j  a  v  a  2s . co m*/
    assertThat(txt.isFile()).isTrue();
    waitPullPeriod();
    assertThat(textWatcher.added).containsExactly("touch.txt");
    assertThat(mdWatcher.added).isEmpty();

    FileUtils.touch(txt);
    waitPullPeriod();
    assertThat(textWatcher.added).containsExactly("touch.txt");
    assertThat(textWatcher.updated).containsExactly("touch.txt");

    FileUtils.deleteQuietly(txt);
    waitPullPeriod();
    assertThat(textWatcher.added).containsExactly("touch.txt");
    assertThat(textWatcher.updated).containsExactly("touch.txt");
    assertThat(textWatcher.deleted).containsExactly("touch.txt");
}

From source file:org.wisdom.maven.pipeline.PipelineTest.java

@Test
public void testAdditionUpdateAndDeleteOfSubFiles() throws IOException {
    File dir = new File(SOURCES, "foo");
    dir.mkdirs();// ww  w  .jav a2s  .co  m
    File md = new File(SOURCES, "foo/touch.md");
    md.createNewFile();
    assertThat(md.isFile()).isTrue();
    waitPullPeriod();
    assertThat(mdWatcher.added).containsExactly("touch.md");

    FileUtils.touch(md);
    waitPullPeriod();
    assertThat(mdWatcher.added).containsExactly("touch.md");
    assertThat(mdWatcher.updated).containsExactly("touch.md");

    FileUtils.deleteQuietly(md);
    waitPullPeriod();
    assertThat(mdWatcher.added).containsExactly("touch.md");
    assertThat(mdWatcher.updated).containsExactly("touch.md");
    assertThat(mdWatcher.deleted).containsExactly("touch.md");
}

From source file:org.wisdom.myth.MythMojoTest.java

@Test
public void testWatching() throws MojoFailureException, MojoExecutionException, IOException, WatchingException,
        InterruptedException {//from   w ww.  jav a 2s .  co m
    cleanup();

    // Copy var to var2 (do not modify var as it is used by other tests).
    final File originalVar = new File(FAKE_PROJECT, "src/main/resources/var.css");
    final File newVar = new File(FAKE_PROJECT, "src/main/resources/var2.css");
    final File originalLink = new File(FAKE_PROJECT, "src/main/assets/myth/link.css");
    String originalVarContent = FileUtils.readFileToString(originalVar);
    FileUtils.copyFile(originalVar, newVar);

    mojo.execute();

    final File var = new File(FAKE_PROJECT_TARGET, "classes/var2.css");
    assertThat(var).isFile();
    String content = FileUtils.readFileToString(var);
    assertThat(content).contains("color: #847AD1;");
    assertThat(content).contains("padding: 10px;");

    final File link = new File(FAKE_PROJECT_TARGET, "wisdom/assets/myth/link.css");
    assertThat(link).isFile();
    content = FileUtils.readFileToString(link);
    assertThat(content).contains("color: rgb(157, 149, 218);");
    assertThat(content).contains(" -webkit-transition: color .2s;");

    // Delete var
    newVar.delete();
    mojo.fileDeleted(newVar);

    assertThat(var.isFile()).isFalse();

    // Recreate the file with another name (same content)
    File newFile = new File(FAKE_PROJECT, "src/main/resources/var3.css");
    FileUtils.write(newFile, originalVarContent);
    mojo.fileCreated(newFile);
    File var3 = new File(FAKE_PROJECT_TARGET, "classes/var3.css");
    assertThat(var3).isFile();
    content = FileUtils.readFileToString(var3);
    assertThat(content).contains("color: #847AD1;");
    assertThat(content).contains("padding: 10px;");

    // Update link
    long originalLastModified = link.lastModified();
    FileUtils.touch(originalLink);
    mojo.fileUpdated(originalLink);
    // The file should have been updated
    assertThat(link.lastModified()).isGreaterThanOrEqualTo(originalLastModified);
}

From source file:org.wisdom.ractivejs.RactiveJSTemplateCompilerMojoTest.java

@Test
public void testWatching() throws MojoFailureException, MojoExecutionException, IOException, WatchingException,
        InterruptedException {// w  w w .ja va  2s  . c o m
    // Copy var to var2 (do not modify var as it is used by other tests).
    final File originalView = new File(FAKE_PROJECT, "src/main/resources/view.ract");
    final File newView = new File(FAKE_PROJECT, "src/main/resources/newView.ract");
    String originalVarContent = FileUtils.readFileToString(originalView);
    FileUtils.copyFile(originalView, newView);

    mojo.execute();

    final File var = new File(FAKE_PROJECT_TARGET, "classes/newView.js");
    assertThat(var).isFile();
    String content = FileUtils.readFileToString(var);
    assertThat(content).isEqualTo(PARSED_TEMPLATE.replace(".view", ".newView"));

    final File link = new File(FAKE_PROJECT_TARGET, "classes/view.js");
    assertThat(link).isFile();
    content = FileUtils.readFileToString(link);
    assertThat(content).isEqualTo(PARSED_TEMPLATE);

    // Delete view
    newView.delete();
    mojo.fileDeleted(newView);

    assertThat(var.isFile()).isFalse();

    // Recreate the file with another name (same content)
    File newFile = new File(FAKE_PROJECT, "src/main/resources/view2.ract");
    FileUtils.write(newFile, originalVarContent);
    mojo.fileCreated(newFile);
    File view2 = new File(FAKE_PROJECT_TARGET, "classes/view2.js");
    assertThat(view2).isFile();
    content = FileUtils.readFileToString(view2);
    assertThat(content).isEqualTo(PARSED_TEMPLATE.replace(".view", ".view2"));

    // Update view
    long lastModified = newFile.lastModified();
    FileUtils.touch(newFile);
    mojo.fileUpdated(newFile);
    // The file should have been updated
    assertThat(newFile.lastModified()).isGreaterThanOrEqualTo(lastModified);
}

From source file:org.wisdom.typescript.TypeScriptMojoTest.java

@Test
public void testWatching() throws MojoFailureException, MojoExecutionException, IOException, WatchingException,
        InterruptedException {//  w  w w  .  jav  a  2 s . c o  m
    cleanup();

    // Copy animal to animal (do not modify var as it is used by other tests).
    final File originalAnimal = new File(FAKE_PROJECT, "src/main/resources/Animal.ts");
    final File newAnimal = new File(FAKE_PROJECT, "src/main/resources/Animal2.ts");
    final File originalRT = new File(FAKE_PROJECT, "src/main/assets/ts/raytracer.ts");
    String originalAnimalContent = FileUtils.readFileToString(originalAnimal);
    FileUtils.copyFile(originalAnimal, newAnimal);

    mojo.execute();

    final File anim = new File(FAKE_PROJECT_TARGET, "classes/Animal2.js");
    assertThat(anim).isFile();
    String content = FileUtils.readFileToString(anim);
    assertThat(content).contains("var sam = new Snake(\"Sammy the Python\");");

    final File rt = new File(FAKE_PROJECT_TARGET, "wisdom/assets/ts/raytracer.js");
    assertThat(rt).isFile();
    content = FileUtils.readFileToString(rt);
    assertThat(content).contains("new Plane(new Vector(0.0, 1.0, 0.0), 0.0, Surfaces.checkerboard),");

    // Delete new animal
    newAnimal.delete();
    mojo.fileDeleted(newAnimal);

    assertThat(anim.isFile()).isFalse();
    // Check that the .d.ts and the .js.map are deleted too
    assertThat(new File(FAKE_PROJECT_TARGET, "classes/Animal2.d.ts")).doesNotExist();
    assertThat(new File(FAKE_PROJECT_TARGET, "classes/Animal2.js.map")).doesNotExist();

    // Recreate the file with another name (same content)
    File newFile = new File(FAKE_PROJECT, "src/main/resources/Animal3.ts");
    FileUtils.write(newFile, originalAnimalContent);
    mojo.fileCreated(newFile);
    File var3 = new File(FAKE_PROJECT_TARGET, "classes/Animal3.js");
    assertThat(var3).isFile();
    content = FileUtils.readFileToString(var3);
    assertThat(content).contains("var sam = new Snake(\"Sammy the Python\");");

    // Update link
    long originalLastModified = rt.lastModified();
    FileUtils.touch(originalRT);
    mojo.fileUpdated(originalRT);
    // The file should have been updated
    assertThat(rt.lastModified()).isGreaterThanOrEqualTo(originalLastModified);
}

From source file:org.wso2.carbon.automation.extensions.servers.axis2server.Axis2ServerManager.java

/**
 * replace key store paths of file for HTTPS transport
 * @param file - name of the file to read
 * @param newFile - name of the new file to e created
 * @throws IOException// w  w w  .  j  a  va  2  s.  c om
 */
private void changeConfiguration(String file, String newFile) throws IOException {
    StringBuilder sb = new StringBuilder();
    // axis2 config
    File config = new File(FrameworkPathUtil.getSystemResourceLocation() + File.separator + "artifacts"
            + File.separator + "AXIS2" + File.separator + "config" + File.separator + file);
    BufferedReader br = null;
    OutputStream os = null;

    try {

        if (config != null) {
            String currentLine;

            br = new BufferedReader(
                    new InputStreamReader(new FileInputStream(config), Charset.defaultCharset()));
            while ((currentLine = br.readLine()) != null) {
                if (currentLine.contains("REPLACE_CK")) {
                    currentLine = currentLine.replace("REPLACE_CK",
                            System.getProperty(FrameworkConstants.CARBON_HOME) + File.separator + "repository"
                                    + File.separator + "resources" + File.separator + "security"
                                    + File.separator + "wso2carbon.jks");
                } else if (currentLine.contains("REPLACE_TS")) {
                    currentLine = currentLine.replace("REPLACE_TS",
                            System.getProperty(FrameworkConstants.CARBON_HOME) + File.separator + "repository"
                                    + File.separator + "resources" + File.separator + "security"
                                    + File.separator + "client-truststore.jks");
                }
                sb.append(currentLine);
            }
        }
        // created axis2 config
        File newConfig = new File(ExtensionUtils.getSystemResourceLocation() + File.separator + "artifacts"
                + File.separator + "AXIS2" + File.separator + "config" + File.separator + newFile);
        if (newConfig.exists()) {
            FileUtils.deleteQuietly(newConfig);
        }

        FileUtils.touch(newConfig);
        os = FileUtils.openOutputStream(newConfig);
        os.write(sb.toString().getBytes("UTF-8"));

    } finally {
        if (os != null)
            os.close();

        if (br != null)
            br.close();
    }

}