/*! ******************************************************************************
 *
 * Pentaho Data Integration
 *
 * Copyright (C) 2002-2016 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.entries.checkdbconnection;

import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.Result;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.LogLevel;
import org.pentaho.di.core.util.Assert;
import org.pentaho.di.job.Job;

public class JobEntryCheckDbConnectionsTest {

  @BeforeClass
  public static void setUpBeforeClass() throws KettleException {
    KettleEnvironment.init( false );
  }


  /**
   * Test whether a Millisecond-level timeout actually waits for N milliseconds, instead of N seconds
   */
  @Test( timeout = 10000 )
  public void testMillisecondWait() {
    int waitMilliseconds = 15;
    Job mockedJob = Mockito.mock( Job.class );
    Mockito.when( mockedJob.isStopped() ).thenReturn( false );

    JobEntryCheckDbConnections meta = new JobEntryCheckDbConnections();
    meta.setParentJob( mockedJob );
    meta.setLogLevel( LogLevel.BASIC );

    DatabaseMeta db = new DatabaseMeta( "InMemory H2", "H2", null, null, "myDb", "-1", null, null );
    meta.setConnections( new DatabaseMeta[]{ db } );
    meta.setWaittimes( new int[]{ JobEntryCheckDbConnections.UNIT_TIME_MILLI_SECOND } );
    meta.setWaitfors( new String[]{ String.valueOf( waitMilliseconds ) } );
    Result result = meta.execute( new Result(), 0 );
    Assert.assertTrue( result.getResult() );
  }
}

@@ -953,16 +953,135 @@
 ction;%0A%0A
+import org.apache.commons.vfs2.FileObject;%0Aimport org.apache.commons.vfs2.FileSystemException;%0Aimport org.junit.After;%0A
 import o
@@ -1102,16 +1102,16 @@
 eClass;%0A
-
 import o
@@ -1330,32 +1330,90 @@
 ettleException;%0A
+import org.pentaho.di.core.exception.KettleFileException;%0A
 import org.penta
@@ -1481,16 +1481,58 @@
 Assert;%0A
+import org.pentaho.di.core.vfs.KettleVFS;%0A
 import o
@@ -1602,16 +1602,69 @@
 Test %7B%0A%0A
+  private static final String H2_DATABASE = %22myDb%22;%0A%0A
   @Befor
@@ -1777,16 +1777,365 @@
 );%0A  %7D%0A%0A
+  @After%0A  public void cleanup() %7B%0A    try %7B%0A      FileObject dbFile = KettleVFS.getFileObject( H2_DATABASE + %22.h2.db%22 );%0A      if ( dbFile.exists() ) %7B%0A        System.out.println( %22deleting file%22 );%0A        dbFile.delete();%0A      %7D%0A    %7D catch ( KettleFileException %7C FileSystemException ignored ) %7B%0A      // Ignore, we tried cleaning up%0A    %7D%0A  %7D%0A
 %0A  /**%0A 
@@ -2670,22 +2670,27 @@
 , null, 
-%22myDb%22
+H2_DATABASE
 , %22-1%22, 

Delete Temp File on Exit