From e42bdf150dc7dda76eacf36cdb8d7ea1baa75a9e Mon Sep 17 00:00:00 2001 From: mwebster Date: Wed, 9 Aug 2006 15:52:23 +0000 Subject: [PATCH] Bug 150487 "Tracing and Logging Framework" (TraceMessageHandler) --- .../weaver/loadtime/TraceMessageHandler.java | 81 +++++++++++++++++++ .../aspectj/loadtime/LoadtimeModuleTests.java | 2 + .../loadtime/TraceMessageHandlerTest.java | 58 +++++++++++++ .../systemtest/tracing/TracingTests.java | 4 + .../aspectj/systemtest/tracing/tracing.xml | 21 +++++ tests/tracing/ExcludedAspect.aj | 3 + tests/tracing/IncludedAspect.aj | 3 + .../tracing/MyDefaultTraceMessageHandler.java | 12 +++ .../aop-defaulttracemessagehandler.xml | 13 +++ 9 files changed, 197 insertions(+) create mode 100644 loadtime/src/org/aspectj/weaver/loadtime/TraceMessageHandler.java create mode 100644 loadtime/testsrc/org/aspectj/weaver/loadtime/TraceMessageHandlerTest.java create mode 100644 tests/tracing/ExcludedAspect.aj create mode 100644 tests/tracing/IncludedAspect.aj create mode 100644 tests/tracing/MyDefaultTraceMessageHandler.java create mode 100644 tests/tracing/aop-defaulttracemessagehandler.xml diff --git a/loadtime/src/org/aspectj/weaver/loadtime/TraceMessageHandler.java b/loadtime/src/org/aspectj/weaver/loadtime/TraceMessageHandler.java new file mode 100644 index 000000000..b661bc799 --- /dev/null +++ b/loadtime/src/org/aspectj/weaver/loadtime/TraceMessageHandler.java @@ -0,0 +1,81 @@ +/******************************************************************************* + * Copyright (c) 2006 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Matthew Webster - initial implementation + *******************************************************************************/ +package org.aspectj.weaver.loadtime; + +import org.aspectj.bridge.AbortException; +import org.aspectj.bridge.IMessage; +import org.aspectj.bridge.IMessageHandler; +import org.aspectj.bridge.WeaveMessage; +import org.aspectj.bridge.IMessage.Kind; +import org.aspectj.weaver.tools.Trace; +import org.aspectj.weaver.tools.TraceFactory; + +public class TraceMessageHandler implements IMessageHandler { + + private Trace trace; + + public TraceMessageHandler () { + this(TraceFactory.getTraceFactory().getTrace(TraceMessageHandler.class)); + } + + /** + * Used for testing + * + */ + protected TraceMessageHandler (Trace t) { + this.trace = t; + } + + /* Ignore this and defer all decisions about what we log to the Trace + * configuration + */ + public void dontIgnore(Kind kind) { + } + + public boolean handleMessage(IMessage message) throws AbortException { + if (message instanceof WeaveMessage) { + trace.debug(render(message)); + } + else if (message.isDebug()) { + trace.debug(render(message)); + } + else if (message.isInfo()) { + trace.info(render(message)); + } + else if (message.isWarning()) { + trace.warn(render(message),message.getThrown()); + } + else if (message.isError()) { + trace.error(render(message),message.getThrown()); + } + else if (message.isFailed()) { + trace.fatal(render(message),message.getThrown()); + } + else if (message.isAbort()) { + trace.fatal(render(message),message.getThrown()); + } + else return false; + + return true; + } + + /* Ignore this and defer all decisions about what we log to the Trace + * configuration + */ + public boolean isIgnoring(Kind kind) { + return false; + } + + protected String render(IMessage message) { + return message.toString(); + } + +} diff --git a/loadtime/testsrc/org/aspectj/loadtime/LoadtimeModuleTests.java b/loadtime/testsrc/org/aspectj/loadtime/LoadtimeModuleTests.java index da7509b6f..bfd89b8de 100644 --- a/loadtime/testsrc/org/aspectj/loadtime/LoadtimeModuleTests.java +++ b/loadtime/testsrc/org/aspectj/loadtime/LoadtimeModuleTests.java @@ -19,6 +19,7 @@ import junit.textui.TestRunner; import org.aspectj.weaver.loadtime.AjTest; import org.aspectj.weaver.loadtime.ClassLoaderWeavingAdaptorTest; import org.aspectj.weaver.loadtime.JRockitAgentTest; +import org.aspectj.weaver.loadtime.TraceMessageHandlerTest; import org.aspectj.weaver.loadtime.WeavingContextTest; import org.aspectj.weaver.loadtime.WeavingURLClassLoaderTest; import org.aspectj.weaver.loadtime.test.DocumentParserTest; @@ -35,6 +36,7 @@ public class LoadtimeModuleTests extends TestCase { suite.addTestSuite(AjTest.class); suite.addTestSuite(ClassLoaderWeavingAdaptorTest.class); suite.addTestSuite(JRockitAgentTest.class); + suite.addTestSuite(TraceMessageHandlerTest.class); suite.addTestSuite(WeavingContextTest.class); suite.addTestSuite(WeavingURLClassLoaderTest.class); return suite; diff --git a/loadtime/testsrc/org/aspectj/weaver/loadtime/TraceMessageHandlerTest.java b/loadtime/testsrc/org/aspectj/weaver/loadtime/TraceMessageHandlerTest.java new file mode 100644 index 000000000..7bd499a44 --- /dev/null +++ b/loadtime/testsrc/org/aspectj/weaver/loadtime/TraceMessageHandlerTest.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright (c) 2006 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Matthew Webster - initial implementation + *******************************************************************************/ +package org.aspectj.weaver.loadtime; + +import junit.framework.TestCase; + +import org.aspectj.bridge.IMessage; +import org.aspectj.bridge.MessageUtil; +import org.aspectj.bridge.IMessage.Kind; + +public class TraceMessageHandlerTest extends TestCase { + + private TraceMessageHandler messageHandler; + + protected void setUp() throws Exception { + super.setUp(); + messageHandler = new TraceMessageHandler(); + } + + public void testTraceMessageHandler() { + TraceMessageHandler tmh = new TraceMessageHandler(); + } + + public void testDontIgnore() { + Kind kind = IMessage.WEAVEINFO; + messageHandler.dontIgnore(kind); + boolean b = messageHandler.isIgnoring(kind); + assertFalse("Don't filter message kinds",b); + } + + public void testHandleMessageInfo () { + IMessage message = MessageUtil.info("testHandleMessage"); + boolean b = messageHandler.handleMessage(message); + assertTrue("Message not handled",b); + } + + public void testIsIgnoring() { + Kind kind = IMessage.WEAVEINFO; + boolean b = messageHandler.isIgnoring(kind); + assertFalse("Don't filter message kinds",b); + } + + public void testRender() { + String text = "testRender"; + IMessage message = MessageUtil.info(text); + String s = messageHandler.render(message); + assertTrue("Message not rendered correctly",s.indexOf(text) != -1); + } + +} diff --git a/tests/src/org/aspectj/systemtest/tracing/TracingTests.java b/tests/src/org/aspectj/systemtest/tracing/TracingTests.java index 0153d757d..e782e0887 100644 --- a/tests/src/org/aspectj/systemtest/tracing/TracingTests.java +++ b/tests/src/org/aspectj/systemtest/tracing/TracingTests.java @@ -33,5 +33,9 @@ public class TracingTests extends org.aspectj.testing.XMLBasedAjcTestCase { public void testDefaultTracing () { runTest("Default tracing"); } + + public void testDefaultTracingMessageHandler () { + runTest("Default TraceMessageHandler"); + } } diff --git a/tests/src/org/aspectj/systemtest/tracing/tracing.xml b/tests/src/org/aspectj/systemtest/tracing/tracing.xml index 8528761ca..b0dca8d8c 100644 --- a/tests/src/org/aspectj/systemtest/tracing/tracing.xml +++ b/tests/src/org/aspectj/systemtest/tracing/tracing.xml @@ -17,4 +17,25 @@ + + + + + + + + + + + + + + + + + diff --git a/tests/tracing/ExcludedAspect.aj b/tests/tracing/ExcludedAspect.aj new file mode 100644 index 000000000..13cf4b753 --- /dev/null +++ b/tests/tracing/ExcludedAspect.aj @@ -0,0 +1,3 @@ +public aspect ExcludedAspect { + +} \ No newline at end of file diff --git a/tests/tracing/IncludedAspect.aj b/tests/tracing/IncludedAspect.aj new file mode 100644 index 000000000..3bd4d7007 --- /dev/null +++ b/tests/tracing/IncludedAspect.aj @@ -0,0 +1,3 @@ +public aspect IncludedAspect { + +} \ No newline at end of file diff --git a/tests/tracing/MyDefaultTraceMessageHandler.java b/tests/tracing/MyDefaultTraceMessageHandler.java new file mode 100644 index 000000000..b617568a6 --- /dev/null +++ b/tests/tracing/MyDefaultTraceMessageHandler.java @@ -0,0 +1,12 @@ +import org.aspectj.weaver.loadtime.TraceMessageHandler; +import org.aspectj.weaver.tools.*; + +public class MyDefaultTraceMessageHandler extends TraceMessageHandler { + + private static Trace trace = new DefaultTrace(MyDefaultTraceMessageHandler.class); + + public MyDefaultTraceMessageHandler () { + super(trace); + trace.setTraceEnabled(true); + } +} \ No newline at end of file diff --git a/tests/tracing/aop-defaulttracemessagehandler.xml b/tests/tracing/aop-defaulttracemessagehandler.xml new file mode 100644 index 000000000..bf1a587ee --- /dev/null +++ b/tests/tracing/aop-defaulttracemessagehandler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file -- 2.39.5