// Path: src/main/java/com/github/gquintana/metrics/util/SqlObjectNameFactory.java
// public class SqlObjectNameFactory implements ObjectNameFactory {
//     private final ObjectNameFactory defaultObjectFactory;
// 
//     public SqlObjectNameFactory(ObjectNameFactory defaultObjectNameFactory) {
//         this.defaultObjectFactory = defaultObjectNameFactory;
//     }
// 
//     public SqlObjectNameFactory() {
//         this.defaultObjectFactory = new DefaultObjectNameFactory();
//     }
//     
//     private static final Pattern PATTERN = Pattern.compile(
//             "(java\\.sql"           // Package
//             + "\\.[A-Z]\\w+)"          // Class
//             + "\\.(\\w+)"              // Database
//             + "(?:\\.\\[([^]]*)\\])?"  // SQL Query
//             + "(?:\\.(\\w+))?");       // Event
//     @Override
//     public ObjectName createName(String type, String domain, String name) {
//         Matcher matcher = PATTERN.matcher(name);
//         ObjectName objectName = null;
//         if (matcher.matches()) {
//             String className = matcher.group(1);
//             String database  = matcher.group(2);
//             String sql       = matcher.group(3);
//             String event     = matcher.group(4);
//             Hashtable<String, String> props = new Hashtable<>();
//             props.put("class", className);
//             props.put("database", database);
//             if (sql!=null) {
//                 // , and \ are not allowed
//                 props.put("sql", sql.replaceAll("[:=*?,\\n\\\\]", " ").replaceAll("[\\s]+", " "));
//             }
//             if (event!=null) {
//                 props.put("event", event);
//             }
//             if (type!=null) {
//                 props.put("metricType", type);
//             }
//             try {
//                 objectName = new ObjectName(domain, props);
//             } catch (MalformedObjectNameException malformedObjectNameException) {
//             }
//         } 
//         if (objectName == null) {
//             objectName = defaultObjectFactory.createName(type, domain, name);
//         }
//         return objectName;
//     }
// 
// }


import java.sql.*;
import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry;
import com.github.gquintana.metrics.util.SqlObjectNameFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import javax.management.MBeanServer;
import javax.sql.DataSource;
import java.lang.management.ManagementFactory;
package com.github.gquintana.metrics.sql;

/*
 * #%L
 * Metrics SQL
 * %%
 * Copyright (C) 2014 Open-Source
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */



/**
 * Test the integration between Metric SQL and the JMX Reporter
 */
public class JmxReportingTest {
    private MBeanServer mBeanServer;
    private MetricRegistry metricRegistry;
    private JdbcProxyFactory proxyFactory;
    private DataSource rawDataSource;
    private DataSource dataSource;
    private JmxReporter jmxReporter;

    @Before
    public void setUp() throws SQLException {
        mBeanServer=ManagementFactory.getPlatformMBeanServer();
        metricRegistry = new MetricRegistry();
        jmxReporter = JmxReporter.forRegistry(metricRegistry)
                .registerWith(mBeanServer)
// Path: src/main/java/com/github/gquintana/metrics/util/SqlObjectNameFactory.java
// public class SqlObjectNameFactory implements ObjectNameFactory {
//     private final ObjectNameFactory defaultObjectFactory;
// 
//     public SqlObjectNameFactory(ObjectNameFactory defaultObjectNameFactory) {
//         this.defaultObjectFactory = defaultObjectNameFactory;
//     }
// 
//     public SqlObjectNameFactory() {
//         this.defaultObjectFactory = new DefaultObjectNameFactory();
//     }
//     
//     private static final Pattern PATTERN = Pattern.compile(
//             "(java\\.sql"           // Package
//             + "\\.[A-Z]\\w+)"          // Class
//             + "\\.(\\w+)"              // Database
//             + "(?:\\.\\[([^]]*)\\])?"  // SQL Query
//             + "(?:\\.(\\w+))?");       // Event
//     @Override
//     public ObjectName createName(String type, String domain, String name) {
//         Matcher matcher = PATTERN.matcher(name);
//         ObjectName objectName = null;
//         if (matcher.matches()) {
//             String className = matcher.group(1);
//             String database  = matcher.group(2);
//             String sql       = matcher.group(3);
//             String event     = matcher.group(4);
//             Hashtable<String, String> props = new Hashtable<>();
//             props.put("class", className);
//             props.put("database", database);
//             if (sql!=null) {
//                 // , and \ are not allowed
//                 props.put("sql", sql.replaceAll("[:=*?,\\n\\\\]", " ").replaceAll("[\\s]+", " "));
//             }
//             if (event!=null) {
//                 props.put("event", event);
//             }
//             if (type!=null) {
//                 props.put("metricType", type);
//             }
//             try {
//                 objectName = new ObjectName(domain, props);
//             } catch (MalformedObjectNameException malformedObjectNameException) {
//             }
//         } 
//         if (objectName == null) {
//             objectName = defaultObjectFactory.createName(type, domain, name);
//         }
//         return objectName;
//     }
// 
// }


// Path: src/test/java/com/github/gquintana/metrics/sql/JmxReportingTest.java
import java.sql.*;
import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry;
import com.github.gquintana.metrics.util.SqlObjectNameFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import javax.management.MBeanServer;
import javax.sql.DataSource;
import java.lang.management.ManagementFactory;

package com.github.gquintana.metrics.sql;

/*
 * #%L
 * Metrics SQL
 * %%
 * Copyright (C) 2014 Open-Source
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */



/**
 * Test the integration between Metric SQL and the JMX Reporter
 */
public class JmxReportingTest {
    private MBeanServer mBeanServer;
    private MetricRegistry metricRegistry;
    private JdbcProxyFactory proxyFactory;
    private DataSource rawDataSource;
    private DataSource dataSource;
    private JmxReporter jmxReporter;

    @Before
    public void setUp() throws SQLException {
        mBeanServer=ManagementFactory.getPlatformMBeanServer();
        metricRegistry = new MetricRegistry();
        jmxReporter = JmxReporter.forRegistry(metricRegistry)
                .registerWith(mBeanServer)
                .createsObjectNamesWith(new SqlObjectNameFactory())