carfield.com.hk BlackBoxTest.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c02:test:BlackBoxTest.java import c02.testable.*; import com.bruceeckel.test.*; public class BlackBoxTest extends UnitTest { Testable tst = new Testable(); public void test1() { //! tst.f2(); // Nope! //! tst.f3(); // Nope! tst.f4(); // Only public methods available } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z TestDemo.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c02:TestDemo.java // Creating a test import com.bruceeckel.test.*; public class TestDemo { private static int objCounter = 0; private int id = ++objCounter; public TestDemo(String s) { System.out.println(s + &quot;: count = &quot; + id); } public void close() { System.out.println(&quot;Cleaning up: &quot; + id); } public boolean someCondition() { return true; } public static class Test extends UnitTest { TestDemo test1 = new TestDemo(&quot;test1&quot;); TestDemo test2 = new TestDemo(&quot;test2&quot;); public void cleanup() { test2.close(); test1.close(); } public void testA() { System.out.println(&quot;TestDemo.testA&quot;); affirm(test1.someCondition()); } public void testB() { System.out.println(&quot;TestDemo.testB&quot;); affirm(test2.someCondition()); affirm(TestDemo.objCounter != 0); } // Causes the build to halt: //! public void test3() { affirm(false); } } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z TestDemo2.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c02:TestDemo2.java // Inheriting from a class that // already has a test is no problem. import com.bruceeckel.test.*; public class TestDemo2 extends TestDemo { public TestDemo2(String s) { super(s); } // You can even use the same name // as the test class in the base class: public static class Test extends UnitTest { public void testA() { System.out.println(&quot;TestDemo2.testA&quot;); affirm(1 + 1 == 2); } public void testB() { System.out.println(&quot;TestDemo2.testB&quot;); affirm(2 * 2 == 4); } } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z Testable.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c02:testable:Testable.java package c02.testable; public class Testable { private void f1() {} void f2() {} // &quot;Friendly&quot;: package access protected void f3() {} // Also package access public void f4() {} } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z TooMuchAccess.java 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: c02:TooMuchAccess.java import com.bruceeckel.test.*; public class TooMuchAccess extends UnitTest { Testable tst = new Testable(); public void test1() { tst.f2(); // Oops! tst.f3(); // Oops! tst.f4(); // OK } } ///:~ </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z makefile 2001-12-26T16:00:00Z 2001-12-26T16:00:00Z <br/><TEXTAREA name="code" class="" rows="16" cols="100"># From Thinking in Patterns (with Java) by Bruce Eckel # At http://www.BruceEckel.com # (c)2001 Bruce Eckel # Copyright notice in Copyright.txt # Automatically-generated MAKEFILE # For examples in directory .\c02\testable # using the JDK 1.3 compiler # Invoke with: make HOME := ../../ ifndef MAKECMDGOALS MAKECMDGOALS := javac endif # Command.com is too weak to build this under Windows NT/2000: ifeq ($(OS),Windows_NT) COMSPEC=$(SYSTEMROOT)\system32\cmd.exe endif ifneq ($(MAKECMDGOALS),clean) include $(HOME)/$(MAKECMDGOALS).mac endif .SUFFIXES : .class .java .java.class : $(JVC) $(JVCFLAGS) $&lt; javac: \ Testable.class jikes: \ Testable.class clean: ifeq ($(notdir $(SHELL)),COMMAND.COM) del *.class else rm -f *.class endif Testable.class: Testable.java </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2001-12-26T16:00:00Z