diff --git a/engine/src/org/pentaho/di/job/Job.java b/engine/src/org/pentaho/di/job/Job.java index b37ad535c281..0016757f75a0 100644 --- a/engine/src/org/pentaho/di/job/Job.java +++ b/engine/src/org/pentaho/di/job/Job.java @@ -1257,21 +1257,21 @@ protected void writeLogChannelInformation() throws KettleException { */ protected void writeJobEntryLogInformation() throws KettleException { Database db = null; - JobEntryLogTable jobEntryLogTable = jobMeta.getJobEntryLogTable(); + JobEntryLogTable jobEntryLogTable = getJobMeta().getJobEntryLogTable(); try { - db = new Database( this, jobEntryLogTable.getDatabaseMeta() ); + db = createDataBase( jobEntryLogTable.getDatabaseMeta() ); db.shareVariablesWith( this ); db.connect(); db.setCommit( logCommitSize ); - for ( JobEntryCopy copy : jobMeta.getJobCopies() ) { + for ( JobEntryCopy copy : getJobMeta().getJobCopies() ) { db.writeLogRecord( jobEntryLogTable, LogStatus.START, copy, this ); } db.cleanupLogRecords( jobEntryLogTable ); } catch ( Exception e ) { throw new KettleException( BaseMessages.getString( PKG, "Job.Exception.UnableToJobEntryInformationToLogTable" ), - e ); + e ); } finally { if ( !db.isAutoCommit() ) { db.commitLog( true, jobEntryLogTable ); @@ -1280,6 +1280,10 @@ protected void writeJobEntryLogInformation() throws KettleException { } } + protected Database createDataBase( DatabaseMeta databaseMeta ) { + return new Database( this, databaseMeta ); + } + /** * Checks if is active. * diff --git a/engine/test-src/org/pentaho/di/job/JobTest.java b/engine/test-src/org/pentaho/di/job/JobTest.java new file mode 100644 index 000000000000..fa54187bd4d9 --- /dev/null +++ b/engine/test-src/org/pentaho/di/job/JobTest.java @@ -0,0 +1,57 @@ +/*! ****************************************************************************** + * + * Pentaho Data Integration + * + * Copyright (C) 2002-2015 by Pentaho : http://www.pentaho.com + * + ******************************************************************************* + * + * 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. + * + ******************************************************************************/ + +package org.pentaho.di.job; + +import org.junit.Test; +import org.pentaho.di.core.database.Database; +import org.pentaho.di.core.database.DatabaseMeta; +import org.pentaho.di.core.exception.KettleException; +import org.pentaho.di.core.logging.JobEntryLogTable; +import org.pentaho.di.core.variables.VariableSpace; +import org.pentaho.di.trans.HasDatabasesInterface; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +public class JobTest { + + @Test + public void recordsCleanUpMethodIsCalled() throws KettleException { + Database mockedDataBase = mock( Database.class ); + Job job = mock( Job.class ); + + JobEntryLogTable jobEntryLogTable = JobEntryLogTable.getDefault( mock( VariableSpace.class ), mock( HasDatabasesInterface.class ) ); + jobEntryLogTable.setConnectionName( "connection" ); + + JobMeta jobMeta = new JobMeta( ); + jobMeta.setJobEntryLogTable( jobEntryLogTable ); + + when( job.createDataBase( any( DatabaseMeta.class ) ) ).thenReturn( mockedDataBase ); + when( job.getJobMeta() ).thenReturn( jobMeta ); + doCallRealMethod().when( job ).writeJobEntryLogInformation(); + + job.writeJobEntryLogInformation(); + + verify( mockedDataBase ).cleanupLogRecords( jobEntryLogTable ); + } +}