From: Jakub Skoczen Date: Thu, 15 Apr 2010 09:09:10 +0000 (+0200) Subject: Return null if a record cannot be retrieved. X-Git-Tag: v1.2~5 X-Git-Url: http://sru.miketaylor.org.uk/cgi-bin?a=commitdiff_plain;h=392b45440ddab42427a0a6e5f24d1a5e8eb28375;p=yaz4j-moved-to-github.git Return null if a record cannot be retrieved. Fix bug #3115, add test cases. --- diff --git a/src/main/java/org/yaz4j/ResultSet.java b/src/main/java/org/yaz4j/ResultSet.java index 3d89bd7..fd55c1b 100644 --- a/src/main/java/org/yaz4j/ResultSet.java +++ b/src/main/java/org/yaz4j/ResultSet.java @@ -1,5 +1,6 @@ package org.yaz4j; +import org.yaz4j.exception.ZoomException; import org.yaz4j.jni.SWIGTYPE_p_ZOOM_record_p; import org.yaz4j.jni.SWIGTYPE_p_ZOOM_resultset_p; import org.yaz4j.jni.yaz4jlib; @@ -42,9 +43,15 @@ public class ResultSet { return this; } - public Record getRecord(int index) { + public Record getRecord(int index) throws ZoomException { SWIGTYPE_p_ZOOM_record_p record = yaz4jlib.ZOOM_resultset_record(resultSet, index); + //may be out of range or unsupported syntax + if (record == null) { + return null; + } + int errorCode = yaz4jlib.ZOOM_record_error(record, null, null, null); + if (errorCode != 0) throw new ZoomException("Record excpetion, code " + errorCode); return new Record(record, this); } diff --git a/src/test/java/yaz4jtest/ConnectionTest.java b/src/test/java/yaz4jtest/ConnectionTest.java deleted file mode 100644 index 9a73b6a..0000000 --- a/src/test/java/yaz4jtest/ConnectionTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package yaz4jtest; - -import org.junit.*; -import static org.junit.Assert.*; -import org.yaz4j.Connection.QueryType; -import org.yaz4j.exception.ZoomException; - -public class ConnectionTest { - - @Test - public void testConnection() { - org.yaz4j.Connection con = new org.yaz4j.Connection("z3950.indexdata.dk:210/gils", 0); - assertNotNull(con); - try { - con.setSyntax("sutrs"); - System.out.println("Open connection to z3950.indexdata.dk:210/gils..."); - con.connect(); - org.yaz4j.ResultSet s = con.search("@attr 1=4 utah", QueryType.PrefixQuery); - assertNotNull(s); - assertEquals(s.getSize(), 9); - org.yaz4j.Record rec = s.getRecord(0); - assertNotNull(rec); - byte[] content = rec.getContent(); - // first SUTRS record - assertEquals(content.length, 1940); - assertEquals(content[0], 103); - assertEquals(rec.getSyntax(), "SUTRS"); - assertEquals(rec.getDatabase(), "gils"); - } catch (ZoomException ze) { - fail(ze.getMessage()); - } finally { - con.close(); - } - } -} diff --git a/src/test/java/yaz4jtest/DinosaurTest.java b/src/test/java/yaz4jtest/DinosaurTest.java deleted file mode 100644 index 44c9876..0000000 --- a/src/test/java/yaz4jtest/DinosaurTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package yaz4jtest; - -import org.junit.*; -import static org.junit.Assert.*; -import org.yaz4j.*; -import org.yaz4j.exception.ZoomException; - -/** - * @author adam - */ -public class DinosaurTest { - - @Test - public void test() { - Connection con = new Connection("z3950.loc.gov:7090/voyager", 0); - try { - assertNotNull(con); - con.setSyntax("usmarc"); - con.connect(); - ResultSet set = con.search("@attr 1=7 0253333490", Connection.QueryType.PrefixQuery); - assertNotNull(set); - Record rec = set.getRecord(0); - assertNotNull(rec); - } catch (ZoomException ze) { - fail(ze.getMessage()); - } finally { - con.close(); - } - } -} diff --git a/src/test/org/yaz4j/ConnectionTest.java b/src/test/org/yaz4j/ConnectionTest.java new file mode 100644 index 0000000..826f546 --- /dev/null +++ b/src/test/org/yaz4j/ConnectionTest.java @@ -0,0 +1,60 @@ +package org.yaz4j; + +import org.junit.*; +import static org.junit.Assert.*; +import org.yaz4j.exception.*; + +public class ConnectionTest { + + @Test + public void testConnection() { + Connection con = new Connection("z3950.indexdata.dk:210/gils", 0); + assertNotNull(con); + try { + con.setSyntax("sutrs"); + System.out.println("Open connection to z3950.indexdata.dk:210/gils..."); + con.connect(); + ResultSet s = con.search("@attr 1=4 utah", Connection.QueryType.PrefixQuery); + System.out.println("Search for 'utah'..."); + assertNotNull(s); + assertEquals(s.getSize(), 9); + Record rec = s.getRecord(0); + assertNotNull(rec); + byte[] content = rec.getContent(); + // first SUTRS record + assertEquals(content.length, 1940); + assertEquals(content[0], 103); + assertEquals(rec.getSyntax(), "SUTRS"); + assertEquals(rec.getDatabase(), "gils"); + } catch (ZoomException ze) { + fail(ze.getMessage()); + } finally { + con.close(); + } + } + + @Test + public void unsupportedSyntax() { + System.out.println("Open connection to z3950.loc.gov:7090/voyager..."); + Connection con = new Connection("z3950.loc.gov:7090/voyager", 0); + try { + System.out.println("Set syntax to 'rusmarc'"); + con.setSyntax("rusmarc"); + con.connect(); + System.out.println("Search for something that exists..."); + ResultSet set = con.search("@attr 1=7 0253333490", Connection.QueryType.PrefixQuery); + System.out.println("Result set size: " + set.getSize()); + System.out.println("Get the first record..."); + Record rec = set.getRecord(0); + if (rec == null) { + System.out.println("Record is null"); + } else { + System.out.print(rec.render()); + } + } catch (ZoomException ze) { + //fail(ze.getMessage()); + } finally { + con.close(); + } + } +} diff --git a/src/test/org/yaz4j/DinosaurTest.java b/src/test/org/yaz4j/DinosaurTest.java new file mode 100644 index 0000000..5e1e4c0 --- /dev/null +++ b/src/test/org/yaz4j/DinosaurTest.java @@ -0,0 +1,29 @@ +package org.yaz4j; + +import org.junit.*; +import static org.junit.Assert.*; +import org.yaz4j.exception.ZoomException; + +/** + * @author adam + */ +public class DinosaurTest { + + @Test + public void test() { + Connection con = new Connection("z3950.loc.gov:7090/voyager", 0); + try { + assertNotNull(con); + con.setSyntax("usmarc"); + con.connect(); + ResultSet set = con.search("@attr 1=7 0253333490", Connection.QueryType.PrefixQuery); + assertNotNull(set); + Record rec = set.getRecord(0); + assertNotNull(rec); + } catch (ZoomException ze) { + fail(ze.getMessage()); + } finally { + con.close(); + } + } +}