Skip to content

Apache Lucene open-source search software

Fix bug in test by using a local file instead of HTTP URL. Also fix unclosed file in FileStream.getContentType(), remove dead code.

Uwe Schindler00167f1514b5
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java b/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java
index 7f154845054..05543cdd3d7 100755
--- a/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/ContentStreamBase.java
@@ -29,6 +29,8 @@ import java.net.URL;
import java.net.URLConnection;
import java.util.Locale;
+import org.apache.lucene.util.IOUtils;
+
/**
* Three concrete implementations for ContentStream - one for File/URL/String
*
@@ -76,6 +78,7 @@ public abstract class ContentStreamBase implements ContentStream
sourceInfo = "url";
}
+ @Override
public InputStream getStream() throws IOException {
URLConnection conn = this.url.openConnection();
@@ -102,37 +105,31 @@ public abstract class ContentStreamBase implements ContentStream
sourceInfo = file.toURI().toString();
}
+ @Override
public String getContentType() {
if(contentType==null) {
+ InputStream stream = null;
try {
- char first = (char)new FileInputStream( file ).read();
+ stream = new FileInputStream(file);
+ char first = (char)stream.read();
if(first == '<') {
return "application/xml";
}
if(first == '{') {
return "application/json";
}
+ } catch(Exception ex) {
+ } finally {
+ IOUtils.closeWhileHandlingException(stream);
}
- catch(Exception ex) {}
}
return contentType;
}
+ @Override
public InputStream getStream() throws IOException {
return new FileInputStream( file );
}
-
- /**
- * If an charset is defined (by the contentType) use that, otherwise
- * use a UTF-8 reader
- */
- @Override
- public Reader getReader() throws IOException {
- String charset = getCharsetFromContentType( contentType );
- return charset == null
- ? new InputStreamReader(getStream(), "UTF-8")
- : new InputStreamReader( getStream(), charset );
- }
}
@@ -152,6 +149,7 @@ public abstract class ContentStreamBase implements ContentStream
sourceInfo = "string";
}
+ @Override
public String getContentType() {
if(contentType==null && str.length() > 0) {
char first = str.charAt(0);
@@ -166,6 +164,7 @@ public abstract class ContentStreamBase implements ContentStream
return contentType;
}
+ @Override
public InputStream getStream() throws IOException {
return new ByteArrayInputStream( str.getBytes(DEFAULT_CHARSET) );
}
diff --git a/solr/solrj/src/test/org/apache/solr/common/util/ContentStreamTest.java b/solr/solrj/src/test/org/apache/solr/common/util/ContentStreamTest.java
index 2a7893a9768..65dae081a3f 100755
--- a/solr/solrj/src/test/org/apache/solr/common/util/ContentStreamTest.java
+++ b/solr/solrj/src/test/org/apache/solr/common/util/ContentStreamTest.java
@@ -17,19 +17,16 @@
package org.apache.solr.common.util;
-import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.Reader;
-import java.net.HttpURLConnection;
import java.net.URL;
+
import org.apache.commons.io.IOUtils;
import org.apache.lucene.util.LuceneTestCase;
-import org.apache.solr.common.util.ContentStreamBase;
import org.apache.solr.core.SolrResourceLoader;
/**
@@ -63,56 +60,17 @@ public class ContentStreamTest extends LuceneTestCase
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 {
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- conn.setConnectTimeout(1000);
- conn.setReadTimeout(1000);
- conn.connect();
- int code = conn.getResponseCode();
- assumeTrue("wrong response code from server: " + code, 200 == code);
- 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 );
- in = stream.getStream(); // getStream is needed before getSize is valid
- assertEquals( content.length, stream.getSize().intValue() );
+ 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();
- 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 );
- Reader reader = stream.getReader();
- try {
- String streamContent = IOUtils.toString(reader);
- assertEquals(new String(content, charset), streamContent);
- } finally {
- IOUtils.closeQuietly(reader);
- }
+ ContentStreamBase stream = new ContentStreamBase.URLStream( new URL(file.toURI().toASCIIString()) );
+ assertTrue( IOUtils.contentEquals( new FileInputStream( file ), stream.getStream() ) );
+ assertEquals( file.length(), stream.getSize().intValue() );
+ assertTrue( IOUtils.contentEquals( new InputStreamReader(new FileInputStream(file), "UTF-8"), stream.getReader() ) );
+ assertEquals( file.length(), stream.getSize().intValue() );
}
}