mirror of https://github.com/apache/lucene.git
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:
parent
e41ced8f49
commit
f2ac110482
|
@ -40,15 +40,15 @@ public class TestDateTools extends TestCase {
|
|||
try {
|
||||
d = DateTools.stringToDate("97"); // no date
|
||||
fail();
|
||||
} catch(ParseException e) { /* expected excpetion */ }
|
||||
} catch(ParseException e) { /* expected exception */ }
|
||||
try {
|
||||
d = DateTools.stringToDate("200401011235009999"); // no date
|
||||
fail();
|
||||
} catch(ParseException e) { /* expected excpetion */ }
|
||||
} catch(ParseException e) { /* expected exception */ }
|
||||
try {
|
||||
d = DateTools.stringToDate("aaaa"); // no date
|
||||
fail();
|
||||
} catch(ParseException e) { /* expected excpetion */ }
|
||||
} catch(ParseException e) { /* expected exception */ }
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -172,19 +172,8 @@ public class TestDocument extends TestCase
|
|||
Hits hits = searcher.search(query);
|
||||
assertEquals(1, hits.length());
|
||||
|
||||
try
|
||||
{
|
||||
doAssert(hits.doc(0), true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace(System.err);
|
||||
System.err.print("\n");
|
||||
}
|
||||
finally
|
||||
{
|
||||
searcher.close();
|
||||
}
|
||||
doAssert(hits.doc(0), true);
|
||||
searcher.close();
|
||||
}
|
||||
|
||||
private Document makeDocumentWithFields()
|
||||
|
|
|
@ -87,8 +87,9 @@ class DocHelper {
|
|||
* Writes the document to the directory using a segment named "test"
|
||||
* @param dir
|
||||
* @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);
|
||||
}
|
||||
|
@ -98,8 +99,9 @@ class DocHelper {
|
|||
* @param dir
|
||||
* @param segment
|
||||
* @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();
|
||||
Similarity similarity = Similarity.getDefault();
|
||||
|
@ -112,8 +114,9 @@ class DocHelper {
|
|||
* @param analyzer
|
||||
* @param similarity
|
||||
* @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);
|
||||
}
|
||||
|
@ -125,15 +128,12 @@ class DocHelper {
|
|||
* @param similarity
|
||||
* @param segment
|
||||
* @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);
|
||||
try {
|
||||
writer.addDocument(segment, doc);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
writer.addDocument(segment, doc);
|
||||
}
|
||||
|
||||
public static int numFields(Document doc) {
|
||||
|
|
|
@ -33,7 +33,7 @@ public class TestSegmentTermDocs extends TestCase {
|
|||
super(s);
|
||||
}
|
||||
|
||||
protected void setUp() {
|
||||
protected void setUp() throws IOException {
|
||||
DocHelper.setupDoc(testDoc);
|
||||
DocHelper.writeDoc(dir, testDoc);
|
||||
}
|
||||
|
|
|
@ -497,19 +497,19 @@ public class TestQueryParser extends TestCase {
|
|||
public void testCustomQueryParserWildcard() {
|
||||
try {
|
||||
new QPTestParser("contents", new WhitespaceAnalyzer()).parse("a?t");
|
||||
fail("Wildcard queries should not be allowed");
|
||||
} catch (ParseException expected) {
|
||||
return;
|
||||
// expected exception
|
||||
}
|
||||
fail("Wildcard queries should not be allowed");
|
||||
}
|
||||
|
||||
public void testCustomQueryParserFuzzy() throws Exception {
|
||||
try {
|
||||
new QPTestParser("contents", new WhitespaceAnalyzer()).parse("xunit~");
|
||||
fail("Fuzzy queries should not be allowed");
|
||||
} catch (ParseException expected) {
|
||||
return;
|
||||
// expected exception
|
||||
}
|
||||
fail("Fuzzy queries should not be allowed");
|
||||
}
|
||||
|
||||
public void testBooleanQuery() throws Exception {
|
||||
|
|
|
@ -112,19 +112,11 @@ public class TestMultiSearcher extends TestCase
|
|||
|
||||
assertEquals(3, hits.length());
|
||||
|
||||
try {
|
||||
// iterating over the hit documents
|
||||
for (int i = 0; i < hits.length(); i++) {
|
||||
Document d = hits.doc(i);
|
||||
}
|
||||
}
|
||||
catch (ArrayIndexOutOfBoundsException e)
|
||||
{
|
||||
fail("ArrayIndexOutOfBoundsException thrown: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally{
|
||||
mSearcher.close();
|
||||
// iterating over the hit documents
|
||||
for (int i = 0; i < hits.length(); i++) {
|
||||
Document d = hits.doc(i);
|
||||
}
|
||||
mSearcher.close();
|
||||
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
@ -149,20 +141,12 @@ public class TestMultiSearcher extends TestCase
|
|||
|
||||
assertEquals(4, hits2.length());
|
||||
|
||||
try {
|
||||
// iterating over the hit documents
|
||||
for (int i = 0; i < hits2.length(); i++) {
|
||||
// no exception should happen at this point
|
||||
Document d = hits2.doc(i);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
fail("Exception thrown: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally{
|
||||
mSearcher2.close();
|
||||
// iterating over the hit documents
|
||||
for (int i = 0; i < hits2.length(); i++) {
|
||||
// no exception should happen at this point
|
||||
Document d = hits2.doc(i);
|
||||
}
|
||||
mSearcher2.close();
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// scenario 3
|
||||
|
@ -191,18 +175,10 @@ public class TestMultiSearcher extends TestCase
|
|||
|
||||
assertEquals(3, hits3.length());
|
||||
|
||||
try {
|
||||
// iterating over the hit documents
|
||||
for (int i = 0; i < hits3.length(); i++) {
|
||||
Document d = hits3.doc(i);
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
fail("IOException thrown: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally{
|
||||
mSearcher3.close();
|
||||
// iterating over the hit documents
|
||||
for (int i = 0; i < hits3.length(); i++) {
|
||||
Document d = hits3.doc(i);
|
||||
}
|
||||
mSearcher3.close();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue