Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate tests to keep the log files that were created. Put the log fi… #459

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ jobs:
21
- name: Build and Test
run: mvn -B clean verify "-Djava11.home=${{env.JAVA_HOME_11_X64}}" "-Djava17.home=${{env.JAVA_HOME_17_X64}}"
- uses: actions/upload-artifact@v4
if: failure()
with:
name: surefire-reports-${{ matrix.os }}-${{ matrix.java }}
path: |
**/logs/
**/failsafe-reports/
**/surefire-reports/

format-check:
runs-on: ubuntu-latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.jboss.logmanager.handlers;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
Expand All @@ -32,74 +31,80 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;

import org.jboss.logmanager.ExtHandler;
import org.jboss.logmanager.ExtLogRecord;
import org.jboss.logmanager.formatters.PatternFormatter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
public class AbstractHandlerTest {
static final File BASE_LOG_DIR;

private static final Path BASE_LOG_DIR;

static {
BASE_LOG_DIR = new File(System.getProperty("log.dir"));
BASE_LOG_DIR = Path.of(System.getProperty("log.dir"));
}

final static PatternFormatter FORMATTER = new PatternFormatter("%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n");

private TestInfo testInfo;

@BeforeEach
public void setup() throws Exception {
BASE_LOG_DIR.mkdir();
}

@AfterEach
public void cleanUp() throws Exception {
deleteChildrenRecursively(BASE_LOG_DIR);
}

static boolean deleteRecursively(final File dir) {
if (dir.isDirectory()) {
final File[] files = dir.listFiles();
if (files != null) {
for (final File f : files) {
if (f.isDirectory()) {
if (!deleteRecursively(f)) {
return false;
}
}
if (!f.delete()) {
return false;
}
}
}
public void setup(final TestInfo testInfo) throws Exception {
this.testInfo = testInfo;
deletePath(logDirectory(testInfo));
}

@Test
public void simple() {
Assertions.assertTrue(testInfo.getTestMethod().isPresent());
}

protected Path resolvePath(final String filename) throws IOException {
return logDirectory().resolve(filename);
}

protected Path logDirectory() throws IOException {
return logDirectory(testInfo);
}

protected Path logDirectory(final TestInfo testInfo) throws IOException {
Assertions.assertTrue(testInfo.getTestClass().isPresent());
Assertions.assertTrue(testInfo.getTestMethod().isPresent());
final Path dir = BASE_LOG_DIR
.resolve(testInfo.getTestClass().get().getSimpleName() + "-" + testInfo.getTestMethod().get().getName());
if (Files.notExists(dir)) {
Files.createDirectories(dir);
}
return dir.delete();
}

static boolean deleteChildrenRecursively(final File dir) {
if (dir.isDirectory()) {
final File[] files = dir.listFiles();
if (files != null) {
for (final File f : files) {
if (f.isDirectory()) {
if (!deleteRecursively(f)) {
return false;
}
}
if (!f.delete()) {
return false;
}
}
return dir;
}

private static void deletePath(final Path path) throws IOException {
if (Files.isDirectory(path)) {
try (Stream<Path> paths = Files.walk(path)) {
paths.sorted(Comparator.reverseOrder())
.forEach(p -> {
try {
Files.delete(p);
} catch (IOException e) {
System.out.printf("Failed to delete path %s%n", p);
e.printStackTrace(System.out);
}
});
}
} else {
Files.delete(path);
}
return true;
}

protected static void configureHandlerDefaults(final ExtHandler handler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package org.jboss.logmanager.handlers;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -44,13 +44,16 @@
public class PeriodicRotatingFileHandlerFailureTests extends AbstractHandlerTest {
private final static String FILENAME = "periodic-rotating-file-handler.log";

private final Path logFile = BASE_LOG_DIR.toPath().resolve(FILENAME);
private Path logFile;

private final SimpleDateFormat rotateFormatter = new SimpleDateFormat(".dd");
private PeriodicRotatingFileHandler handler;

@BeforeEach
public void createHandler() throws FileNotFoundException {
public void createHandler() throws IOException {
if (logFile == null) {
logFile = resolvePath(FILENAME);
}
// Create the handler
handler = new PeriodicRotatingFileHandler(logFile.toFile(), rotateFormatter.toPattern(), false);
handler.setFormatter(FORMATTER);
Expand All @@ -69,7 +72,7 @@ public void closeHandler() {
@BMRule(name = "Test failed rotated", targetClass = "java.nio.file.Files", targetMethod = "move", targetLocation = "AT ENTRY", condition = "$2.getFileName().toString().matches(\"periodic-rotating-file-handler\\\\.log\\\\.\\\\d+\")", action = "throw new IOException(\"Fail on purpose\")")
public void testFailedRotate() throws Exception {
final Calendar cal = Calendar.getInstance();
final Path rotatedFile = BASE_LOG_DIR.toPath().resolve(FILENAME + rotateFormatter.format(cal.getTime()));
final Path rotatedFile = resolvePath(FILENAME + rotateFormatter.format(cal.getTime()));
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
final int currentDay = cal.get(Calendar.DAY_OF_MONTH);
final int nextDay = currentDay + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package org.jboss.logmanager.handlers;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -47,13 +47,16 @@
public class PeriodicRotatingFileHandlerTests extends AbstractHandlerTest {
private final static String FILENAME = "periodic-rotating-file-handler.log";

private final Path logFile = BASE_LOG_DIR.toPath().resolve(FILENAME);
private Path logFile;

private final SimpleDateFormat rotateFormatter = new SimpleDateFormat(".dd");
private PeriodicRotatingFileHandler handler;

@BeforeEach
public void createHandler() throws FileNotFoundException {
public void createHandler() throws IOException {
if (logFile == null) {
logFile = resolvePath(FILENAME);
}
// Create the handler
handler = new PeriodicRotatingFileHandler(logFile.toFile(), rotateFormatter.toPattern(), false);
handler.setFormatter(FORMATTER);
Expand All @@ -71,14 +74,14 @@ public void closeHandler() {
@Test
public void testRotate() throws Exception {
final Calendar cal = Calendar.getInstance();
final Path rotatedFile = BASE_LOG_DIR.toPath().resolve(FILENAME + rotateFormatter.format(cal.getTime()));
final Path rotatedFile = resolvePath(FILENAME + rotateFormatter.format(cal.getTime()));
testRotate(cal, rotatedFile);
}

@Test
public void testOverwriteRotate() throws Exception {
final Calendar cal = Calendar.getInstance();
final Path rotatedFile = BASE_LOG_DIR.toPath().resolve(FILENAME + rotateFormatter.format(cal.getTime()));
final Path rotatedFile = resolvePath(FILENAME + rotateFormatter.format(cal.getTime()));

// Create the rotated file to ensure at some point it gets overwritten
Files.deleteIfExists(rotatedFile);
Expand All @@ -102,7 +105,7 @@ public void testArchiveRotateZip() throws Exception {
@BMRule(name = "Test failed rotated", targetClass = "java.nio.file.Files", targetMethod = "move", targetLocation = "AT ENTRY", condition = "$2.getFileName().toString().matches(\"periodic-rotating-file-handler\\\\.log\\\\.\\\\d+\")", action = "throw new IOException(\"Fail on purpose\")")
public void testFailedRotate() throws Exception {
final Calendar cal = Calendar.getInstance();
final Path rotatedFile = BASE_LOG_DIR.toPath().resolve(FILENAME + rotateFormatter.format(cal.getTime()));
final Path rotatedFile = resolvePath(FILENAME + rotateFormatter.format(cal.getTime()));
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
final int currentDay = cal.get(Calendar.DAY_OF_MONTH);
final int nextDay = currentDay + 1;
Expand Down Expand Up @@ -209,7 +212,7 @@ record = createLogRecord(Level.INFO, "Date: %s", thirdDay);
handler.publish(record);

// There should be three files
final Path logDir = BASE_LOG_DIR.toPath();
final Path logDir = logDirectory();
final Path rotated1 = logDir.resolve(FILENAME + firstDateSuffix + archiveSuffix);
final Path rotated2 = logDir.resolve(FILENAME + secondDateSuffix + archiveSuffix);
Assertions.assertTrue(Files.exists(logFile), () -> "Missing file " + logFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package org.jboss.logmanager.handlers;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -29,6 +29,7 @@
import org.jboss.byteman.contrib.bmunit.BMRule;
import org.jboss.byteman.contrib.bmunit.WithByteman;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -38,7 +39,14 @@
public class PeriodicSizeRotatingFileHandlerFailureTests extends AbstractHandlerTest {
private final static String FILENAME = "rotating-file-handler.log";

private final File logFile = new File(BASE_LOG_DIR, FILENAME);
private Path logFile;

@BeforeEach
public void setup() throws IOException {
if (logFile == null) {
logFile = resolvePath(FILENAME);
}
}

@Test
@BMRule(name = "Test failed rotated", targetClass = "java.nio.file.Files", targetMethod = "move", targetLocation = "AT ENTRY", condition = "$2.getFileName().toString().equals(\"rotating-file-handler.log.2\")", action = "throw new IOException(\"Fail on purpose\")")
Expand All @@ -48,7 +56,7 @@ public void testFailedRotate() throws Exception {
handler.setErrorManager(AssertingErrorManager.of(ErrorManager.GENERIC_FAILURE));
handler.setRotateSize(1024L);
handler.setMaxBackupIndex(5);
handler.setFile(logFile);
handler.setFile(logFile.toFile());

// Allow a few rotates
for (int i = 0; i < 100; i++) {
Expand All @@ -58,12 +66,12 @@ public void testFailedRotate() throws Exception {
handler.close();

// The log file should exist, as should one rotated file since we fail the rotation on the second rotate
Assertions.assertTrue(logFile.exists(), () -> String.format("Expected log file %s to exist", logFile));
final Path rotatedFile = BASE_LOG_DIR.toPath().resolve(FILENAME + ".1");
Assertions.assertTrue(Files.exists(logFile), () -> String.format("Expected log file %s to exist", logFile));
final Path rotatedFile = resolvePath(FILENAME + ".1");
Assertions.assertTrue(Files.exists(rotatedFile), () -> String.format("Expected rotated file %s to exist", rotatedFile));

// The last line of the log file should end with "99" as it should be the last record
final List<String> lines = Files.readAllLines(logFile.toPath(), StandardCharsets.UTF_8);
final List<String> lines = Files.readAllLines(logFile, StandardCharsets.UTF_8);
final String lastLine = lines.get(lines.size() - 1);
Assertions.assertTrue(lastLine.endsWith("99"), "Expected the last line to end with 99: " + lastLine);
}
Expand Down
Loading