diff options
| author | luccioman <luccioman@users.noreply.github.com> | 2017-06-02 12:14:29 +0200 |
|---|---|---|
| committer | luccioman <luccioman@users.noreply.github.com> | 2017-06-02 12:14:29 +0200 |
| commit | d98c04853dd39a452b0b98fb269a2013396cbb42 (patch) | |
| tree | fac4a719ef3524e2c8f4ce3d267e557298638710 /test/java | |
| parent | c53c58fa859842c895468a30fea547e6f8981d5d (diff) | |
Ensure proper closing of file input streams.
Diffstat (limited to 'test/java')
| -rw-r--r-- | test/java/net/yacy/document/ParserTest.java | 243 | ||||
| -rw-r--r-- | test/java/net/yacy/document/parser/xlsParserTest.java | 52 |
2 files changed, 216 insertions, 79 deletions
diff --git a/test/java/net/yacy/document/ParserTest.java b/test/java/net/yacy/document/ParserTest.java index 9d00c9b07..9c2e40879 100644 --- a/test/java/net/yacy/document/ParserTest.java +++ b/test/java/net/yacy/document/ParserTest.java @@ -29,28 +29,51 @@ public class ParserTest { }; for (final String[] testFile : testFiles) { + FileInputStream inStream = null; + final String filename = "test/parsertest/" + testFile[0]; try { - final String filename = "test/parsertest/" + testFile[0]; final File file = new File(filename); final String mimetype = testFile[1]; final AnchorURL url = new AnchorURL("http://localhost/"+filename); AbstractParser p = new ooxmlParser(); - final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, new FileInputStream(file)); + inStream = new FileInputStream(file); + final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, inStream); for (final Document doc: docs) { - final Reader content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); - final StringBuilder str = new StringBuilder(); - int c; - while( (c = content.read()) != -1 ) - str.append((char)c); + Reader content = null; + try { + content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); + final StringBuilder str = new StringBuilder(); + int c; + while( (c = content.read()) != -1 ) + str.append((char)c); - System.out.println("Parsed " + filename + ": " + str); - assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); - assertThat(doc.dc_title(), containsString(testFile[2])); - assertThat(doc.dc_creator(), containsString(testFile[3])); - if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4])); + System.out.println("Parsed " + filename + ": " + str); + assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); + assertThat(doc.dc_title(), containsString(testFile[2])); + assertThat(doc.dc_creator(), containsString(testFile[3])); + if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4])); + } finally { + if(content != null) { + try { + content.close(); + } catch(IOException ioe) { + System.out.println("Could not close text input stream"); + } + } + } } - } catch (final InterruptedException ex) {} + } catch (final InterruptedException ex) { + + } finally { + if(inStream != null) { + try { + inStream.close(); + } catch(IOException ioe) { + System.out.println("Could not close input stream on file " + filename); + } + } + } } } @@ -63,28 +86,50 @@ public class ParserTest { }; for (final String[] testFile : testFiles) { + FileInputStream inStream = null; + final String filename = "test/parsertest/" + testFile[0]; try { - final String filename = "test/parsertest/" + testFile[0]; final File file = new File(filename); final String mimetype = testFile[1]; final AnchorURL url = new AnchorURL("http://localhost/"+filename); AbstractParser p = new odtParser(); - final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, new FileInputStream(file)); + inStream = new FileInputStream(file); + final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, inStream); for (final Document doc: docs) { - final Reader content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); - final StringBuilder str = new StringBuilder(); - int c; - while( (c = content.read()) != -1 ) + Reader content = null; + try { + content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); + final StringBuilder str = new StringBuilder(); + int c; + while( (c = content.read()) != -1 ) str.append((char)c); - System.out.println("Parsed " + filename + ": " + str); - assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); - assertThat(doc.dc_title(), containsString(testFile[2])); - assertThat(doc.dc_creator(), containsString(testFile[3])); - if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4])); + System.out.println("Parsed " + filename + ": " + str); + assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); + assertThat(doc.dc_title(), containsString(testFile[2])); + assertThat(doc.dc_creator(), containsString(testFile[3])); + if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4])); + } finally { + if(content != null) { + try { + content.close(); + } catch(IOException ioe) { + System.out.println("Could not close text input stream"); + } + } + } } - } catch (final InterruptedException ex) {} + } catch (final InterruptedException ex) { + } finally { + if(inStream != null) { + try { + inStream.close(); + } catch(IOException ioe) { + System.out.println("Could not close input stream on file " + filename); + } + } + } } } @@ -95,28 +140,50 @@ public class ParserTest { }; for (final String[] testFile : testFiles) { + final String filename = "test/parsertest/" + testFile[0]; + FileInputStream inStream = null; try { - final String filename = "test/parsertest/" + testFile[0]; final File file = new File(filename); final String mimetype = testFile[1]; final AnchorURL url = new AnchorURL("http://localhost/"+filename); AbstractParser p = new pdfParser(); - final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, new FileInputStream(file)); + inStream = new FileInputStream(file); + final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, inStream); for (final Document doc: docs) { - final Reader content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); - final StringBuilder str = new StringBuilder(); - int c; - while( (c = content.read()) != -1 ) + Reader content = null; + try { + content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); + final StringBuilder str = new StringBuilder(); + int c; + while( (c = content.read()) != -1 ) str.append((char)c); - System.out.println("Parsed " + filename + ": " + str); - assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); - assertThat(doc.dc_title(), containsString(testFile[2])); - assertThat(doc.dc_creator(), containsString(testFile[3])); - if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4])); + System.out.println("Parsed " + filename + ": " + str); + assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); + assertThat(doc.dc_title(), containsString(testFile[2])); + assertThat(doc.dc_creator(), containsString(testFile[3])); + if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4])); + } finally { + if(content != null) { + try { + content.close(); + } catch(IOException ioe) { + System.out.println("Could not close text input stream"); + } + } + } } - } catch (final InterruptedException ex) {} + } catch (final InterruptedException ex) { + } finally { + if(inStream != null) { + try { + inStream.close(); + } catch(IOException ioe) { + System.out.println("Could not close input stream on file " + filename); + } + } + } } } @@ -127,30 +194,52 @@ public class ParserTest { }; for (final String[] testFile : testFiles) { + final String filename = "test/parsertest/" + testFile[0]; + FileInputStream inStream = null; try { - final String filename = "test/parsertest/" + testFile[0]; final File file = new File(filename); final String mimetype = testFile[1]; final AnchorURL url = new AnchorURL("http://localhost/"+filename); AbstractParser p = new docParser(); - final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, new FileInputStream(file)); + inStream = new FileInputStream(file); + final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, inStream); for (final Document doc: docs) { - final Reader content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); - final StringBuilder str = new StringBuilder(); - int c; - while( (c = content.read()) != -1 ) + Reader content = null; + try { + content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); + final StringBuilder str = new StringBuilder(); + int c; + while( (c = content.read()) != -1 ) str.append((char)c); - System.out.println("Parsed " + filename + ": " + str); - assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); - assertThat(doc.dc_title(), containsString(testFile[2])); - assertThat(doc.dc_creator(), containsString(testFile[3])); - if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4])); + System.out.println("Parsed " + filename + ": " + str); + assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); + assertThat(doc.dc_title(), containsString(testFile[2])); + assertThat(doc.dc_creator(), containsString(testFile[3])); + if (testFile[4].length() > 0) assertThat(doc.dc_description()[0], containsString(testFile[4])); + } finally { + if(content != null) { + try { + content.close(); + } catch(IOException ioe) { + System.out.println("Could not close text input stream"); + } + } + } } - } catch (final InterruptedException ex) {} + } catch (final InterruptedException ex) { + } finally { + if(inStream != null) { + try { + inStream.close(); + } catch(IOException ioe) { + System.out.println("Could not close input stream on file " + filename); + } + } } } + } /** * Powerpoint parser test * @@ -172,22 +261,46 @@ public class ParserTest { final AnchorURL url = new AnchorURL("http://localhost/" + filename); AbstractParser p = new pptParser(); - final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, new FileInputStream(file)); - for (final Document doc : docs) { - final Reader content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); - final StringBuilder str = new StringBuilder(); - int c; - while ((c = content.read()) != -1) { - str.append((char) c); - } - - System.out.println("Parsed " + filename + ": " + str); - assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); - assertThat(doc.dc_title(), containsString(testFile[2])); - assertThat(doc.dc_creator(), containsString(testFile[3])); - if (testFile[4].length() > 0) { - assertThat(doc.dc_description()[0], containsString(testFile[4])); - } + FileInputStream inStream = null; + try { + inStream = new FileInputStream(file); + final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, inStream); + for (final Document doc : docs) { + Reader content = null; + try { + content = new InputStreamReader(doc.getTextStream(), doc.getCharset()); + final StringBuilder str = new StringBuilder(); + int c; + while ((c = content.read()) != -1) { + str.append((char) c); + } + + System.out.println("Parsed " + filename + ": " + str); + assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen")); + assertThat(doc.dc_title(), containsString(testFile[2])); + assertThat(doc.dc_creator(), containsString(testFile[3])); + if (testFile[4].length() > 0) { + assertThat(doc.dc_description()[0], containsString(testFile[4])); + } + } finally { + if(content != null) { + try { + content.close(); + } catch(IOException ioe) { + System.out.println("Could not close text input stream"); + } + } + } + + } + } finally { + if(inStream != null) { + try { + inStream.close(); + } catch(IOException ioe) { + System.out.println("Could not close input stream on file " + filename); + } + } } } } diff --git a/test/java/net/yacy/document/parser/xlsParserTest.java b/test/java/net/yacy/document/parser/xlsParserTest.java index eff6b82cb..804776379 100644 --- a/test/java/net/yacy/document/parser/xlsParserTest.java +++ b/test/java/net/yacy/document/parser/xlsParserTest.java @@ -2,6 +2,7 @@ package net.yacy.document.parser; import java.io.File;
import java.io.FileInputStream;
+import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import net.yacy.cora.document.id.AnchorURL;
@@ -35,21 +36,44 @@ public class xlsParserTest { final AnchorURL url = new AnchorURL("http://localhost/" + filename);
AbstractParser p = new xlsParser();
- final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, new FileInputStream(file));
- for (final Document doc : docs) {
- final Reader content = new InputStreamReader(doc.getTextStream(), doc.getCharset());
- final StringBuilder str = new StringBuilder();
- int c;
- while ((c = content.read()) != -1) {
- str.append((char) c);
- }
-
- System.out.println("Parsed " + filename + ": " + str);
- assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen"));
- assertThat(doc.dc_creator(), containsString(testFile[3]));
- if (testFile[4].length() > 0) {
- assertThat(doc.dc_description()[0], containsString(testFile[4]));
+ FileInputStream inStream = null;
+ try {
+ inStream = new FileInputStream(file);
+ final Document[] docs = p.parse(url, mimetype, null, new VocabularyScraper(), 0, inStream);
+ for (final Document doc : docs) {
+ Reader content = null;
+ try {
+ content = new InputStreamReader(doc.getTextStream(), doc.getCharset());
+ final StringBuilder str = new StringBuilder();
+ int c;
+ while ((c = content.read()) != -1) {
+ str.append((char) c);
+ }
+
+ System.out.println("Parsed " + filename + ": " + str);
+ assertThat(str.toString(), containsString("In München steht ein Hofbräuhaus, dort gibt es Bier in Maßkrügen"));
+ assertThat(doc.dc_creator(), containsString(testFile[3]));
+ if (testFile[4].length() > 0) {
+ assertThat(doc.dc_description()[0], containsString(testFile[4]));
+ }
+ } finally {
+ if(content != null) {
+ try {
+ content.close();
+ } catch(IOException ioe) {
+ System.out.println("Could not close text input stream");
+ }
+ }
+ }
}
+ } finally {
+ if(inStream != null) {
+ try {
+ inStream.close();
+ } catch(IOException ioe) {
+ System.out.println("Could not close input stream on file " + filename);
+ }
+ }
}
}
}
|
