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.apache.sling.replication.serialization.impl.VoidReplicationPackageTest.java

@Test
public void testCreatedAndReadPackagesEquality() throws Exception {
    ReplicationRequest request = new ReplicationRequest(123l, ReplicationActionType.DELETE, "/abc");
    VoidReplicationPackage createdPackage = new VoidReplicationPackage(request, "VOID");
    VoidReplicationPackage readPackage = VoidReplicationPackage
            .fromStream(new ByteArrayInputStream("DELETE:[/abc]:123:VOID".getBytes()));
    assertEquals(createdPackage.getId(), readPackage.getId());
    assertEquals(createdPackage.getAction(), readPackage.getAction());
    assertEquals(createdPackage.getType(), readPackage.getType());
    assertEquals(createdPackage.getLength(), readPackage.getLength());
    assertEquals(Arrays.toString(createdPackage.getPaths()), Arrays.toString(readPackage.getPaths()));
    assertTrue(IOUtils.contentEquals(createdPackage.createInputStream(), readPackage.createInputStream()));
}

From source file:org.apache.sling.testing.samples.integrationtests.serverside.sling.post.SlingPostChunkUploadTest.java

/**
 * Test chunk upload without interruption.
 *//*from w w w. j av  a 2 s. c om*/
@Test
public void testChunkUpload() {
    try {
        // create 1000 byte file
        File file = createFile("helloworld", 100);
        int chunkSize = 400;
        String nodeName = file.getName();
        uploadChunks(parentPath, file, nodeName, 0, chunkSize, Integer.MAX_VALUE);

        // retrieve the stream on get and validate its content with uploaded
        // file
        HttpResponse response = httpGet(parentPath + "/" + nodeName);
        InputStream fis = new FileInputStream(file);
        Assert.assertEquals("content stream doesn't match", true, IOUtils.contentEquals(fis,
                new ByteArrayInputStream(getRequestExecutor().getContent().getBytes())));
        fis.close();
        // clean uploaded file from repository
        Map<String, String> reqParams = new HashMap<String, String>();
        reqParams.put(SlingPostConstants.RP_OPERATION, "delete");
        response = uploadMultiPart(parentPath + "/" + nodeName, reqParams, null, null);

        // status should be 404
        response = httpGet(parentPath + "/" + nodeName);
        Assert.assertEquals("status should be 404 not found ", 404, response.getStatusLine().getStatusCode());
        file.delete();
    } catch (Exception e) {
        log.error("error:", e);
        Assert.fail("exception caught: " + e.getMessage());
    }
}

From source file:org.apache.sling.testing.samples.integrationtests.serverside.sling.post.SlingPostChunkUploadTest.java

/**
 * Test chunk upload after interruption. Test the use of variable chunk
 * size. After interruption, client retrieves chunk upload information and
 * resume upload with variable chunk size.
 *///from www .  j  a va 2s .  co m
@Test
public void testInterruptedChunkUpload() {
    try {
        // create 1700 bytes file
        File file = createFile("helloworld", 170);
        String nodeName = file.getName();
        int chunkSize = 200;
        // uplaod first chunk 200 bytes uploaded
        uploadChunks(parentPath, file, nodeName, 0, chunkSize, 1);
        JSONObject json = getChunkJson(parentPath + "/" + nodeName);
        validate(json, 200, 1);

        chunkSize = 300;
        // upload next two chunks of 300 each.total 800 bytes
        // uploaded
        uploadChunks(parentPath, file, nodeName, 200, chunkSize, 2);
        json = getChunkJson(parentPath + "/" + nodeName);
        validate(json, 800, 3);

        chunkSize = 400;
        // upload two chunk of 400 each. total 1600 bytes and 5 chunks
        // uploaded
        uploadChunks(parentPath, file, nodeName, json.getInt(SlingPostConstants.NT_SLING_CHUNKS_LENGTH),
                chunkSize, 2);
        json = getChunkJson(parentPath + "/" + nodeName);
        validate(json, 1600, 5);

        chunkSize = 500;
        uploadChunks(parentPath, file, nodeName, json.getInt(SlingPostConstants.NT_SLING_CHUNKS_LENGTH),
                chunkSize, Integer.MAX_VALUE);

        HttpResponse response = httpGet(parentPath + "/" + nodeName);
        InputStream fis = new FileInputStream(file);
        Assert.assertEquals("content stream doesn't match", true, IOUtils.contentEquals(fis,
                new ByteArrayInputStream(getRequestExecutor().getContent().getBytes())));
        fis.close();

        // clean uploaded file from repository
        Map<String, String> reqParams = new HashMap<String, String>();
        reqParams.put(SlingPostConstants.RP_OPERATION, "delete");
        response = uploadMultiPart(parentPath + "/" + nodeName, reqParams, null, null);

        // status should be 404
        response = httpGet(parentPath + "/" + nodeName);
        Assert.assertEquals("status should be 404 not found ", 404, response.getStatusLine().getStatusCode());
        file.delete();
    } catch (Exception e) {
        log.error("error:", e);
        Assert.fail("exception caught: " + e.getMessage());
    }
}

From source file:org.apache.sling.testing.samples.integrationtests.serverside.sling.post.SlingPostChunkUploadTest.java

/**
 * Test two concurrent chunk upload. Second will fail. Test deletion of
 * incomplete upload and test new upload on the same path.
 *///from  w  w w  .  j a  v  a 2 s  .  c  o  m
@Test
public void testConcurrentChunkUpload() {
    try {
        // create 1700 bytes file
        File file = createFile("helloworld", 170);

        String nodeName = file.getName();
        int chunkSize = 200;
        // uplaod 3 chunk of 200 bytes uploaded
        uploadChunks(parentPath, file, nodeName, 0, chunkSize, 3);
        JSONObject json = getChunkJson(parentPath + "/" + file.getName());
        validate(json, 600, 3);

        // create 1000 bytes file
        File secondFile = createFile("helloearth", 100);
        chunkSize = 300;
        // upload next two chunks of 300 each.total 800 bytes
        // uploaded
        try {
            uploadChunks(parentPath, secondFile, nodeName, 0, chunkSize, 1);
            Assert.fail("second upload should fail");
        } catch (Exception ignore) {

        }
        try {
            uploadChunks(parentPath, secondFile, nodeName, 200, chunkSize, 2);
            Assert.fail("second upload should fail");
        } catch (Exception ignore) {

        }
        // clean uploaded file from repository
        Map<String, String> reqParams = new HashMap<String, String>();
        reqParams.put(SlingPostConstants.RP_OPERATION, "delete");
        reqParams.put(":applyToChunks", "true");
        HttpResponse response = uploadMultiPart(parentPath + "/" + file.getName(), reqParams, null, null);
        Assert.assertEquals("status should be 200 OK ", 200, response.getStatusLine().getStatusCode());

        chunkSize = 200;
        uploadChunks(parentPath, secondFile, nodeName, 0, chunkSize, Integer.MAX_VALUE);

        response = httpGet(parentPath + "/" + nodeName);
        InputStream fis = new FileInputStream(secondFile);
        Assert.assertEquals("content stream doesn't match", true, IOUtils.contentEquals(fis,
                new ByteArrayInputStream(getRequestExecutor().getContent().getBytes())));
        fis.close();

        // clean uploaded file from repository
        reqParams = new HashMap<String, String>();
        reqParams.put(SlingPostConstants.RP_OPERATION, "delete");
        response = uploadMultiPart(parentPath + "/" + nodeName, reqParams, null, null);

        // status should be 404
        response = httpGet(parentPath + "/" + nodeName);
        Assert.assertEquals("status should be 404 not found ", 404, response.getStatusLine().getStatusCode());
        file.delete();
        secondFile.delete();
    } catch (Exception e) {
        log.error("error:", e);
        Assert.fail("exception caught: " + e.getMessage());
    }
}

From source file:org.apache.sling.testing.samples.integrationtests.serverside.sling.post.SlingPostChunkUploadTest.java

/**
 * Test upload on a existing node. Test that binary content doesn't get
 * updated until chunk upload finishes./* ww  w.j a va2  s .c  om*/
 */
@Test
public void testChunkUploadOnExistingNode() {
    try {
        // create 1700 bytes file
        File file = createFile("helloworld", 170);
        String nodeName = file.getName();
        InputStream fis = new FileInputStream(file);
        uploadMultiPart(parentPath, null, fis, file.getName());
        fis.close();
        HttpResponse response = httpGet(parentPath + "/" + nodeName);
        fis = new FileInputStream(file);
        Assert.assertEquals("content stream doesn't match", true, IOUtils.contentEquals(fis,
                new ByteArrayInputStream(getRequestExecutor().getContent().getBytes())));
        fis.close();
        // create 1000 bytes file
        File secondFile = createFile("helloearth", 100);
        int chunkSize = 200;
        // uplaod 3 chunk of 200 bytes uploaded
        uploadChunks(parentPath, secondFile, nodeName, 0, chunkSize, 3);
        JSONObject json = getChunkJson(parentPath + "/" + nodeName);
        validate(json, 600, 3);

        response = httpGet(parentPath + "/" + nodeName);
        fis = new FileInputStream(file);
        Assert.assertEquals("content stream doesn't match", true, IOUtils.contentEquals(fis,
                new ByteArrayInputStream(getRequestExecutor().getContent().getBytes())));
        fis.close();

        uploadChunks(parentPath, secondFile, nodeName, 600, chunkSize, Integer.MAX_VALUE);
        response = httpGet(parentPath + "/" + nodeName);
        fis = new FileInputStream(secondFile);
        Assert.assertEquals("content stream doesn't match", true, IOUtils.contentEquals(fis,
                new ByteArrayInputStream(getRequestExecutor().getContent().getBytes())));
        fis.close();

        // clean uploaded file from repository
        Map<String, String> reqParams = new HashMap<String, String>();
        reqParams.put(SlingPostConstants.RP_OPERATION, "delete");
        response = uploadMultiPart(parentPath + "/" + nodeName, reqParams, null, null);

        // status should be 404
        response = httpGet(parentPath + "/" + nodeName);
        Assert.assertEquals("status should be 404 not found ", 404, response.getStatusLine().getStatusCode());
        file.delete();
        secondFile.delete();
    } catch (Exception e) {
        log.error("error:", e);
        Assert.fail("exception caught: " + e.getMessage());
    } finally {

    }
}

From source file:org.apache.sling.testing.samples.integrationtests.serverside.sling.post.SlingPostChunkUploadTest.java

/**
 * Test use case where file size is not known in advance. File parameter
 * "@Completed" indicates file completion.
 *//*from   w  w  w.  j av  a2s .co m*/
@Test
public void testChunkUploadOnStreaming() {
    try {
        // create 1700 bytes file
        File file = createFile("helloworld", 170);
        String nodeName = file.getName();
        InputStream fis = new FileInputStream(file);
        int chunkSize = 200;
        uploadPart(parentPath, file, file.getName(), 0, 0, chunkSize, false);
        uploadPart(parentPath, file, file.getName(), 0, 200, chunkSize, false);

        uploadPart(parentPath, file, file.getName(), 0, 400, chunkSize, true);
        fis.close();

        File secondFile = createFile("helloworld", 60);
        HttpResponse response = httpGet(parentPath + "/" + nodeName);
        fis = new FileInputStream(secondFile);
        Assert.assertEquals("content stream doesn't match", true, IOUtils.contentEquals(fis,
                new ByteArrayInputStream(getRequestExecutor().getContent().getBytes())));
        fis.close();
        // clean uploaded file from repository
        Map<String, String> reqParams = new HashMap<String, String>();
        reqParams.put(SlingPostConstants.RP_OPERATION, "delete");
        response = uploadMultiPart(parentPath + "/" + nodeName, reqParams, null, null);

        // status should be 404
        response = httpGet(parentPath + "/" + nodeName);
        Assert.assertEquals("status should be 404 not found ", 404, response.getStatusLine().getStatusCode());
        file.delete();
        secondFile.delete();
    } catch (Exception e) {
        log.error("error:", e);
        Assert.fail("exception caught: " + e.getMessage());
    } finally {
    }
}

From source file:org.apache.solr.common.util.ContentStreamTest.java

public void testFileStream() throws IOException {
    InputStream is = new SolrResourceLoader().openResource("solrj/README");
    assertNotNull(is);/*from w  w w . j  a  v a2 s  . co m*/
    File file = new File(createTempDir().toFile(), "README");
    FileOutputStream os = new FileOutputStream(file);
    IOUtils.copy(is, os);
    os.close();
    is.close();

    ContentStreamBase stream = new ContentStreamBase.FileStream(file);
    InputStream s = stream.getStream();
    FileInputStream fis = new FileInputStream(file);
    InputStreamReader isr = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
    Reader r = stream.getReader();
    try {
        assertEquals(file.length(), stream.getSize().intValue());
        assertTrue(IOUtils.contentEquals(fis, s));
        assertTrue(IOUtils.contentEquals(isr, r));
    } finally {
        s.close();
        r.close();
        isr.close();
        fis.close();
    }
}

From source file:org.apache.solr.common.util.ContentStreamTest.java

public void testURLStream() throws IOException {
    InputStream is = new SolrResourceLoader().openResource("solrj/README");
    assertNotNull(is);/*from   w  w w .  j  a v  a2 s  . c  om*/
    File file = new File(createTempDir().toFile(), "README");
    FileOutputStream os = new FileOutputStream(file);
    IOUtils.copy(is, os);
    os.close();
    is.close();

    ContentStreamBase stream = new ContentStreamBase.URLStream(new URL(file.toURI().toASCIIString()));
    InputStream s = stream.getStream();
    FileInputStream fis = new FileInputStream(file);
    FileInputStream fis2 = new FileInputStream(file);
    InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
    Reader r = stream.getReader();
    try {
        assertTrue(IOUtils.contentEquals(fis2, s));
        assertEquals(file.length(), stream.getSize().intValue());
        assertTrue(IOUtils.contentEquals(isr, r));
        assertEquals(file.length(), stream.getSize().intValue());
    } finally {
        r.close();
        s.close();
        isr.close();
        fis.close();
        fis2.close();
    }
}

From source file:org.apache.synapse.commons.json.StreamBuilderFormatterTest.java

public void test2() {
    try {/*w  w  w.  j  a  v a  2  s. c  om*/
        InputStream is = Util.getJson(1);
        MessageContext message = Util.newMessageContext();

        OMElement element = jsonBuilder.processDocument(is, "application/json", message);

        OutputStream out = Util.newOutputStream();
        formatter.writeTo(message, null, out, false);
        InputStream outContent = new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray());
        InputStream compare = Util.getJson(1);
        assertTrue(IOUtils.contentEquals(outContent, compare));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        assertTrue(false);
    } catch (AxisFault axisFault) {
        axisFault.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.tiles.autotag.freemarker.FMModelGeneratorTest.java

/**
 * Test method for {@link TagClassGenerator#generate(File, String, TemplateSuite, TemplateClass, java.util.Map)}.
 * @throws Exception If something goes wrong.
 *///from   ww w.j a v  a 2 s . com
@Test
public void testGenerate() throws Exception {
    Properties props = new Properties();
    InputStream propsStream = getClass().getResourceAsStream("/org/apache/tiles/autotag/velocity.properties");
    props.load(propsStream);
    propsStream.close();
    VelocityEngine velocityEngine = new VelocityEngine(props);

    FMModelGenerator generator = new FMModelGenerator(velocityEngine);
    File tempDir = new File(System.getProperty("java.io.tmpdir"), "autotag");
    OutputLocator locator = new DirectoryOutputLocator(tempDir);
    tempDir.deleteOnExit();
    TemplateSuite suite = new TemplateSuite("tldtest", "Test for TLD docs.");

    List<TemplateParameter> params = new ArrayList<TemplateParameter>();
    TemplateParameter param = new TemplateParameter("one", "one", "java.lang.String", null, true, false);
    param.setDocumentation("Parameter one.");
    params.add(param);
    param = new TemplateParameter("two", "two", "int", null, false, false);
    param.setDocumentation("Parameter two.");
    params.add(param);
    param = new TemplateParameter("three", "three", "boolean", null, false, false);
    param.setDocumentation("Parameter three.");
    params.add(param);
    param = new TemplateParameter("request", "request", REQUEST_CLASS, null, false, true);
    param.setDocumentation("The request.");
    params.add(param);
    param = new TemplateParameter("modelBody", "modelBody", ModelBody.class.getName(), null, false, false);
    param.setDocumentation("The body.");
    params.add(param);
    TemplateMethod executeMethod = new TemplateMethod("execute", params);

    TemplateClass clazz = new TemplateClass("org.apache.tiles.autotag.template.DoStuffTemplate", "doStuff",
            "DoStuff", executeMethod);
    clazz.setDocumentation("Documentation of the DoStuff class.");

    generator.generate(locator, "org.apache.tiles.autotag.freemarker.test", suite, clazz, null,
            "org.apache.tiles.autotag.freemarker.test.Runtime", REQUEST_CLASS);

    InputStream expected = getClass()
            .getResourceAsStream("/org/apache/tiles/autotag/freemarker/test/DoStuffFMModel.javat");
    File effectiveFile = new File(tempDir, "/org/apache/tiles/autotag/freemarker/test/DoStuffFMModel.java");
    assertTrue(effectiveFile.exists());
    InputStream effective = new FileInputStream(effectiveFile);
    assertTrue(IOUtils.contentEquals(effective, expected));
    effective.close();
    expected.close();

    suite.addTemplateClass(clazz);
    params = new ArrayList<TemplateParameter>();
    param = new TemplateParameter("one", "one", "java.lang.Double", null, true, false);
    param.setDocumentation("Parameter one.");
    params.add(param);
    param = new TemplateParameter("two", "two", "float", null, false, false);
    param.setDocumentation("Parameter two.");
    params.add(param);
    param = new TemplateParameter("three", "three", "java.util.Date", null, false, false);
    param.setDocumentation("Parameter three.");
    params.add(param);
    param = new TemplateParameter("request", "request", REQUEST_CLASS, null, false, true);
    param.setDocumentation("The request.");
    params.add(param);
    executeMethod = new TemplateMethod("execute", params);

    clazz = new TemplateClass("org.apache.tiles.autotag.template.DoStuffNoBodyTemplate", "doStuffNoBody",
            "DoStuffNoBody", executeMethod);
    clazz.setDocumentation("Documentation of the DoStuffNoBody class.");

    suite.addTemplateClass(clazz);

    generator.generate(locator, "org.apache.tiles.autotag.freemarker.test", suite, clazz, null,
            "org.apache.tiles.autotag.freemarker.test.Runtime", REQUEST_CLASS);

    expected = getClass()
            .getResourceAsStream("/org/apache/tiles/autotag/freemarker/test/DoStuffNoBodyFMModel.javat");
    effectiveFile = new File(tempDir, "/org/apache/tiles/autotag/freemarker/test/DoStuffNoBodyFMModel.java");
    assertTrue(effectiveFile.exists());
    effective = new FileInputStream(effectiveFile);
    assertTrue(IOUtils.contentEquals(effective, expected));
    effective.close();
    expected.close();

    FileUtils.deleteDirectory(tempDir);
}