2 * Copyright (c) 1995-2015, Index Data
4 * See the file LICENSE for details.
8 import org.junit.After;
9 import org.junit.AfterClass;
10 import org.junit.Before;
11 import org.junit.BeforeClass;
12 import org.junit.Test;
13 import static org.junit.Assert.*;
14 import org.yaz4j.exception.ZoomException;
16 import static java.lang.System.out;
22 public class AsyncConnectionsTest {
38 void setItem(T item) {
43 public AsyncConnectionsTest() {
47 public static void setUpClass() {
51 public static void tearDownClass() {
59 public void tearDown() {
63 * Test async ZOOM operation.
66 public void testSingleTarget() {
67 out.println("Trying async connection...");
68 AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:210/gils", 0);
69 AsyncConnections conns = new AsyncConnections();
71 int expectedHitCount = 9;
72 final Box<Long> actualHitCount = new Box<Long>();
73 final Box<Integer> actualRecordCounter = new Box<Integer>(0);
75 conn.setSyntax("sutrs");
77 conn.search(new PrefixQuery("@attr 1=4 utah"));
79 .onSearch(new AsyncConnection.SearchHandler() {
80 public void handle(ResultSet rs) {
81 out.println("Received search, hit count "+rs.getHitCount());
82 actualHitCount.setItem(rs.getHitCount());
85 .onRecord(new AsyncConnection.RecordHandler() {
86 public void handle(Record r) {
87 out.println("Received a record of type "+r.getSyntax());
88 actualRecordCounter.setItem(actualRecordCounter.getItem()+1);
92 } catch (ZoomException ex) {
93 fail(ex.getMessage());
96 assertEquals(expectedHitCount, actualHitCount.item);
97 assertEquals(expectedHitCount, actualRecordCounter.item);
103 * Test async ZOOM operation.
106 public void testMulitTarget() {
107 out.println("Trying async with multile connections...");
108 AsyncConnections conns = new AsyncConnections();
109 AsyncConnection conn = new AsyncConnection("z3950.indexdata.dk:210/gils", 0);
111 AsyncConnection conn2 = new AsyncConnection("z3950.indexdata.dk:210/marc", 0);
113 int expectedHitCount = 19; //for both
114 final Box<Long> actualHitCount = new Box<Long>(0L);
115 final Box<Integer> actualRecordCounter = new Box<Integer>(0);
117 //we need to simplify the API for multiple
118 conn.setSyntax("sutrs");
120 conn.search(new PrefixQuery("@attr 1=4 utah"));
122 .onSearch(new AsyncConnection.SearchHandler() {
123 public void handle(ResultSet rs) {
124 out.println("Received search, hit count "+rs.getHitCount());
125 actualHitCount.setItem(actualHitCount.getItem() + rs.getHitCount());
128 .onRecord(new AsyncConnection.RecordHandler() {
129 public void handle(Record r) {
130 out.println("Received a record of type "+r.getSyntax());
131 actualRecordCounter.setItem(actualRecordCounter.getItem()+1);
134 conn2.setSyntax("marc21");
136 conn2.search(new PrefixQuery("@attr 1=4 computer"));
138 .onSearch(new AsyncConnection.SearchHandler() {
139 public void handle(ResultSet rs) {
140 out.println("Received search, hit count "+rs.getHitCount());
141 actualHitCount.setItem(actualHitCount.getItem() + rs.getHitCount());
144 .onRecord(new AsyncConnection.RecordHandler() {
145 public void handle(Record r) {
146 out.println("Received a record of type "+r.getSyntax());
147 actualRecordCounter.setItem(actualRecordCounter.getItem()+1);
150 .onError(new AsyncConnection.ErrorHandler() {
152 public void handle(ZoomException e) {
153 out.println("Caught error: "+e.getMessage());
157 } catch (ZoomException ex) {
158 fail(ex.getMessage());
161 assertEquals(expectedHitCount, actualHitCount.item);
162 assertEquals(expectedHitCount, actualRecordCounter.item);