clean up exception handling: there's no need to manually catch exceptions in test

cases (unless you expect the exception to be thrown)

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@208846 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Daniel Naber 2005-07-02 16:40:44 +00:00
parent e41ced8f49
commit f2ac110482
6 changed files with 32 additions and 67 deletions

View File

@ -40,15 +40,15 @@ public class TestDateTools extends TestCase {
try { try {
d = DateTools.stringToDate("97"); // no date d = DateTools.stringToDate("97"); // no date
fail(); fail();
} catch(ParseException e) { /* expected excpetion */ } } catch(ParseException e) { /* expected exception */ }
try { try {
d = DateTools.stringToDate("200401011235009999"); // no date d = DateTools.stringToDate("200401011235009999"); // no date
fail(); fail();
} catch(ParseException e) { /* expected excpetion */ } } catch(ParseException e) { /* expected exception */ }
try { try {
d = DateTools.stringToDate("aaaa"); // no date d = DateTools.stringToDate("aaaa"); // no date
fail(); fail();
} catch(ParseException e) { /* expected excpetion */ } } catch(ParseException e) { /* expected exception */ }
} }

View File

@ -172,19 +172,8 @@ public class TestDocument extends TestCase
Hits hits = searcher.search(query); Hits hits = searcher.search(query);
assertEquals(1, hits.length()); assertEquals(1, hits.length());
try doAssert(hits.doc(0), true);
{ searcher.close();
doAssert(hits.doc(0), true);
}
catch (Exception e)
{
e.printStackTrace(System.err);
System.err.print("\n");
}
finally
{
searcher.close();
}
} }
private Document makeDocumentWithFields() private Document makeDocumentWithFields()

View File

@ -87,8 +87,9 @@ class DocHelper {
* Writes the document to the directory using a segment named "test" * Writes the document to the directory using a segment named "test"
* @param dir * @param dir
* @param doc * @param doc
* @throws IOException
*/ */
public static void writeDoc(Directory dir, Document doc) public static void writeDoc(Directory dir, Document doc) throws IOException
{ {
writeDoc(dir, "test", doc); writeDoc(dir, "test", doc);
} }
@ -98,8 +99,9 @@ class DocHelper {
* @param dir * @param dir
* @param segment * @param segment
* @param doc * @param doc
* @throws IOException
*/ */
public static void writeDoc(Directory dir, String segment, Document doc) public static void writeDoc(Directory dir, String segment, Document doc) throws IOException
{ {
Analyzer analyzer = new WhitespaceAnalyzer(); Analyzer analyzer = new WhitespaceAnalyzer();
Similarity similarity = Similarity.getDefault(); Similarity similarity = Similarity.getDefault();
@ -112,8 +114,9 @@ class DocHelper {
* @param analyzer * @param analyzer
* @param similarity * @param similarity
* @param doc * @param doc
* @throws IOException
*/ */
public static void writeDoc(Directory dir, Analyzer analyzer, Similarity similarity, Document doc) public static void writeDoc(Directory dir, Analyzer analyzer, Similarity similarity, Document doc) throws IOException
{ {
writeDoc(dir, analyzer, similarity, "test", doc); writeDoc(dir, analyzer, similarity, "test", doc);
} }
@ -125,15 +128,12 @@ class DocHelper {
* @param similarity * @param similarity
* @param segment * @param segment
* @param doc * @param doc
* @throws IOException
*/ */
public static void writeDoc(Directory dir, Analyzer analyzer, Similarity similarity, String segment, Document doc) public static void writeDoc(Directory dir, Analyzer analyzer, Similarity similarity, String segment, Document doc) throws IOException
{ {
DocumentWriter writer = new DocumentWriter(dir, analyzer, similarity, 50); DocumentWriter writer = new DocumentWriter(dir, analyzer, similarity, 50);
try { writer.addDocument(segment, doc);
writer.addDocument(segment, doc);
} catch (IOException e) {
e.printStackTrace();
}
} }
public static int numFields(Document doc) { public static int numFields(Document doc) {

View File

@ -33,7 +33,7 @@ public class TestSegmentTermDocs extends TestCase {
super(s); super(s);
} }
protected void setUp() { protected void setUp() throws IOException {
DocHelper.setupDoc(testDoc); DocHelper.setupDoc(testDoc);
DocHelper.writeDoc(dir, testDoc); DocHelper.writeDoc(dir, testDoc);
} }

View File

@ -497,19 +497,19 @@ public class TestQueryParser extends TestCase {
public void testCustomQueryParserWildcard() { public void testCustomQueryParserWildcard() {
try { try {
new QPTestParser("contents", new WhitespaceAnalyzer()).parse("a?t"); new QPTestParser("contents", new WhitespaceAnalyzer()).parse("a?t");
fail("Wildcard queries should not be allowed");
} catch (ParseException expected) { } catch (ParseException expected) {
return; // expected exception
} }
fail("Wildcard queries should not be allowed");
} }
public void testCustomQueryParserFuzzy() throws Exception { public void testCustomQueryParserFuzzy() throws Exception {
try { try {
new QPTestParser("contents", new WhitespaceAnalyzer()).parse("xunit~"); new QPTestParser("contents", new WhitespaceAnalyzer()).parse("xunit~");
fail("Fuzzy queries should not be allowed");
} catch (ParseException expected) { } catch (ParseException expected) {
return; // expected exception
} }
fail("Fuzzy queries should not be allowed");
} }
public void testBooleanQuery() throws Exception { public void testBooleanQuery() throws Exception {

View File

@ -112,19 +112,11 @@ public class TestMultiSearcher extends TestCase
assertEquals(3, hits.length()); assertEquals(3, hits.length());
try { // iterating over the hit documents
// iterating over the hit documents for (int i = 0; i < hits.length(); i++) {
for (int i = 0; i < hits.length(); i++) { Document d = hits.doc(i);
Document d = hits.doc(i);
}
}
catch (ArrayIndexOutOfBoundsException e)
{
fail("ArrayIndexOutOfBoundsException thrown: " + e.getMessage());
e.printStackTrace();
} finally{
mSearcher.close();
} }
mSearcher.close();
//-------------------------------------------------------------------- //--------------------------------------------------------------------
@ -149,20 +141,12 @@ public class TestMultiSearcher extends TestCase
assertEquals(4, hits2.length()); assertEquals(4, hits2.length());
try { // iterating over the hit documents
// iterating over the hit documents for (int i = 0; i < hits2.length(); i++) {
for (int i = 0; i < hits2.length(); i++) { // no exception should happen at this point
// no exception should happen at this point Document d = hits2.doc(i);
Document d = hits2.doc(i);
}
}
catch (Exception e)
{
fail("Exception thrown: " + e.getMessage());
e.printStackTrace();
} finally{
mSearcher2.close();
} }
mSearcher2.close();
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// scenario 3 // scenario 3
@ -191,18 +175,10 @@ public class TestMultiSearcher extends TestCase
assertEquals(3, hits3.length()); assertEquals(3, hits3.length());
try { // iterating over the hit documents
// iterating over the hit documents for (int i = 0; i < hits3.length(); i++) {
for (int i = 0; i < hits3.length(); i++) { Document d = hits3.doc(i);
Document d = hits3.doc(i);
}
}
catch (IOException e)
{
fail("IOException thrown: " + e.getMessage());
e.printStackTrace();
} finally{
mSearcher3.close();
} }
mSearcher3.close();
} }
} }