mil.dod.th.ose.logging/test/mil/dod/th/ose/logging/TestLogWriter.java
@Test
    public void testLogged()
    {
        BundleContext context = mock(BundleContext.class);
        Map<String, Object> props = new HashMap<String, Object>(getComponentProperties());

        // Verify stdout
        when(context.getProperty(LogWriter.LOG_DIR_PROPERTY)).thenReturn(null);
        when(context.getProperty(LogWriter.LOG_STDOUT_PROPERTY)).thenReturn("true");
        when(context.getProperty(LogWriter.LOG_EXCEPTIONS_PROPERTY)).thenReturn("true");
        m_SUT.activate(context, props);
        // This is to mimic the log file created during activation.
        setUpFileNaming();
        
        LogEntry logEntry = mock(LogEntry.class);
        //redirect system out
        PrintStream origOutStream = System.out;
        PrintStream testOutStream = mock(PrintStream.class);
        try
        {
            System.setOut(testOutStream);
            
            when(logEntry.getBundle()).thenReturn(m_Bundle);
            when(logEntry.getLevel()).thenReturn(LogService.LOG_DEBUG);
            when(logEntry.getTime()).thenReturn(1L);
            when(logEntry.getMessage()).thenReturn("blah");

            m_SUT.logged(logEntry);

            verify(testOutStream).println(anyString());
        }
        finally
        { //need to make sure that even if there is a failure that system out is reset
            System.setOut(origOutStream);
        }

        m_SUT.deactivate();

        // Verify file output
        when(context.getProperty(LogWriter.LOG_DIR_PROPERTY)).thenReturn("TempLogDir");
        when(context.getProperty(LogWriter.LOG_STDOUT_PROPERTY)).thenReturn("false");
        when(context.getProperty(LogWriter.LOG_EXCEPTIONS_PROPERTY)).thenReturn("false");

        m_SUT.activate(context, props);
        // This is to mimic the log file created during activation.
        setUpFileNaming();

        long fileSize = m_LogFile.length();
        assertThat(m_LogFile.exists(), is(true));

        m_SUT.logged(logEntry);

        m_SUT.deactivate();

        assertThat(m_LogFile.length(), greaterThan(fileSize));
    }
/**
     * Verify that messages can in fact be logged.
     */
Verify that messages can in fact be logged.
Verify that messages can in fact be logged .
@Test
    public void testLogged()
    {
        BundleContext context = mock(BundleContext.class);
        Map<String, Object> props = new HashMap<String, Object>(getComponentProperties());
        when(context.getProperty(LogWriter.LOG_DIR_PROPERTY)).thenReturn(null);
        when(context.getProperty(LogWriter.LOG_STDOUT_PROPERTY)).thenReturn("true");
        when(context.getProperty(LogWriter.LOG_EXCEPTIONS_PROPERTY)).thenReturn("true");
        m_SUT.activate(context, props);
        setUpFileNaming();
        LogEntry logEntry = mock(LogEntry.class);
        PrintStream origOutStream = System.out;
        PrintStream testOutStream = mock(PrintStream.class);
        try
        {
            System.setOut(testOutStream);
            when(logEntry.getBundle()).thenReturn(m_Bundle);
            when(logEntry.getLevel()).thenReturn(LogService.LOG_DEBUG);
            when(logEntry.getTime()).thenReturn(1L);
            when(logEntry.getMessage()).thenReturn("blah");
            m_SUT.logged(logEntry);
            verify(testOutStream).println(anyString());
        }
        finally
        { 
            System.setOut(origOutStream);
        }
        m_SUT.deactivate();
        when(context.getProperty(LogWriter.LOG_DIR_PROPERTY)).thenReturn("TempLogDir");
        when(context.getProperty(LogWriter.LOG_STDOUT_PROPERTY)).thenReturn("false");
        when(context.getProperty(LogWriter.LOG_EXCEPTIONS_PROPERTY)).thenReturn("false");
        m_SUT.activate(context, props);
        setUpFileNaming();
        long fileSize = m_LogFile.length();
        assertThat(m_LogFile.exists(), is(true));
        m_SUT.logged(logEntry);
        m_SUT.deactivate();
        assertThat(m_LogFile.length(), greaterThan(fileSize));
    }
@ Test public void testLogged ( ) { BundleContext context = mock ( BundleContext . class ) ; Map < String , Object > props = new HashMap < String , Object > ( getComponentProperties ( ) ) ; when ( context . getProperty ( LogWriter . LOG_DIR_PROPERTY ) ) . thenReturn ( null ) ; when ( context . getProperty ( LogWriter . LOG_STDOUT_PROPERTY ) ) . thenReturn ( "true" ) ; when ( context . getProperty ( LogWriter . LOG_EXCEPTIONS_PROPERTY ) ) . thenReturn ( "true" ) ; m_SUT . activate ( context , props ) ; setUpFileNaming ( ) ; LogEntry logEntry = mock ( LogEntry . class ) ; PrintStream origOutStream = System . out ; PrintStream testOutStream = mock ( PrintStream . class ) ; try { System . setOut ( testOutStream ) ; when ( logEntry . getBundle ( ) ) . thenReturn ( m_Bundle ) ; when ( logEntry . getLevel ( ) ) . thenReturn ( LogService . LOG_DEBUG ) ; when ( logEntry . getTime ( ) ) . thenReturn ( 1L ) ; when ( logEntry . getMessage ( ) ) . thenReturn ( "blah" ) ; m_SUT . logged ( logEntry ) ; verify ( testOutStream ) . println ( anyString ( ) ) ; } finally { System . setOut ( origOutStream ) ; } m_SUT . deactivate ( ) ; when ( context . getProperty ( LogWriter . LOG_DIR_PROPERTY ) ) . thenReturn ( "TempLogDir" ) ; when ( context . getProperty ( LogWriter . LOG_STDOUT_PROPERTY ) ) . thenReturn ( "false" ) ; when ( context . getProperty ( LogWriter . LOG_EXCEPTIONS_PROPERTY ) ) . thenReturn ( "false" ) ; m_SUT . activate ( context , props ) ; setUpFileNaming ( ) ; long fileSize = m_LogFile . length ( ) ; assertThat ( m_LogFile . exists ( ) , is ( true ) ) ; m_SUT . logged ( logEntry ) ; m_SUT . deactivate ( ) ; assertThat ( m_LogFile . length ( ) , greaterThan ( fileSize ) ) ; }
Verify that messages can in fact be logged.
Verify that messages can in fact be logged .
// Verify stdout // This is to mimic the log file created during activation. //redirect system out //need to make sure that even if there is a failure that system out is reset // Verify file output // This is to mimic the log file created during activation.