1
0

Use try-with-resources

- Import instead of FQCN
- Remove whitespace
This commit is contained in:
Gary Gregory
2024-08-14 09:02:35 -04:00
parent d0c4e26317
commit d72cadb7b9

View File

@@ -17,7 +17,6 @@
package org.apache.commons.logging.jdk14; package org.apache.commons.logging.jdk14;
import java.io.ByteArrayOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Iterator; import java.util.Iterator;
@@ -29,6 +28,7 @@ import java.util.logging.Logger;
import junit.framework.Test; import junit.framework.Test;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.DummyException; import org.apache.commons.logging.DummyException;
import org.apache.commons.logging.PathableClassLoader; import org.apache.commons.logging.PathableClassLoader;
import org.apache.commons.logging.PathableTestSuite; import org.apache.commons.logging.PathableTestSuite;
@@ -85,19 +85,10 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
protected static byte[] readClass(final String name, final ClassLoader srcCL) throws Exception { protected static byte[] readClass(final String name, final ClassLoader srcCL) throws Exception {
final String resName = name.replace('.', '/') + ".class"; final String resName = name.replace('.', '/') + ".class";
System.err.println("Trying to load resource [" + resName + "]"); System.err.println("Trying to load resource [" + resName + "]");
final InputStream is = srcCL.getResourceAsStream(resName); try (InputStream is = srcCL.getResourceAsStream(resName)) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); System.err.println("Reading resource [" + resName + "]");
System.err.println("Reading resource [" + resName + "]"); return IOUtils.toByteArray(is);
final byte[] buf = new byte[1000];
for(;;) {
final int read = is.read(buf);
if (read <= 0) {
break;
}
baos.write(buf, 0, read);
} }
is.close();
return baos.toByteArray();
} }
/** /**
@@ -184,10 +175,10 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
// Check the recorded messages // Check the recorded messages
protected void checkLogRecords(final boolean thrown) { protected void checkLogRecords(final boolean thrown) {
final Iterator records = handler.records(); final Iterator<LogRecord> records = handler.records();
for (int i = 0; i < testMessages.length; i++) { for (int i = 0; i < testMessages.length; i++) {
assertTrue(records.hasNext()); assertTrue(records.hasNext());
final LogRecord record = (LogRecord) records.next(); final LogRecord record = records.next();
assertEquals("LogRecord level", assertEquals("LogRecord level",
testLevels[i], record.getLevel()); testLevels[i], record.getLevel());
assertEquals("LogRecord message", assertEquals("LogRecord message",
@@ -286,10 +277,9 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
// Set up LogManager instance // Set up LogManager instance
protected void setUpManager(final String config) throws Exception { protected void setUpManager(final String config) throws Exception {
manager = LogManager.getLogManager(); manager = LogManager.getLogManager();
final InputStream is = try (InputStream is = this.getClass().getClassLoader().getResourceAsStream(config)) {
this.getClass().getClassLoader().getResourceAsStream(config); manager.readConfiguration(is);
manager.readConfiguration(is); }
is.close();
} }
/** /**
@@ -305,33 +295,26 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
// Test logging message strings with exceptions // Test logging message strings with exceptions
public void testExceptionMessages() throws Exception { public void testExceptionMessages() throws Exception {
logExceptionMessages(); logExceptionMessages();
checkLogRecords(true); checkLogRecords(true);
} }
// Test logging plain message strings // Test logging plain message strings
public void testPlainMessages() throws Exception { public void testPlainMessages() throws Exception {
logPlainMessages(); logPlainMessages();
checkLogRecords(false); checkLogRecords(false);
} }
// Test pristine Handlers instances // Test pristine Handlers instances
public void testPristineHandlers() { public void testPristineHandlers() {
assertNotNull(handlers); assertNotNull(handlers);
assertEquals(1, handlers.length); assertEquals(1, handlers.length);
assertTrue(handlers[0] instanceof TestHandler); assertTrue(handlers[0] instanceof TestHandler);
assertNotNull(handler); assertNotNull(handler);
} }
// Test pristine Logger instance // Test pristine Logger instance
public void testPristineLogger() { public void testPristineLogger() {
assertNotNull("Logger exists", logger); assertNotNull("Logger exists", logger);
assertEquals("Logger name", this.getClass().getName(), logger.getName()); assertEquals("Logger name", this.getClass().getName(), logger.getName());
@@ -343,16 +326,13 @@ public class CustomConfigTestCase extends DefaultConfigTestCase {
assertTrue(logger.isLoggable(Level.FINE)); assertTrue(logger.isLoggable(Level.FINE));
assertFalse(logger.isLoggable(Level.FINER)); assertFalse(logger.isLoggable(Level.FINER));
assertFalse(logger.isLoggable(Level.FINEST)); assertFalse(logger.isLoggable(Level.FINEST));
} }
// Test Serializability of Log instance // Test Serializability of Log instance
@Override @Override
public void testSerializable() throws Exception { public void testSerializable() throws Exception {
super.testSerializable(); super.testSerializable();
testExceptionMessages(); testExceptionMessages();
} }
} }