1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.monitoring.jdbc;
18
19 import org.apache.commons.monitoring.Role;
20 import org.apache.commons.monitoring.counters.Counter;
21 import org.apache.commons.monitoring.repositories.Repository;
22 import org.hsqldb.jdbcDriver;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25
26 import java.lang.reflect.InvocationHandler;
27 import java.lang.reflect.Proxy;
28 import java.sql.Connection;
29 import java.sql.DriverManager;
30 import java.sql.PreparedStatement;
31 import java.sql.Statement;
32
33 import static org.hamcrest.CoreMatchers.instanceOf;
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertNotNull;
36 import static org.junit.Assert.assertThat;
37 import static org.junit.Assert.assertTrue;
38
39 public class HsqlDBTest {
40
41 @BeforeClass
42 public static void init() {
43 MonitoringDriver.load();
44 }
45
46 @Test
47 public void driverMonitoring() throws Exception {
48 final Connection connection = DriverManager.getConnection("jdbc:monitoring:hsqldb:mem:monitoring?delegateDriver=" + jdbcDriver.class.getName(), "SA", "");
49 assertNotNull(connection);
50 assertTrue(Proxy.isProxyClass(connection.getClass()));
51 final InvocationHandler handler = Proxy.getInvocationHandler(connection);
52 assertThat(handler, instanceOf(MonitoredConnection.class));
53
54 final String create = "CREATE TABLE Address (Nr INTEGER, Name VARCHAR(128));";
55 final Statement statement = connection.createStatement();
56 statement.execute(create);
57 assertEquals(1, Repository.INSTANCE.getCounter(new Counter.Key(Role.JDBC, create)).getMaxConcurrency(), 0.);
58
59 final String insert = "INSERT INTO Address (Nr, Name) VALUES(1, 'foo')";
60 final PreparedStatement preparedStatement = connection.prepareStatement(insert);
61 preparedStatement.execute();
62 assertEquals(1, Repository.INSTANCE.getCounter(new Counter.Key(Role.JDBC, insert)).getMaxConcurrency(), 0.);
63 preparedStatement.execute();
64 assertEquals(1, Repository.INSTANCE.getCounter(new Counter.Key(Role.JDBC, insert)).getMaxConcurrency(), 0.);
65 assertEquals(2, Repository.INSTANCE.getCounter(new Counter.Key(Role.JDBC, insert)).getHits(), 0.);
66 }
67 }