Return null if a record cannot be retrieved.
authorJakub Skoczen <jakub@indexdata.dk>
Thu, 15 Apr 2010 09:09:10 +0000 (11:09 +0200)
committerJakub Skoczen <jakub@indexdata.dk>
Thu, 15 Apr 2010 09:09:10 +0000 (11:09 +0200)
Fix bug #3115, add test cases.

src/main/java/org/yaz4j/ResultSet.java
src/test/java/yaz4jtest/ConnectionTest.java [deleted file]
src/test/java/yaz4jtest/DinosaurTest.java [deleted file]
src/test/org/yaz4j/ConnectionTest.java [new file with mode: 0644]
src/test/org/yaz4j/DinosaurTest.java [new file with mode: 0644]

index 3d89bd7..fd55c1b 100644 (file)
@@ -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 (file)
index 9a73b6a..0000000
+++ /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 (file)
index 44c9876..0000000
+++ /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 (file)
index 0000000..826f546
--- /dev/null
@@ -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 (file)
index 0000000..5e1e4c0
--- /dev/null
@@ -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();
+        }
+    }
+}