JAVA-8405: reducing logging for tutorials-build-job

This commit is contained in:
chaos2418
2021-11-18 11:19:23 +05:30
parent 157d7ee61c
commit 58e6087b3d
81 changed files with 715 additions and 126 deletions
@@ -23,19 +23,19 @@ public class BeforeAndAfterAnnotationsUnitTest {
@Before
public void init() {
LOG.info("startup");
LOG.debug("startup");
list = new ArrayList<>(Arrays.asList("test1", "test2"));
}
@After
public void teardown() {
LOG.info("teardown");
LOG.debug("teardown");
list.clear();
}
@Test
public void whenCheckingListSize_thenSizeEqualsToInit() {
LOG.info("executing test");
LOG.debug("executing test");
assertEquals(2, list.size());
list.add("another test");
@@ -43,7 +43,7 @@ public class BeforeAndAfterAnnotationsUnitTest {
@Test
public void whenCheckingListSizeAgain_thenSizeEqualsToInit() {
LOG.info("executing another test");
LOG.debug("executing another test");
assertEquals(2, list.size());
list.add("yet another test");
@@ -12,24 +12,24 @@ import org.slf4j.LoggerFactory;
public class BeforeClassAndAfterClassAnnotationsUnitTest {
private static final Logger LOG = LoggerFactory.getLogger(BeforeClassAndAfterClassAnnotationsUnitTest.class);
@BeforeClass
public static void setup() {
LOG.info("startup - creating DB connection");
LOG.debug("startup - creating DB connection");
}
@AfterClass
public static void tearDown() {
LOG.info("closing DB connection");
LOG.debug("closing DB connection");
}
@Test
public void simpleTest() {
LOG.info("simple test");
LOG.debug("simple test");
}
@Test
public void anotherSimpleTest() {
LOG.info("another simple test");
LOG.debug("another simple test");
}
}
@@ -1,18 +1,21 @@
package com.baeldung.migration.junit4;
import com.baeldung.migration.junit4.rules.TraceUnitTestRule;
import org.junit.Rule;
import org.junit.Test;
import com.baeldung.migration.junit4.rules.TraceUnitTestRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RuleExampleUnitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(RuleExampleUnitTest.class);
@Rule
public final TraceUnitTestRule traceRuleTests = new TraceUnitTestRule();
@Test
public void whenTracingTests() {
System.out.println("This is my test");
LOGGER.debug("This is my test");
/*...*/
}
}
@@ -1,15 +1,19 @@
package com.baeldung.migration.junit4.rules;
import java.util.ArrayList;
import java.util.List;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
public class TraceUnitTestRule implements TestRule {
private static final Logger LOGGER = LoggerFactory.getLogger(TraceUnitTestRule.class);
@Override
public Statement apply(Statement base, Description description) {
@@ -18,13 +22,13 @@ public class TraceUnitTestRule implements TestRule {
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<Throwable>();
System.out.println("Starting test ... " + description.getMethodName());
LOGGER.debug("Starting test ... {}", description.getMethodName());
try {
base.evaluate();
} catch (Throwable e) {
errors.add(e);
} finally {
System.out.println("... test finished. " + description.getMethodName());
LOGGER.debug("... test finished. {}", description.getMethodName());
}
MultipleFailureException.assertEmpty(errors);
@@ -15,21 +15,21 @@ public class BeforeAllAndAfterAllAnnotationsUnitTest {
@BeforeAll
public static void setup() {
LOG.info("startup - creating DB connection");
LOG.debug("startup - creating DB connection");
}
@AfterAll
public static void tearDown() {
LOG.info("closing DB connection");
LOG.debug("closing DB connection");
}
@Test
public void simpleTest() {
LOG.info("simple test");
LOG.debug("simple test");
}
@Test
public void anotherSimpleTest() {
LOG.info("another simple test");
LOG.debug("another simple test");
}
}
@@ -23,19 +23,19 @@ public class BeforeEachAndAfterEachAnnotationsUnitTest {
@BeforeEach
public void init() {
LOG.info("startup");
LOG.debug("startup");
list = new ArrayList<>(Arrays.asList("test1", "test2"));
}
@AfterEach
public void teardown() {
LOG.info("teardown");
LOG.debug("teardown");
list.clear();
}
@Test
public void whenCheckingListSize_ThenSizeEqualsToInit() {
LOG.info("executing test");
LOG.debug("executing test");
assertEquals(2, list.size());
list.add("another test");
@@ -43,7 +43,7 @@ public class BeforeEachAndAfterEachAnnotationsUnitTest {
@Test
public void whenCheckingListSizeAgain_ThenSizeEqualsToInit() {
LOG.info("executing another test");
LOG.debug("executing another test");
assertEquals(2, list.size());
list.add("yet another test");
@@ -1,19 +1,22 @@
package com.baeldung.migration.junit5;
import com.baeldung.migration.junit5.extensions.TraceUnitExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import com.baeldung.migration.junit5.extensions.TraceUnitExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RunWith(JUnitPlatform.class)
@ExtendWith(TraceUnitExtension.class)
public class RuleExampleUnitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(RuleExampleUnitTest.class);
@Test
public void whenTracingTests() {
System.out.println("This is my test");
LOGGER.debug("This is my test");
/*...*/
}
}
@@ -3,17 +3,21 @@ package com.baeldung.migration.junit5.extensions;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TraceUnitExtension implements AfterEachCallback, BeforeEachCallback {
private static final Logger LOGGER = LoggerFactory.getLogger(TraceUnitExtension.class);
@Override
public void beforeEach(ExtensionContext context) throws Exception {
System.out.println("Starting test ... " + context.getDisplayName());
LOGGER.debug("Starting test ... {}", context.getDisplayName());
}
@Override
public void afterEach(ExtensionContext context) throws Exception {
System.out.println("... test finished. " + context.getDisplayName());
LOGGER.debug("... test finished. {}", context.getDisplayName());
}
}
@@ -1,7 +1,10 @@
package com.baeldung.resourcedirectory;
import com.baeldung.migration.junit5.extensions.TraceUnitExtension;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.nio.file.Path;
@@ -9,6 +12,8 @@ import java.nio.file.Paths;
public class ReadResourceDirectoryUnitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(TraceUnitExtension.class);
@Test
public void givenResourcePath_whenReadAbsolutePathWithFile_thenAbsolutePathEndsWithDirectory() {
String path = "src/test/resources";
@@ -16,7 +21,7 @@ public class ReadResourceDirectoryUnitTest {
File file = new File(path);
String absolutePath = file.getAbsolutePath();
System.out.println(absolutePath);
LOGGER.debug(absolutePath);
Assert.assertTrue(absolutePath.endsWith("src" + File.separator + "test" + File.separator + "resources"));
}
@@ -26,7 +31,7 @@ public class ReadResourceDirectoryUnitTest {
String absolutePath = resourceDirectory.toFile().getAbsolutePath();
System.out.println(absolutePath);
LOGGER.debug(absolutePath);
Assert.assertTrue(absolutePath.endsWith("src" + File.separator + "test" + File.separator + "resources"));
}
@@ -38,7 +43,7 @@ public class ReadResourceDirectoryUnitTest {
File file = new File(classLoader.getResource(resourceName).getFile());
String absolutePath = file.getAbsolutePath();
System.out.println(absolutePath);
LOGGER.debug(absolutePath);
Assert.assertTrue(absolutePath.endsWith(File.separator + "example_resource.txt"));
}
@@ -20,12 +20,12 @@ public class RegisterExtensionSampleExtension implements BeforeAllCallback, Befo
@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
logger.info("Type {} In beforeAll : {}", type, extensionContext.getDisplayName());
logger.debug("Type {} In beforeAll : {}", type, extensionContext.getDisplayName());
}
@Override
public void beforeEach(ExtensionContext extensionContext) throws Exception {
logger.info("Type {} In beforeEach : {}", type, extensionContext.getDisplayName());
logger.debug("Type {} In beforeEach : {}", type, extensionContext.getDisplayName());
}
public String getType() {
@@ -1,41 +1,45 @@
package com.baeldung.junit5;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.TestInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class RepeatedTestAnnotationUnitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(RepeatedTestAnnotationUnitTest.class);
@BeforeEach
void beforeEachTest() {
System.out.println("Before Each Test");
LOGGER.debug("Before Each Test");
}
@AfterEach
void afterEachTest() {
System.out.println("After Each Test");
System.out.println("=====================");
LOGGER.debug("After Each Test");
LOGGER.debug("=====================");
}
@RepeatedTest(3)
void repeatedTest(TestInfo testInfo) {
System.out.println("Executing repeated test");
LOGGER.debug("Executing repeated test");
assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
}
@RepeatedTest(value = 3, name = RepeatedTest.LONG_DISPLAY_NAME)
void repeatedTestWithLongName() {
System.out.println("Executing repeated test with long name");
LOGGER.debug("Executing repeated test with long name");
assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
}
@RepeatedTest(value = 3, name = RepeatedTest.SHORT_DISPLAY_NAME)
void repeatedTestWithShortName() {
System.out.println("Executing repeated test with long name");
LOGGER.debug("Executing repeated test with long name");
assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
}
@@ -46,7 +50,7 @@ public class RepeatedTestAnnotationUnitTest {
@RepeatedTest(3)
void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) {
System.out.println("Repetition #" + repetitionInfo.getCurrentRepetition());
LOGGER.debug("Repetition # {}", repetitionInfo.getCurrentRepetition());
assertEquals(3, repetitionInfo.getTotalRepetitions());
}
}
@@ -2,7 +2,20 @@ package com.baeldung.junit5.conditional;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.*;
import org.junit.jupiter.api.condition.DisabledForJreRange;
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.condition.OS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -11,64 +24,66 @@ import java.lang.annotation.Target;
public class ConditionalAnnotationsUnitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ConditionalAnnotationsUnitTest.class);
@Test
@EnabledOnOs({OS.WINDOWS, OS.MAC})
public void shouldRunBothWindowsAndMac() {
System.out.println("runs on Windows and Mac");
LOGGER.debug("runs on Windows and Mac");
}
@Test
@DisabledOnOs(OS.LINUX)
public void shouldNotRunAtLinux() {
System.out.println("will not run on Linux");
LOGGER.debug("will not run on Linux");
}
@Test
@EnabledOnJre({JRE.JAVA_10, JRE.JAVA_11})
public void shouldOnlyRunOnJava10And11() {
System.out.println("runs with java 10 and 11");
LOGGER.debug("runs with java 10 and 11");
}
@Test
@EnabledForJreRange(min = JRE.JAVA_8, max = JRE.JAVA_13)
public void shouldOnlyRunOnJava8UntilJava13() {
System.out.println("runs with Java 8, 9, 10, 11, 12 and 13!");
LOGGER.debug("runs with Java 8, 9, 10, 11, 12 and 13!");
}
@Test
@DisabledForJreRange(min = JRE.JAVA_14, max = JRE.JAVA_15)
public void shouldNotBeRunOnJava14AndJava15() {
System.out.println("Shouldn't be run on Java 14 and 15.");
LOGGER.debug("Shouldn't be run on Java 14 and 15.");
}
@Test
@DisabledOnJre(JRE.OTHER)
public void thisTestOnlyRunsWithUpToDateJREs() {
System.out.println("this test will only run on java8, 9, 10 and 11.");
LOGGER.debug("this test will only run on java8, 9, 10 and 11.");
}
@Test
@EnabledIfSystemProperty(named = "java.vm.vendor", matches = "Oracle.*")
public void onlyIfVendorNameStartsWithOracle() {
System.out.println("runs only if vendor name starts with Oracle");
LOGGER.debug("runs only if vendor name starts with Oracle");
}
@Test
@DisabledIfSystemProperty(named = "file.separator", matches = "[/]")
public void disabledIfFileSeperatorIsSlash() {
System.out.println("Will not run if file.sepeartor property is /");
LOGGER.debug("Will not run if file.sepeartor property is /");
}
@Test
@EnabledIfEnvironmentVariable(named = "GDMSESSION", matches = "ubuntu")
public void onlyRunOnUbuntuServer() {
System.out.println("only runs if GDMSESSION is ubuntu");
LOGGER.debug("only runs if GDMSESSION is ubuntu");
}
@Test
@DisabledIfEnvironmentVariable(named = "LC_TIME", matches = ".*UTF-8.")
public void shouldNotRunWhenTimeIsNotUTF8() {
System.out.println("will not run if environment variable LC_TIME is UTF-8");
LOGGER.debug("will not run if environment variable LC_TIME is UTF-8");
}
// Commented codes are going to work prior JUnit 5.5
@@ -76,13 +91,13 @@ public class ConditionalAnnotationsUnitTest {
@Test
// @EnabledIf("'FR' == systemProperty.get('user.country')")
public void onlyFrenchPeopleWillRunThisMethod() {
System.out.println("will run only if user.country is FR");
LOGGER.debug("will run only if user.country is FR");
}
@Test
// @DisabledIf("java.lang.System.getProperty('os.name').toLowerCase().contains('mac')")
public void shouldNotRunOnMacOS() {
System.out.println("will not run if our os.name is mac");
LOGGER.debug("will not run if our os.name is mac");
}
@Test
@@ -97,14 +112,14 @@ public class ConditionalAnnotationsUnitTest {
engine = "nashorn",
reason = "Self-fulfilling: {result}")*/
public void onlyRunsInFebruary() {
System.out.println("this test only runs in February");
LOGGER.debug("this test only runs in February");
}
@Test
/*@DisabledIf("systemEnvironment.get('XPC_SERVICE_NAME') != null " +
"&& systemEnvironment.get('XPC_SERVICE_NAME').contains('intellij')")*/
public void notValidForIntelliJ() {
System.out.println("this test will run if our ide is INTELLIJ");
LOGGER.debug("this test will run if our ide is INTELLIJ");
}
@Target(ElementType.METHOD)
@@ -117,7 +132,7 @@ public class ConditionalAnnotationsUnitTest {
@ThisTestWillOnlyRunAtLinuxAndMacWithJava9Or10Or11
public void someSuperTestMethodHere() {
System.out.println("this method will run with java9, 10, 11 and Linux or macOS.");
LOGGER.debug("this method will run with java9, 10, 11 and Linux or macOS.");
}
@Target(ElementType.METHOD)
@@ -129,6 +144,6 @@ public class ConditionalAnnotationsUnitTest {
@RepeatedTest(2)
@CoinToss
public void gamble() {
System.out.println("This tests run status is a gamble with %50 rate");
LOGGER.debug("This tests run status is a gamble with %50 rate");
}
}
@@ -3,11 +3,16 @@ package com.baeldung.junit5.templates;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Properties;
public class DisabledOnQAEnvironmentExtension implements ExecutionCondition {
private static final Logger LOGGER = LoggerFactory.getLogger(DisabledOnQAEnvironmentExtension.class);
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
Properties properties = new Properties();
@@ -16,7 +21,7 @@ public class DisabledOnQAEnvironmentExtension implements ExecutionCondition {
.getResourceAsStream("application.properties"));
if ("qa".equalsIgnoreCase(properties.getProperty("env"))) {
String reason = String.format("The test '%s' is disabled on QA environment", context.getDisplayName());
System.out.println(reason);
LOGGER.debug(reason);
return ConditionEvaluationResult.disabled(reason);
}
} catch (IOException e) {
@@ -1,6 +1,15 @@
package com.baeldung.junit5.templates;
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.stream.Stream;
@@ -9,6 +18,8 @@ import static java.util.Arrays.asList;
public class UserIdGeneratorTestInvocationContextProvider implements TestTemplateInvocationContextProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(UserIdGeneratorTestInvocationContextProvider.class);
@Override
public boolean supportsTestTemplate(ExtensionContext extensionContext) {
return true;
@@ -44,13 +55,13 @@ public class UserIdGeneratorTestInvocationContextProvider implements TestTemplat
new BeforeTestExecutionCallback() {
@Override
public void beforeTestExecution(ExtensionContext extensionContext) {
System.out.println("BeforeTestExecutionCallback:Disabled context");
LOGGER.debug("BeforeTestExecutionCallback:Disabled context");
}
},
new AfterTestExecutionCallback() {
@Override
public void afterTestExecution(ExtensionContext extensionContext) {
System.out.println("AfterTestExecutionCallback:Disabled context");
LOGGER.debug("AfterTestExecutionCallback:Disabled context");
}
});
}
@@ -72,13 +83,13 @@ public class UserIdGeneratorTestInvocationContextProvider implements TestTemplat
new BeforeEachCallback() {
@Override
public void beforeEach(ExtensionContext extensionContext) {
System.out.println("BeforeEachCallback:Enabled context");
LOGGER.debug("BeforeEachCallback:Enabled context");
}
},
new AfterEachCallback() {
@Override
public void afterEach(ExtensionContext extensionContext) {
System.out.println("AfterEachCallback:Enabled context");
LOGGER.debug("AfterEachCallback:Enabled context");
}
});
}
@@ -4,7 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BusinessWorker {
private static Logger LOGGER = LoggerFactory.getLogger(BusinessWorker.class);
private static final Logger LOGGER = LoggerFactory.getLogger(BusinessWorker.class);
public void generateLogs(String msg) {
LOGGER.trace(msg);