Search in sources :

Example 1 with LogRecordCollectingLogger

use of alma.acs.testsupport.LogRecordCollectingLogger in project ACS by ACS-Community.

the class CorbaNullFinderTest method testNullFinderInContainerSealant.

/**
	 * Strictly speaking this is a test for {@link ContainerSealant}, but practically fits well in here.
	 */
public void testNullFinderInContainerSealant() throws Exception {
    // activate the null checker
    System.setProperty(ContainerSealant.CHECK_NULLS_CORBA_OUT_PROPERTYNAME, "true");
    // The test component, with the real interceptor around it
    LogRecordCollectingLogger collectingLogger = LogRecordCollectingLogger.getCollectingLogger("Collecting_" + getName());
    ComponentWithBadNullsImpl compImpl = new ComponentWithBadNullsImpl();
    ComponentWithBadNullsOperations sealedComponent = ContainerSealant.createContainerSealant(ComponentWithBadNullsOperations.class, compImpl, "InstanceForUnitTest", false, collectingLogger, null, null);
    // invocation of methodWithReturnData
    String instring = null;
    Struct1 instruct1 = ComponentWithBadNullsImpl.createGoodStruct1();
    instruct1.myenum1 = null;
    instruct1.mystring = null;
    StringHolder inoutstringHolder = new StringHolder();
    Struct1Holder inoutstruct1Holder = new Struct1Holder();
    StringHolder outstringHolder = new StringHolder();
    Struct1Holder outstruct1Holder = new Struct1Holder();
    sealedComponent.methodWithReturnData(instring, instruct1, inoutstringHolder, inoutstruct1Holder, outstringHolder, outstruct1Holder);
    // check the logs produced by the ContainerSealant who uses CorbaNullFinder
    LogRecord[] logRecords = collectingLogger.getCollectedLogRecords();
    assertEquals(3, logRecords.length);
    assertEquals("intercepted a call to 'InstanceForUnitTest#methodWithReturnData'.", logRecords[0].getMessage());
    assertTrue(logRecords[1].getMessage().startsWith("returning from InstanceForUnitTest#methodWithReturnData after"));
    System.out.println(logRecords[2].getMessage());
    assertEquals("Illegal null value in out parameter(s) of method methodWithReturnData:\n" + "  Parameter StringHolder: \n" + "    Null string in field StringHolder/value\n" + "  Parameter Struct1Holder: \n" + "    Null string in field Struct1Holder/value/mystring\n" + "    Null enum in field Struct1Holder/value/myenum1\n" + "  Parameter StringHolder: \n" + "    Null string in field StringHolder/value\n" + "  Parameter Struct1Holder: \n" + "    Null string in field Struct1Holder/value/mystring\n" + "    Null enum in field Struct1Holder/value/myenum1\n", logRecords[2].getMessage());
}
Also used : StringHolder(org.omg.CORBA.StringHolder) LogRecord(java.util.logging.LogRecord) Struct1Holder(alma.jconttest.ComponentWithBadNullsPackage.Struct1Holder) Struct1(alma.jconttest.ComponentWithBadNullsPackage.Struct1) LogRecordCollectingLogger(alma.acs.testsupport.LogRecordCollectingLogger) ComponentWithBadNullsOperations(alma.jconttest.ComponentWithBadNullsOperations) ComponentWithBadNullsImpl(alma.jconttest.ComponentWithBadNullsImpl.ComponentWithBadNullsImpl)

Example 2 with LogRecordCollectingLogger

use of alma.acs.testsupport.LogRecordCollectingLogger in project ACS by ACS-Community.

the class AcsJExceptionTest method testLogAcsJException.

/**
	 * Checks if logging of ACS exceptions (including caused-by exceptions) works.
	 * Here "logging" refers to special logging via
	 * {@link AcsJException#log(java.util.logging.Logger)}.
	 * <p>
	 * See also <code>alma.demo.client.XmlComponentClient#testException()</code> in module jcontexmpl.
	 */
public void testLogAcsJException() {
    LogRecordCollectingLogger logger = LogRecordCollectingLogger.getCollectingLogger("AcsJExceptionTest-Logger");
    long timeBefore = System.currentTimeMillis();
    try {
        exSystem.throwWrapperAcsJACSErrTest0Ex(true);
    } catch (AcsJACSErrTest0Ex e) {
        long timeAfter = System.currentTimeMillis();
        assertEquals(3, e.getTraceDepth());
        String firstLogMsg = "Will log exception coming from throwWrapperAcsJACSErrTest0Ex";
        logger.info(firstLogMsg);
        e.log(logger);
        LogRecord[] logRecords = logger.getCollectedLogRecords();
        assertNotNull(logRecords);
        assertEquals(4, logRecords.length);
        assertEquals(firstLogMsg, logRecords[0].getMessage());
        // The top-level exception is logged first, with stack level = 2
        LogRecord lr2 = logRecords[1];
        assertEquals("Test error 0 (type=" + ACSErrTypeTest.value + ", code=" + ACSErrTest0.value + ") :: remote call failed", lr2.getMessage());
        assertTrue(lr2.getMillis() >= timeBefore);
        assertTrue(lr2.getMillis() <= timeAfter);
        Map logProperties = (Map) lr2.getParameters()[0];
        assertTrue(logProperties.containsKey("Line"));
        assertTrue(logProperties.containsKey("ThreadName"));
        assertEquals(hostName, logProperties.get("HostName"));
        assertEquals("throwWrapperAcsJACSErrTest0Ex", lr2.getSourceMethodName());
        final String stackId = (String) logProperties.get("StackId");
        assertNotNull(stackId);
        assertEquals(2, ((Long) logProperties.get("StackLevel")).intValue());
        Map userProps2 = (Map) lr2.getParameters()[1];
        assertEquals(0, userProps2.size());
        // The causing exception is logged next, with stack level = 1
        LogRecord lr1 = logRecords[2];
        String msg1 = lr1.getMessage();
        assertEquals("Test error 0 (type=" + ACSErrTypeTest.value + ", code=" + ACSErrTest0.value + ") :: low level ex", msg1);
        assertTrue(lr1.getMillis() >= timeBefore);
        assertTrue(lr1.getMillis() <= timeAfter);
        logProperties = (Map) lr1.getParameters()[0];
        assertTrue(logProperties.containsKey("Line"));
        assertTrue(logProperties.containsKey("ThreadName"));
        assertEquals(hostName, logProperties.get("HostName"));
        assertEquals("throwOriginalAcsJACSErrTest0Ex", lr1.getSourceMethodName());
        assertEquals(stackId, logProperties.get("StackId"));
        assertEquals(1, ((Long) logProperties.get("StackLevel")).intValue());
        Map userProps1 = (Map) lr1.getParameters()[1];
        assertEquals(1, userProps1.size());
        assertEquals("Poverty", userProps1.get("MyStupidProperty"));
        // last comes the wrapped NullPointerException, with stack level = 0
        LogRecord lr0 = logRecords[3];
        String msg0 = lr0.getMessage();
        assertEquals("java.lang.NullPointerException (type=0, code=0) :: mean NPE", msg0);
        logProperties = (Map) lr0.getParameters()[0];
        assertTrue(logProperties.containsKey("Line"));
        assertTrue(logProperties.containsKey("ThreadName"));
        assertEquals(hostName, logProperties.get("HostName"));
        assertEquals("throwOriginalAcsJACSErrTest0Ex", lr0.getSourceMethodName());
        assertEquals(stackId, logProperties.get("StackId"));
        assertEquals(0, ((Long) logProperties.get("StackLevel")).intValue());
        Map userProps0 = (Map) lr0.getParameters()[1];
        assertEquals(0, userProps0.size());
    }
}
Also used : LogRecord(java.util.logging.LogRecord) LogRecordCollectingLogger(alma.acs.testsupport.LogRecordCollectingLogger) AcsJACSErrTest0Ex(alma.ACSErrTypeTest.wrappers.AcsJACSErrTest0Ex) Map(java.util.Map)

Example 3 with LogRecordCollectingLogger

use of alma.acs.testsupport.LogRecordCollectingLogger in project ACS by ACS-Community.

the class AcsLoggingHandlerTest method testOffLevel.

/**
	 * Tests if logs with the silly log level <code>Level.OFF</code> are successfully ignored.
	 * Note that Level.OFF is only meant to be used as an argument to the setLevel() method, 
	 * but not for the log methods. Yet the JDK processes logs with Level.OFF, which ACS does not.
	 */
public void testOffLevel() {
    LogRecordCollectingLogger colLogger = LogRecordCollectingLogger.getCollectingLogger("colLogger");
    // first an INFO log to warm up
    colLogger.info("test message INFO");
    LogRecord testRecord = colLogger.getCollectedLogRecords()[0];
    assertEquals(Level.INFO.intValue(), testRecord.getLevel().intValue());
    loggingHandler.publish(testRecord);
    assertEquals(1, logQueue.logRecords.size());
    colLogger.clearLogRecords();
    logQueue.reset();
    // now comes the silly OFF log
    // the JDK logger and handler are stupid enough to process this log at highest possible level!
    colLogger.log(Level.OFF, "test message OFF");
    testRecord = colLogger.getCollectedLogRecords()[0];
    assertEquals(Level.OFF.intValue(), testRecord.getLevel().intValue());
    // but our publish method is supposed to catch this...
    loggingHandler.publish(testRecord);
    assertEquals(0, logQueue.logRecords.size());
}
Also used : LogRecord(java.util.logging.LogRecord) LogRecordCollectingLogger(alma.acs.testsupport.LogRecordCollectingLogger)

Example 4 with LogRecordCollectingLogger

use of alma.acs.testsupport.LogRecordCollectingLogger in project ACS by ACS-Community.

the class ThreadLoopRunnerTest method testSimple.

public void testSimple() throws Exception {
    final LogRecordCollectingLogger logger2 = LogRecordCollectingLogger.getCollectingLogger("testSimple_collecting_logger");
    logger2.setDelegateLogger(logger);
    Runnable action = new Runnable() {

        public void run() {
            logger2.info("Simple action run.");
        }
    };
    ThreadLoopRunner threadLoopRunner = new ThreadLoopRunner(action, 1, TimeUnit.SECONDS, tf, logger, "testSimple");
    assertEquals(ScheduleDelayMode.FIXED_RATE, threadLoopRunner.getDelayMode());
    assertFalse(threadLoopRunner.isLoopRunning());
    threadLoopRunner.runLoop();
    assertTrue(threadLoopRunner.isLoopRunning());
    assertFalse(threadLoopRunner.isDisabled());
    assertEquals(ScheduleDelayMode.FIXED_RATE, threadLoopRunner.getDelayMode());
    // sleep 2.5 seconds, so that the loop can run 3 times (with our 1 sec delay) at around times 0, 1, 2 seconds
    int expectedNumberOfInvocations = 3;
    Thread.sleep((expectedNumberOfInvocations - 1) * 1000 + 500);
    boolean shutdownRet = threadLoopRunner.shutdown(100, TimeUnit.MILLISECONDS);
    assertTrue("should have finished within 100 ms", shutdownRet);
    LogRecord[] records = logger2.getCollectedLogRecords();
    assertEquals("three records expected", expectedNumberOfInvocations, records.length);
    for (int i = 0; i < records.length; i++) {
        assertEquals("Simple action run.", records[i].getMessage());
    }
}
Also used : LogRecord(java.util.logging.LogRecord) LogRecordCollectingLogger(alma.acs.testsupport.LogRecordCollectingLogger)

Example 5 with LogRecordCollectingLogger

use of alma.acs.testsupport.LogRecordCollectingLogger in project ACS by ACS-Community.

the class ThreadLoopRunnerTest method testActionError.

/**
	 * Tests logging of the RuntimeException or Error that can be thrown by 
	 * the user-supplied Runnable.
	 */
public void testActionError() throws Exception {
    final LogRecordCollectingLogger logger2 = LogRecordCollectingLogger.getCollectingLogger("testActionError_collecting_logger");
    logger2.setDelegateLogger(logger);
    final String errorLogMsg = "Evil error in user runnable";
    Runnable action = new Runnable() {

        public void run() {
            throw new Error(errorLogMsg);
        }
    };
    ThreadLoopRunner threadLoopRunner = new ThreadLoopRunner(action, 1, TimeUnit.SECONDS, tf, logger2, "testActionError");
    assertEquals(ScheduleDelayMode.FIXED_RATE, threadLoopRunner.getDelayMode());
    assertFalse(threadLoopRunner.isLoopRunning());
    threadLoopRunner.runLoop();
    assertTrue(threadLoopRunner.isLoopRunning());
    assertFalse(threadLoopRunner.isDisabled());
    assertEquals(ScheduleDelayMode.FIXED_RATE, threadLoopRunner.getDelayMode());
    // sleep 1.5 seconds, so that the loop can run 2 times (with our 1 sec delay) at around times 0, 1 seconds
    int expectedNumberOfInvocations = 2;
    Thread.sleep((expectedNumberOfInvocations - 1) * 1000 + 500);
    boolean shutdownRet = threadLoopRunner.shutdown(100, TimeUnit.MILLISECONDS);
    assertTrue("should have finished within 100 ms", shutdownRet);
    LogRecord[] records = logger2.getCollectedLogRecords();
    List<LogRecord> errorLogRecords = new ArrayList<LogRecord>();
    for (LogRecord logRecord : records) {
        if (logRecord.getMessage().equals("Uncaught error in scheduled Runnable.")) {
            errorLogRecords.add(logRecord);
        }
    }
    assertEquals("two error logs expected", expectedNumberOfInvocations, errorLogRecords.size());
    for (LogRecord logRecord : errorLogRecords) {
        Throwable loggedError = logRecord.getThrown();
        assertEquals(errorLogMsg, loggedError.getMessage());
    }
}
Also used : LogRecord(java.util.logging.LogRecord) ArrayList(java.util.ArrayList) LogRecordCollectingLogger(alma.acs.testsupport.LogRecordCollectingLogger)

Aggregations

LogRecordCollectingLogger (alma.acs.testsupport.LogRecordCollectingLogger)9 LogRecord (java.util.logging.LogRecord)9 Test (org.junit.Test)2 AcsJACSErrTest0Ex (alma.ACSErrTypeTest.wrappers.AcsJACSErrTest0Ex)1 ComponentWithBadNullsImpl (alma.jconttest.ComponentWithBadNullsImpl.ComponentWithBadNullsImpl)1 ComponentWithBadNullsOperations (alma.jconttest.ComponentWithBadNullsOperations)1 Struct1 (alma.jconttest.ComponentWithBadNullsPackage.Struct1)1 Struct1Holder (alma.jconttest.ComponentWithBadNullsPackage.Struct1Holder)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 StringHolder (org.omg.CORBA.StringHolder)1