Propchange: sling/whiteboard/ieb/log/src/main/resources/OSGI-INF/metatype/metatype.xml ------------------------------------------------------------------------------ svn:eol-style = native
Propchange: sling/whiteboard/ieb/log/src/main/resources/OSGI-INF/metatype/metatype.xml ------------------------------------------------------------------------------ svn:mime-type = text/xml Added: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/ActivatorTest.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/ActivatorTest.java?rev=1450675&view=auto ============================================================================== --- sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/ActivatorTest.java (added) +++ sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/ActivatorTest.java Wed Feb 27 08:36:42 2013 @@ -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/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/ActivatorTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/AbstractSlingConfigTest.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/AbstractSlingConfigTest.java?rev=1450675&view=auto ============================================================================== --- sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/AbstractSlingConfigTest.java (added) +++ sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/AbstractSlingConfigTest.java Wed Feb 27 08:36:42 2013 @@ -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/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/AbstractSlingConfigTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/GlobalConfigurationTest.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/GlobalConfigurationTest.java?rev=1450675&view=auto ============================================================================== --- sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/GlobalConfigurationTest.java (added) +++ sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/GlobalConfigurationTest.java Wed Feb 27 08:36:42 2013 @@ -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/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/GlobalConfigurationTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/LogWriterManagedServiceFactoryTest.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/LogWriterManagedServiceFactoryTest.java?rev=1450675&view=auto ============================================================================== --- sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/LogWriterManagedServiceFactoryTest.java (added) +++ sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/LogWriterManagedServiceFactoryTest.java Wed Feb 27 08:36:42 2013 @@ -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/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/config/LogWriterManagedServiceFactoryTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/AbstractSlingLogTest.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/AbstractSlingLogTest.java?rev=1450675&view=auto ============================================================================== --- sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/AbstractSlingLogTest.java (added) +++ sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/AbstractSlingLogTest.java Wed Feb 27 08:36:42 2013 @@ -0,0 +1,50 @@ +/* + * 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.File; +import junit.framework.TestCase; + +abstract class AbstractSlingLogTest extends TestCase { + + private static int counter; + + /** + * 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; + } + + /** + * Returns a base file name for testing ensuring the parent path directory + * hierarchy exists. The file itself is located below the target folder of + * the current working directory. + */ + protected String getBase() { + return getBaseFile().getAbsolutePath(); + } + +} Propchange: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/AbstractSlingLogTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/PrivilegedWriterTest.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/PrivilegedWriterTest.java?rev=1450675&view=auto ============================================================================== --- sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/PrivilegedWriterTest.java (added) +++ sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/PrivilegedWriterTest.java Wed Feb 27 08:36:42 2013 @@ -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/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/PrivilegedWriterTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SizeLimitedFileRotatorTest.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SizeLimitedFileRotatorTest.java?rev=1450675&view=auto ============================================================================== --- sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SizeLimitedFileRotatorTest.java (added) +++ sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SizeLimitedFileRotatorTest.java Wed Feb 27 08:36:42 2013 @@ -0,0 +1,168 @@ +/* + * 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.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; + +public class SizeLimitedFileRotatorTest extends AbstractSlingLogTest { + + private File theFile; + + private List<File> rotatedFiles = new ArrayList<File>(); + + @Override + protected void setUp() throws Exception { + super.setUp(); + + theFile = getBaseFile(); + fillFile(theFile); + } + + public void test_size_check() { + // basic requirements for size cmparison test + assertTrue("Expect file to contain 10 bytes", theFile.length() == 10); + + final SizeLimitedFileRotator slfr05 = new SizeLimitedFileRotator(-1, 5); + assertTrue("Expect isRotationDue for 5 bytes", + slfr05.isRotationDue(theFile)); + + final SizeLimitedFileRotator slfr09 = new SizeLimitedFileRotator(-1, 9); + assertTrue("Expect isRotationDue for 9 bytes", + slfr09.isRotationDue(theFile)); + + final SizeLimitedFileRotator slfr10 = new SizeLimitedFileRotator(-1, 10); + assertFalse("Not expecting isRotationDue for 10 bytes", + slfr10.isRotationDue(theFile)); + + final SizeLimitedFileRotator slfr15 = new SizeLimitedFileRotator(-1, 15); + assertFalse("Not expecting isRotationDue for 15 bytes", + slfr15.isRotationDue(theFile)); + } + + public void test_no_rotation_with_negative_maxNum() { + // basic requirement: theFile must exist + assertTrue("Require test file", theFile.exists()); + + // no rotation, just remove the log file on rotation + final SizeLimitedFileRotator slfr_none = new SizeLimitedFileRotator(-1, + 100); + slfr_none.rotate(theFile); + assertFalse("The file must be removed", theFile.exists()); + + // ensure no rotation exists + assertFalse("No rotation file expected", new File( + theFile.getAbsolutePath() + ".0").exists()); + } + + public void test_no_rotation_with_zero_maxNum() { + // basic requirement: theFile must exist + assertTrue("Require test file", theFile.exists()); + + // no rotation, just remove the log file on rotation + final SizeLimitedFileRotator slfr_none = new SizeLimitedFileRotator(0, + 100); + slfr_none.rotate(theFile); + assertFalse("The file must be removed", theFile.exists()); + + // ensure no rotation exists + assertFalse("No rotation file expected", new File( + theFile.getAbsolutePath() + ".0").exists()); + } + + public void test_single_file_rotation() { + // basic requirement: theFile must exist + assertTrue("Require test file", theFile.exists()); + + // no rotation, just remove the log file on rotation + final SizeLimitedFileRotator slfr_single = new SizeLimitedFileRotator( + 1, 100); + slfr_single.rotate(theFile); + assertFalse("The file must be removed", theFile.exists()); + + // ensure one rotation exists + assertTrue("Rotation file 0 expected", getRotatedFile(0).exists()); + assertFalse("No rotation file 1 expected", getRotatedFile(1).exists()); + + // fill and rotate the file again + fillFile(theFile); + slfr_single.rotate(theFile); + assertFalse("The file must be removed", theFile.exists()); + + // ensure one rotations exists + assertTrue("Rotation file 0 expected", getRotatedFile(0).exists()); + assertFalse("No rotation file 1 expected", getRotatedFile(1).exists()); + assertFalse("No rotation file 2 expected", getRotatedFile(2).exists()); + + } + + public void test_two_file_rotation() { + // basic requirement: theFile must exist + assertTrue("Require test file", theFile.exists()); + + // no rotation, just remove the log file on rotation + final SizeLimitedFileRotator slfr_two = new SizeLimitedFileRotator(2, + 100); + slfr_two.rotate(theFile); + assertFalse("The file must be removed", theFile.exists()); + + // ensure one rotation exists + assertTrue("Rotation file 0 expected", getRotatedFile(0).exists()); + assertFalse("No rotation file 1 expected", getRotatedFile(1).exists()); + + // fill and rotate the file again + fillFile(theFile); + slfr_two.rotate(theFile); + assertFalse("The file must be removed", theFile.exists()); + + // ensure one rotations exists + assertTrue("Rotation file 0 expected", getRotatedFile(0).exists()); + assertTrue("Rotation file 1 expected", getRotatedFile(1).exists()); + assertFalse("No rotation file 2 expected", getRotatedFile(2).exists()); + + } + + private static void fillFile(File file) { + OutputStream out = null; + try { + out = new FileOutputStream(file); + out.write("cafebabe!!".getBytes("ISO-8859-1")); + } catch (IOException ioe) { + System.err.println("Failed prefilling " + file); + ioe.printStackTrace(System.err); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException ignore) { + } + } + } + } + + private File getRotatedFile(int index) { + File rotatedFile = new File(theFile.getAbsolutePath() + "." + index); + rotatedFiles.add(rotatedFile); + return rotatedFile; + } +} Propchange: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SizeLimitedFileRotatorTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingConfigurationPrinterTest.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingConfigurationPrinterTest.java?rev=1450675&view=auto ============================================================================== --- sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingConfigurationPrinterTest.java (added) +++ sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingConfigurationPrinterTest.java Wed Feb 27 08:36:42 2013 @@ -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/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingConfigurationPrinterTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogPanelTest.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogPanelTest.java?rev=1450675&view=auto ============================================================================== --- sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogPanelTest.java (added) +++ sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogPanelTest.java Wed Feb 27 08:36:42 2013 @@ -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/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogPanelTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java?rev=1450675&view=auto ============================================================================== --- sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java (added) +++ sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java Wed Feb 27 08:36:42 2013 @@ -0,0 +1,370 @@ +/* + * 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.File; +import java.io.IOException; +import java.lang.reflect.Field; +import java.text.DateFormat; +import java.text.ParseException; +import java.util.Date; +import java.util.Locale; + +public class SlingLogWriterTest extends AbstractSlingLogTest { + + private long july21; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + july21 = DateFormat.getDateInstance(DateFormat.LONG, + Locale.US).parse("July 21, 2009").getTime(); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void test_size_no_rotation() throws IOException { + final String base = getBase(); + SlingLoggerWriter slfw = createLogWriter(base, -1, 10); + + // only base file should exist with size 0 (for now) + File test = new File(base); + assertTrue(test.exists()); + assertEquals(0, test.length()); + + File test0 = new File(base + ".0"); + assertFalse(test0.exists()); + File testn1 = new File(base + ".-1"); + assertFalse(testn1.exists()); + + // write some bytes and ensure size + slfw.write("012345"); + slfw.writeln(); + slfw.checkRotate(); + assertTrue(test.exists()); + assertTrue(test.length() > 0); + assertFalse(test0.exists()); + assertFalse(testn1.exists()); + + // write some more, ensuring rotation does happen + slfw.write("012345"); + slfw.writeln(); + slfw.checkRotate(); + assertTrue(test.exists()); + assertEquals(0, test.length()); + assertFalse(test0.exists()); + assertFalse(testn1.exists()); + } + + public void test_size_rotation_1() throws IOException { + final String base = getBase(); + SlingLoggerWriter slfw = createLogWriter(base, 1, 10); + + // only base file should exist with size 0 (for now) + File test = new File(base); + assertTrue(test.exists()); + assertEquals(0, test.length()); + + File test0 = new File(base + ".0"); + assertFalse(test0.exists()); + File test1 = new File(base + ".1"); + assertFalse(test1.exists()); + File testn1 = new File(base + ".-1"); + assertFalse(testn1.exists()); + + // write some bytes and ensure size + slfw.write("012345"); + slfw.writeln(); + slfw.checkRotate(); + assertTrue(test.exists()); + assertTrue(test.length() > 0); + assertFalse(test0.exists()); + assertFalse(testn1.exists()); + + // write some more, ensuring rotation does happen + slfw.write("012345"); + slfw.writeln(); + slfw.checkRotate(); + assertTrue(test.exists()); + assertEquals(0, test.length()); + assertTrue(test0.exists()); + assertFalse(test1.exists()); + assertFalse(testn1.exists()); + } + + public void test_size_rotation_2() throws IOException { + final String base = getBase(); + SlingLoggerWriter slfw = createLogWriter(base, 2, 10); + + // only base file should exist with size 0 (for now) + File test = new File(base); + assertTrue(test.exists()); + assertTrue(test.length() == 0); + + File test0 = new File(base + ".0"); + assertFalse(test0.exists()); + File test1 = new File(base + ".1"); + assertFalse(test1.exists()); + File testn1 = new File(base + ".-1"); + assertFalse(testn1.exists()); + + // write some bytes and ensure size + slfw.write("012345"); + slfw.writeln(); + slfw.checkRotate(); + assertTrue(test.exists()); + assertTrue(test.length() > 0); + assertFalse(test0.exists()); + assertFalse(testn1.exists()); + + // write some more, ensuring rotation does happen + slfw.write("012345"); + slfw.writeln(); + slfw.checkRotate(); + assertTrue(test.exists()); + assertEquals(0, test.length()); + assertTrue(test0.exists()); + assertFalse(test1.exists()); + assertFalse(testn1.exists()); + + // write bytes to rotate in one fell swoop + slfw.write("0123456789 - more"); + slfw.writeln(); + slfw.checkRotate(); + assertTrue(test.exists()); + assertEquals(0, test.length()); + assertTrue(test0.exists()); + assertTrue(test1.exists()); + assertFalse(testn1.exists()); + } + + public void test_daily_rotation() throws IOException { + final String base = getBase(); + final String limit = "'.'yyyy-MM-dd"; + SlingLoggerWriter slfw = createLogWriter(base, -1, limit); + setNow(slfw, july21); + + // only base file should exist with size 0 (for now) + final File test = new File(base); + assertTrue(test.exists()); + assertEquals(0, test.length()); + + final File test0 = new File(base + ".2009-07-21"); + assertFalse(test0.exists()); + final File testn1 = new File(base + ".2009-07-23"); + assertFalse(testn1.exists()); + + // write some bytes and ensure size + slfw.write("012345"); + slfw.writeln(); + + slfw.checkRotate(); + assertTrue(test.exists()); + assertTrue(test.length() > 0); + assertFalse(test0.exists()); + assertFalse(testn1.exists()); + + // simulate July 23rd + setNow(slfw, july21 + 24*60*60*1000L); + forceRotate(slfw); + // setLastModified fails under Windows if file is still open in logger + slfw.close(); + test.setLastModified(july21); + slfw = createLogWriter(base, -1, limit); + + // rotate the file now + slfw.checkRotate(); + assertTrue(test.exists()); + assertEquals(0, test.length()); + assertTrue(test0.exists()); + assertFalse(testn1.exists()); + } + + public void test_something() throws ParseException { + } + + public void test_createFileRotator() { + assertSize(1, "1"); + + // kilo + assertSize(1 * 1024, "1K"); + assertSize(1 * 1024, "1k"); + assertSize(1 * 1024, "1KB"); + assertSize(1 * 1024, "1kb"); + + // mega + assertSize(1 * 1024 * 1024, "1M"); + assertSize(1 * 1024 * 1024, "1m"); + assertSize(1 * 1024 * 1024, "1MB"); + assertSize(1 * 1024 * 1024, "1mb"); + + // giga + assertSize(1 * 1024 * 1024 * 1024, "1G"); + assertSize(1 * 1024 * 1024 * 1024, "1g"); + assertSize(1 * 1024 * 1024 * 1024, "1GB"); + assertSize(1 * 1024 * 1024 * 1024, "1gb"); + + // some time stuff testing + assertTime("'.'yyyy-MM-dd", null); + assertTime("'.'yyyy-MM-dd", ""); + assertTime("'.'yyyy-MM-dd-mm", "'.'yyyy-MM-dd-mm"); + } + + public void test_create_denied_parent() throws IOException { + File baseFile = getBaseFile(); + File protectedParent = new File(baseFile,"protected"); + protectedParent.mkdirs(); + File loggingParent = new File(protectedParent,"logging"); + try { + // these methods are JDK 1.6 and later so we have introspect to invoke + File.class.getMethod("setWritable", boolean.class).invoke(protectedParent, false); + File.class.getMethod("setExecutable", boolean.class).invoke(protectedParent, false); + } catch ( Exception e ) { + if ( System.getProperty("java.version").startsWith("1.5") ) { + return; // cant perform this test on JDKs before 1.5 + } + } + try{ + if (!protectedParent.canWrite()) { + SlingLoggerWriter writer = createLogWriter( + loggingParent.getAbsolutePath(), -1, 10); + assertNotNull(writer); + assertNull(writer.getFile()); + assertNull(writer.getPath()); + writer.append("Testing Stdout"); + } else { + return; + // If the folder isn't made unwriteable, the test is invalid + // This happens on certain versions of windows when running as an administrative user. + } + } finally { + try { + // these methods are JDK 1.6 and later so we have introspect to invoke + File.class.getMethod("setWritable", boolean.class).invoke(loggingParent, true); + File.class.getMethod("setExecutable", boolean.class).invoke(protectedParent, true); + } catch ( Exception e ) { + // no need. + } + } + } + + public void test_create_denied() throws IOException { + File baseFile = getBaseFile(); + File protectedParent = new File(baseFile,"protected"); + File loggingParent = new File(protectedParent,"logging"); + loggingParent.mkdirs(); + try { + // these methods are JDK 1.6 and later so we have introspect to invoke + File.class.getMethod("setWritable", boolean.class).invoke(loggingParent, false); + File.class.getMethod("setExecutable", boolean.class).invoke(protectedParent, false); + } catch ( Exception e ) { + if ( System.getProperty("java.version").startsWith("1.5") ) { + return; // cant perform this test on JDKs before 1.5 + } + } + try { + if (!loggingParent.canWrite()) { + SlingLoggerWriter writer = createLogWriter( + loggingParent.getAbsolutePath(), -1, 10); + assertNotNull(writer); + assertNull(writer.getFile()); + assertNull(writer.getPath()); + writer.append("Testing Stdout"); + } else { + return; + // If the folder isn't made unwriteable, the test is invalid + // This happens on certain versions of windows when running as an administrative user. + } + } finally { + try { + // these methods are JDK 1.6 and later so we have introspect to invoke + File.class.getMethod("setWritable", boolean.class).invoke(loggingParent, true); + File.class.getMethod("setExecutable", boolean.class).invoke(protectedParent, true); + } catch ( Exception e ) { + // no need. + } + } + } + + private SlingLoggerWriter createLogWriter(String file, int numFiles, + long size) throws IOException { + return createLogWriter(file, numFiles, String.valueOf(size)); + } + + private SlingLoggerWriter createLogWriter(String file, int numFiles, + String limit) throws IOException { + SlingLoggerWriter slw = new SlingLoggerWriter(getClass().getName()); + slw.configure(file, numFiles, limit); + return slw; + } + + private void assertSize(final long expected, final String config) { + final FileRotator checker = SlingLoggerWriter.createFileRotator(-1, + config); + assertTrue("Size checker expeced", + checker instanceof SizeLimitedFileRotator); + assertEquals(expected, ((SizeLimitedFileRotator) checker).getMaxSize()); + } + + private void assertTime(final String expected, final String config) { + final FileRotator checker = SlingLoggerWriter.createFileRotator(-1, + config); + assertTrue("Size checker expeced", + checker instanceof ScheduledFileRotator); + assertEquals(expected, + ((ScheduledFileRotator) checker).getDatePattern()); + } + + private void setNow(SlingLoggerWriter writer, long now) { + try { + Field f = writer.getClass().getDeclaredField("fileRotator"); + f.setAccessible(true); + ScheduledFileRotator sfr = (ScheduledFileRotator) f.get(writer); + + // set the "now" time to the indicated time + f = sfr.getClass().getDeclaredField("now"); + f.setAccessible(true); + ((Date) f.get(sfr)).setTime(now); + + // clear scheduled filename to force its reasteblishment + f = sfr.getClass().getDeclaredField("scheduledFilename"); + f.setAccessible(true); + f.set(sfr, null); + } catch (Throwable t) { + fail("Cannot set now on ScheduledFileRotator: " + t); + } + } + + private void forceRotate(SlingLoggerWriter writer) { + try { + Field f = writer.getClass().getDeclaredField("fileRotator"); + f.setAccessible(true); + ScheduledFileRotator sfr = (ScheduledFileRotator) f.get(writer); + f = sfr.getClass().getDeclaredField("nextCheck"); + f.setAccessible(true); + f.setLong(sfr, -1); + } catch (Throwable t) { + fail("Cannot set now on ScheduledFileRotator: " + t); + } + } +} Propchange: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLogWriterTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevelTest.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevelTest.java?rev=1450675&view=auto ============================================================================== --- sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevelTest.java (added) +++ sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevelTest.java Wed Feb 27 08:36:42 2013 @@ -0,0 +1,81 @@ +/* + * 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 org.slf4j.spi.LocationAwareLogger; + +import junit.framework.TestCase; + +public class SlingLoggerLevelTest extends TestCase { + + public void test_fromSlf4jLevel() { + assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.TRACE_INT-1)); + assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.TRACE_INT)); + assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.TRACE_INT+1)); + + assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.DEBUG_INT-1)); + assertEquals(SlingLoggerLevel.DEBUG, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.DEBUG_INT)); + assertEquals(SlingLoggerLevel.DEBUG, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.DEBUG_INT+1)); + + assertEquals(SlingLoggerLevel.DEBUG, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.INFO_INT-1)); + assertEquals(SlingLoggerLevel.INFO, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.INFO_INT)); + assertEquals(SlingLoggerLevel.INFO, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.INFO_INT+1)); + + assertEquals(SlingLoggerLevel.INFO, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.WARN_INT-1)); + assertEquals(SlingLoggerLevel.WARN, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.WARN_INT)); + assertEquals(SlingLoggerLevel.WARN, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.WARN_INT+1)); + + assertEquals(SlingLoggerLevel.WARN, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.ERROR_INT-1)); + assertEquals(SlingLoggerLevel.ERROR, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.ERROR_INT)); + assertEquals(SlingLoggerLevel.ERROR, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.ERROR_INT+1)); + } + + public void test_fromString() { + assertNull(SlingLoggerLevel.fromString(null)); + + assertNull(SlingLoggerLevel.fromString("traze")); + assertNull(SlingLoggerLevel.fromString("-1")); + assertNull(SlingLoggerLevel.fromString("7")); + + assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromString("trace")); + assertEquals(SlingLoggerLevel.DEBUG, SlingLoggerLevel.fromString("debug")); + assertEquals(SlingLoggerLevel.INFO, SlingLoggerLevel.fromString("info")); + assertEquals(SlingLoggerLevel.WARN, SlingLoggerLevel.fromString("warn")); + assertEquals(SlingLoggerLevel.ERROR, SlingLoggerLevel.fromString("error")); + + assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromString("TRACE")); + assertEquals(SlingLoggerLevel.DEBUG, SlingLoggerLevel.fromString("DEBUG")); + assertEquals(SlingLoggerLevel.INFO, SlingLoggerLevel.fromString("INFO")); + assertEquals(SlingLoggerLevel.WARN, SlingLoggerLevel.fromString("WARN")); + assertEquals(SlingLoggerLevel.ERROR, SlingLoggerLevel.fromString("ERROR")); + + assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromString("TrAcE")); + assertEquals(SlingLoggerLevel.DEBUG, SlingLoggerLevel.fromString("dEbUg")); + assertEquals(SlingLoggerLevel.INFO, SlingLoggerLevel.fromString("Info")); + assertEquals(SlingLoggerLevel.WARN, SlingLoggerLevel.fromString("Warn")); + assertEquals(SlingLoggerLevel.ERROR, SlingLoggerLevel.fromString("ErroR")); + + assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromString("5")); + assertEquals(SlingLoggerLevel.DEBUG, SlingLoggerLevel.fromString("4")); + assertEquals(SlingLoggerLevel.INFO, SlingLoggerLevel.fromString("3")); + assertEquals(SlingLoggerLevel.WARN, SlingLoggerLevel.fromString("2")); + assertEquals(SlingLoggerLevel.ERROR, SlingLoggerLevel.fromString("1")); + assertEquals(SlingLoggerLevel.ERROR, SlingLoggerLevel.fromString("0")); + } +} \ No newline at end of file Propchange: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevelTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerTest.java URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerTest.java?rev=1450675&view=auto ============================================================================== --- sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerTest.java (added) +++ sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerTest.java Wed Feb 27 08:36:42 2013 @@ -0,0 +1,186 @@ +/* + * 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 java.lang.reflect.Method; +import java.util.HashSet; + +import junit.framework.TestCase; + +public class SlingLoggerTest extends TestCase { + + private SlingLoggerWriter output = new SlingLoggerWriter(null) { + { + try { + configure(null, -1, "1GB"); + } catch (IOException ioe) { + + } + } + + public void writeln() throws IOException { + // just flush, no end of line + flush(); + } + }; + + private String messageOnly = "{5}"; + + private SlingLoggerConfig config; + + private SlingLogger logger; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + config = new SlingLoggerConfig(getClass().getName(), "", + new HashSet<String>(), SlingLoggerLevel.DEBUG, output); + + logger = new SlingLogger("sample"); + logger.setLoggerConfig(config); + } + + @Override + protected void tearDown() throws Exception { + output.close(); + super.tearDown(); + } + + public void testCheckLogLevelTrace() { + // initial assertion + config.setLogLevel(SlingLoggerLevel.TRACE); + assertEquals(SlingLoggerLevel.TRACE, config.getLogLevel()); + + // ensure logging disabled + // none for trace + + // ensure logging enabled + assertTrue(logger.isTraceEnabled()); + assertTrue(logger.isDebugEnabled()); + assertTrue(logger.isInfoEnabled()); + assertTrue(logger.isWarnEnabled()); + assertTrue(logger.isErrorEnabled()); + } + + public void testCheckLogLevelDebug() { + // initial assertion + config.setLogLevel(SlingLoggerLevel.DEBUG); + assertEquals(SlingLoggerLevel.DEBUG, config.getLogLevel()); + + // ensure logging disabled + assertFalse(logger.isTraceEnabled()); + + // ensure logging enabled + assertTrue(logger.isDebugEnabled()); + assertTrue(logger.isInfoEnabled()); + assertTrue(logger.isWarnEnabled()); + assertTrue(logger.isErrorEnabled()); + } + + public void testCheckLogLevelInfo() { + // initial assertion + config.setLogLevel(SlingLoggerLevel.INFO); + assertEquals(SlingLoggerLevel.INFO, config.getLogLevel()); + + // ensure logging disabled + assertFalse(logger.isTraceEnabled()); + assertFalse(logger.isDebugEnabled()); + + // ensure logging enabled + assertTrue(logger.isInfoEnabled()); + assertTrue(logger.isWarnEnabled()); + assertTrue(logger.isErrorEnabled()); + } + + public void testCheckLogLevelWarn() { + // initial assertion + config.setLogLevel(SlingLoggerLevel.WARN); + assertEquals(SlingLoggerLevel.WARN, config.getLogLevel()); + + // ensure logging disabled + assertFalse(logger.isTraceEnabled()); + assertFalse(logger.isDebugEnabled()); + assertFalse(logger.isInfoEnabled()); + + // ensure logging enabled + assertTrue(logger.isWarnEnabled()); + assertTrue(logger.isErrorEnabled()); + } + + public void testCheckLogLevelError() { + // initial assertion + config.setLogLevel(SlingLoggerLevel.ERROR); + assertEquals(SlingLoggerLevel.ERROR, config.getLogLevel()); + + // ensure logging disabled + assertFalse(logger.isTraceEnabled()); + assertFalse(logger.isDebugEnabled()); + assertFalse(logger.isInfoEnabled()); + assertFalse(logger.isWarnEnabled()); + + // ensure logging enabled + assertTrue(logger.isErrorEnabled()); + } + + public void testFormat() { + StringWriter w = new StringWriter(); + setDelagateeOn(output, w); + + // a single message + config.configure(messageOnly, new HashSet<String>(), + SlingLoggerLevel.DEBUG, output); + + String message = "This is a message"; + logger.warn(message); + assertEquals(message, w.toString()); + + // reset output buffer and format with logger name and message + w.getBuffer().delete(0, w.getBuffer().length()); + config.configure("{3}|{5}", new HashSet<String>(), + SlingLoggerLevel.DEBUG, output); + + logger.warn(message); + assertEquals(logger.getName() + "|" + message, w.toString()); + + // reset output buffer and format with logger name, level, thread and + // message + w.getBuffer().delete(0, w.getBuffer().length()); + config.configure("{2}|{3}|{4}|{5}", new HashSet<String>(), + SlingLoggerLevel.DEBUG, output); + logger.warn(message); + assertEquals(Thread.currentThread().getName() + "|" + logger.getName() + + "|" + SlingLoggerLevel.WARN + "|" + message, w.toString()); + } + + private static void setDelagateeOn(SlingLoggerWriter output, Writer w) { + try { + Method delegateeSetter = SlingLoggerWriter.class.getDeclaredMethod( + "setDelegatee", new Class[] { Writer.class }); + delegateeSetter.setAccessible(true); + delegateeSetter.invoke(output, new Object[] { w }); + } catch (Throwable t) { + fail("Cannot get or invoke SlingLoggerWriter.setDelagetee method: " + + t.getMessage()); + } + } +} Propchange: sling/whiteboard/ieb/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerTest.java ------------------------------------------------------------------------------ svn:eol-style = native