Ejemplo n.º 1
0
  private static void _testConnection(
      String driverClassName, String url, String userName, String password) throws Exception {

    Class.forName(driverClassName);

    Connection connection = null;

    try {
      DataSource dataSource =
          DataSourceFactoryUtil.initDataSource(driverClassName, url, userName, password);

      connection = dataSource.getConnection();
    } finally {
      DataAccess.cleanUp(connection);
    }
  }
  @JVMArgsLine("-Dcatalina.base=. -D" + _HIKARICP_JAR_URL + "=${" + _HIKARICP_JAR_URL + "}")
  @NewEnv(type = NewEnv.Type.JVM)
  @Test
  public void testHikariCP() throws Exception {
    RegistryUtil.setRegistry(new BasicRegistryImpl());

    System.setProperty("portal:jdbc.default.liferay.pool.provider", "hikaricp");

    String hikaricpJarURL = System.getProperty(_HIKARICP_JAR_URL);

    if (hikaricpJarURL != null) {
      System.setProperty(
          "portal:" + PropsKeys.SETUP_LIFERAY_POOL_PROVIDER_JAR_URL + "[hikaricp]", hikaricpJarURL);
    }

    InitUtil.init();

    DataSource dataSource = null;

    try (CaptureAppender captureAppender =
        Log4JLoggerTestUtil.configureLog4JLogger(JarUtil.class.getName(), Level.INFO)) {

      dataSource = DataSourceFactoryUtil.initDataSource(_properties);

      List<LoggingEvent> loggingEvents = captureAppender.getLoggingEvents();

      Assert.assertEquals(4, loggingEvents.size());

      LoggingEvent loggingEvent = loggingEvents.get(0);

      String message = (String) loggingEvent.getMessage();

      Assert.assertTrue(message.startsWith("Downloading "));

      loggingEvent = loggingEvents.get(1);

      message = (String) loggingEvent.getMessage();

      Assert.assertTrue(message.startsWith("Downloaded "));

      loggingEvent = loggingEvents.get(2);

      message = (String) loggingEvent.getMessage();

      Assert.assertTrue(message.startsWith("Installing "));

      loggingEvent = loggingEvents.get(3);

      message = (String) loggingEvent.getMessage();

      Assert.assertTrue(message.startsWith("Installed "));
    }

    Class<?> dataSourceClass = dataSource.getClass();

    Assert.assertEquals("com.zaxxer.hikari.HikariDataSource", dataSourceClass.getName());

    for (int i = 0; i < _CHECKOUT_COUNT; i++) {
      Connection connection = dataSource.getConnection();

      PreparedStatement preparedStatement =
          connection.prepareStatement("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS");

      preparedStatement.execute();
    }

    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();

    ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (TestJDBCPool)");

    int activeConnections = (int) mBeanServer.getAttribute(poolName, "ActiveConnections");

    Assert.assertEquals(_CHECKOUT_COUNT, activeConnections);

    int idleConnections = (int) mBeanServer.getAttribute(poolName, "IdleConnections");

    int totalConnections = (int) mBeanServer.getAttribute(poolName, "TotalConnections");

    Assert.assertEquals(totalConnections, activeConnections + idleConnections);
  }