01: /*-------------------------------------------------------------------------
02: *
03: * Copyright (c) 2004-2005, PostgreSQL Global Development Group
04: *
05: * IDENTIFICATION
06: * $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/NotifyTest.java,v 1.7 2005/11/24 02:31:43 oliver Exp $
07: *
08: *-------------------------------------------------------------------------
09: */
10: package org.postgresql.test.jdbc2;
11:
12: import org.postgresql.test.TestUtil;
13: import junit.framework.TestCase;
14: import java.sql.*;
15:
16: import org.postgresql.PGNotification;
17:
18: public class NotifyTest extends TestCase {
19: private Connection conn;
20:
21: public NotifyTest(String name) {
22: super (name);
23: }
24:
25: protected void setUp() throws Exception {
26: conn = TestUtil.openDB();
27: }
28:
29: protected void tearDown() throws SQLException {
30: TestUtil.closeDB(conn);
31: }
32:
33: public void testNotify() throws SQLException {
34: Statement stmt = conn.createStatement();
35: stmt.executeUpdate("LISTEN mynotification");
36: stmt.executeUpdate("NOTIFY mynotification");
37:
38: PGNotification notifications[] = ((org.postgresql.PGConnection) conn)
39: .getNotifications();
40: assertNotNull(notifications);
41: assertEquals(1, notifications.length);
42: assertEquals("mynotification", notifications[0].getName());
43: assertEquals("", notifications[0].getParameter());
44:
45: stmt.close();
46: }
47:
48: public void testAsyncNotify() throws Exception {
49: Statement stmt = conn.createStatement();
50: stmt.executeUpdate("LISTEN mynotification");
51:
52: // Notify on a separate connection to get an async notify on the first.
53: Connection conn2 = TestUtil.openDB();
54: try {
55: Statement stmt2 = conn2.createStatement();
56: stmt2.executeUpdate("NOTIFY mynotification");
57: stmt2.close();
58: } finally {
59: conn2.close();
60: }
61:
62: // Wait a bit to let the notify come through..
63: try {
64: Thread.sleep(2000);
65: } catch (InterruptedException ie) {
66: }
67:
68: PGNotification notifications[] = ((org.postgresql.PGConnection) conn)
69: .getNotifications();
70: assertNotNull(notifications);
71: assertEquals(1, notifications.length);
72: assertEquals("mynotification", notifications[0].getName());
73: assertEquals("", notifications[0].getParameter());
74:
75: stmt.close();
76: }
77: }
|