Skip to content

Commit

Permalink
[TESTS] Unit test written.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Nikolaichuk authored and Ivan Nikolaichuk committed Dec 9, 2015
1 parent 1189012 commit 79e9f3d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
12 changes: 8 additions & 4 deletions engine/src/org/pentaho/di/job/Job.java
Expand Up @@ -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 );
Expand All @@ -1280,6 +1280,10 @@ protected void writeJobEntryLogInformation() throws KettleException {
}
}

protected Database createDataBase( DatabaseMeta databaseMeta ) {
return new Database( this, databaseMeta );
}

/**
* Checks if is active.
*
Expand Down
57 changes: 57 additions & 0 deletions 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 );
}
}

0 comments on commit 79e9f3d

Please sign in to comment.