This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-log.git


The following commit(s) were added to refs/heads/master by this push:
     new f97f6b2  SLING-9487 - adding logic & test to set default for 
Logfilepath in Ma… (#6)
f97f6b2 is described below

commit f97f6b26e1e29c0157a41f6799369a553b728f9c
Author: Dominik Süß <[email protected]>
AuthorDate: Thu Jun 18 11:40:06 2020 +0200

    SLING-9487 - adding logic & test to set default for Logfilepath in Ma… (#6)
    
    * SLING-9487 - adding logic & test to set default for Logfilepath in 
ManagedServiceFactory
    
    * SLING-9487 - building up new dictionary instead of assuming mutable 
implementation of dictionary
    
    * SLING-9487 - removing obsolete assertionf or virtual case
    
    * SLING-9487 - bumping versions of test libs to eliminate pax exam https 
issues
---
 pom.xml                                            | 16 +++--
 .../config/LoggerManagedServiceFactory.java        | 20 +++++-
 .../logback/integration/ITConfigAdminSupport.java  |  4 --
 .../log/logback/integration/ITPackagingData.java   |  5 +-
 .../config/TestLoggerManagedServiceFactory.java    | 74 ++++++++++++++++++++++
 5 files changed, 108 insertions(+), 11 deletions(-)

diff --git a/pom.xml b/pom.xml
index d984641..d345b38 100644
--- a/pom.xml
+++ b/pom.xml
@@ -48,7 +48,7 @@
   <properties>
     <slf4j.version>1.7.21</slf4j.version>
     <logback.version>1.2.3</logback.version>
-    <pax-exam.version>3.5.0</pax-exam.version>
+    <pax-exam.version>4.13.2</pax-exam.version>
     <sling.java.version>8</sling.java.version>
 
     <bundle.build.dir>
@@ -324,13 +324,13 @@
     <dependency>
       <groupId>org.ops4j.pax.tinybundles</groupId>
       <artifactId>tinybundles</artifactId>
-      <version>2.0.0</version>
+      <version>3.0.0</version>
       <scope>test</scope>
     </dependency>
     <dependency>
       <groupId>biz.aQute.bnd</groupId>
-      <artifactId>bndlib</artifactId>
-      <version>2.4.0</version>
+      <artifactId>biz.aQute.bndlib</artifactId>
+      <version>3.5.0</version>
       <scope>test</scope>
       <exclusions>
         <exclusion>
@@ -340,6 +340,12 @@
       </exclusions>
     </dependency>
     <dependency>
+        <groupId>org.osgi</groupId>
+        <artifactId>org.osgi.service.log</artifactId>
+        <version>1.3.0</version>
+        <scope>test</scope>
+    </dependency>
+    <dependency>
       <groupId>javax.inject</groupId>
       <artifactId>javax.inject</artifactId>
       <version>1</version>
@@ -397,7 +403,7 @@
     <dependency>
       <groupId>org.mockito</groupId>
       <artifactId>mockito-core</artifactId>
-      <version>2.11.0</version>
+      <version>2.28.2</version>
       <scope>test</scope>
     </dependency>
   </dependencies>
diff --git 
a/src/main/java/org/apache/sling/commons/log/logback/internal/config/LoggerManagedServiceFactory.java
 
b/src/main/java/org/apache/sling/commons/log/logback/internal/config/LoggerManagedServiceFactory.java
index e25def2..ea65117 100644
--- 
a/src/main/java/org/apache/sling/commons/log/logback/internal/config/LoggerManagedServiceFactory.java
+++ 
b/src/main/java/org/apache/sling/commons/log/logback/internal/config/LoggerManagedServiceFactory.java
@@ -18,20 +18,38 @@
  */
 package org.apache.sling.commons.log.logback.internal.config;
 
+import java.util.Collections;
 import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
+import org.apache.sling.commons.log.logback.internal.LogConfigManager;
 import org.osgi.service.cm.ManagedServiceFactory;
 
 class LoggerManagedServiceFactory extends LogConfigurator implements 
ManagedServiceFactory {
 
+    public static final String LOG_FILE_DEFAULT = "logs/error.log";
+
     public String getName() {
         return "Logger configurator";
     }
 
+    @SuppressWarnings("unchecked")
     public void updated(String pid, @SuppressWarnings("rawtypes") Dictionary 
configuration)
             throws org.osgi.service.cm.ConfigurationException {
         try {
-            getLogConfigManager().updateLoggerConfiguration(pid, 
configuration, true);
+            Dictionary<String, Object> conf = configuration;
+            if (configuration.get(LogConfigManager.LOG_FILE) == null) {
+                List<String> keys = Collections.list(configuration.keys());
+                Map<String, Object> confCopy = keys.stream()
+                           .collect(Collectors.toMap(Function.identity(), 
configuration::get)); 
+                confCopy.put(LogConfigManager.LOG_FILE, LOG_FILE_DEFAULT);
+                conf = new Hashtable<>(confCopy);
+            }
+            getLogConfigManager().updateLoggerConfiguration(pid, conf, true);
         } catch (ConfigurationException ce) {
             throw new 
org.osgi.service.cm.ConfigurationException(ce.getProperty(), ce.getReason(), 
ce);
         }
diff --git 
a/src/test/java/org/apache/sling/commons/log/logback/integration/ITConfigAdminSupport.java
 
b/src/test/java/org/apache/sling/commons/log/logback/integration/ITConfigAdminSupport.java
index 36977d5..169d8bf 100644
--- 
a/src/test/java/org/apache/sling/commons/log/logback/integration/ITConfigAdminSupport.java
+++ 
b/src/test/java/org/apache/sling/commons/log/logback/integration/ITConfigAdminSupport.java
@@ -112,10 +112,6 @@ public class ITConfigAdminSupport extends LogTestBase {
         assertTrue(slf4jLogger.isDebugEnabled());
         
assertTrue(LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME).isInfoEnabled());
         
assertFalse(LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME).isDebugEnabled());
-
-        // foo1.bar should not have explicit appender attached with it
-        Iterator<Appender<ILoggingEvent>> itr = 
((ch.qos.logback.classic.Logger) slf4jLogger).iteratorForAppenders();
-        assertFalse(itr.hasNext());
     }
 
     @Test
diff --git 
a/src/test/java/org/apache/sling/commons/log/logback/integration/ITPackagingData.java
 
b/src/test/java/org/apache/sling/commons/log/logback/integration/ITPackagingData.java
index e30e976..db3da86 100644
--- 
a/src/test/java/org/apache/sling/commons/log/logback/integration/ITPackagingData.java
+++ 
b/src/test/java/org/apache/sling/commons/log/logback/integration/ITPackagingData.java
@@ -65,6 +65,7 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThat;
 import static org.ops4j.pax.exam.CoreOptions.composite;
 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.CoreOptions.systemProperty;
 
 @RunWith(PaxExam.class)
 @ExamReactorStrategy(PerMethod.class)
@@ -79,9 +80,11 @@ public class ITPackagingData extends LogTestBase {
     @Override
     protected Option addExtraOptions() {
         return composite(
+                systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
                 configAdmin(),
+                mavenBundle("org.osgi", 
"org.osgi.service.log").versionAsInProject(),
+                mavenBundle("biz.aQute.bnd", 
"biz.aQute.bndlib").versionAsInProject(),
                 mavenBundle("org.ops4j.pax.tinybundles", 
"tinybundles").versionAsInProject(),
-                mavenBundle("biz.aQute.bnd", "bndlib").versionAsInProject(),
                 mavenBundle("commons-io", "commons-io").versionAsInProject()
         );
     }
diff --git 
a/src/test/java/org/apache/sling/commons/log/logback/internal/config/TestLoggerManagedServiceFactory.java
 
b/src/test/java/org/apache/sling/commons/log/logback/internal/config/TestLoggerManagedServiceFactory.java
new file mode 100644
index 0000000..42d2000
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/commons/log/logback/internal/config/TestLoggerManagedServiceFactory.java
@@ -0,0 +1,74 @@
+/*
+ * 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.logback.internal.config;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.sling.commons.log.logback.internal.LogConfigManager;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.osgi.service.cm.ConfigurationException;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TestLoggerManagedServiceFactory {
+    
+    private static final String TEST_OVERRIDE = "TEST_OVERRIDE";
+
+    @Test
+    public void testFileAddition() throws ConfigurationException, 
org.apache.sling.commons.log.logback.internal.config.ConfigurationException {
+        LoggerManagedServiceFactory lmsf = 
Mockito.spy(LoggerManagedServiceFactory.class);
+        LogConfigManager lcm = mock(LogConfigManager.class);
+        doReturn(lcm).when(lmsf).getLogConfigManager();
+
+        ArgumentCaptor<Dictionary<String, String>> effectiveConfigCaptor = 
ArgumentCaptor.forClass(Dictionary.class);
+        lmsf.updated("test", new Hashtable<String, String>());
+        verify(lcm).updateLoggerConfiguration(anyString(), 
effectiveConfigCaptor.capture(), anyBoolean());
+        assertEquals("logs/error.log", 
effectiveConfigCaptor.getValue().get(LogConfigManager.LOG_FILE));
+    }
+    
+    @Test
+    public void testFileNoOverride() throws ConfigurationException, 
org.apache.sling.commons.log.logback.internal.config.ConfigurationException {
+        LoggerManagedServiceFactory lmsf = 
Mockito.spy(LoggerManagedServiceFactory.class);
+        LogConfigManager lcm = mock(LogConfigManager.class);
+        doReturn(lcm).when(lmsf).getLogConfigManager();
+        
+        ArgumentCaptor<Dictionary<String, String>> effectiveConfigCaptor = 
ArgumentCaptor.forClass(Dictionary.class);
+        Dictionary<String, String> dict = new Hashtable<String, String>();
+        dict.put(LogConfigManager.LOG_FILE, TEST_OVERRIDE);
+        lmsf.updated("test", dict);
+        verify(lcm).updateLoggerConfiguration(anyString(), 
effectiveConfigCaptor.capture(), anyBoolean());
+        assertEquals(TEST_OVERRIDE, 
effectiveConfigCaptor.getValue().get(LogConfigManager.LOG_FILE));
+    }
+
+}

Reply via email to