View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  }