mil.dod.th.ose.logging/test/mil/dod/th/ose/logging/TestLogWriter.java
@Test
    public void testLogSizeLimitPropertyValue()
    {
        BundleContext context = mock(BundleContext.class);
        Map<String, Object> props = new HashMap<String, Object>(getComponentProperties());
        when(context.getProperty(LogWriter.LOG_DIR_PROPERTY)).thenReturn("TempLogDir");
        when(context.getProperty(LogWriter.LOG_SIZE_PROPERTY)).thenReturn(null);

        // Setting key value pair
        m_SUT.activate(context, props);
        setUpFileNaming();
        
        //redirect print stream
        PrintStream origOutStream = System.out;
        PrintStream testOutStream = mock(PrintStream.class);
        
        try
        {
            System.setOut(testOutStream);

            // mock a log entry
            while (m_LogFile.length() < (1 * 1024 * 1024L))
            {
                LogEntry logEntry = mock(LogEntry.class);
                when(logEntry.getBundle()).thenReturn(m_Bundle);
                when(logEntry.getLevel()).thenReturn(LogService.LOG_DEBUG);
                when(logEntry.getTime()).thenReturn(1L);
                when(logEntry.getMessage()).thenReturn("Rut Row Raggy!");
                m_SUT.logged(logEntry);
            }
            // because a new log file should of been created
            File[] files = m_LogFileDir.listFiles();
            // look for the new log file
            File logFile2 = null;
            for (File file : files)
            {
                if (!file.equals(m_LogFile))
                {
                    logFile2 = file;
                }
            }
            if (logFile2 != null)
            {
                assertThat(logFile2.exists(), is(true));
                // assert that the file is at least the length of the timestamp that starts every new log file.
                assertThat(logFile2.length(), greaterThan(40L));
                // make sure that this isn't the same log file
                assertThat(logFile2.getName(), not(equalTo(m_LogFile.getName())));
            }
            else
            {
                fail("expected file to exist");
            }
            // check that the original file reached the default limit of (about) 1MB
            assertThat(m_LogFile.length(), greaterThan(1 * 1024 * 1024L));
        }
        finally
        {
            //reset print stream
            System.setOut(origOutStream);
        }
        
        m_SUT.deactivate();
    }
/**
     * Test that if config admin is not available that the property file value is used for the log size.
     */
Test that if config admin is not available that the property file value is used for the log size.
Test that if config admin is not available that the property file value is used for the log size .
@Test
    public void testLogSizeLimitPropertyValue()
    {
        BundleContext context = mock(BundleContext.class);
        Map<String, Object> props = new HashMap<String, Object>(getComponentProperties());
        when(context.getProperty(LogWriter.LOG_DIR_PROPERTY)).thenReturn("TempLogDir");
        when(context.getProperty(LogWriter.LOG_SIZE_PROPERTY)).thenReturn(null);
        m_SUT.activate(context, props);
        setUpFileNaming();
        PrintStream origOutStream = System.out;
        PrintStream testOutStream = mock(PrintStream.class);
        try
        {
            System.setOut(testOutStream);
            while (m_LogFile.length() < (1 * 1024 * 1024L))
            {
                LogEntry logEntry = mock(LogEntry.class);
                when(logEntry.getBundle()).thenReturn(m_Bundle);
                when(logEntry.getLevel()).thenReturn(LogService.LOG_DEBUG);
                when(logEntry.getTime()).thenReturn(1L);
                when(logEntry.getMessage()).thenReturn("Rut Row Raggy!");
                m_SUT.logged(logEntry);
            }
            File[] files = m_LogFileDir.listFiles();
            File logFile2 = null;
            for (File file : files)
            {
                if (!file.equals(m_LogFile))
                {
                    logFile2 = file;
                }
            }
            if (logFile2 != null)
            {
                assertThat(logFile2.exists(), is(true));
                assertThat(logFile2.length(), greaterThan(40L));
                assertThat(logFile2.getName(), not(equalTo(m_LogFile.getName())));
            }
            else
            {
                fail("expected file to exist");
            }
            assertThat(m_LogFile.length(), greaterThan(1 * 1024 * 1024L));
        }
        finally
        {
            System.setOut(origOutStream);
        }
        m_SUT.deactivate();
    }
@ Test public void testLogSizeLimitPropertyValue ( ) { BundleContext context = mock ( BundleContext . class ) ; Map < String , Object > props = new HashMap < String , Object > ( getComponentProperties ( ) ) ; when ( context . getProperty ( LogWriter . LOG_DIR_PROPERTY ) ) . thenReturn ( "TempLogDir" ) ; when ( context . getProperty ( LogWriter . LOG_SIZE_PROPERTY ) ) . thenReturn ( null ) ; m_SUT . activate ( context , props ) ; setUpFileNaming ( ) ; PrintStream origOutStream = System . out ; PrintStream testOutStream = mock ( PrintStream . class ) ; try { System . setOut ( testOutStream ) ; while ( m_LogFile . length ( ) < ( 1 * 1024 * 1024L ) ) { LogEntry logEntry = mock ( LogEntry . class ) ; when ( logEntry . getBundle ( ) ) . thenReturn ( m_Bundle ) ; when ( logEntry . getLevel ( ) ) . thenReturn ( LogService . LOG_DEBUG ) ; when ( logEntry . getTime ( ) ) . thenReturn ( 1L ) ; when ( logEntry . getMessage ( ) ) . thenReturn ( "Rut Row Raggy!" ) ; m_SUT . logged ( logEntry ) ; } File [ ] files = m_LogFileDir . listFiles ( ) ; File logFile2 = null ; for ( File file : files ) { if ( ! file . equals ( m_LogFile ) ) { logFile2 = file ; } } if ( logFile2 != null ) { assertThat ( logFile2 . exists ( ) , is ( true ) ) ; assertThat ( logFile2 . length ( ) , greaterThan ( 40L ) ) ; assertThat ( logFile2 . getName ( ) , not ( equalTo ( m_LogFile . getName ( ) ) ) ) ; } else { fail ( "expected file to exist" ) ; } assertThat ( m_LogFile . length ( ) , greaterThan ( 1 * 1024 * 1024L ) ) ; } finally { System . setOut ( origOutStream ) ; } m_SUT . deactivate ( ) ; }
Test that if config admin is not available that the property file value is used for the log size.
Test that if config admin is not available that the property file value is used for the log size .
// Setting key value pair //redirect print stream // mock a log entry // because a new log file should of been created // look for the new log file // assert that the file is at least the length of the timestamp that starts every new log file. // make sure that this isn't the same log file // check that the original file reached the default limit of (about) 1MB //reset print stream