Skip to content

Commit

Permalink
[BACKLOG-5408] - tests for kettle5-log4j-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyBurikhin committed Oct 13, 2015
1 parent 42d65b4 commit 9b0aebf
Show file tree
Hide file tree
Showing 8 changed files with 434 additions and 11 deletions.
4 changes: 3 additions & 1 deletion plugins/kettle5-log4j-plugin/ivy.xml
Expand Up @@ -25,7 +25,9 @@

<dependency org="log4j" name="log4j" rev="1.2.17" conf="default->default" transitive="false"/>

<dependency org="junit" name="junit" rev="4.7" conf="test->default"/>
<!-- test dependencies -->
<dependency org="junit" name="junit" rev="4.11" conf="test->default"/>
<dependency org="org.mockito" name="mockito-all" rev="1.9.5" conf="test->default" transitive="false" />

</dependencies>
</ivy-module>
Expand Up @@ -22,9 +22,6 @@

package org.pentaho.di.core.logging.log4j;

import java.io.FileNotFoundException;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
Expand All @@ -38,9 +35,9 @@
@LoggingPlugin(
id = "Log4jLogging", isSeparateClassLoaderNeeded = true )
public class Log4jLogging implements LoggingPluginInterface {

public static final String PLUGIN_PROPERTIES_FILE = "plugins/kettle5-log4j-plugin/log4j.xml";

public static final String STRING_PENTAHO_DI_LOGGER_NAME = "org.pentaho.di";

public static final String STRING_PENTAHO_DI_CONSOLE_APPENDER = "ConsoleAppender:" + STRING_PENTAHO_DI_LOGGER_NAME;
Expand Down Expand Up @@ -68,20 +65,35 @@ public void eventAdded( KettleLoggingEvent event ) {

@Override
public void init() {
applyLog4jConfiguration();
pentahoLogger = Logger.getLogger( STRING_PENTAHO_DI_LOGGER_NAME );
pentahoLogger = createLogger( STRING_PENTAHO_DI_LOGGER_NAME );
pentahoLogger.setAdditivity( false );
KettleLogStore.getAppender().addLoggingEventListener( this );
}

public void dispose() {
KettleLogStore.getAppender().removeLoggingEventListener( this );
}
private static void applyLog4jConfiguration() {

private void applyLog4jConfiguration() {
LogLog.setQuietMode( true );
LogManager.resetConfiguration();
LogLog.setQuietMode( false );
DOMConfigurator.configure( PLUGIN_PROPERTIES_FILE );
DOMConfigurator.configure( getConfigurationFileName() );
}

/**
* package-local visibility for testing purposes
*
*/
Logger createLogger( String loggerName ) {
applyLog4jConfiguration();
return Logger.getLogger( loggerName );
}

/**
* package-local visibility for testing purposes
*/
String getConfigurationFileName() {
return PLUGIN_PROPERTIES_FILE;
}
}
@@ -0,0 +1,55 @@
/*! ******************************************************************************
*
* 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.core.logging;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;

import org.apache.log4j.Appender;
import org.apache.log4j.spi.Filter;
import org.junit.Ignore;
import org.junit.Test;

@Ignore
public abstract class Log4jAppenderTest {

public abstract Appender getAppender();

@Test
public void addFilter() {
Appender appender = getAppender();
Filter filter = mock( Filter.class );
appender.addFilter( filter );
assertThat( appender.getFilter(), is( filter ) );
}

@Test
public void clearFilters() {
Appender appender = getAppender();
appender.addFilter( mock( Filter.class ) );
appender.clearFilters();
assertNull( appender.getFilter() );
}

}
@@ -0,0 +1,78 @@
/*! ******************************************************************************
*
* 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.core.logging;

import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.vfs2.FileContent;
import org.apache.commons.vfs2.FileObject;
import org.apache.log4j.Appender;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LoggingEvent;
import org.junit.Before;
import org.junit.Test;
import org.pentaho.di.core.Const;

public class Log4jFileAppenderTest extends Log4jAppenderTest {

private Log4jFileAppender log4jFileAppender;

private OutputStream outputStream;

@Before
public void before() throws IOException {
outputStream = mock( OutputStream.class );
FileContent fileContent = mock( FileContent.class );
when( fileContent.getOutputStream( anyBoolean() ) ).thenReturn( outputStream );
FileObject file = mock( FileObject.class );
when( file.getContent() ).thenReturn( fileContent );
log4jFileAppender = new Log4jFileAppender( file );
}

@Test
public void doAppend() throws IOException {
LoggingEvent loggingEvent = mock( LoggingEvent.class );
Layout testLayout = mock( Layout.class );
when( testLayout.format( loggingEvent ) ).thenReturn( "LOG_TEST_LINE" );
log4jFileAppender.setLayout( testLayout );
log4jFileAppender.doAppend( loggingEvent );
verify( outputStream ).write( ( "LOG_TEST_LINE" + Const.CR ).getBytes() );
}

@Test
public void close() throws IOException {
log4jFileAppender.close();
verify( outputStream ).close();
}

@Override
public Appender getAppender() {
return log4jFileAppender;
}

}
@@ -0,0 +1,85 @@
/*! ******************************************************************************
*
* 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.core.logging;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.TimeZone;

import org.apache.log4j.spi.LoggingEvent;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class Log4jKettleLayoutTest {

private TimeZone defaultTimeZone;

@Before
public void before() {
defaultTimeZone = TimeZone.getDefault();
TimeZone.setDefault( TimeZone.getTimeZone( "UTC" ) );
}

@After
public void after() {
TimeZone.setDefault( defaultTimeZone );
}

@Test
public void formatNonLogMessage_without_add_time() {
Log4jKettleLayout layout = new Log4jKettleLayout( false );
String actualResult = layout.format( mock( LoggingEvent.class ) );
assertThat( actualResult, equalTo( "<null>" ) );
}

@Test
public void formatNonLogMessage_with_add_time() {
Log4jKettleLayout layout = new Log4jKettleLayout( true );
String actualResult = layout.format( mock( LoggingEvent.class ) );
assertThat( actualResult, equalTo( "1970/01/01 00:00:00 - <null>" ) );
}

@Test
public void formatLogMessage_without_add_time() {
Log4jKettleLayout layout = new Log4jKettleLayout( false );
LoggingEvent loggingEvent = mock( LoggingEvent.class );
LogMessage logMessage = new LogMessage( "TEST_MESSAGE", "TEST_ID", LogLevel.BASIC );
when( loggingEvent.getMessage() ).thenReturn( logMessage );
String actualResult = layout.format( loggingEvent );
assertThat( actualResult, equalTo( "TEST_MESSAGE" ) );
}

@Test
public void formatLogMessage_with_add_time() {
Log4jKettleLayout layout = new Log4jKettleLayout( true );
LoggingEvent loggingEvent = mock( LoggingEvent.class );
LogMessage logMessage = new LogMessage( "TEST_MESSAGE", "TEST_ID", LogLevel.BASIC );
when( loggingEvent.getMessage() ).thenReturn( logMessage );
String actualResult = layout.format( loggingEvent );
assertThat( actualResult, equalTo( "1970/01/01 00:00:00 - TEST_MESSAGE" ) );
}

}
@@ -0,0 +1,81 @@
/*! ******************************************************************************
*
* 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.core.logging;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.io.PipedOutputStream;

import org.apache.log4j.Appender;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LoggingEvent;
import org.junit.Before;
import org.junit.Test;
import org.pentaho.di.core.Const;

public class Log4jPipedAppenderTest extends Log4jAppenderTest {

private Log4jPipedAppender log4jPipedAppender;

@Before
public void before() {
log4jPipedAppender = new Log4jPipedAppender();
}

@Test
public void setPipedOutputStream() {
PipedOutputStream pipedOutputStream = new PipedOutputStream();
log4jPipedAppender.setPipedOutputStream( pipedOutputStream );
assertThat( log4jPipedAppender.getPipedOutputStream(), is( pipedOutputStream ) );
}

@Test
public void doAppend() throws IOException {
PipedOutputStream pipedOutputStream = mock( PipedOutputStream.class );
LoggingEvent loggingEvent = mock( LoggingEvent.class );
Layout testLayout = mock( Layout.class );
when( testLayout.format( loggingEvent ) ).thenReturn( "LOG_TEST_LINE" );
log4jPipedAppender.setLayout( testLayout );
log4jPipedAppender.setPipedOutputStream( pipedOutputStream );
log4jPipedAppender.doAppend( loggingEvent );
verify( pipedOutputStream ).write( ( "LOG_TEST_LINE" + Const.CR ).getBytes() );
}

@Test
public void close() throws IOException {
PipedOutputStream pipedOutputStream = mock( PipedOutputStream.class );
log4jPipedAppender.setPipedOutputStream( pipedOutputStream );
log4jPipedAppender.close();
verify( pipedOutputStream ).close();
}

@Override
public Appender getAppender() {
return log4jPipedAppender;
}

}

0 comments on commit 9b0aebf

Please sign in to comment.