001/* 002 * (C) Copyright 2019 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Salem Aouana 018 */ 019 020package org.nuxeo.runtime.test.logging; 021 022import static org.junit.Assert.assertEquals; 023import static org.junit.Assert.assertTrue; 024import static org.nuxeo.runtime.api.Framework.NUXEO_TESTING_SYSTEM_PROP; 025 026import java.util.List; 027 028import javax.inject.Inject; 029 030import org.junit.AfterClass; 031import org.junit.BeforeClass; 032import org.junit.Test; 033import org.junit.runner.RunWith; 034import org.nuxeo.runtime.logging.DeprecationLogger; 035import org.nuxeo.runtime.test.runner.Features; 036import org.nuxeo.runtime.test.runner.FeaturesRunner; 037import org.nuxeo.runtime.test.runner.LogCaptureFeature; 038import org.nuxeo.runtime.test.runner.LogFeature; 039import org.nuxeo.runtime.test.runner.LoggerLevel; 040import org.nuxeo.runtime.test.runner.RuntimeFeature; 041 042/** 043 * @since 11.1 044 */ 045@RunWith(FeaturesRunner.class) 046@Features({ RuntimeFeature.class, LogFeature.class, LogCaptureFeature.class }) 047public class DeprecationLoggerTest { 048 049 protected static final String MESSAGE = "Deprecation contribution on component: oldComponent should now be contributed to extension point: newExtension"; 050 051 protected static final String DEPRECATED_VERSION = "11.1"; 052 053 protected static String ORIGINAL_TESTING_PROPERTY_VALUE; 054 055 @Inject 056 protected LogCaptureFeature.Result logCaptureResult; 057 058 @BeforeClass 059 public static void beforeAll() { 060 ORIGINAL_TESTING_PROPERTY_VALUE = System.getProperty(NUXEO_TESTING_SYSTEM_PROP); 061 } 062 063 @AfterClass 064 public static void afterAll() { 065 System.setProperty(NUXEO_TESTING_SYSTEM_PROP, ORIGINAL_TESTING_PROPERTY_VALUE); 066 } 067 068 @Test 069 @LogCaptureFeature.FilterOn(loggerClass = DeprecationLogger.class, logLevel = "WARN") 070 public void shouldLogMessageAsWarning() { 071 logDeprecatedMessageAndVerify(true); 072 } 073 074 @Test 075 @LogCaptureFeature.FilterOn(loggerClass = DeprecationLogger.class, logLevel = "WARN") 076 public void shouldNotLogMessageAsWarning() { 077 System.setProperty(NUXEO_TESTING_SYSTEM_PROP, "false"); 078 logDeprecatedMessageAndVerify(false); 079 } 080 081 @Test 082 @LogCaptureFeature.FilterOn(loggerClass = DeprecationLogger.class, logLevel = "INFO") 083 @LoggerLevel(klass = DeprecationLogger.class, level = "INFO") 084 public void shouldLogMessageAsInfo() { 085 System.setProperty(NUXEO_TESTING_SYSTEM_PROP, "false"); 086 logDeprecatedMessageAndVerify(true); 087 } 088 089 @Test 090 @LogCaptureFeature.FilterOn(loggerClass = DeprecationLogger.class, logLevel = "INFO") 091 @LoggerLevel(klass = DeprecationLogger.class, level = "FATAL") 092 public void shouldNotLogMessageAsInfo() { 093 logDeprecatedMessageAndVerify(false); 094 } 095 096 protected void logDeprecatedMessageAndVerify(boolean messageShouldBeLogged) { 097 DeprecationLogger.log(MESSAGE, DEPRECATED_VERSION); 098 List<String> caughtEvents = logCaptureResult.getCaughtEventMessages(); 099 if (messageShouldBeLogged) { 100 assertEquals(1, caughtEvents.size()); 101 assertEquals(String.format("Since version %s: %s", DEPRECATED_VERSION, MESSAGE), caughtEvents.get(0)); 102 } else { 103 assertTrue(caughtEvents.isEmpty()); 104 } 105 } 106}