You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ace.apache.org by ma...@apache.org on 2010/05/08 13:08:56 UTC

svn commit: r942360 - in /incubator/ace/trunk/ace-log-listener/src/test/java/org/apache/ace/log: ./ listener/ listener/LogTest.java listener/MockLog.java

Author: marrs
Date: Sat May  8 11:08:56 2010
New Revision: 942360

URL: http://svn.apache.org/viewvc?rev=942360&view=rev
Log:
Added unit tests.

Added:
    incubator/ace/trunk/ace-log-listener/src/test/java/org/apache/ace/log/
    incubator/ace/trunk/ace-log-listener/src/test/java/org/apache/ace/log/listener/
    incubator/ace/trunk/ace-log-listener/src/test/java/org/apache/ace/log/listener/LogTest.java
    incubator/ace/trunk/ace-log-listener/src/test/java/org/apache/ace/log/listener/MockLog.java

Added: incubator/ace/trunk/ace-log-listener/src/test/java/org/apache/ace/log/listener/LogTest.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-log-listener/src/test/java/org/apache/ace/log/listener/LogTest.java?rev=942360&view=auto
==============================================================================
--- incubator/ace/trunk/ace-log-listener/src/test/java/org/apache/ace/log/listener/LogTest.java (added)
+++ incubator/ace/trunk/ace-log-listener/src/test/java/org/apache/ace/log/listener/LogTest.java Sat May  8 11:08:56 2010
@@ -0,0 +1,167 @@
+/*
+ * 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.ace.log.listener;
+
+import static org.apache.ace.test.utils.TestUtils.UNIT;
+
+import java.util.Dictionary;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.ace.log.AuditEvent;
+import org.apache.ace.log.Log;
+import org.apache.ace.log.listener.MockLog.LogEntry;
+import org.apache.ace.test.utils.TestUtils;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.Constants;
+import org.osgi.framework.FrameworkEvent;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class LogTest {
+
+    private LogProxy m_logProxy;
+
+    private Log m_mockLog;
+
+    @BeforeMethod(alwaysRun = true)
+    protected void setUp() throws Exception {
+        m_logProxy = new LogProxy();
+        m_mockLog = new MockLog();
+    }
+
+    /**
+     * Test whether logging to the cache and setting a new Log causes the log entries to be flushed to this new Log.
+     */
+    @SuppressWarnings("unchecked")
+    @Test(groups = { UNIT })
+    public void testLogCacheFlush() throws Exception {
+        assert ((MockLog) m_mockLog).getLogEntries().size() == 0 : "MockLog is not empty on start of test";
+
+        Dictionary props = new Properties();
+        String test = "test";
+        String value = "value";
+        props.put(test, value);
+        m_logProxy.log(1, props);
+
+        assert ((MockLog) m_mockLog).getLogEntries().size() == 0 : "MockLog is not empty, but should be as the log should be in the cache";
+
+        m_logProxy.setLog(m_mockLog);
+
+        assert ((MockLog) m_mockLog).getLogEntries().size() == 1 : "Log should contain 1 entry";
+        assert ((MockLog.LogEntry) ((MockLog) m_mockLog).getLogEntries().get(0)).getProperties().get(test).equals(value) : "The property should be 'test:value'";
+    }
+
+    /**
+     * Test whether after unsetting the Log, no new log entries are added, but that they are added to the cache instead
+     * (test the latter by flushing the cache).
+     */
+    @SuppressWarnings("unchecked")
+    @Test(groups = { UNIT })
+    public void testUnsettingLog() throws Exception {
+        assert ((MockLog) m_mockLog).getLogEntries().size() == 0 : "MockLog is not empty on start of test";
+        m_logProxy.setLog(m_mockLog);
+
+        Dictionary props = new Properties();
+        props.put("test", "value");
+        m_logProxy.log(1, props);
+
+        assert ((MockLog) m_mockLog).getLogEntries().size() == 1 : "MockLog should have 1 log entry";
+
+        m_logProxy.setLog(null);
+
+        Dictionary props2 = new Properties();
+        props2.put("test2", "value2");
+        m_logProxy.log(2, props2);
+
+        assert ((MockLog) m_mockLog).getLogEntries().size() == 1 : "MockLog should still have 1 log entry";
+
+        m_logProxy.setLog(m_mockLog);
+
+        assert ((MockLog) m_mockLog).getLogEntries().size() == 2 : "MockLog should have 2 log entries";
+    }
+
+    /**
+     * Basic functionality of the ListenerImpl is covered, the rest of the situations will probably be covered by integration
+     * tests. Note: test the deployment event INSTALL only when a BundleContext is available
+     */
+    @SuppressWarnings("unchecked")
+    @Test(groups = { UNIT })
+    public void testEventConverting() throws Exception {
+        ListenerImpl listeners = new ListenerImpl(null, m_logProxy);
+        listeners.startInternal();
+        m_logProxy.setLog(m_mockLog);
+
+        final String symbolicName = "org.apache.ace.auditlog.listener.testbundle.a";
+        final long bundleId = 123;
+        final String bundleVersion = "1.2.3";
+        final String bundleLocation = "/home/apache/ace/testbundlea.jar";
+
+        Bundle testBundleA = TestUtils.createMockObjectAdapter(Bundle.class, new Object() {
+            @SuppressWarnings("all")
+            public long getBundleId() {
+                return bundleId;
+            }
+
+            @SuppressWarnings("all")
+            public String getSymbolicName() {
+                return symbolicName;
+            }
+
+            @SuppressWarnings("all")
+            public Dictionary getHeaders() {
+                Dictionary dict = new Properties();
+                dict.put(Constants.BUNDLE_VERSION, bundleVersion);
+                return dict;
+            }
+
+            @SuppressWarnings("all")
+            public String getLocation() {
+                return bundleLocation;
+            }
+        });
+
+        BundleEvent bundleEvent = new BundleEvent(BundleEvent.INSTALLED, testBundleA);
+        FrameworkEvent frameworkEvent = new FrameworkEvent(FrameworkEvent.INFO, testBundleA, new IllegalStateException());
+
+        listeners.bundleChanged(bundleEvent);
+        listeners.frameworkEvent(frameworkEvent);
+        listeners.stopInternal();
+
+        List logEntries = ((MockLog) m_mockLog).getLogEntries();
+        assert logEntries.size() == 2 : "2 log entries should be logged";
+
+        LogEntry bundleEntry = (LogEntry) logEntries.get(0);
+        assert bundleEntry.getType() == AuditEvent.BUNDLE_INSTALLED : "state BUNDLE_INSTALLED (" + AuditEvent.BUNDLE_INSTALLED + ") should be in log but '" + bundleEntry.getType() + "' is in log instead";
+        Dictionary bundleProps = bundleEntry.getProperties();
+        assert bundleProps.size() == 4 : "4 properties should be stored, but found: " + bundleProps.size();
+        assert bundleProps.get(AuditEvent.KEY_ID).equals(Long.toString(bundleId)) : "id should be " + bundleId + " but is: " + bundleProps.get(AuditEvent.KEY_ID);
+        assert bundleProps.get(AuditEvent.KEY_NAME).equals(symbolicName) : "symbolicName should be " + symbolicName + " but is " + bundleProps.get(AuditEvent.KEY_NAME);
+        assert bundleProps.get(AuditEvent.KEY_VERSION).equals(bundleVersion) : "version should be " + bundleVersion + " but is " + bundleProps.get(AuditEvent.KEY_VERSION);
+        assert bundleProps.get(AuditEvent.KEY_LOCATION).equals(bundleLocation) : "location should be " + bundleLocation + " but is " + bundleProps.get(AuditEvent.KEY_LOCATION);
+
+        LogEntry frameworkEntry = (LogEntry) logEntries.get(1);
+        assert frameworkEntry.getType() == AuditEvent.FRAMEWORK_INFO : "state FRAMEWORK_INFO (" + AuditEvent.FRAMEWORK_INFO + ") should be in log but '" + frameworkEntry.getType() + "' is in log instead";
+        Dictionary frameworkProps = frameworkEntry.getProperties();
+        assert frameworkProps.size() == 2 : "2 properties should be stored, but found: " + frameworkProps.size();
+        assert frameworkProps.get(AuditEvent.KEY_ID).equals(Long.toString(bundleId)) : "id should be " + bundleId + " but is: " + frameworkProps.get(AuditEvent.KEY_ID);
+        assert frameworkProps.get(AuditEvent.KEY_TYPE).equals(IllegalStateException.class.getName()) : "exceptionType should be " + IllegalStateException.class.getName() + " but is: " + frameworkProps.get(AuditEvent.KEY_TYPE);
+    }
+}

Added: incubator/ace/trunk/ace-log-listener/src/test/java/org/apache/ace/log/listener/MockLog.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-log-listener/src/test/java/org/apache/ace/log/listener/MockLog.java?rev=942360&view=auto
==============================================================================
--- incubator/ace/trunk/ace-log-listener/src/test/java/org/apache/ace/log/listener/MockLog.java (added)
+++ incubator/ace/trunk/ace-log-listener/src/test/java/org/apache/ace/log/listener/MockLog.java Sat May  8 11:08:56 2010
@@ -0,0 +1,71 @@
+/*
+ * 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.ace.log.listener;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.List;
+
+import org.apache.ace.log.Log;
+
+public class MockLog implements Log {
+
+    @SuppressWarnings("unchecked")
+    private List m_logEntries;
+
+    @SuppressWarnings("unchecked")
+    public MockLog() {
+        m_logEntries = Collections.synchronizedList(new ArrayList());
+    }
+
+    @SuppressWarnings("unchecked")
+    public void log(int type, Dictionary properties) {
+        m_logEntries.add(new LogEntry(type, properties));
+    }
+
+    @SuppressWarnings("unchecked")
+    public List getLogEntries() {
+        return new ArrayList(m_logEntries);
+    }
+
+    public void clear() {
+        m_logEntries.clear();
+    }
+
+    public class LogEntry {
+        private int m_type;
+        @SuppressWarnings("unchecked")
+        private Dictionary m_properties;
+        @SuppressWarnings("unchecked")
+        public LogEntry(int type, Dictionary properties) {
+            m_type = type;
+            m_properties = properties;
+        }
+
+        public int getType() {
+            return m_type;
+        }
+
+        @SuppressWarnings("unchecked")
+        public Dictionary getProperties() {
+            return m_properties;
+        }
+    }
+}