001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.monitoring.jdbc;
018
019 import org.apache.commons.monitoring.Role;
020 import org.apache.commons.monitoring.counters.Counter;
021 import org.apache.commons.monitoring.repositories.Repository;
022 import org.hsqldb.jdbcDriver;
023 import org.junit.BeforeClass;
024 import org.junit.Test;
025
026 import java.lang.reflect.InvocationHandler;
027 import java.lang.reflect.Proxy;
028 import java.sql.Connection;
029 import java.sql.DriverManager;
030 import java.sql.PreparedStatement;
031 import java.sql.Statement;
032
033 import static org.hamcrest.CoreMatchers.instanceOf;
034 import static org.junit.Assert.assertEquals;
035 import static org.junit.Assert.assertNotNull;
036 import static org.junit.Assert.assertThat;
037 import static org.junit.Assert.assertTrue;
038
039 public class HsqlDBTest {
040
041 @BeforeClass
042 public static void init() {
043 MonitoringDriver.load();
044 }
045
046 @Test
047 public void driverMonitoring() throws Exception {
048 final Connection connection = DriverManager.getConnection("jdbc:monitoring:hsqldb:mem:monitoring?delegateDriver=" + jdbcDriver.class.getName(), "SA", "");
049 assertNotNull(connection);
050 assertTrue(Proxy.isProxyClass(connection.getClass()));
051 final InvocationHandler handler = Proxy.getInvocationHandler(connection);
052 assertThat(handler, instanceOf(MonitoredConnection.class));
053
054 final String create = "CREATE TABLE Address (Nr INTEGER, Name VARCHAR(128));";
055 final Statement statement = connection.createStatement();
056 statement.execute(create);
057 assertEquals(1, Repository.INSTANCE.getCounter(new Counter.Key(Role.JDBC, create)).getMaxConcurrency(), 0.);
058
059 final String insert = "INSERT INTO Address (Nr, Name) VALUES(1, 'foo')";
060 final PreparedStatement preparedStatement = connection.prepareStatement(insert);
061 preparedStatement.execute();
062 assertEquals(1, Repository.INSTANCE.getCounter(new Counter.Key(Role.JDBC, insert)).getMaxConcurrency(), 0.);
063 preparedStatement.execute();
064 assertEquals(1, Repository.INSTANCE.getCounter(new Counter.Key(Role.JDBC, insert)).getMaxConcurrency(), 0.);
065 assertEquals(2, Repository.INSTANCE.getCounter(new Counter.Key(Role.JDBC, insert)).getHits(), 0.);
066 }
067 }