⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij317.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font>
  verifyIgnoreOrder(List output, Object[] expected) {
    verifyLength(expected.length,output.size(),Test.EXACT);
    <font color=#0000ff>if</font>(!(expected <font color=#0000ff>instanceof</font> String[]))
      <font color=#0000ff>throw</font> <font color=#0000ff>new</font> RuntimeException(
        <font color=#004488>"IGNORE_ORDER only works with String objects"</font>);
    String[] out = <font color=#0000ff>new</font> String[output.size()];
    Iterator it = output.iterator();
    <font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i &lt; out.length; i++)
      out[i] = it.next().toString();
    Arrays.sort(out);
    Arrays.sort(expected);
    <font color=#0000ff>int</font> i =0;
    <font color=#0000ff>if</font>(!Arrays.equals(expected, out)) {
      <font color=#0000ff>while</font>(expected[i].equals(out[i])) {i++;}
      <font color=#0000ff>throw</font> <font color=#0000ff>new</font> SimpleTestException(
        ((String) out[i]).compareTo(expected[i]) &lt; 0
          ? <font color=#004488>"output: &lt;"</font> + out[i] + <font color=#004488>"&gt;"</font>
          : <font color=#004488>"expected: &lt;"</font> + expected[i] + <font color=#004488>"&gt;"</font>);
    }
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font>
  verifyAtLeast(List output, List expected) {
    verifyLength(output.size(), expected.size(),
      Test.AT_LEAST);
    <font color=#0000ff>if</font>(!output.containsAll(expected)) {
      ListIterator it = expected.listIterator();
      <font color=#0000ff>while</font>(output.contains(it.next())) {}
      <font color=#0000ff>throw</font> <font color=#0000ff>new</font> SimpleTestException(
        <font color=#004488>"expected: &lt;"</font> + it.previous().toString() + <font color=#004488>"&gt;"</font>);
    }
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The &#147;verify&#148; methods test whether the output produced by a program matches the expected output as specified by the particular mode. If this is not the case, the &#147;verify&#148; methods raise an exception that aborts the build process. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0647" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Each of the &#147;verify&#148; methods uses <b>verifyLength(&#160;) </b>to test the number of lines of output. <b>EXACT</b> mode requires that both output and expected output arrays be the same size, and that each output line is equal to the corresponding line in the expected output array. <b>IGNORE_ORDER</b> still requires that both arrays be the same size, but the actual order of appearance of the lines is disregarded (the two output arrays must be permutations of one another). <b>IGNORE_ORDER</b> mode is used to test threading examples where, due to non-deterministic scheduling of threads by the JVM, it is possible that the sequence of output lines produced by a program cannot be predicted. <b>AT_LEAST</b> mode does not require the two arrays to be the same size, but each line of expected output must be contained in the actual output produced by a program, regardless of order. This feature is particularly useful for testing program examples that contain output lines that may or may not be printed, as is the case with most of the examples dealing with garbage collection. Notice that the three modes are canonical; that is, if a test passes in <b>IGNORE_ORDER</b> mode, then it will also pass in <b>AT_LEAST</b> mode, and if it passes in <b>EXACT</b> mode, it will also pass in the other two modes. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0010" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Notice how simple the implementation of the &#147;verify&#148; methods is. <b>verify(&#160;)</b>, for example, simply calls the <b>equals(&#160;)</b> method provided by the <b>List</b> class, and <b>verifyAtLeast(&#160;)</b> calls <b>List.containsAll(&#160;)</b>. Remember that the two output <b>List</b>s can contain both <b>OutputLine</b> or <b>RegularExpression</b> objects. The reason for wrapping the simple <b>String</b> object in <b>OutputLines</b> should now become clear; this approach allows us to override the <b>equals(&#160;)</b> method, which is necessary in order to take advantage of the Java <b>Collections</b> API. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0207" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Objects in the <b>expect(&#160;) </b>array can be either <b>Strings</b> or <b>TestExpression</b>s, which can encapsulate a regular expression (described in Chapter 12), which is useful for testing examples that produce random output. The <b>TestExpression</b> class encapsulates a <b>String</b> representing a particular regular expression. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0007" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: com:bruceeckel:simpletest:TestExpression.java</font>
<font color=#009900>// Regular expression for testing program output lines</font>
<font color=#0000ff>package</font> com.bruceeckel.simpletest;
<font color=#0000ff>import</font> java.util.regex.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> TestExpression <font color=#0000ff>implements</font> Comparable {
  <font color=#0000ff>private</font> Pattern p;
  <font color=#0000ff>private</font> String expression;
  <font color=#0000ff>private</font> <font color=#0000ff>boolean</font> isRegEx;
  <font color=#009900>// Default to only one instance of this expression:</font>
  <font color=#0000ff>private</font> <font color=#0000ff>int</font> duplicates = 1;
  <font color=#0000ff>public</font> TestExpression(String s) {
    <font color=#0000ff>this</font>.expression = s;
    <font color=#0000ff>if</font>(expression.startsWith(<font color=#004488>"%% "</font>)) {
      <font color=#0000ff>this</font>.isRegEx = <font color=#0000ff>true</font>;
      expression = expression.substring(3);
      <font color=#0000ff>this</font>.p = Pattern.compile(expression);
    }
  }
  <font color=#009900>// For duplicate instances:</font>
  <font color=#0000ff>public</font> TestExpression(String s, <font color=#0000ff>int</font> duplicates) {
    <font color=#0000ff>this</font>(s);
    <font color=#0000ff>this</font>.duplicates = duplicates;
  }
  <font color=#0000ff>public</font> String toString() {
    <font color=#0000ff>if</font>(isRegEx) <font color=#0000ff>return</font> p.pattern();
    <font color=#0000ff>return</font> expression;
  }
  <font color=#0000ff>public</font> <font color=#0000ff>boolean</font> equals(Object obj) {
    <font color=#0000ff>if</font>(<font color=#0000ff>this</font> == obj) <font color=#0000ff>return</font> <font color=#0000ff>true</font>;
    <font color=#0000ff>if</font>(isRegEx) <font color=#0000ff>return</font> (compareTo(obj) == 0);
    <font color=#0000ff>return</font> expression.equals(obj.toString());
  }
  <font color=#0000ff>public</font> <font color=#0000ff>int</font> compareTo(Object obj) {
    <font color=#0000ff>if</font>((isRegEx) &amp;&amp; (p.matcher(obj.toString()).matches()))
      <font color=#0000ff>return</font> 0;
    <font color=#0000ff>return</font>
      expression.compareTo(obj.toString());
  }
  <font color=#0000ff>public</font> <font color=#0000ff>int</font> getNumber() {  <font color=#0000ff>return</font> duplicates; }
  <font color=#0000ff>public</font> String getExpression() { <font color=#0000ff>return</font> expression;}
  <font color=#0000ff>public</font> <font color=#0000ff>boolean</font> isRegEx() { <font color=#0000ff>return</font> isRegEx; }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p><b>TestExpression </b>can distinguish regular expression patterns from <b>String</b> literals. The second constructor allows multiple identical expression lines to be wrapped in a single object for convenience. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0189" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>This test system has been reasonably useful, and the exercise of creating it and putting it into use has been invaluable. However, in the end I&#146;m not that pleased with it and have ideas that will probably be implemented in the next edition of the book (or possibly sooner). <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0646" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc24775934"></a><a name="Heading22680"></a>JUnit</h3>
<p>Although the testing framework just described allows you to verify program output simply and easily, in some cases you may want to perform more extensive functionality testing on a program. <a name="Index2018"></a><i>JUnit</i>, available at <i>www.junit.org</i>, is a quickly emerging standard for writing repeatable tests for Java programs, and provides both simple and complex testing. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0191" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The original JUnit was presumably based on JDK 1.0 and thus could not make use of Java&#146;s reflection facilities. As a result, writing unit tests with the old JUnit was a rather busy and wordy activity, and I found the design to be unpleasant. Because of this, I wrote my own unit testing framework for Java,<sup><a name="fnB90" href="#fn90">[90]</a></sup> going to the other extreme and &#147;doing the simplest thing that could possibly work.&#148;<sup><a name="fnB91" href="#fn91">[91]</a></sup> Since then, JUnit has been modified and uses reflection to greatly simplify the process of writing unit test code. Although you still have the option of writing code the &#147;old&#148; way with test suites and all the other complex details, I believe that in the great majority of cases you can follow the simple approach shown here (and make your life more pleasant). <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0195" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>In the simplest approach to using JUnit, you put all your tests in a subclass of <b>TestCase</b>. Each test must be <b>public</b>, take no arguments, return <b>void</b>, and have a method name beginning with the word &#147;test.&#148; Junit&#146;s reflection will identify these methods as individual tests and set up and run them one at a time, taking measures to avoid side effects between the tests. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0194" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Traditionally, the <a name="Index2019"></a><b>setUp(&#160;)</b> method creates and initializes a common set of objects that will be used in all the tests; however, you can also just put all such initialization in the constructor for the test class. JUnit creates an object for each test to ensure there will be no side effects between test runs. However, all the objects for all the tests are created at once (rather than creating the object right before the test), so the only difference between using <b>setUp(&#160;) </b>and the constructor is that <b>setUp(&#160;) </b>is called directly before the test. In most situations this will not be an issue, and you can use the constructor approach for simplicity. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0192" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>If you need to perform any cleanup after each test (if you modify any statics that need to be restored, open files that need to be closed, open network connections, etc.), you write a <a name="Index2020"></a><b>tearDown(&#160;)</b> method. This is also optional. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0011" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The following example uses this simple approach to create JUnit tests that exercise the standard Java <a name="Index2021"></a><b>ArrayList</b> class. To trace how JUnit creates and cleans up its test objects, <b>CountedList</b> is inherited from <b>ArrayList</b> and tracking information is added: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0196" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c15:JUnitDemo.java</font>
<font color=#009900>// Simple use of JUnit to test ArrayList</font>
<font color=#009900>// {Depends: junit.jar}</font>
<font color=#0000ff>import</font> java.util.*;
<font color=#0000ff>import</font> junit.framework.*;

<font color=#009900>// So we can see the list objects being created,</font>
<font color=#009900>// and keep track of when they are cleaned up:</font>
<font color=#0000ff>class</font> CountedList <font color=#0000ff>extends</font> ArrayList {
  <font color=#0000ff>private</font> <font color=#0000ff>static</font> <font color=#0000ff>int</font> counter = 0;
  <font color=#0000ff>private</font> <font color=#0000ff>int</font> id = counter++;
  <font color=#0000ff>public</font> CountedList() {
    System.out.println(<font color=#004488>"CountedList #"</font> + id);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>int</font> getId() { <font color=#0000ff>return</font> id; }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> JUnitDemo <font color=#0000ff>extends</font> TestCase {
  <font color=#0000ff>private</font> <font color=#0000ff>static</font> com.bruceeckel.simpletest.Test monitor =
    <font color=#0000ff>new</font> com.bruceeckel.simpletest.Test();
  <font color=#0000ff>private</font> CountedList list = <font color=#0000ff>new</font> CountedList();
  <font color=#009900>// You can use the constructor instead of setUp():</font>
  <font color=#0000ff>public</font> JUnitDemo(String name) {
    <font color=#0000ff>super</font>(name);
    <font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i &lt; 3; i++)
      list.add(<font color=#004488>""</font> + i);
  }
  <font color=#009900>// Thus, setUp() is optional, but is run right</font>
  <font color=#009900>// before the test:</font>
  <font color=#0000ff>protected</font> <font color=#0000ff>void</font> setUp() {
    System.out.println(<font color=#004488>"Set up for "</font> + list.getId());
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -