From f699eaee4f2921800d8054d97373d348fd43dd75 Mon Sep 17 00:00:00 2001 From: Cheng Fang Date: Fri, 6 May 2016 15:58:31 -0400 Subject: [PATCH] Issue #72 add test withSqlFile to wildfly-jberet-samples/purgeJdbcRepository2. --- .../org/jberet/repository/JdbcRepository.java | 16 +++++++++------- .../src/main/resources/purgeJdbcRepository2.sql | 5 ++--- .../PurgeJdbcRepository2IT.java | 12 ++++++++++++ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/jberet-core/src/main/java/org/jberet/repository/JdbcRepository.java b/jberet-core/src/main/java/org/jberet/repository/JdbcRepository.java index 91f9a6bec..268cea05b 100644 --- a/jberet-core/src/main/java/org/jberet/repository/JdbcRepository.java +++ b/jberet-core/src/main/java/org/jberet/repository/JdbcRepository.java @@ -185,7 +185,7 @@ public JdbcRepository(final DataSource dataSource, final Properties configProper this.dataSource = dataSource; dbUrl = null; String sqlFile = DEFAULT_SQL_FILE; - final InputStream sqlResource = getClassLoader().getResourceAsStream(sqlFile); + final InputStream sqlResource = getClassLoader(false).getResourceAsStream(sqlFile); try { if (sqlResource == null) { throw BatchMessages.MESSAGES.failToLoadSqlProperties(null, sqlFile); @@ -213,7 +213,7 @@ private void createTables(final Properties configProperties) { if (sqlFile == null || sqlFile.isEmpty()) { sqlFile = DEFAULT_SQL_FILE; } - final InputStream sqlResource = getClassLoader().getResourceAsStream(sqlFile); + final InputStream sqlResource = getClassLoader(false).getResourceAsStream(sqlFile); try { if (sqlResource == null) { throw BatchMessages.MESSAGES.failToLoadSqlProperties(null, sqlFile); @@ -258,7 +258,7 @@ private void createTables(final Properties configProperties) { rs = countPartitionExecutionStatement.executeQuery(); } catch (final SQLException e) { final String ddlFile = getDDLLocation(databaseProductName); - ddlResource = getClassLoader().getResourceAsStream(ddlFile); + ddlResource = getClassLoader(false).getResourceAsStream(ddlFile); if (ddlResource == null) { throw BatchMessages.MESSAGES.failToLoadDDL(ddlFile); } @@ -921,7 +921,7 @@ public void executeStatements(final String statements, final String statementsRe if (statements == null) { InputStream statementsResource = null; try { - statementsResource = getClassLoader().getResourceAsStream(statementsResourcePath); + statementsResource = getClassLoader(true).getResourceAsStream(statementsResourcePath); if (statementsResource != null) { statementList = new ArrayList(); } else { @@ -1054,16 +1054,18 @@ private String getDDLLocation(final String databaseProductName) { return ddlFile; } - private static ClassLoader getClassLoader() { + private static ClassLoader getClassLoader(final boolean isContextClassLoader) { if (WildFlySecurityManager.isChecking()) { return AccessController.doPrivileged(new PrivilegedAction() { @Override public ClassLoader run() { - return JdbcRepository.class.getClassLoader(); + return isContextClassLoader ? Thread.currentThread().getContextClassLoader() : + JdbcRepository.class.getClassLoader(); } }); } - return JdbcRepository.class.getClassLoader(); + return isContextClassLoader ? Thread.currentThread().getContextClassLoader() : + JdbcRepository.class.getClassLoader(); } private static Timestamp createTimestamp(final Date date) { diff --git a/wildfly-jberet-samples/purgeJdbcRepository2/src/main/resources/purgeJdbcRepository2.sql b/wildfly-jberet-samples/purgeJdbcRepository2/src/main/resources/purgeJdbcRepository2.sql index fe7010c3c..30e8c5a00 100644 --- a/wildfly-jberet-samples/purgeJdbcRepository2/src/main/resources/purgeJdbcRepository2.sql +++ b/wildfly-jberet-samples/purgeJdbcRepository2/src/main/resources/purgeJdbcRepository2.sql @@ -3,6 +3,5 @@ delete from STEP_EXECUTION where JOBEXECUTIONID in where JOB_EXECUTION.JOBINSTANCEID = JOB_INSTANCE.JOBINSTANCEID and JOB_INSTANCE.JOBNAME = 'prepurge2'); -delete from JOB_EXECUTION where JOBEXECUTIONID in - (select JOBEXECUTIONID from JOB_EXECUTION, JOB_INSTANCE - where JOB_EXECUTION.JOBINSTANCEID = JOB_INSTANCE.JOBINSTANCEID and JOB_INSTANCE.JOBNAME = 'prepurge2') +delete from JOB_EXECUTION where JOBINSTANCEID in + (select DISTINCT JOBINSTANCEID from JOB_INSTANCE where JOBNAME = 'prepurge2') diff --git a/wildfly-jberet-samples/purgeJdbcRepository2/src/test/java/org/jberet/samples/wildfly/purgeJdbcRepository2/PurgeJdbcRepository2IT.java b/wildfly-jberet-samples/purgeJdbcRepository2/src/test/java/org/jberet/samples/wildfly/purgeJdbcRepository2/PurgeJdbcRepository2IT.java index b1ce81469..58b061dfe 100644 --- a/wildfly-jberet-samples/purgeJdbcRepository2/src/test/java/org/jberet/samples/wildfly/purgeJdbcRepository2/PurgeJdbcRepository2IT.java +++ b/wildfly-jberet-samples/purgeJdbcRepository2/src/test/java/org/jberet/samples/wildfly/purgeJdbcRepository2/PurgeJdbcRepository2IT.java @@ -79,6 +79,18 @@ public void withSql() throws Exception { // assertEquals(null, prepurgeJobExecution); } + @Test + public void withSqlFile() throws Exception { + final long prepurgeExecutionId = prepurge(); + JobExecutionEntity prepurgeJobExecution = batchClient.getJobExecution(prepurgeExecutionId); + assertEquals(BatchStatus.COMPLETED, prepurgeJobExecution.getBatchStatus()); + + final String sqlFile = "purgeJdbcRepository2.sql"; + final Properties jobParams = new Properties(); + jobParams.setProperty("sqlFile", sqlFile); + startJobCheckStatus(jobName, jobParams, 3000, BatchStatus.COMPLETED); + } + private long prepurge() throws Exception { final JobExecutionEntity jobExecutionEntity = batchClient.startJob(prepurge2JobName, null); Thread.sleep(1000);