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:com.barchart.udt.TestSocketFile.java

/**
 * verify basic file send/receive/*from w ww .ja  va2s .com*/
 */
@Test(timeout = 10 * 1000)
public void fileTransfer() throws Exception {

    final InetSocketAddress addr1 = localSocketAddress();
    final InetSocketAddress addr2 = localSocketAddress();

    final SocketUDT peer1 = new SocketUDT(TypeUDT.STREAM);
    final SocketUDT peer2 = new SocketUDT(TypeUDT.STREAM);

    peer1.setBlocking(false);
    peer2.setBlocking(false);

    peer1.setRendezvous(true);
    peer2.setRendezvous(true);

    peer1.bind(addr1);
    peer2.bind(addr2);

    socketAwait(peer1, StatusUDT.OPENED);
    socketAwait(peer2, StatusUDT.OPENED);

    peer1.connect(addr2);
    peer2.connect(addr1);

    socketAwait(peer1, StatusUDT.CONNECTED);
    socketAwait(peer2, StatusUDT.CONNECTED);

    log.info("state 0 - connected");
    log.info("peer1 : {}", peer1);
    log.info("peer2 : {}", peer2);

    final int size = 64 * 1024;

    final Random random = new Random(0);
    final byte[] array1 = new byte[size];
    final byte[] array2 = new byte[size];
    random.nextBytes(array1);
    random.nextBytes(array2);

    final File folder = new File("./target/file");
    folder.mkdirs();

    final File source = File.createTempFile("source", "data", folder);
    final File target = File.createTempFile("target", "data", folder);

    FileUtils.writeByteArrayToFile(source, array1);
    FileUtils.writeByteArrayToFile(target, array2);

    assertEquals(size, source.length());
    assertEquals(size, target.length());

    assertFalse("files are different", FileUtils.contentEquals(source, target));

    // sender
    final Runnable task1 = new Runnable() {
        @Override
        public void run() {
            try {
                log.info("init send");
                final long length = peer1.sendFile(source, 0, size);
                assertEquals(length, size);
            } catch (final Exception e) {
                log.error("", e);
            }
        }
    };

    // receiver
    final Runnable task2 = new Runnable() {
        @Override
        public void run() {
            try {
                log.info("init recv");
                final long length = peer2.receiveFile(target, 0, size);
                assertEquals(length, size);
            } catch (final Exception e) {
                log.error("", e);
            }
        }
    };

    final ExecutorService executor = Executors.newFixedThreadPool(2);

    executor.submit(task1);
    executor.submit(task2);

    Thread.sleep(5 * 1000);

    executor.shutdownNow();

    assertTrue("files are the same", FileUtils.contentEquals(source, target));

    peer1.close();
    peer2.close();

}

From source file:com.sonarsource.cobol.ebcdic.MainTest.java

@Test
public void should_convert_all_files_in_directory_and_sub_directories() throws Exception {
    URL ebcdicSource = FileConverterTest.class.getResource("/EBCDIC.txt");
    File expectedOutputFile = new File(FileConverterTest.class.getResource("/ASCII.txt").toURI());

    File source = tempFolder.newFolder();
    File destination = tempFolder.newFolder();
    File inputFile1 = new File(source, "cobol.txt");
    File outputFile1 = new File(destination, "cobol.txt");
    FileUtils.copyURLToFile(ebcdicSource, inputFile1);
    File inputFile2 = new File(source, "sub/cobol.txt");
    File outputFile2 = new File(destination, "sub/cobol.txt");
    FileUtils.copyURLToFile(ebcdicSource, inputFile2);
    File inputFile3 = new File(source, "sub/sub/cobol.txt");
    File outputFile3 = new File(destination, "sub/sub/cobol.txt");
    FileUtils.copyURLToFile(ebcdicSource, inputFile3);

    String[] args = new String[] { "-f", "CP1047", "-t", "UTF-8", "-l", "80", source.getAbsolutePath(),
            destination.getAbsolutePath() };
    Main.main(args);/*from w  w w  .j  a  v  a 2  s . com*/

    assertThat(FileUtils.contentEquals(outputFile1, expectedOutputFile)).isTrue();
    assertThat(FileUtils.contentEquals(outputFile1, inputFile1)).isFalse();
    assertThat(FileUtils.contentEquals(outputFile2, expectedOutputFile)).isTrue();
    assertThat(FileUtils.contentEquals(outputFile2, inputFile2)).isFalse();
    assertThat(FileUtils.contentEquals(outputFile3, expectedOutputFile)).isTrue();
    assertThat(FileUtils.contentEquals(outputFile2, inputFile2)).isFalse();
}

From source file:com.microsoft.alm.plugin.idea.common.setup.WindowsStartupTest.java

@Test
public void testDoesKeyNeedUpdated_ExistingCmdOlder() throws Exception {
    TEST_EXE = File.createTempFile(TEST_CMD_NAME, ".cmd");
    Mockito.when(FileUtils.contentEquals(TEST_EXE, mockCmdFile)).thenReturn(false);
    Mockito.when(mockCmdFile.getPath()).thenReturn(TEST_CMD_NAME);
    Mockito.when(mockCmdFile.lastModified()).thenReturn(TEST_EXE.lastModified() + 1000);
    Mockito.when(Advapi32Util.registryGetStringValue(WinReg.HKEY_CLASSES_ROOT, WindowsStartup.VSOI_KEY, ""))
            .thenReturn(TEST_EXE.getPath() + " \"%1\"");
    Assert.assertTrue(WindowsStartup.doesKeyNeedUpdated(mockCmdFile));
}

From source file:azkaban.storage.LocalStorageTest.java

@Test
public void testPutGet() throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    File testFile = new File(classLoader.getResource(SAMPLE_FILE).getFile());

    final StorageMetadata metadata = new StorageMetadata(1, 1, "testuser", Md5Hasher.md5Hash(testFile));
    final String key = localStorage.put(metadata, testFile);
    assertNotNull(key);//ww  w. j  ava2 s  .  c o  m
    log.info("Key URI: " + key);

    File expectedTargetFile = new File(BASE_DIRECTORY,
            new StringBuilder().append(metadata.getProjectId()).append(File.separator)
                    .append(metadata.getProjectId()).append("-").append(new String(metadata.getHash()))
                    .append(".zip").toString());
    assertTrue(expectedTargetFile.exists());
    assertTrue(FileUtils.contentEquals(testFile, expectedTargetFile));

    // test get
    InputStream getIs = localStorage.get(key);
    assertNotNull(getIs);
    File getFile = new File("tmp.get");
    FileUtils.copyInputStreamToFile(getIs, getFile);
    assertTrue(FileUtils.contentEquals(testFile, getFile));
    getFile.delete();
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.text.TokenizedTextWriterTest.java

@Test
public void testDefault() throws UIMAException, IOException {
    String text = "This is the 1st sentence .\nHere is another sentence .";
    File targetFile = new File(context.getTestOutputFolder(), "TokenizedTextWriterTest.out");
    File tokenized = new File("src/test/resources/tokenizedTexts/textTokenized.txt");

    AnalysisEngineDescription writer = createEngineDescription(TokenizedTextWriter.class,
            TokenizedTextWriter.PARAM_TARGET_LOCATION, targetFile, TokenizedTextWriter.PARAM_SINGULAR_TARGET,
            true, TokenizedTextWriter.PARAM_OVERWRITE, true);
    TestRunner.runTest("id", writer, "en", text);
    assertTrue(FileUtils.contentEquals(tokenized, targetFile));
}

From source file:edu.snu.dolphin.bsp.examples.ml.algorithms.graph.PageRankTest.java

/**
 * Run PageRank test./*from  w w w .j  a v  a  2 s .co m*/
 */
@Test
public void testPageRank() throws Exception {
    final String[] args = { "-convThr", "0.01", "-maxIter", "10", "-dampingFactor", "0.85", "-local", "true",
            "-split", "1", "-input", ClassLoader.getSystemResource("data").getPath() + "/pagerank", "-output",
            OUTPUT_PATH, "-maxNumEvalLocal", "2" };

    DolphinLauncher.run(Configurations.merge(
            DolphinConfiguration.getConfiguration(args, PageRankParameters.getCommandLine()),
            Tang.Factory.getTang().newConfigurationBuilder().bindNamedParameter(JobIdentifier.class, "PageRank")
                    .bindImplementation(UserJobInfo.class, PageRankJobInfo.class)
                    .bindImplementation(UserParameters.class, PageRankParameters.class).build()));

    final File expected = new File(ClassLoader.getSystemResource("result").getPath() + "/pagerank");
    final File actual = new File(OUTPUT_PATH + "/rank/CtrlTask-0");

    Assert.assertTrue(FileUtils.contentEquals(expected, actual));
}

From source file:edu.cornell.med.icb.goby.modes.TestReformatCompactReadsMode.java

/**
 * Validates that the reformat compact reads mode is capable of writing the same contents.
 *
 * @throws IOException if there is a problem reading or writing to the files
 *//* w w  w  . j a  v a2  s  . com*/
@Test
public void noChange() throws IOException {
    final ReformatCompactReadsMode reformat = new ReformatCompactReadsMode();

    final String inputFilename = "test-data/compact-reads/s_1_sequence_short.compact-reads";
    reformat.setInputFilenames(inputFilename);

    final String outputFilename = "test-results/reformat-test.compact-reads";
    reformat.setOutputFile(outputFilename);
    reformat.execute();

    assertTrue(FileUtils.contentEquals(new File(inputFilename), new File(outputFilename)));
}

From source file:edu.snu.dolphin.bsp.examples.ml.algorithms.regression.LinearRegTest.java

/**
 * Run LinearReg test./*from   w  ww  .j  av a2  s . c om*/
 */
@Test
public void testLinearReg() throws Exception {
    final String[] args = { "-dim", "3", "-maxIter", "20", "-stepSize", "0.001", "-lambda", "0.1", "-local",
            "true", "-split", "4", "-input", ClassLoader.getSystemResource("data").getPath() + "/regression",
            "-output", OUTPUT_PATH, "-maxNumEvalLocal", "5" };

    DolphinLauncher.run(Configurations.merge(
            DolphinConfiguration.getConfiguration(args, LinearRegParameters.getCommandLine()),
            Tang.Factory.getTang().newConfigurationBuilder()
                    .bindNamedParameter(JobIdentifier.class, "Linear Regression")
                    .bindImplementation(UserJobInfo.class, LinearRegJobInfo.class)
                    .bindImplementation(UserParameters.class, LinearRegParameters.class).build()));

    final File expectedModel = new File(ClassLoader.getSystemResource("result").getPath() + "/linearreg_model");
    final File actualModel = new File(OUTPUT_PATH + "/model/CtrlTask-0");
    Assert.assertTrue(FileUtils.contentEquals(expectedModel, actualModel));

    final File expectedAccuracy = new File(
            ClassLoader.getSystemResource("result").getPath() + "/linearreg_loss");
    final File actualAccuracy = new File(OUTPUT_PATH + "/loss/CtrlTask-0");
    Assert.assertTrue(FileUtils.contentEquals(expectedAccuracy, actualAccuracy));
}

From source file:edu.snu.dolphin.bsp.examples.ml.algorithms.classification.LogisticRegTest.java

/**
 * Run LogisticReg test.// ww  w.ja  v  a2  s  .c o m
 */
@Test
public void testLogisticReg() throws Exception {
    final String[] args = { "-dim", "3", "-maxIter", "20", "-stepSize", "0.00001", "-lambda", "0.1", "-local",
            "true", "-split", "4", "-input",
            ClassLoader.getSystemResource("data").getPath() + "/classification", "-output", OUTPUT_PATH,
            "-maxNumEvalLocal", "5" };

    DolphinLauncher.run(Configurations.merge(
            DolphinConfiguration.getConfiguration(args, LogisticRegParameters.getCommandLine()),
            Tang.Factory.getTang().newConfigurationBuilder()
                    .bindNamedParameter(JobIdentifier.class, "Logistic Regression")
                    .bindImplementation(UserJobInfo.class, LogisticRegJobInfo.class)
                    .bindImplementation(UserParameters.class, LogisticRegParameters.class).build()));

    final File expectedModel = new File(
            ClassLoader.getSystemResource("result").getPath() + "/logisticreg_model");
    final File actualModel = new File(OUTPUT_PATH + "/model/CtrlTask-0");
    Assert.assertTrue(FileUtils.contentEquals(expectedModel, actualModel));

    final File expectedAccuracy = new File(
            ClassLoader.getSystemResource("result").getPath() + "/logisticreg_accuracy");
    final File actualAccuracy = new File(OUTPUT_PATH + "/accuracy/CtrlTask-0");
    Assert.assertTrue(FileUtils.contentEquals(expectedAccuracy, actualAccuracy));
}

From source file:de.douglas.maven.plugin.rpmsystemd.rpm.PostInstMojoTest.java

public void testGenerateRpmPostInstFileWithOverwrittenSettings() throws Exception {
    final Path testBaseDir = createDir(postInstTestsBaseDir.resolve("overwrite"));
    createDir(testBaseDir.resolve("target"));

    File pom = getTestFile(postInstSrcDir.resolve("pom-overwrite.xml").toString());

    PostInstMojo postInstMojo = (PostInstMojo) lookupMojo("generate-rpm-postinst-file", pom);
    postInstMojo.execute();//from  w  w  w. ja va2 s. c o  m

    assertTrue("Generated postinst file does not equal expected one", FileUtils.contentEquals(
            postInstSrcDir.resolve("postinst-overwrite-expected").toFile(),
            testBaseDir.resolve("target").resolve("rpm-systemd-maven-plugin").resolve("postinst").toFile()));
}