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

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

Introduction

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

Prototype

public static boolean contentEquals(Reader input1, Reader input2) throws IOException 

Source Link

Document

Compare the contents of two Readers to determine if they are equal or not.

Usage

From source file:org.onesec.raven.ivr.vmail.impl.VMailBoxNodeTest.java

@Test
public void getSavedMessagesTest() throws Exception {
    Date messDate = new Date();
    vbox.addMessage(new NewVMailMessageImpl("123", "333", messDate, new FileDataSource(testFile)));
    Thread.sleep(10);/*from w  w  w. j  a  va2s  .c o m*/
    vbox.addMessage(new NewVMailMessageImpl("123", "222", new Date(), new FileDataSource(testFile)));
    Thread.sleep(10);
    vbox.addMessage(new NewVMailMessageImpl("123", "111", new Date(), new FileDataSource(testFile)));
    assertEquals(0, vbox.getSavedMessagesCount());
    for (SavableStoredVMailMessage mess : vbox.getNewMessages())
        mess.save();
    assertEquals(3, vbox.getSavedMessagesCount());
    List<StoredVMailMessage> messages = vbox.getSavedMessages();
    StoredVMailMessage mess = messages.get(0);
    assertEquals(messDate, mess.getMessageDate());
    assertEquals("333", mess.getSenderPhoneNumber());
    assertTrue(IOUtils.contentEquals(new FileInputStream(testFile), mess.getAudioSource().getInputStream()));
    assertEquals("222", messages.get(1).getSenderPhoneNumber());
    assertEquals("111", messages.get(2).getSenderPhoneNumber());
}

From source file:org.opencastproject.staticfiles.impl.StaticFileServiceImplTest.java

@Test
public void testGetStaticFile() throws Exception {
    StaticFileServiceImpl staticFileServiceImpl = new StaticFileServiceImpl();
    staticFileServiceImpl.setOrganizationDirectoryService(orgDir);
    staticFileServiceImpl.activate(getComponentContext(null));
    staticFileServiceImpl.setSecurityService(getSecurityService());

    String videoUUID = staticFileServiceImpl.storeFile(videoFilename, new FileInputStream(videoFile));
    IOUtils.contentEquals(new FileInputStream(videoFile), staticFileServiceImpl.getFile(videoUUID));

    String imageUUID = staticFileServiceImpl.storeFile(imageFilename, new FileInputStream(imageFile));
    IOUtils.contentEquals(new FileInputStream(imageFile), staticFileServiceImpl.getFile(imageUUID));
}

From source file:org.opencastproject.staticfiles.impl.StaticFileServiceImplTest.java

@Test
public void testPersistFile() throws Exception {
    StaticFileServiceImpl staticFileServiceImpl = new StaticFileServiceImpl();
    staticFileServiceImpl.setOrganizationDirectoryService(orgDir);
    staticFileServiceImpl.activate(getComponentContext(null));
    staticFileServiceImpl.setSecurityService(getSecurityService());

    String videoUUID = staticFileServiceImpl.storeFile(videoFilename, new FileInputStream(videoFile));
    String imageUUID = staticFileServiceImpl.storeFile(imageFilename, new FileInputStream(imageFile));

    staticFileServiceImpl.persistFile(videoUUID);
    staticFileServiceImpl.purgeTemporaryStorageSection(getSecurityService().getOrganization().getId(), 0);

    IOUtils.contentEquals(new FileInputStream(videoFile), staticFileServiceImpl.getFile(videoUUID));
    try {/*from ww w  . j a va  2  s .  c  om*/
        staticFileServiceImpl.getFile(imageUUID);
        fail("File should no longer exist");
    } catch (NotFoundException e) {
        // expected
    }
}

From source file:org.openehealth.ipf.platform.camel.lbs.cxf.process.LbsCxfHugeFileTest.java

@Test
@Ignore//from  w  ww .j  a v a  2  s . c om
public void testHugeFile() throws Exception {
    TrackMemThread memTracker = new TrackMemThread();
    memTracker.start();
    try {
        DataSource dataSourceAttachInfo = new ByteArrayDataSource("Smaller content",
                "application/octet-stream");
        DataHandler dataHandlerAttachInfo = new DataHandler(dataSourceAttachInfo);
        Holder<DataHandler> handlerHolderAttachInfo = new Holder(dataHandlerAttachInfo);

        DataSource dataSourceOneWay = new InputStreamDataSource();
        DataHandler dataHandlerOneWay = new DataHandler(dataSourceOneWay);

        Holder<String> nameHolder = new Holder("Hello Camel!!");
        greeter.postMe(nameHolder, handlerHolderAttachInfo, dataHandlerOneWay);

        assertEquals("resultText", nameHolder.value);
        InputStream resultInputStream = handlerHolderAttachInfo.value.getInputStream();
        try {
            assertTrue(IOUtils.contentEquals(new HugeContentInputStream(), resultInputStream));
        } finally {
            resultInputStream.close();
        }
    } finally {
        memTracker.waitForStop();
        assertTrue("Memory consumption not constant. Difference was: " + memTracker.getDiff(),
                memTracker.getDiff() < 10000);
    }
}

From source file:org.openmrs.util.OpenmrsUtilTest.java

/**
 * @throws IOException/*from  w  w  w  . ja va  2 s . c o  m*/
 * @see OpenmrsUtil#copyFile(InputStream, OutputStream)
 */
@Test
public void copyFile_shouldCopyInputstreamToOutputstreamAndCloseTheOutputstream() throws IOException {

    String exampleInputStreamString = "ExampleInputStream";
    ByteArrayInputStream expectedByteArrayInputStream = new ByteArrayInputStream(
            exampleInputStreamString.getBytes());

    ByteArrayOutputStream output = spy(new ByteArrayOutputStream());
    OpenmrsUtil.copyFile(expectedByteArrayInputStream, output);

    expectedByteArrayInputStream.reset();
    ByteArrayInputStream byteArrayInputStreamFromOutputStream = new ByteArrayInputStream(output.toByteArray());

    assertTrue(IOUtils.contentEquals(expectedByteArrayInputStream, byteArrayInputStreamFromOutputStream));
    verify(output, times(1)).close();
}

From source file:org.openspotlight.storage.test.AbstractStorageSessionTest.java

@Test
public void shouldWorkWithInputStreamPropertiesOnAutoFlush() throws Exception {

    final StorageSession session = autoFlushInjector.getInstance(StorageSession.class);
    final StorageNode newNode = session.withPartition(ExamplePartition.DEFAULT).createNodeWithType("newNode1")
            .withSimpleKey("sequence", "1").withSimpleKey("name", "name").andCreate();

    final InputStream stream = new ByteArrayInputStream("streamValue".getBytes());

    newNode.setSimpleProperty(session, "streamProperty", stream);

    final StorageNode loadedNode = session.withPartition(ExamplePartition.DEFAULT).createCriteria()
            .withNodeType("newNode1").withProperty("sequence").equalsTo("1").withProperty("name")
            .equalsTo("name").buildCriteria().andSearchUnique(session);

    stream.reset();/*from w ww.ja v a 2  s.  co  m*/
    assertThat(IOUtils.contentEquals(newNode.getPropertyValueAsStream(session, "streamProperty"), stream),
            is(true));

    final InputStream loaded1 = loadedNode.getPropertyValueAsStream(session, "streamProperty");

    final ByteArrayOutputStream temporary1 = new ByteArrayOutputStream();
    IOUtils.copy(loaded1, temporary1);
    final String asString1 = new String(temporary1.toByteArray());
    final ByteArrayOutputStream temporary2 = new ByteArrayOutputStream();
    final InputStream loaded2 = loadedNode.getPropertyValueAsStream(session, "streamProperty");

    IOUtils.copy(loaded2, temporary2);
    final String asString2 = new String(temporary2.toByteArray());
    assertThat(asString1, is("streamValue"));
    assertThat(asString2, is("streamValue"));

}

From source file:org.openspotlight.storage.test.AbstractStorageSessionTest.java

@Test
public void shouldWorkWithInputStreamPropertiesOnExplicitFlush() throws Exception {

    final StorageSession session = explicitFlushInjector.getInstance(StorageSession.class);
    final StorageNode newNode = session.withPartition(ExamplePartition.DEFAULT).createNodeWithType("newNode1")
            .withSimpleKey("sequence", "1").withSimpleKey("name", "name").andCreate();

    final InputStream stream = new ByteArrayInputStream("streamValue".getBytes());

    newNode.setSimpleProperty(session, "streamProperty", stream);

    final StorageNode nullNode = session.withPartition(ExamplePartition.DEFAULT).createCriteria()
            .withNodeType("newNode1").withProperty("sequence").equalsTo("1").withProperty("name")
            .equalsTo("name").buildCriteria().andSearchUnique(session);

    assertThat(nullNode, is(nullValue()));
    session.flushTransient();//  w w  w .  j a  v a 2 s. co  m
    final StorageNode loadedNode = session.withPartition(ExamplePartition.DEFAULT).createCriteria()
            .withNodeType("newNode1").withProperty("sequence").equalsTo("1").withProperty("name")
            .equalsTo("name").buildCriteria().andSearchUnique(session);

    stream.reset();
    assertThat(IOUtils.contentEquals(newNode.getPropertyValueAsStream(session, "streamProperty"), stream),
            is(true));

    final InputStream loaded1 = loadedNode.getPropertyValueAsStream(session, "streamProperty");

    final ByteArrayOutputStream temporary1 = new ByteArrayOutputStream();
    IOUtils.copy(loaded1, temporary1);
    final String asString1 = new String(temporary1.toByteArray());
    final ByteArrayOutputStream temporary2 = new ByteArrayOutputStream();
    final InputStream loaded2 = loadedNode.getPropertyValueAsStream(session, "streamProperty");

    IOUtils.copy(loaded2, temporary2);
    final String asString2 = new String(temporary2.toByteArray());
    assertThat(asString1, is("streamValue"));
    assertThat(asString2, is("streamValue"));
}

From source file:org.osaf.cosmo.io.BufferedContentTest.java

public void testBufferedContent() throws Exception {
    Random random = new Random();

    // 100K test/*w  w  w. j  av a2s  . c  o m*/
    byte[] bytes = new byte[1024 * 100];
    random.nextBytes(bytes);

    BufferedContent content = new BufferedContent(new ByteArrayInputStream(bytes));

    Assert.assertTrue(content.getLength() == (1024 * 100));

    // verify streams are the same
    Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(bytes), content.getInputStream()));
    // verify we can re-consume the same stream
    Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(bytes), content.getInputStream()));

    // should fit into memory
    Assert.assertTrue(content.getInputStream() instanceof ByteArrayInputStream);

    // should be buffered into file
    content = new BufferedContent(new ByteArrayInputStream(bytes), 1024 * 50);
    Assert.assertTrue(content.getLength() == (1024 * 100));
    Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(bytes), content.getInputStream()));
    Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(bytes), content.getInputStream()));

    // should be in a file
    Assert.assertTrue(content.getInputStream() instanceof FileInputStream);
}

From source file:org.osaf.cosmo.util.BufferedServletOutputStreamTest.java

public void testBufferedServletOutputStream() throws Exception {
    byte[] testData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    BufferedServletOutputStream buf = new BufferedServletOutputStream();

    // no data yet
    Assert.assertTrue(buf.isEmpty());/*from  ww w.  j a va  2 s .  co  m*/

    // write 8 bytes
    buf.write(testData);

    // should be data
    Assert.assertFalse(buf.isEmpty());
    Assert.assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(testData), buf.getBufferInputStream()));
}

From source file:org.overlord.sramp.test.wagon.SrampWagonTest.java

/**
 * Verifies that the correct file content was downloaded.
 * @param expected/* w ww . j a v a2s  . co  m*/
 * @param actual
 * @throws IOException
 */
private void assertContents(String expected, File actual) throws IOException {
    InputStream expectedStream = null;
    InputStream actualStream = null;
    try {
        expectedStream = getClass().getResourceAsStream(expected);
        actualStream = FileUtils.openInputStream(actual);
        Assert.assertTrue("File contents failed to match: " + actual.getName(), //$NON-NLS-1$
                IOUtils.contentEquals(expectedStream, actualStream));
    } finally {
        IOUtils.closeQuietly(actualStream);
        IOUtils.closeQuietly(expectedStream);
    }
}