01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.solr.util;
17:
18: import java.io.ByteArrayInputStream;
19: import java.io.File;
20: import java.io.FileInputStream;
21: import java.io.FileReader;
22: import java.io.IOException;
23: import java.io.InputStream;
24: import java.io.Reader;
25: import java.io.StringReader;
26: import java.net.URL;
27:
28: import org.apache.commons.io.IOUtils;
29:
30: import junit.framework.TestCase;
31:
32: /**
33: * @author ryan
34: */
35: public class ContentStreamTest extends TestCase {
36: public void testStringStream() throws IOException {
37: String input = "aads ghaskdgasgldj asl sadg ajdsg &jag # @ hjsakg hsakdg hjkas s";
38: ContentStreamBase stream = new ContentStreamBase.StringStream(
39: input);
40: assertEquals(input.length(), stream.getSize().intValue());
41: assertEquals(input, IOUtils.toString(stream.getStream()));
42: assertEquals(input, IOUtils.toString(stream.getReader()));
43: }
44:
45: public void testFileStream() throws IOException {
46: File file = new File("README");
47: assertTrue(file.exists()); // "make sure you are running from: solr\src\test\test-files"
48:
49: ContentStreamBase stream = new ContentStreamBase.FileStream(
50: file);
51: assertEquals(file.length(), stream.getSize().intValue());
52: assertTrue(IOUtils.contentEquals(new FileInputStream(file),
53: stream.getStream()));
54: assertTrue(IOUtils.contentEquals(new FileReader(file), stream
55: .getReader()));
56: }
57:
58: public void testURLStream() throws IOException {
59: String content = null;
60: URL url = new URL(
61: "http://svn.apache.org/repos/asf/lucene/solr/trunk/");
62: InputStream in = url.openStream();
63: try {
64: content = IOUtils.toString(in);
65: } finally {
66: IOUtils.closeQuietly(in);
67: }
68:
69: assertTrue(content.length() > 10); // found something...
70:
71: ContentStreamBase stream = new ContentStreamBase.URLStream(url);
72: assertEquals(content.length(), stream.getSize().intValue());
73:
74: // Test the stream
75: in = stream.getStream();
76: try {
77: assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(
78: content.getBytes()), in));
79: } finally {
80: IOUtils.closeQuietly(in);
81: }
82:
83: // Re-open the stream and this time use a reader
84: stream = new ContentStreamBase.URLStream(url);
85: assertTrue(IOUtils.contentEquals(new StringReader(content),
86: stream.getReader()));
87: }
88: }
|