You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ie...@apache.org on 2012/10/26 08:33:35 UTC
svn commit: r1402406 - in /sling/trunk/bundles/commons/log: ./
src/test/java/org/apache/sling/commons/log/internal/
src/test/java/org/apache/sling/commons/log/internal/config/
src/test/java/org/apache/sling/commons/log/internal/slf4j/
Author: ieb
Date: Fri Oct 26 06:33:35 2012
New Revision: 1402406
URL: http://svn.apache.org/viewvc?rev=1402406&view=rev
Log:
SLING-2633 Raised unit test coverage from arround 40% to about 70%. Not everything is covered. I have tried not to be too strict so it doesnt become a maintanence overhead if dependencies change.
Added:
sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/ActivatorTest.java (with props)
sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/
sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/AbstractSlingConfigTest.java (with props)
sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/GlobalConfigurationTest.java (with props)
sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/LogWriterManagedServiceFactoryTest.java (with props)
sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/PrivilegedWriterTest.java (with props)
sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingConfigurationPrinterTest.java (with props)
sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogPanelTest.java (with props)
Modified:
sling/trunk/bundles/commons/log/pom.xml
sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java
Modified: sling/trunk/bundles/commons/log/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/pom.xml?rev=1402406&r1=1402405&r2=1402406&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/pom.xml (original)
+++ sling/trunk/bundles/commons/log/pom.xml Fri Oct 26 06:33:35 2012
@@ -142,8 +142,15 @@
<version>0.6.1</version>
<scope>provided</scope>
</dependency>
-
<!-- testing -->
+ <!-- using mockito because its a bit more relaxed and makes it easier to maintain
+ the test cases if dependencies change -->
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ <version>1.8.2</version>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Added: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/ActivatorTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/ActivatorTest.java?rev=1402406&view=auto
==============================================================================
--- sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/ActivatorTest.java (added)
+++ sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/ActivatorTest.java Fri Oct 26 06:33:35 2012
@@ -0,0 +1,56 @@
+/*
+ * 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.sling.commons.log.internal;
+
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.osgi.framework.BundleContext;
+
+/**
+ * A simple but fairly relaxed test of the activator. Also tests LogManager.
+ */
+public class ActivatorTest {
+
+ @Mock
+ private BundleContext context;
+
+ public ActivatorTest() {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ @Test
+ public void testActivator() throws Exception {
+ Activator ac = new Activator();
+ ac.start(context);
+ ac.start(context);
+ }
+
+ @Test
+ public void testActivatorWithJUL() throws Exception {
+ Activator ac = new Activator();
+ Mockito.when(context.getProperty("org.apache.sling.commons.log.julenabled")).thenReturn("true");
+ ac.start(context);
+ ac.start(context);
+
+ }
+
+
+}
Propchange: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/ActivatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/AbstractSlingConfigTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/AbstractSlingConfigTest.java?rev=1402406&view=auto
==============================================================================
--- sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/AbstractSlingConfigTest.java (added)
+++ sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/AbstractSlingConfigTest.java Fri Oct 26 06:33:35 2012
@@ -0,0 +1,63 @@
+/*
+ * 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.sling.commons.log.internal.config;
+
+import java.io.File;
+import java.util.Hashtable;
+
+import org.apache.sling.commons.log.internal.LogManager;
+
+public class AbstractSlingConfigTest {
+
+
+ private int counter = 0;
+
+
+
+ protected Hashtable<String, Object> getGoodConfiguration() {
+ Hashtable<String, Object> config = new Hashtable<String, Object>();
+ config.put(LogManager.LOG_PATTERN,"%s");
+ config.put(LogManager.LOG_LEVEL,"DEBUG");
+ config.put(LogManager.LOG_FILE,getBaseFile().getAbsolutePath());
+ config.put(LogManager.LOG_LOGGERS, new String[]{"loggerA,loggerB,loggerC","log.er.D,logger.E"});
+ return config;
+ }
+
+ protected Hashtable<String, Object> getBadConfiguration() {
+ Hashtable<String, Object> config = new Hashtable<String, Object>();
+ config.put(LogManager.LOG_PATTERN,"%s");
+ config.put(LogManager.LOG_FILE,getBaseFile().getAbsolutePath());
+ return config;
+ }
+
+
+ /**
+ * Returns a base file for testing ensuring the parent path directory
+ * hierarchy exists. The file itself is located below the target folder of
+ * the current working directory.
+ */
+ protected File getBaseFile() {
+ final File baseFile = new File("target/" + getClass().getSimpleName()
+ + "/" + (counter++) + "-" + System.currentTimeMillis() + "/"
+ + getClass().getSimpleName());
+ baseFile.getParentFile().mkdirs();
+ return baseFile;
+ }
+
+}
Propchange: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/AbstractSlingConfigTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/GlobalConfigurationTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/GlobalConfigurationTest.java?rev=1402406&view=auto
==============================================================================
--- sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/GlobalConfigurationTest.java (added)
+++ sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/GlobalConfigurationTest.java Fri Oct 26 06:33:35 2012
@@ -0,0 +1,59 @@
+/*
+ * 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.sling.commons.log.internal.config;
+
+import java.util.Hashtable;
+
+import org.apache.sling.commons.log.internal.LogManager;
+import org.apache.sling.commons.log.internal.slf4j.LogConfigManager;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.MockitoAnnotations;
+import org.osgi.service.cm.ConfigurationException;
+
+public class GlobalConfigurationTest extends AbstractSlingConfigTest {
+
+ public GlobalConfigurationTest() {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ @Test
+ public void testGlobalConfigorator() throws ConfigurationException {
+ GlobalConfigurator g = new GlobalConfigurator();
+ LogConfigManager lcm = LogConfigManager.getInstance();
+ g.setLogConfigManager(lcm);
+ Assert.assertSame(lcm, g.getLogConfigManager());
+ Hashtable<String, Object> config = getGoodConfiguration();
+ g.updated(config);
+ Assert.assertTrue(config.containsKey(LogManager.LOG_LOGGERS));
+ }
+
+ @Test
+ public void testGlobalConfigoratorFail() throws ConfigurationException {
+ GlobalConfigurator g = new GlobalConfigurator();
+ g.setLogConfigManager(LogConfigManager.getInstance());
+ try {
+ g.updated(getBadConfiguration());
+ Assert.fail("Should have failed with a LOG_LEVEL not set");
+ } catch ( ConfigurationException e ) {
+ // good
+ }
+ }
+
+}
Propchange: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/GlobalConfigurationTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/LogWriterManagedServiceFactoryTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/LogWriterManagedServiceFactoryTest.java?rev=1402406&view=auto
==============================================================================
--- sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/LogWriterManagedServiceFactoryTest.java (added)
+++ sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/LogWriterManagedServiceFactoryTest.java Fri Oct 26 06:33:35 2012
@@ -0,0 +1,35 @@
+/*
+ * 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.sling.commons.log.internal.config;
+
+import org.apache.sling.commons.log.internal.slf4j.LogConfigManager;
+import org.junit.Test;
+import org.osgi.service.cm.ConfigurationException;
+
+public class LogWriterManagedServiceFactoryTest extends AbstractSlingConfigTest {
+
+ @Test
+ public void testLogWriterManagedServiceFactory() throws ConfigurationException {
+ LogWriterManagedServiceFactory l = new LogWriterManagedServiceFactory();
+ l.setLogConfigManager(LogConfigManager.getInstance());
+ l.updated("test-pid", getGoodConfiguration());
+ l.deleted("test-pid");
+ }
+
+}
Propchange: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/config/LogWriterManagedServiceFactoryTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/PrivilegedWriterTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/PrivilegedWriterTest.java?rev=1402406&view=auto
==============================================================================
--- sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/PrivilegedWriterTest.java (added)
+++ sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/PrivilegedWriterTest.java Fri Oct 26 06:33:35 2012
@@ -0,0 +1,54 @@
+/*
+ * 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.sling.commons.log.internal.slf4j;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+public class PrivilegedWriterTest {
+
+ @Mock
+ private Writer mockWriter;
+
+ public PrivilegedWriterTest() {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ @Test
+ public void testPrivilegedWriter() throws IOException {
+ StringWriter sw = new StringWriter();
+ PrivilegedWriter p = new PrivilegedWriter(sw);
+ char[] c = "Testing".toCharArray();
+ p.write(c, 0, c.length);
+ p.flush();
+ p.close();
+ Assert.assertEquals("Testing", sw.toString());
+ }
+
+ // Not certain how to test failures here. Mocking the Security manager doesnt work.
+
+}
Propchange: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/PrivilegedWriterTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingConfigurationPrinterTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingConfigurationPrinterTest.java?rev=1402406&view=auto
==============================================================================
--- sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingConfigurationPrinterTest.java (added)
+++ sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingConfigurationPrinterTest.java Fri Oct 26 06:33:35 2012
@@ -0,0 +1,57 @@
+/*
+ * 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.sling.commons.log.internal.slf4j;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.net.URL;
+
+import org.apache.sling.commons.log.internal.config.GlobalConfigurationTest;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.cm.ConfigurationException;
+
+public class SlingConfigurationPrinterTest extends GlobalConfigurationTest {
+
+ @Mock
+ private BundleContext context;
+
+ public SlingConfigurationPrinterTest() {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ @Test
+ public void testSlingLogConfigurationPrinter() throws ConfigurationException {
+ testGlobalConfigorator();
+ SlingConfigurationPrinter p = new SlingConfigurationPrinter();
+ SlingConfigurationPrinter.registerPrinter(context);
+ URL[] u = p.getAttachments("zip");
+ Assert.assertNotNull(u);
+ Assert.assertEquals(u.length,1);
+ StringWriter stringWriter = new StringWriter();
+ PrintWriter printWriter = new PrintWriter(stringWriter);
+ p.printConfiguration(printWriter);
+ SlingConfigurationPrinter.unregisterPrinter();
+ Assert.assertTrue(stringWriter.toString().length() > 0);
+ }
+
+}
Propchange: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingConfigurationPrinterTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogPanelTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogPanelTest.java?rev=1402406&view=auto
==============================================================================
--- sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogPanelTest.java (added)
+++ sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogPanelTest.java Fri Oct 26 06:33:35 2012
@@ -0,0 +1,58 @@
+/*
+ * 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.sling.commons.log.internal.slf4j;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+public class SlingLogPanelTest {
+
+
+ @Mock
+ private HttpServletRequest request;
+
+ @Mock
+ private HttpServletResponse response;
+
+ public SlingLogPanelTest() {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ @Test
+ public void testGet() throws IOException {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ Mockito.when(response.getWriter()).thenReturn(pw);
+ SlingLogPanel p = new SlingLogPanel(LogConfigManager.getInstance());
+ p.doGet(request, response);
+ String html = sw.toString();
+ Assert.assertTrue(html.length()>0);
+ }
+}
Propchange: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogPanelTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java?rev=1402406&r1=1402405&r2=1402406&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java (original)
+++ sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java Fri Oct 26 06:33:35 2012
@@ -240,7 +240,6 @@ public class SlingLogWriterTest extends
File.class.getMethod("setWritable", boolean.class).invoke(protectedParent, false);
File.class.getMethod("setExecutable", boolean.class).invoke(protectedParent, false);
} catch ( Exception e ) {
- e.printStackTrace();
if ( System.getProperty("java.version").startsWith("1.5") ) {
return; // cant perform this test on JDKs before 1.5
}
@@ -273,7 +272,6 @@ public class SlingLogWriterTest extends
File.class.getMethod("setWritable", boolean.class).invoke(loggingParent, false);
File.class.getMethod("setExecutable", boolean.class).invoke(protectedParent, false);
} catch ( Exception e ) {
- e.printStackTrace();
if ( System.getProperty("java.version").startsWith("1.5") ) {
return; // cant perform this test on JDKs before 1.5
}