Testing and Debugging: CS 310 Lecture notes

Object-Oriented Programming with Java Lecture Notes

5 March 2009 • Testing and Debugging


Outline

Logging Example

$ cat u.java
import java.util.logging.Logger;

class u {
  public static void main(String args[]) {
    final Logger printLog = 
      Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
    printLog.info("In main()");
    }
  }

$ javac -Xlint u.java

$ java u
Mar 5, 2008 5:10:25 PM u main
INFO: In main()

$ 

Logging Classes

Logging Levels

Level Filtering

The Logger Manager

Class-Variable Example

class LoggerHome {
  public static final Logger logger;
  static {
    // initialize logger appropriately.
    }
  }

class Blob {
  int moo() {
    LoggerHome.logger.info("In Blob.moo()");
    }
  }

Constructor-Parameter Example

public static void main(String arg[]) {
  Logger logger = ...;
  Blob b = new Blob(logger);
  Glob g = new Glob(logger);
  }

Logger Manager

The Logger Name Space

Name-Space Access

Assertions

Enabling Assertions

Example

$ cat u.java
class u {
  public static void
  main(String args[]) {
    assert false;
    }
  }

$ javac u.java

$ java u

$ java -ea u
Exception in thread "main" java.lang.AssertionError
 at u.main(u.java:4)

$ 

Assertion Messages

Example

$ cat u.java
class u {
  public static void
  main(String args[]) {
    assert false : "boom!";
    }
  }

$ javac u.java

$ java -ea u
Exception in thread "main" 
        java.lang.AssertionError: boom!
        at u.main(u.java:4)

$ 

Using Assertions

Unreliable Assertions


This page last modified on 6 March 2008.

Creative
    Commons License