You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2007/04/07 10:14:51 UTC

svn commit: r526399 [2/2] - in /incubator/tuscany/java/sca/modules/implementation-java-runtime/src: main/java/org/apache/tuscany/core/component/ main/java/org/apache/tuscany/core/implementation/ main/java/org/apache/tuscany/core/implementation/java/ ma...

Added: incubator/tuscany/java/sca/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/core/monitor/DefaultExceptionFormatterTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/core/monitor/DefaultExceptionFormatterTestCase.java?view=auto&rev=526399
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/core/monitor/DefaultExceptionFormatterTestCase.java (added)
+++ incubator/tuscany/java/sca/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/core/monitor/DefaultExceptionFormatterTestCase.java Sat Apr  7 01:14:49 2007
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.tuscany.core.monitor;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.spi.TuscanyException;
+import org.apache.tuscany.spi.TuscanyRuntimeException;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class DefaultExceptionFormatterTestCase extends TestCase {
+    private DefaultExceptionFormatter formatter = new DefaultExceptionFormatter();
+
+    public void testTuscanyExceptionFormat() throws Exception {
+        StringWriter writer = new StringWriter();
+        PrintWriter pw = new PrintWriter(writer);
+        TuscanyException e = new TuscanyException("somemessage") {
+        };
+        formatter.write(pw, e);
+        assertTrue(writer.toString().indexOf("somemessage") >= 0);
+    }
+
+    public void testTuscanyRuntimeExceptionFormat() throws Exception {
+        StringWriter writer = new StringWriter();
+        PrintWriter pw = new PrintWriter(writer);
+        TuscanyRuntimeException e = new TuscanyRuntimeException("somemessage") {
+        };
+        formatter.write(pw, e);
+        assertTrue(writer.toString().indexOf("somemessage") >= 0);
+    }
+
+    public void testNormalExceptionFormat() throws Exception {
+        StringWriter writer = new StringWriter();
+        PrintWriter pw = new PrintWriter(writer);
+        Exception e = new Exception();
+        formatter.write(pw, e); // just verify there are no errors since no formatting needs to be doen
+    }
+
+}

Propchange: incubator/tuscany/java/sca/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/core/monitor/DefaultExceptionFormatterTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/core/monitor/DefaultExceptionFormatterTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/java/sca/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/core/monitor/JavaLoggingTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/core/monitor/JavaLoggingTestCase.java?view=auto&rev=526399
==============================================================================
--- incubator/tuscany/java/sca/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/core/monitor/JavaLoggingTestCase.java (added)
+++ incubator/tuscany/java/sca/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/core/monitor/JavaLoggingTestCase.java Sat Apr  7 01:14:49 2007
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.tuscany.core.monitor;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.api.annotation.LogLevel;
+import org.apache.tuscany.host.MonitorFactory;
+
+/**
+ * Test case for the JavaLoggingMonitorFactory.
+ *
+ * @version $Rev$ $Date$
+ */
+public class JavaLoggingTestCase extends TestCase {
+    private static final Logger LOGGER = Logger.getLogger(Monitor.class.getName());
+    private static final MockHandler HANDLER = new MockHandler();
+
+    private MonitorFactory factory;
+
+    /**
+     * Smoke test to ensure the LOGGER is working.
+     */
+    public void testLogger() {
+        LOGGER.info("test");
+        assertEquals(1, HANDLER.logs.size());
+    }
+
+    /**
+     * Test that no record is logged.
+     */
+    public void testUnloggedEvent() {
+        Monitor mon = factory.getMonitor(Monitor.class);
+        mon.eventNotToLog();
+        assertEquals(0, HANDLER.logs.size());
+    }
+
+    /**
+     * Test the correct record is written for an event with no arguments.
+     */
+    public void testEventWithNoArgs() {
+        Monitor mon = factory.getMonitor(Monitor.class);
+        mon.eventWithNoArgs();
+        assertEquals(1, HANDLER.logs.size());
+        LogRecord record = HANDLER.logs.get(0);
+        assertEquals(Level.INFO, record.getLevel());
+        assertEquals(LOGGER.getName(), record.getLoggerName());
+        assertEquals(Monitor.class.getName() + "#eventWithNoArgs", record.getMessage());
+    }
+
+    /**
+     * Test the correct record is written for an event defined by annotation.
+     */
+    public void testEventWithAnnotation() {
+        Monitor mon = factory.getMonitor(Monitor.class);
+        mon.eventWithAnnotation();
+        assertEquals(1, HANDLER.logs.size());
+        LogRecord record = HANDLER.logs.get(0);
+        assertEquals(Level.INFO, record.getLevel());
+        assertEquals(LOGGER.getName(), record.getLoggerName());
+        assertEquals(Monitor.class.getName() + "#eventWithAnnotation", record.getMessage());
+    }
+
+    /**
+     * Test the argument is logged.
+     */
+    public void testEventWithOneArg() {
+        Monitor mon = factory.getMonitor(Monitor.class);
+        mon.eventWithOneArg("ARG");
+        assertEquals(1, HANDLER.logs.size());
+        LogRecord record = HANDLER.logs.get(0);
+        assertEquals(Monitor.class.getName() + "#eventWithOneArg", record.getMessage());
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        LOGGER.setUseParentHandlers(false);
+        LOGGER.addHandler(HANDLER);
+        HANDLER.flush();
+
+        String sourceClass = Monitor.class.getName();
+        Properties levels = new Properties();
+        levels.setProperty(sourceClass + "#eventWithNoArgs", "INFO");
+        levels.setProperty(sourceClass + "#eventWithOneArg", "INFO");
+        levels.setProperty(sourceClass + "#eventWithThrowable", "WARNING");
+        factory = new JavaLoggingMonitorFactory(levels, Level.FINE, "TestMessages");
+    }
+
+    protected void tearDown() throws Exception {
+        LOGGER.removeHandler(HANDLER);
+        HANDLER.flush();
+        super.tearDown();
+    }
+
+    /**
+     * Mock log HANDLER to capture records.
+     */
+    public static class MockHandler extends Handler {
+        List<LogRecord> logs = new ArrayList<LogRecord>();
+
+        public void publish(LogRecord record) {
+            logs.add(record);
+        }
+
+        public void flush() {
+            logs.clear();
+        }
+
+        public void close() throws SecurityException {
+        }
+    }
+
+    @SuppressWarnings({"JavaDoc"})
+    public static interface Monitor {
+        void eventNotToLog();
+
+        @LogLevel("INFO")
+        void eventWithNoArgs();
+
+        @LogLevel("INFO")
+        void eventWithOneArg(String msg);
+
+        @LogLevel("WARNING")
+        void eventWithThrowable(Exception e);
+
+        @LogLevel("INFO")
+        void eventWithAnnotation();
+    }
+}

Propchange: incubator/tuscany/java/sca/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/core/monitor/JavaLoggingTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/implementation-java-runtime/src/test/java/org/apache/tuscany/core/monitor/JavaLoggingTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org