Closer to working with JUnit5

This commit is contained in:
Bruce Eckel 2016-08-23 16:48:02 -06:00
parent 2161792f99
commit 2ca00a4fa5
19 changed files with 27 additions and 729 deletions

View File

@ -161,7 +161,7 @@ subprojects {
// If you also want to support JUnit 3 and JUnit 4 tests
testCompile("junit:junit:${junit4Version}")
testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}")*/
compile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2"
compile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2"
testCompile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2"
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.0.0-M2"
/* testCompile "junit:junit:4.12"

View File

@ -1,102 +0,0 @@
// test/ConfigureLogging.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {java ConfigureLogging
// -Djava.util.logging.config.file=log.prop}
// {ErrorOutputExpected}
import java.util.logging.*;
public class ConfigureLogging {
static Logger
lgr = Logger.getLogger("com"),
lgr2 = Logger.getLogger("com.mindviewinc"),
util= Logger.getLogger("onjava"),
test= Logger.getLogger("com.mindviewinc.test"),
rand = Logger.getLogger("random");
public ConfigureLogging() {
/*
Set Additional formatters, Filters and
Handlers for the loggers here. You cannot
specify the Handlers for loggers except
the root logger from the configuration
file.
*/
}
public static void main(String[] args) {
sendLogMessages(lgr);
sendLogMessages(lgr2);
sendLogMessages(util);
sendLogMessages(test);
sendLogMessages(rand);
}
private static void
sendLogMessages(Logger logger) {
System.out.println(" Logger Name : "
+ logger.getName() + " Level: "
+ logger.getLevel());
logger.finest("Finest");
logger.finer("Finer");
logger.fine("Fine");
logger.config("Config");
logger.info("Info");
logger.warning("Warning");
logger.severe("Severe");
}
}
/* Output:
Logger Name : com Level: null
Logger Name : com.mindviewinc Level: FINEST
Logger Name : onjava Level: INFO
Logger Name : com.mindviewinc.test Level: FINER
Logger Name : random Level: SEVERE
___[ Error Output ]___
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
FINEST: Finest
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
FINER: Finer
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
FINE: Fine
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
CONFIG: Config
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
INFO: Info
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
WARNING: Warning
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
SEVERE: Severe
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
FINEST: Finest
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
FINER: Finer
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
FINE: Fine
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
CONFIG: Config
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
INFO: Info
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
WARNING: Warning
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
SEVERE: Severe
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
INFO: Info
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
WARNING: Warning
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
SEVERE: Severe
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
FINER: Finer
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
FINE: Fine
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
CONFIG: Config
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
INFO: Info
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
WARNING: Warning
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
SEVERE: Severe
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
SEVERE: Severe
*/

View File

@ -1,47 +0,0 @@
// test/CustomHandler.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// How to write custom handler
// {ErrorOutputExpected}
import java.util.logging.*;
import java.util.*;
public class CustomHandler {
private static Logger logger =
Logger.getLogger("CustomHandler");
private static List<String> trace =
new ArrayList<>();
public static void main(String[] args) {
logger.addHandler(new Handler() {
@Override
public void publish(LogRecord logRecord) {
trace.add(logRecord.getLevel() + ":");
trace.add(logRecord.getSourceClassName()
+ ":");
trace.add(
logRecord.getSourceMethodName() +":");
trace.add("<" + logRecord.getMessage()
+ ">");
trace.add("\n");
}
@Override
public void flush() {}
@Override
public void close() {}
});
logger.warning("Logging Warning");
logger.info("Logging Info");
System.out.print(trace);
}
}
/* Output:
[WARNING:, CustomHandler:, main:, <Logging Warning>,
, INFO:, CustomHandler:, main:, <Logging Info>,
]
___[ Error Output ]___
Jul 27, 2016 10:50:39 AM CustomHandler main
WARNING: Logging Warning
Jul 27, 2016 10:50:39 AM CustomHandler main
INFO: Logging Info
*/

View File

@ -1,19 +0,0 @@
// test/InfoLogging.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {ErrorOutputExpected}
import java.util.logging.*;
public class InfoLogging {
private static Logger logger =
Logger.getLogger("InfoLogging");
public static void main(String[] args) {
logger.info("Logging: INFO-level message");
}
}
/* Output:
___[ Error Output ]___
Jul 27, 2016 10:50:39 AM InfoLogging main
INFO: Logging: INFO-level message
*/

View File

@ -1,21 +0,0 @@
// test/InfoLogging2.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Guaranteeing proper class and method names
// {ErrorOutputExpected}
import java.util.logging.*;
public class InfoLogging2 {
private static Logger logger =
Logger.getLogger("InfoLogging2");
public static void main(String[] args) {
logger.logp(Level.INFO, "InfoLogging2",
"main", "Logging an INFO-level message");
}
}
/* Output:
___[ Error Output ]___
Jul 27, 2016 10:50:40 AM InfoLogging2 main
INFO: Logging an INFO-level message
*/

View File

@ -1,22 +0,0 @@
// test/LogToFile.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {ErrorOutputExpected}
import java.util.logging.*;
public class LogToFile {
private static Logger logger =
Logger.getLogger("LogToFile");
public static void
main(String[] args) throws Exception {
logger.addHandler(
new FileHandler("LogToFile.xml"));
logger.info("A message logged to the file");
}
}
/* Output:
___[ Error Output ]___
Jul 27, 2016 10:50:41 AM LogToFile main
INFO: A message logged to the file
*/

View File

@ -1,24 +0,0 @@
// test/LogToFile2.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {ErrorOutputExpected}
import java.util.logging.*;
public class LogToFile2 {
private static Logger logger =
Logger.getLogger("LogToFile2");
public static void
main(String[] args) throws Exception {
FileHandler logFile =
new FileHandler("LogToFile2.txt");
logFile.setFormatter(new SimpleFormatter());
logger.addHandler(logFile);
logger.info("A message logged to the file");
}
}
/* Output:
___[ Error Output ]___
Jul 27, 2016 10:50:41 AM LogToFile2 main
INFO: A message logged to the file
*/

View File

@ -1,184 +0,0 @@
// test/LoggingLevelManipulation.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {ErrorOutputExpected}
import java.util.logging.Level;
import java.util.logging.Logger;
public class LoggingLevelManipulation {
private static Logger
lgr = Logger.getLogger("com"),
lgr2 = Logger.getLogger("com.mindviewinc"),
util= Logger.getLogger("onjava"),
test= Logger.getLogger("com.mindviewinc.test"),
rand = Logger.getLogger("random");
static void printLogMessages(Logger logger) {
logger.finest(logger.getName() + " Finest");
logger.finer(logger.getName() + " Finer");
logger.fine(logger.getName() + " Fine");
logger.config(logger.getName() + " Config");
logger.info(logger.getName() + " Info");
logger.warning(logger.getName()+" Warning");
logger.severe(logger.getName() + " Severe");
}
static void logMessages() {
printLogMessages(lgr);
printLogMessages(lgr2);
printLogMessages(util);
printLogMessages(test);
printLogMessages(rand);
}
static void printLevels() {
System.out.println(" -- printing levels -- "
+ lgr.getName()
+ " : " + lgr.getLevel()
+ " " + lgr2.getName()
+ " : " + lgr2.getLevel()
+ " " + util.getName()
+ " : " + util.getLevel()
+ " " + test.getName()
+ " : " + test.getLevel()
+ " " + rand.getName()
+ " : " + rand.getLevel());
}
public static void main(String[] args) {
printLevels();
lgr.setLevel(Level.SEVERE);
printLevels();
System.out.println("net level: SEVERE");
logMessages();
util.setLevel(Level.FINEST);
test.setLevel(Level.FINEST);
rand.setLevel(Level.FINEST);
printLevels();
System.out.println(
"individual loggers set to FINEST");
logMessages();
lgr.setLevel(Level.FINEST);
printLevels();
System.out.println("net level: FINEST");
logMessages();
}
}
/* Output:
-- printing levels -- com : null com.mindviewinc : null
onjava : null com.mindviewinc.test : null random : null
-- printing levels -- com : SEVERE com.mindviewinc : null
onjava : null com.mindviewinc.test : null random : null
net level: SEVERE
-- printing levels -- com : SEVERE com.mindviewinc : null
onjava : FINEST com.mindviewinc.test : FINEST random :
FINEST
individual loggers set to FINEST
-- printing levels -- com : FINEST com.mindviewinc : null
onjava : FINEST com.mindviewinc.test : FINEST random :
FINEST
net level: FINEST
___[ Error Output ]___
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
SEVERE: com Severe
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
SEVERE: com.mindviewinc Severe
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
INFO: onjava Info
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
WARNING: onjava Warning
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
SEVERE: onjava Severe
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
SEVERE: com.mindviewinc.test Severe
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
INFO: random Info
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
WARNING: random Warning
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
SEVERE: random Severe
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
SEVERE: com Severe
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
SEVERE: com.mindviewinc Severe
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
INFO: onjava Info
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
WARNING: onjava Warning
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
SEVERE: onjava Severe
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
INFO: com.mindviewinc.test Info
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
WARNING: com.mindviewinc.test Warning
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
SEVERE: com.mindviewinc.test Severe
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
INFO: random Info
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
WARNING: random Warning
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
SEVERE: random Severe
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
INFO: com Info
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
WARNING: com Warning
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
SEVERE: com Severe
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
INFO: com.mindviewinc Info
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
WARNING: com.mindviewinc Warning
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
SEVERE: com.mindviewinc Severe
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
INFO: onjava Info
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
WARNING: onjava Warning
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
SEVERE: onjava Severe
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
INFO: com.mindviewinc.test Info
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
WARNING: com.mindviewinc.test Warning
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
SEVERE: com.mindviewinc.test Severe
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
INFO: random Info
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
WARNING: random Warning
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
printLogMessages
SEVERE: random Severe
*/

View File

@ -1,61 +0,0 @@
// test/LoggingLevels.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {ErrorOutputExpected}
import java.util.logging.Level;
import java.util.logging.Logger;
public class LoggingLevels {
private static Logger
lgr = Logger.getLogger("com"),
lgr2 = Logger.getLogger("com.mindviewinc"),
util= Logger.getLogger("onjava"),
test= Logger.getLogger("com.mindviewinc.test"),
rand = Logger.getLogger("random");
private static void logMessages() {
lgr.info("com : info");
lgr2.info("com.mindviewinc : info");
util.info("util : info");
test.severe("test : severe");
rand.info("random : info");
}
public static void main(String[] args) {
lgr.setLevel(Level.SEVERE);
System.out.println("com level: SEVERE");
logMessages();
util.setLevel(Level.FINEST);
test.setLevel(Level.FINEST);
rand.setLevel(Level.FINEST);
System.out.println(
"individual loggers set to FINEST");
logMessages();
lgr.setLevel(Level.SEVERE);
System.out.println("com level: SEVERE");
logMessages();
}
}
/* Output:
com level: SEVERE
individual loggers set to FINEST
com level: SEVERE
___[ Error Output ]___
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
INFO: util : info
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
SEVERE: test : severe
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
INFO: random : info
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
INFO: util : info
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
SEVERE: test : severe
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
INFO: random : info
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
INFO: util : info
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
SEVERE: test : severe
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
INFO: random : info
*/

View File

@ -1,27 +0,0 @@
// test/MultipleHandlers.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {ErrorOutputExpected}
import java.util.logging.*;
public class MultipleHandlers {
private static Logger logger =
Logger.getLogger("MultipleHandlers");
public static void
main(String[] args) throws Exception {
FileHandler logFile =
new FileHandler("MultipleHandlers.xml");
logger.addHandler(logFile);
logger.addHandler(new ConsoleHandler());
logger.warning(
"Output to multiple handlers");
}
}
/* Output:
___[ Error Output ]___
Jul 27, 2016 10:50:43 AM MultipleHandlers main
WARNING: Output to multiple handlers
Jul 27, 2016 10:50:43 AM MultipleHandlers main
WARNING: Output to multiple handlers
*/

View File

@ -1,26 +0,0 @@
// test/MultipleHandlers2.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {ErrorOutputExpected}
import java.util.logging.*;
public class MultipleHandlers2 {
private static Logger logger =
Logger.getLogger("MultipleHandlers2");
public static void
main(String[] args) throws Exception {
FileHandler logFile =
new FileHandler("MultipleHandlers2.xml");
logger.addHandler(logFile);
logger.addHandler(new ConsoleHandler());
logger.setUseParentHandlers(false);
logger.warning(
"Output to multiple handlers");
}
}
/* Output:
___[ Error Output ]___
Jul 27, 2016 10:50:43 AM MultipleHandlers2 main
WARNING: Output to multiple handlers
*/

View File

@ -1,60 +0,0 @@
// test/PrintableLogRecord.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Override LogRecord toString()
import java.util.logging.*;
public class PrintableLogRecord extends LogRecord {
public PrintableLogRecord(Level level, String str) {
super(level, str);
}
@Override
public String toString() {
String result = "Level<" + getLevel()+ ">\n"
+ "LoggerName<" + getLoggerName() + ">\n"
+ "Message<" + getMessage() + ">\n"
+ "CurrentMillis<" + getMillis() + ">\n"
+ "Params";
Object[] objParams = getParameters();
if(objParams == null)
result += "<null>\n";
else
for(int i = 0; i < objParams.length; i++)
result += " Param # <" + i + " value "+
objParams[i].toString() + ">\n";
result += "ResourceBundle<"
+ getResourceBundle()
+ ">\nResourceBundleName<"
+ getResourceBundleName()
+ ">\nSequenceNumber<"
+ getSequenceNumber()
+ ">\nSourceClassName<"
+ getSourceClassName()
+ ">\nSourceMethodName<"
+ getSourceMethodName()
+ ">\nThread Id<" + getThreadID()
+ ">\nThrown<" + getThrown() + ">";
return result;
}
public static void main(String[] args) {
PrintableLogRecord logRecord =
new PrintableLogRecord(
Level.FINEST, "Simple Log Record");
System.out.println(logRecord);
}
}
/* Output:
Level<FINEST>
LoggerName<null>
Message<Simple Log Record>
CurrentMillis<1469638244376>
Params<null>
ResourceBundle<null>
ResourceBundleName<null>
SequenceNumber<0>
SourceClassName<null>
SourceMethodName<null>
Thread Id<1>
Thrown<null>
*/

View File

@ -1,45 +0,0 @@
// test/SimpleFilter.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {ErrorOutputExpected}
import java.util.logging.*;
public class SimpleFilter {
private static Logger logger =
Logger.getLogger("SimpleFilter");
static class Duck {};
static class Wombat {};
static void sendLogMessages() {
logger.log(Level.WARNING,
"A duck in the house!", new Duck());
logger.log(Level.WARNING,
"A Wombat at large!", new Wombat());
}
public static void main(String[] args) {
sendLogMessages();
logger.setFilter(record -> {
Object[] params =
record.getParameters();
if(params == null)
return true; // No parameters
if(record.getParameters()[0]
instanceof Duck)
return true; // Only log Ducks
return false;
});
logger.info("After setting filter..");
sendLogMessages();
}
}
/* Output:
___[ Error Output ]___
Jul 27, 2016 10:50:44 AM SimpleFilter sendLogMessages
WARNING: A duck in the house!
Jul 27, 2016 10:50:44 AM SimpleFilter sendLogMessages
WARNING: A Wombat at large!
Jul 27, 2016 10:50:44 AM SimpleFilter main
INFO: After setting filter..
Jul 27, 2016 10:50:44 AM SimpleFilter sendLogMessages
WARNING: A duck in the house!
*/

View File

@ -1,38 +0,0 @@
// test/SimpleFormatterExample.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {ErrorOutputExpected}
import java.util.logging.*;
public class SimpleFormatterExample {
private static Logger logger =
Logger.getLogger("SimpleFormatterExample");
private static void logMessages() {
logger.info("Line One");
logger.info("Line Two");
}
public static void main(String[] args) {
logger.setUseParentHandlers(false);
Handler conHdlr = new ConsoleHandler();
conHdlr.setFormatter(new Formatter() {
public String format(LogRecord record) {
return record.getLevel() + " : "
+ record.getSourceClassName()
+ " -:- "
+ record.getSourceMethodName()
+ " -:- "
+ record.getMessage() + "\n";
}
});
logger.addHandler(conHdlr);
logMessages();
}
}
/* Output:
___[ Error Output ]___
INFO : SimpleFormatterExample -:- logMessages -:- Line
One
INFO : SimpleFormatterExample -:- logMessages -:- Line
Two
*/

View File

@ -1,32 +0,0 @@
// test/log.prop
#### Configuration File ####
# Global Params
# Handlers installed for the root logger
handlers= java.util.logging.ConsoleHandler java.util.logging.FileHandler
# Level for root logger--is used by any logger
# that does not have its level set
.level= FINEST
# Initialization class--the public no-arg constructor
# of this class is called by the Logging framework
config = ConfigureLogging
# Configure FileHandler
# Logging file name - %u specifies unique
java.util.logging.FileHandler.pattern = java%g.log
# Write 100000 bytes before rotating this file
java.util.logging.FileHandler.limit = 100000
# Number of rotating files
java.util.logging.FileHandler.count = 3
# Formatter for this FileHandler
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
# Configure ConsoleHandler
java.util.logging.ConsoleHandler.level = FINEST
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
# Set Logger Levels #
net.level=SEVERE
com.mindviewinc.level = FINEST
onjava.level = INFO
com.mindviewinc.test.level = FINER
random.level= SEVERE

View File

@ -87,3 +87,4 @@ public class Queue {
", empty() = " + empty() +
", queue = " + Arrays.asList(data);
}
}

View File

@ -3,14 +3,14 @@
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
package verifying;
import verifying.Queue;
import verifying.Queue.*;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
public class QueueTest {
private Queue queue = new Queue(10);
private int i = 0;
@Before
@BeforeEach
public void initialize() {
while(i < 5) // Preload with some data
queue.put(Integer.toString(i++));

View File

@ -21,14 +21,14 @@ class CountedList extends ArrayList<String> {
public class SimpleJUnit {
private CountedList list;
@Before
@BeforeEach
public void initialize() {
list = new CountedList();
System.out.println("Set up for " + list.getId());
for(int i = 0; i < 3; i++)
list.add(Integer.toString(i));
}
@After
@AfterEach
public void cleanup() {
System.out.println("Cleaning up " + list.getId());
}
@ -55,8 +55,8 @@ public class SimpleJUnit {
private
void compare(ArrayList<String> lst, String[] strs) {
String[] array = (String[])lst.toArray();
assertTrue("Arrays not the same length",
array.length == strs.length);
assertTrue(array.length == strs.length,
"Arrays not the same length");
for(int i = 0; i < array.length; i++)
assertEquals(strs[i], array[i]);
}

View File

@ -6,8 +6,7 @@
import java.util.*;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.runner.*;
import org.junit.jupiter.runner.notification.Failure;
// import org.junit.platform.runner.JUnitPlatform;
public class StringInverterTest {
static StringInverter inverter;
@ -17,39 +16,44 @@ public class StringInverterTest {
String out = "eXIT, pURSUED BY A bEAR.";
assertEquals(inverter.invert(in), out);
}
@Test(expected = ComparisonFailure.class)
@Test
public final void basicInversion_Fail() {
assertEquals(inverter.invert("X"), "X");
expectThrows(RuntimeException.class, () -> {
assertEquals(inverter.invert("X"), "X");
});
}
@Test(expected = RuntimeException.class)
@Test
public final void allowedCharacters_Fail() {
inverter.invert(";-_()*&^%$#@!~`");
inverter.invert("0123456789");
expectThrows(RuntimeException.class, () -> {
inverter.invert(";-_()*&^%$#@!~`");
inverter.invert("0123456789");
});
}
/*@Test
@Test
public final void allowedCharacters_Succeed() {
inverter.invert("abcdefghijklmnopqrstuvwxyz ,.");
inverter.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.");
assertTrue(true); // No exception thrown
}*/
@Test(expected = RuntimeException.class)
}
@Test
public final void lengthLessThan26_Fail() {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() > 25);
inverter.invert(str);
expectThrows(RuntimeException.class, () -> {
inverter.invert(str);
});
}
@Test
public final void lengthLessThan26_Succeed() {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() < 26);
inverter.invert(str);
assertTrue(true); // No exception thrown
}
/*
public static void main(String[] args) throws Exception{
assertEquals(args.length, 1);
inverter = (StringInverter)
Class.forName(args[0]).newInstance();
Result result = JUnitCore.runClasses(
Result result = org.junit.runner.JUnitCore.runClasses(
StringInverterTest.class);
List<Failure> failures = result.getFailures();
System.out.printf("%s has %d FAILURES:\n",
@ -61,4 +65,5 @@ public class StringInverterTest {
System.out.println(f.getMessage());
}
}
*/
}