Search in sources :

Example 16 with CleanMojo

use of org.apache.maven.plugins.clean.CleanMojo in project maven-clean-plugin by apache.

the class CleanMojoTest method testCleanLockedFileWithNoError.

/**
 * Test the removal of a locked file on Windows systems.
 * <p>
 * Note: Unix systems doesn't lock any files.
 * </p>
 *
 * @throws Exception in case of an error.
 */
public void testCleanLockedFileWithNoError() throws Exception {
    if (!System.getProperty("os.name").toLowerCase().contains("windows")) {
        assertTrue("Ignored this test on none Windows based systems", true);
        return;
    }
    String pluginPom = getBasedir() + "/src/test/resources/unit/locked-file-test/plugin-pom.xml";
    // safety
    FileUtils.copyDirectory(new File(getBasedir(), "src/test/resources/unit/locked-file-test"), new File(getBasedir(), "target/test-classes/unit/locked-file-test"), null, "**/.svn,**/.svn/**");
    CleanMojo mojo = (CleanMojo) lookupMojo("clean", pluginPom);
    setVariableValueToObject(mojo, "failOnError", Boolean.FALSE);
    assertNotNull(mojo);
    File f = new File(getBasedir(), "target/test-classes/unit/locked-file-test/buildDirectory/file.txt");
    FileChannel channel = null;
    FileLock lock = null;
    try {
        channel = new RandomAccessFile(f, "rw").getChannel();
        lock = channel.lock();
        mojo.execute();
        assertTrue(true);
    } catch (MojoExecutionException expected) {
        fail("Should display a warning when deleting a file that is locked");
    } finally {
        if (lock != null) {
            lock.release();
        }
        if (channel != null) {
            channel.close();
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) CleanMojo(org.apache.maven.plugins.clean.CleanMojo) FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

File (java.io.File)16 RandomAccessFile (java.io.RandomAccessFile)16 CleanMojo (org.apache.maven.plugins.clean.CleanMojo)16 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 FileChannel (java.nio.channels.FileChannel)4 FileLock (java.nio.channels.FileLock)4