Demonstration of Design by Contract (DBC) combined with white-box unit testing : Unit Test « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » Unit Test 




Demonstration of Design by Contract (DBC) combined with white-box unit testing
Demonstration of Design by Contract (DBC) combined with white-box unit testing
 

// : c15:JavaQueue.java
// Demonstration of Design by Contract (DBC) combined
// with white-box unit testing.
// {Depends: junit.jar}
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import junit.framework.*;
import java.util.*;

public class JavaQueue {
  private Object[] data;

  private int in = 0// Next available storage space
      out = 0// Next gettable object

  // Has it wrapped around the circular queue?
  private boolean wrapped = false;

  public static class QueueException extends RuntimeException {
    public QueueException(String why) {
      super(why);
    }
  }

public JavaQueue(int size) {
    data = new Object[size];
    assert invariant()// Must be true after construction
  }  public boolean empty() {
    return !wrapped && in == out;
  }

  public boolean full() {
    return wrapped && in == out;
  }

public void put(Object item) {
    precondition(item != null, "put() null item");
    precondition(!full()"put() into full Queue");
    assert invariant();
    data[in++= item;
    if(in >= data.length) {
      in = 0;
      wrapped = true;
    }
    assert invariant();
  }public Object get() {
    precondition(!empty()"get() from empty Queue");
    assert invariant();
    Object returnVal = data[out];
    data[outnull;
    out++;
    if(out >= data.length) {
      out = 0;
      wrapped = false;
    }
    assert postcondition(
      returnVal != null, "Null item in Queue");
    assert invariant();
    return returnVal;
  }  // Design-by-contract support methods:
  private static void precondition(boolean cond, String msg) {
    if (!cond)
      throw new QueueException(msg);
  }

  private static boolean postcondition(boolean cond, String msg) {
    if (!cond)
      throw new QueueException(msg);
    return true;
  }

  private boolean invariant() {
    // Guarantee that no null values are in the
    // region of 'data' that holds objects:
    for (int i = out; i != in; i = (i + 1% data.length)
      if (data[i== null)
        throw new QueueException("null in queue");
    // Guarantee that only null values are outside the
    // region of 'data' that holds objects:
    if (full())
      return true;
    for (int i = in; i != out; i = (i + 1% data.length)
      if (data[i!= null)
        throw new QueueException("non-null outside of queue range: "
            + dump());
    return true;
  }

  private String dump() {
    return "in = " + in + ", out = " + out + ", full() = " + full()
        ", empty() = " + empty() ", queue = " + Arrays.asList(data);
  }

  // JUnit testing.
  // As an inner class, this has access to privates:
  public static class WhiteBoxTest extends TestCase {
    private JavaQueue queue = new JavaQueue(10);

    private int i = 0;

    public WhiteBoxTest(String name) {
      super(name);
      while (i < 5)
        // Preload with some data
        queue.put("" + i++);
    }

    // Support methods:
    private void showFullness() {
      assertTrue(queue.full());
      assertFalse(queue.empty());
      // Dump is private, white-box testing allows access:
      System.out.println(queue.dump());
    }

    private void showEmptiness() {
      assertFalse(queue.full());
      assertTrue(queue.empty());
      System.out.println(queue.dump());
    }

    public void testFull() {
      System.out.println("testFull");
      System.out.println(queue.dump());
      System.out.println(queue.get());
      System.out.println(queue.get());
      while (!queue.full())
        queue.put("" + i++);
      String msg = "";
      try {
        queue.put("");
      catch (QueueException e) {
        msg = e.getMessage();
        System.out.println(msg);
      }
      assertEquals(msg, "put() into full Queue");
      showFullness();
    }

    public void testEmpty() {
      System.out.println("testEmpty");
      while (!queue.empty())
        System.out.println(queue.get());
      String msg = "";
      try {
        queue.get();
      catch (QueueException e) {
        msg = e.getMessage();
        System.out.println(msg);
      }
      assertEquals(msg, "get() from empty Queue");
      showEmptiness();
    }

    public void testNullPut() {
      System.out.println("testNullPut");
      String msg = "";
      try {
        queue.put(null);
      catch (QueueException e) {
        msg = e.getMessage();
        System.out.println(msg);
      }
      assertEquals(msg, "put() null item");
    }

    public void testCircularity() {
      System.out.println("testCircularity");
      while (!queue.full())
        queue.put("" + i++);
      showFullness();
      // White-box testing accesses private field:
      assertTrue(queue.wrapped);
      while (!queue.empty())
        System.out.println(queue.get());
      showEmptiness();
      while (!queue.full())
        queue.put("" + i++);
      showFullness();
      while (!queue.empty())
        System.out.println(queue.get());
      showEmptiness();
    }
  }

  public static void main(String[] args) {
    junit.textui.TestRunner.run(JavaQueue.WhiteBoxTest.class);
  }
///:~


           
         
  














Related examples in the same category
1.Set JUnit Test case fail information
2.JUnit assertEquals: Float With Delta
3.Assert equals: int
4.Assert equals: Long
5.JUnit assertEquals With Message
6.JUnit assertTrue
7.JUnit assertTrue: ObjectArray
8.Before annotation
9.JUnit BeforeClass
10.JUnit Extends TestCase
11.JUnit Ignore
12.Simple test with JUnit
13.JUnit Test Case With Expected Exception
14.JUnit Test Setup
15.Simple use of JUnit to test ArrayListSimple use of JUnit to test ArrayList
16.Debug frameDebug frame
17.Error HandlerError Handler
18.Redirect or reassign some standard descriptors
19.Utilities for debugging
20.Testing class ClassTesting class Class
21.Simple utility for testing program outputSimple utility for testing program output
22.Assertion tool for debugging
23.Simple DebuggingSimple Debugging
24.Random data for test
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.