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:info.estebanluengo.alfrescoAPI.test.AlfrescoAPICRUDTest.java

@Test
public void getDocumentContent() throws IOException {
    logger.debug("Init getDocumentContent test");
    createSessionIfNeeded();//from w w w  .  jav  a  2 s .  c  o m
    String fileName = getFileName();
    String folderName = testProperties.getUsername();
    byte[] contentFile = getFile();
    Document doc = null;
    try {
        doc = AlfrescoAPI.createDocument(session, getFolder(folderName), fileName, contentFile, PDF_MIME_TYPE);
        byte[] contentDoc = AlfrescoAPI.getDocumentContent(session, doc.getId());
        assertNotNull(contentDoc);
        assertTrue(contentDoc.length == contentFile.length);
        assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(contentFile),
                new ByteArrayInputStream(contentDoc)));
    } finally {
        deleteDocument(doc);
    }
}

From source file:com.example.util.FileUtils.java

/**
 * Compares the contents of two files to determine if they are equal or not.
 * <p>/*from  w w  w.j  a v a 2 s.c  o  m*/
 * This method checks to see if the two files are different lengths
 * or if they point to the same file, before resorting to byte-by-byte
 * comparison of the contents.
 * <p>
 * Code origin: Avalon
 *
 * @param file1  the first file
 * @param file2  the second file
 * @return true if the content of the files are equal or they both don't
 * exist, false otherwise
 * @throws IOException in case of an I/O error
 */
public static boolean contentEquals(File file1, File file2) throws IOException {
    boolean file1Exists = file1.exists();
    if (file1Exists != file2.exists()) {
        return false;
    }

    if (!file1Exists) {
        // two not existing files are equal
        return true;
    }

    if (file1.isDirectory() || file2.isDirectory()) {
        // don't want to compare directory contents
        throw new IOException("Can't compare directories, only files");
    }

    if (file1.length() != file2.length()) {
        // lengths differ, cannot be equal
        return false;
    }

    if (file1.getCanonicalFile().equals(file2.getCanonicalFile())) {
        // same file
        return true;
    }

    InputStream input1 = null;
    InputStream input2 = null;
    try {
        input1 = new FileInputStream(file1);
        input2 = new FileInputStream(file2);
        return IOUtils.contentEquals(input1, input2);

    } finally {
        IOUtils.closeQuietly(input1);
        IOUtils.closeQuietly(input2);
    }
}

From source file:it.JerseyIssueRestClientTest.java

@Test
public void testAddAttachment() throws IOException {
    if (!doesJiraSupportAddingAttachment()) {
        return;/*  w w  w.  j  av  a  2 s  .c  om*/
    }
    final IssueRestClient issueClient = client.getIssueClient();
    final Issue issue = issueClient.getIssue("TST-3", pm);
    assertFalse(issue.getAttachments().iterator().hasNext());

    String str = "Wojtek";
    final String filename1 = "my-test-file";
    issueClient.addAttachment(pm, issue.getAttachmentsUri(), new ByteArrayInputStream(str.getBytes("UTF-8")),
            filename1);
    final String filename2 = "my-picture.png";
    issueClient.addAttachment(pm, issue.getAttachmentsUri(),
            JerseyIssueRestClientTest.class.getResourceAsStream("/attachment-test/transparent-png.png"),
            filename2);

    final Issue issueWithAttachments = issueClient.getIssue("TST-3", pm);
    final Iterable<Attachment> attachments = issueWithAttachments.getAttachments();
    assertEquals(2, Iterables.size(attachments));
    final Iterable<String> attachmentsNames = Iterables.transform(attachments,
            new Function<Attachment, String>() {
                @Override
                public String apply(@Nullable Attachment from) {
                    return from.getFilename();
                }
            });
    assertThat(attachmentsNames, IterableMatcher.hasOnlyElements(filename1, filename2));
    final Attachment pictureAttachment = Iterables.find(attachments, new Predicate<Attachment>() {
        @Override
        public boolean apply(@Nullable Attachment input) {
            return filename2.equals(input.getFilename());
        }
    });

    // let's download it now and compare it's binary content

    assertTrue(IOUtils.contentEquals(
            JerseyIssueRestClientTest.class.getResourceAsStream("/attachment-test/transparent-png.png"),
            issueClient.getAttachment(pm, pictureAttachment.getContentUri())));
}

From source file:it.AsynchronousIssueRestClientTest.java

@Test
// TODO: implement
public void testAddAttachment() throws IOException {

    if (!doesJiraSupportAddingAttachment()) {
        return;/*from ww w.j  a  v  a2 s.c om*/
    }
    final IssueRestClient issueClient = client.getIssueClient();
    final Issue issue = issueClient.getIssue("TST-3").claim();
    assertFalse(issue.getAttachments().iterator().hasNext());

    String str = "Wojtek";
    final String filename1 = "my-test-file";
    issueClient.addAttachment(issue.getAttachmentsUri(), new ByteArrayInputStream(str.getBytes("UTF-8")),
            filename1).claim();
    final String filename2 = "my-picture.png";
    issueClient.addAttachment(issue.getAttachmentsUri(),
            AsynchronousIssueRestClientTest.class.getResourceAsStream("/attachment-test/transparent-png.png"),
            filename2).claim();

    final Issue issueWithAttachments = issueClient.getIssue("TST-3").claim();
    final Iterable<Attachment> attachments = issueWithAttachments.getAttachments();
    assertEquals(2, Iterables.size(attachments));
    final Iterable<String> attachmentsNames = Iterables.transform(attachments,
            new Function<Attachment, String>() {
                @Override
                public String apply(@Nullable Attachment from) {
                    return from.getFilename();
                }
            });
    assertThat(attachmentsNames, containsInAnyOrder(filename1, filename2));
    final Attachment pictureAttachment = Iterables.find(attachments, new Predicate<Attachment>() {
        @Override
        public boolean apply(@Nullable Attachment input) {
            return filename2.equals(input.getFilename());
        }
    });

    // let's download it now and compare it's binary content

    assertTrue(IOUtils.contentEquals(
            AsynchronousIssueRestClientTest.class.getResourceAsStream("/attachment-test/transparent-png.png"),
            issueClient.getAttachment(pictureAttachment.getContentUri()).claim()));
}

From source file:it.AsynchronousIssueRestClientTest.java

@Test
public void testAddAttachmentWithUtf8InNameAndBody() throws IOException {
    final IssueRestClient issueClient = client.getIssueClient();
    final Issue issue = issueClient.getIssue("TST-3").claim();
    assertFalse(issue.getAttachments().iterator().hasNext());

    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
            UTF8_FILE_BODY.getBytes("UTF-8"));
    issueClient.addAttachment(issue.getAttachmentsUri(), byteArrayInputStream, UTF8_FILE_NAME).claim();

    final Issue issueWithAttachments = issueClient.getIssue("TST-3").claim();
    final Iterable<Attachment> attachments = issueWithAttachments.getAttachments();
    assertEquals(1, Iterables.size(attachments));
    final Attachment attachment = attachments.iterator().next();
    assertThat(attachment.getFilename(), equalTo(UTF8_FILE_NAME));

    assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(UTF8_FILE_BODY.getBytes("UTF-8")),
            issueClient.getAttachment(attachment.getContentUri()).claim()));
}

From source file:it.JerseyIssueRestClientTest.java

@Test
public void testAddAttachments() throws IOException {
    if (!doesJiraSupportAddingAttachment()) {
        return;//from  www . j  ava 2 s.  c o  m
    }
    final IssueRestClient issueClient = client.getIssueClient();
    final Issue issue = issueClient.getIssue("TST-4", pm);
    assertFalse(issue.getAttachments().iterator().hasNext());

    final AttachmentInput[] attachmentInputs = new AttachmentInput[3];
    for (int i = 1; i <= 3; i++) {
        attachmentInputs[i - 1] = new AttachmentInput("my-test-file-" + i + ".txt",
                new ByteArrayInputStream(("content-of-the-file-" + i).getBytes("UTF-8")));
    }
    issueClient.addAttachments(pm, issue.getAttachmentsUri(), attachmentInputs);

    final Issue issueWithAttachments = issueClient.getIssue("TST-4", pm);
    final Iterable<Attachment> attachments = issueWithAttachments.getAttachments();
    assertEquals(3, Iterables.size(attachments));
    Pattern pattern = Pattern.compile("my-test-file-(\\d)\\.txt");
    for (Attachment attachment : attachments) {
        assertTrue(pattern.matcher(attachment.getFilename()).matches());
        final Matcher matcher = pattern.matcher(attachment.getFilename());
        matcher.find();
        final String interfix = matcher.group(1);
        assertTrue(IOUtils.contentEquals(
                new ByteArrayInputStream(("content-of-the-file-" + interfix).getBytes("UTF-8")),
                issueClient.getAttachment(pm, attachment.getContentUri())));

    }
}

From source file:it.JerseyIssueRestClientTest.java

@Test
public void testAddFileAttachments() throws IOException {
    if (!doesJiraSupportAddingAttachment()) {
        return;//  ww w.  j  av a  2  s.  c  o  m
    }
    final IssueRestClient issueClient = client.getIssueClient();
    final Issue issue = issueClient.getIssue("TST-5", pm);
    assertFalse(issue.getAttachments().iterator().hasNext());

    final File tempFile = File.createTempFile("jim-integration-test", ".txt");
    tempFile.deleteOnExit();
    FileWriter writer = new FileWriter(tempFile);
    writer.write("This is the content of my file which I am going to upload to JIRA for testing.");
    writer.close();
    issueClient.addAttachments(pm, issue.getAttachmentsUri(), tempFile);

    final Issue issueWithAttachments = issueClient.getIssue("TST-5", pm);
    final Iterable<Attachment> attachments = issueWithAttachments.getAttachments();
    assertEquals(1, Iterables.size(attachments));
    assertTrue(IOUtils.contentEquals(new FileInputStream(tempFile),
            issueClient.getAttachment(pm, attachments.iterator().next().getContentUri())));
}

From source file:it.AsynchronousIssueRestClientTest.java

@Test
// TODO: implement
public void testAddAttachments() throws IOException {
    if (!doesJiraSupportAddingAttachment()) {
        return;/*w w w.j  av  a 2  s .c  om*/
    }
    final IssueRestClient issueClient = client.getIssueClient();
    final Issue issue = issueClient.getIssue("TST-4").claim();
    assertFalse(issue.getAttachments().iterator().hasNext());

    final AttachmentInput[] attachmentInputs = new AttachmentInput[3];
    for (int i = 1; i <= 3; i++) {
        attachmentInputs[i - 1] = new AttachmentInput("my-test-file-" + i + ".txt",
                new ByteArrayInputStream(("content-of-the-file-" + i).getBytes("UTF-8")));
    }
    issueClient.addAttachments(issue.getAttachmentsUri(), attachmentInputs).claim();

    final Issue issueWithAttachments = issueClient.getIssue("TST-4").claim();
    final Iterable<Attachment> attachments = issueWithAttachments.getAttachments();
    assertEquals(3, Iterables.size(attachments));
    Pattern pattern = Pattern.compile("my-test-file-(\\d)\\.txt");
    for (Attachment attachment : attachments) {
        assertTrue(pattern.matcher(attachment.getFilename()).matches());
        final Matcher matcher = pattern.matcher(attachment.getFilename());
        matcher.find();
        final String interfix = matcher.group(1);
        assertTrue(IOUtils.contentEquals(
                new ByteArrayInputStream(("content-of-the-file-" + interfix).getBytes("UTF-8")),
                issueClient.getAttachment(attachment.getContentUri()).claim()));

    }
}

From source file:it.AsynchronousIssueRestClientTest.java

@Test
public void testAddAttachmentsWithUtf8InNameAndBody() throws IOException {
    final IssueRestClient issueClient = client.getIssueClient();
    final Issue issue = issueClient.getIssue("TST-4").claim();
    assertFalse(issue.getAttachments().iterator().hasNext());

    final AttachmentInput[] attachmentInputs = new AttachmentInput[3];
    final String[] names = new String[3];
    final String[] contents = new String[3];
    for (int i = 0; i < 3; i++) {
        names[i] = UTF8_FILE_NAME + "-" + i + ".txt";
        contents[i] = "content-of-the-file-" + i + " with some utf8: " + UTF8_FILE_BODY;
        attachmentInputs[i] = new AttachmentInput(names[i],
                new ByteArrayInputStream(contents[i].getBytes("UTF-8")));
    }// ww w. j  a v a  2 s.  co  m
    issueClient.addAttachments(issue.getAttachmentsUri(), attachmentInputs).claim();

    final Issue issueWithAttachments = issueClient.getIssue("TST-4").claim();
    final Iterable<Attachment> attachments = issueWithAttachments.getAttachments();
    assertEquals(3, Iterables.size(attachments));
    Pattern pattern = Pattern.compile(".*-(\\d)\\.txt");
    for (Attachment attachment : attachments) {
        assertTrue(pattern.matcher(attachment.getFilename()).matches());
        final Matcher matcher = pattern.matcher(attachment.getFilename());
        matcher.find();
        final int attachmentNum = Integer.parseInt(matcher.group(1));
        assertThat(attachment.getFilename(), equalTo(names[attachmentNum]));
        assertTrue(IOUtils.contentEquals(new ByteArrayInputStream((contents[attachmentNum]).getBytes("UTF-8")),
                issueClient.getAttachment(attachment.getContentUri()).claim()));

    }
}

From source file:it.AsynchronousIssueRestClientTest.java

@Test
// TODO: implement
public void testAddFileAttachments() throws IOException {
    if (!doesJiraSupportAddingAttachment()) {
        return;/*ww  w. java 2s. co  m*/
    }
    final IssueRestClient issueClient = client.getIssueClient();
    final Issue issue = issueClient.getIssue("TST-5").claim();
    assertFalse(issue.getAttachments().iterator().hasNext());

    final File tempFile = File.createTempFile("jim-integration-test", ".txt");
    tempFile.deleteOnExit();
    FileWriter writer = new FileWriter(tempFile);
    writer.write("This is the content of my file which I am going to upload to JIRA for testing.");
    writer.close();
    issueClient.addAttachments(issue.getAttachmentsUri(), tempFile).claim();

    final Issue issueWithAttachments = issueClient.getIssue("TST-5").claim();
    final Iterable<Attachment> attachments = issueWithAttachments.getAttachments();
    assertEquals(1, Iterables.size(attachments));
    assertTrue(IOUtils.contentEquals(new FileInputStream(tempFile),
            issueClient.getAttachment(attachments.iterator().next().getContentUri()).claim()));
}