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