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

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

Introduction

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

Prototype

public static boolean contentEquals(File file1, File file2) throws IOException 

Source Link

Document

Compares the contents of two files to determine if they are equal or not.

Usage

From source file:org.berkholz.configurationframework.ConfigurationTest.java

/**
 * Test of init method, of class Configuration.
 *///from ww w.  ja  va2  s .  c o  m
@Test
public void testInit() {
    LOG.trace("Testing init method.");

    // creating instance of configuration
    Configuration instance = new Configuration();

    try {
        // this file is initialized
        File tmpFile = File.createTempFile("pabamo_targetInitFile-", ".xml");
        targetInitFile = new File(tmpFile.getParent() + File.separator + "pabamo_targetInitFile.xml");
        targetInitFile.deleteOnExit();
        tmpFile.deleteOnExit();
    } catch (IOException ex) {
        LOG.error(ex);
    }

    // initialize our testfile(here: targetInitFile)
    Boolean resultOfInit = instance.init(targetInitFile);
    Boolean compareResult = null;

    // we compare both files with Apache Commons IO library
    try {
        compareResult = FileUtils.contentEquals(templateInitFile, targetInitFile);
    } catch (IOException ex) {
        LOG.error(ex);
    }

    // both, init and comparation of files have to be successful
    assertEquals(true, resultOfInit && compareResult);
}

From source file:org.carcv.web.beans.StorageBeanIT.java

/**
 * Test method for {@link org.carcv.web.beans.StorageBean#storeToDirectory(java.io.InputStream, String, Path)}.
 *
 * @throws Exception/*from www . j  a  va  2s.co  m*/
 */
@Test
public void testStoreToDirectory() throws Exception {
    FileEntryTool tool = new FileEntryTool();

    InputStream is1 = getClass().getResourceAsStream("/img/skoda_oct.jpg");
    InputStream is2 = getClass().getResourceAsStream("/img/test_041.jpg");

    Path p1 = Paths.get("/tmp", "storageImage1-" + System.currentTimeMillis() + ".jpg");
    Path p2 = Paths.get("/tmp", "storageImage2-" + System.currentTimeMillis() + ".jpg");

    Files.copy(is1, p1);
    Files.copy(is2, p2);

    FileEntry f = tool.generate(p1, p2);
    assertFileEntry(f);

    assertNotNull(f);
    Path original = f.getCarImages().get(0).getFilepath();
    assertNotNull(original);
    assertTrue(Files.exists(original));

    // store
    String fileName = "test" + f.hashCode() + ".jpg";
    storageBean.storeToDirectory(Files.newInputStream(original), fileName, Paths.get("/tmp"));

    Path newPath = Paths.get("/tmp", fileName);
    assertNotNull(newPath);
    assertTrue(Files.exists(newPath));
    assertTrue(FileUtils.contentEquals(newPath.toFile(), original.toFile()));

    Files.delete(p1);
    Files.delete(p2);
    tool.close();
}

From source file:org.cloudifysource.rest.UploadControllerTest.java

private File assertUploadedFileExists(final File expectedFile, final String uploadKey) throws IOException {
    File restTempDir = new File(CloudifyConstants.REST_FOLDER);
    File uploadsFolder = new File(restTempDir, CloudifyConstants.UPLOADS_FOLDER_NAME);
    File uploadedFileDir = new File(uploadsFolder, uploadKey);
    System.out.println("uploaded file's folder: " + uploadedFileDir.getAbsolutePath());
    Assert.assertNotNull(uploadedFileDir);
    System.out.println("uploaded file's folder exists: " + uploadedFileDir.exists());
    Assert.assertTrue(uploadedFileDir.exists());
    System.out.println("uploaded file's folder isDirectory: " + uploadedFileDir.isDirectory());
    Assert.assertTrue(uploadedFileDir.isDirectory());
    File uploadedFile = new File(uploadedFileDir, UPLOADED_FILE_NAME);
    System.out.println("uploaded file " + uploadedFile.getAbsolutePath() + (uploadedFile.exists() ? "" : " not")
            + " exists.");
    Assert.assertTrue(uploadedFile.exists());
    System.out.println("uploaded file isFile: " + uploadedFile.isFile());
    Assert.assertTrue(uploadedFile.isFile());
    boolean contentEquals = FileUtils.contentEquals(expectedFile, uploadedFile);
    System.out.println(/* w  w  w  .j a v  a 2 s . c o  m*/
            "uploaded file [" + uploadedFile.getAbsolutePath() + "] content is " + (contentEquals ? "" : "not")
                    + " equal to the expected content [ of file - " + expectedFile.getAbsolutePath() + "]");
    Assert.assertTrue(contentEquals);
    return uploadedFile;
}

From source file:org.cloudifysource.rest.UploadRepoTest.java

private void assertUploadedFile(final File uploadedFile) throws IOException {
    Assert.assertNotNull(uploadedFile);/*from www  .java 2s  .c  o m*/
    // file expected to be a file and not a directory.
    Assert.assertTrue(uploadedFile.isFile());
    // unzip file if needed
    File unzippedFile = uploadedFile;
    String name = uploadedFile.getName();
    if (name.endsWith("zip")) {
        File tempDir = new File(new File(CloudifyConstants.TEMP_FOLDER), "tempDir");
        tempDir.mkdirs();
        tempDir.deleteOnExit();
        ZipUtils.unzip(uploadedFile, tempDir);
        unzippedFile = new File(tempDir, TEST_FILE_NAME);
        Assert.assertTrue(unzippedFile.exists());
        unzippedFile.deleteOnExit();
    }
    // check file name and content
    Assert.assertEquals(TEST_FILE_NAME, unzippedFile.getName());
    FileUtils.contentEquals(new File(TXT_FILE_PATH), unzippedFile);
}

From source file:org.codejuicer.java2csharp.BuilderTest.java

@Test
public void testCSharpBuilder() throws MojoExecutionException, IOException {
    String path = getClass().getClassLoader().getResource(".").getPath();
    ConversionConfiguration conf = new ConversionConfiguration();
    conf.setSourcePath(path + "../../src/test/java/org/codejuicer/java2csharp/testclasses");
    conf.setOutputDirectory(path + "outputfolder");
    conf.setName("test");
    Java2CsharpMojo mojo = new Java2CsharpMojo();
    mojo.setConversionConfigurations(new ConversionConfiguration[] { conf });
    mojo.execute();//w w w . j a  v  a2s .  c o  m

    List<String> expectedFiles = listFilesForFolder(new File(path + "expectedFolder"));

    List<String> outputFiles = listFilesForFolder(new File(path + "outputfolder"));
    for (int i = 0; i < expectedFiles.size(); i++) {
        File sourceFile = new File(expectedFiles.get(i));
        File outputFile = new File(outputFiles.get(i));
        assertTrue(FileUtils.contentEquals(sourceFile, outputFile));
    }
}

From source file:org.codice.imaging.nitf.core.AbstractWriterTest.java

protected void roundTripFile(String sourceFileName)
        throws URISyntaxException, NitfFormatException, IOException {
    String outputFile = FilenameUtils.getName(sourceFileName);
    NitfReader reader = new NitfInputStreamReader(new BufferedInputStream(getInputStream(sourceFileName)));
    SlottedParseStrategy parseStrategy = new SlottedParseStrategy(SlottedParseStrategy.ALL_SEGMENT_DATA);
    HeapStrategyConfiguration heapStrategyConfiguration = new HeapStrategyConfiguration(
            length -> length > ABOUT_100K);
    HeapStrategy<ImageInputStream> imageDataStrategy = new ConfigurableHeapStrategy<>(heapStrategyConfiguration,
            file -> new FileImageInputStream(file), is -> new MemoryCacheImageInputStream(is));
    parseStrategy.setImageHeapStrategy(imageDataStrategy);

    NitfParser.parse(reader, parseStrategy);
    NitfWriter writer = new NitfFileWriter(parseStrategy.getDataSource(), outputFile);
    writer.write();//from  w  ww. ja  va2  s . c om
    assertTrue(FileUtils.contentEquals(new File(getClass().getResource(sourceFileName).toURI()),
            new File(outputFile)));
    assertTrue(new File(outputFile).delete());

    // Do the same again, but with stream writing
    try (OutputStream outputStream = new FileOutputStream(outputFile)) {
        writer = new NitfOutputStreamWriter(parseStrategy.getDataSource(), outputStream);
        writer.write();
        assertTrue(FileUtils.contentEquals(new File(getClass().getResource(sourceFileName).toURI()),
                new File(outputFile)));
    }
    assertTrue(new File(outputFile).delete());
}

From source file:org.codice.imaging.nitf.core.StreamingModeTest.java

@Test
public void checkStreamingModeSourceRewrite() throws NitfFormatException, URISyntaxException, IOException {
    final String testfile = "/JitcNitf21Samples/ns3321a.nsf";
    String outputFile = FilenameUtils.getName(testfile);
    assertNotNull("Test file missing", getClass().getResource(testfile));

    File resourceFile = new File(getClass().getResource(testfile).toURI().getPath());
    SlottedParseStrategy parseStrategy = new SlottedParseStrategy(SlottedParseStrategy.ALL_SEGMENT_DATA);
    NitfReader reader = new FileReader(resourceFile);
    NitfParser.parse(reader, parseStrategy);
    NitfWriter writer = new NitfFileWriter(parseStrategy.getDataSource(), outputFile);
    writer.write();/*from w ww .  j  ava 2 s .  c om*/
    assertTrue(FileUtils.contentEquals(new File(getClass().getResource("/ns3321a.nsf.reference").toURI()),
            new File(outputFile)));
    assertTrue(new File(outputFile).delete());
}

From source file:org.commonjava.maven.ext.core.impl.JSONManipulatorTest.java

@Test
public void updateURL() throws ManipulationException, IOException {
    String modifyPath = "$.repository.url";

    File target = tf.newFile();// w  ww  .jav  a 2  s. c om
    FileUtils.copyFile(pluginFile, target);
    Project project = new Project(target, TestUtils.getDummyModel());

    jsonManipulator.internalApplyChanges(project, new JSONState.JSONOperation(target.getName(), modifyPath,
            "https://maven.repository.redhat.com/ga/"));
    assertTrue(FileUtils.readFileToString(target).contains("https://maven.repository.redhat.com/ga/"));
    assertFalse(FileUtils.contentEquals(pluginFile, target));
}

From source file:org.commonjava.maven.ext.io.JSONIOTest.java

@Test
public void writeFile() throws ManipulationException, IOException {
    DocumentContext doc = jsonIO.parseJSON(npmFile);

    File target = tf.newFile();/* ww  w.  ja v  a  2s  .c  om*/

    jsonIO.writeJSON(target, doc.jsonString());

    assertTrue(FileUtils.contentEquals(npmFile, target));
}

From source file:org.commonjava.maven.ext.io.JSONIOTest.java

@Test
public void modifyFile() throws ManipulationException, IOException {
    String deletePath = "$..resolved";
    DocumentContext doc = jsonIO.parseJSON(npmFile);
    doc.delete(deletePath);//from w  w  w. j ava 2s  .  c  om

    File target = tf.newFile();

    jsonIO.writeJSON(target, doc.jsonString());

    assertFalse(FileUtils.contentEquals(npmFile, target));
}