Example #1
0
 public void testStringStream() throws IOException {
   String input = "aads ghaskdgasgldj asl sadg ajdsg &jag # @ hjsakg hsakdg hjkas s";
   ContentStreamBase stream = new ContentStreamBase.StringStream(input);
   assertEquals(input.length(), stream.getSize().intValue());
   assertEquals(input, IOUtils.toString(stream.getStream(), "UTF-8"));
   assertEquals(input, IOUtils.toString(stream.getReader()));
 }
Example #2
0
  public void testFileStream() throws IOException {
    InputStream is = new SolrResourceLoader(null, null).openResource("solrj/README");
    assertNotNull(is);
    File file = new File(TEMP_DIR, "README");
    FileOutputStream os = new FileOutputStream(file);
    IOUtils.copy(is, os);
    os.close();

    ContentStreamBase stream = new ContentStreamBase.FileStream(file);
    assertEquals(file.length(), stream.getSize().intValue());
    assertTrue(IOUtils.contentEquals(new FileInputStream(file), stream.getStream()));
    assertTrue(IOUtils.contentEquals(new FileReader(file), stream.getReader()));
  }
Example #3
0
  public void testURLStream() throws IOException {
    byte[] content = null;
    String contentType = null;
    URL url = new URL("http://svn.apache.org/repos/asf/lucene/dev/trunk/");
    InputStream in = null;
    try {
      URLConnection conn = url.openConnection();
      in = conn.getInputStream();
      contentType = conn.getContentType();
      content = IOUtils.toByteArray(in);

      assumeTrue("not enough content for test to be useful", content.length > 10);

    } catch (IOException ex) {
      assumeNoException("Unable to connect to " + url + " to run the test.", ex);
    } finally {
      if (in != null) {
        IOUtils.closeQuietly(in);
      }
    }

    ContentStreamBase stream = new ContentStreamBase.URLStream(url);
    assertEquals(content.length, stream.getSize().intValue());

    // Test the stream
    in = stream.getStream();
    try {
      assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(content), in));
    } finally {
      IOUtils.closeQuietly(in);
    }

    String charset = ContentStreamBase.getCharsetFromContentType(contentType);
    if (charset == null) charset = ContentStreamBase.DEFAULT_CHARSET;
    // Re-open the stream and this time use a reader
    stream = new ContentStreamBase.URLStream(url);
    assertTrue(
        IOUtils.contentEquals(new StringReader(new String(content, charset)), stream.getReader()));
  }