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

📄 tij317.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<font color=#009900>// Simple utility for testing program output. Intercepts</font>
<font color=#009900>// System.out to print both to the console and a buffer.</font>
<font color=#0000ff>package</font> com.bruceeckel.simpletest;
<font color=#0000ff>import</font> java.io.*;
<font color=#0000ff>import</font> java.util.*;
<font color=#0000ff>import</font> java.util.regex.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Test {
  <font color=#009900>// Bit-shifted so they can be added together:</font>
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>final</font> <font color=#0000ff>int</font>
    EXACT = 1 &lt;&lt; 0, <font color=#009900>// Lines must match exactly</font>
    AT_LEAST = 1 &lt;&lt; 1, <font color=#009900>// Must be at least these lines</font>
    IGNORE_ORDER = 1 &lt;&lt; 2, <font color=#009900>// Ignore line order</font>
    WAIT = 1 &lt;&lt; 3; <font color=#009900>// Delay until all lines are output</font>
  <font color=#0000ff>private</font> String className;
  <font color=#0000ff>private</font> TestStream testStream;
  <font color=#0000ff>public</font> Test() {
    <font color=#009900>// Discover the name of the class this</font>
    <font color=#009900>// object was created within:</font>
    className =
      <font color=#0000ff>new</font> Throwable().getStackTrace()[1].getClassName();
    testStream = <font color=#0000ff>new</font> TestStream(className);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> List fileToList(String fname) {
    ArrayList list = <font color=#0000ff>new</font> ArrayList();
    <font color=#0000ff>try</font> {
      BufferedReader in =
        <font color=#0000ff>new</font> BufferedReader(<font color=#0000ff>new</font> FileReader(fname));
      <font color=#0000ff>try</font> {
        String line;
        <font color=#0000ff>while</font>((line = in.readLine()) != <font color=#0000ff>null</font>) {
          <font color=#0000ff>if</font>(fname.endsWith(<font color=#004488>".txt"</font>))
            list.add(line);
          <font color=#0000ff>else</font>
            list.add(<font color=#0000ff>new</font> TestExpression(line));
        }
      } <font color=#0000ff>finally</font> {
        in.close();
      }
    } <font color=#0000ff>catch</font> (IOException e) {
      <font color=#0000ff>throw</font> <font color=#0000ff>new</font> RuntimeException(e);
    }
    <font color=#0000ff>return</font> list;
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> List arrayToList(Object[] array) {
    List l = <font color=#0000ff>new</font> ArrayList();
    <font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i &lt; array.length; i++) {
      <font color=#0000ff>if</font>(array[i] <font color=#0000ff>instanceof</font> TestExpression) {
        TestExpression re = (TestExpression)array[i];
        <font color=#0000ff>for</font>(<font color=#0000ff>int</font> j = 0; j &lt; re.getNumber(); j++)
          l.add(re);
      } <font color=#0000ff>else</font> {
        l.add(<font color=#0000ff>new</font> TestExpression(array[i].toString()));
      }
    }
    <font color=#0000ff>return</font> l;
  }
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> expect(Object[] exp, <font color=#0000ff>int</font> flags) {
    <font color=#0000ff>if</font>((flags &amp; WAIT) != 0)
      <font color=#0000ff>while</font>(testStream.numOfLines &lt; exp.length) {
        <font color=#0000ff>try</font> {
          Thread.sleep(1000);
        } <font color=#0000ff>catch</font> (InterruptedException e) {
          <font color=#0000ff>throw</font> <font color=#0000ff>new</font> RuntimeException(e);
        }
      }
      List output = fileToList(className + <font color=#004488>"Output.txt"</font>);
      <font color=#0000ff>if</font>((flags &amp; IGNORE_ORDER) == IGNORE_ORDER)
        OutputVerifier.verifyIgnoreOrder(output, exp);
      <font color=#0000ff>else</font> <font color=#0000ff>if</font>((flags &amp; AT_LEAST) == AT_LEAST)
        OutputVerifier.verifyAtLeast(output,
          arrayToList(exp));
      <font color=#0000ff>else</font>
        OutputVerifier.verify(output, arrayToList(exp));
    <font color=#009900>// Clean up the output file - see c06:Detergent.java</font>
    testStream.openOutputFile();
  }
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> expect(Object[] expected) {
    expect(expected, EXACT);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> expect(Object[] expectFirst,
    String fname, <font color=#0000ff>int</font> flags) {
    List expected = fileToList(fname);
    <font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i &lt; expectFirst.length; i++)
      expected.add(i, expectFirst[i]);
    expect(expected.toArray(), flags);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> expect(Object[] expectFirst, String fname) {
    expect(expectFirst, fname, EXACT);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>void</font> expect(String fname) {
    expect(<font color=#0000ff>new</font> Object[] {}, fname, EXACT);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>There are several overloaded versions of <b>expect(&#160;)</b> provided for convenience (so the client programmer can, for example, provide the name of the file containing the expected output instead of an array of expected output lines). These overloaded methods all call the main <b>expect(&#160;)</b> method, which takes as arguments an array of <b>Objects</b> containing expected output lines and an <b>int</b> containing various flags. Flags are implemented using bit shifting, with each bit corresponding to a particular flag as defined at the beginning of <b>Test.java</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0206" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The <b>expect(&#160;)</b> method first inspects the <b>flags</b> argument to see if it should delay processing to allow a slow program to catch up. It then calls a <b>static</b> method <b>fileToList(&#160;)</b>, which converts the contents of the output file produced by a program into a <b>List</b>. The <b>fileToList(&#160;)</b> method also wraps each <b>String</b> object in an <b>OutputLine</b> object; the reason for this will become clear. Finally, the <b>expect(&#160;)</b> method calls the appropriate <b>verify(&#160;)</b> method based on the flags argument. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0645" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>There are three verifiers: <b>verify(&#160;)</b>, <b>verifyIgnoreOrder(&#160;)</b>, and <b>verifyAtLeast(&#160;)</b>, corresponding to <b>EXACT</b>, <b>IGNORE_ORDER</b>, and <b>AT_LEAST</b> modes, respectively:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: com:bruceeckel:simpletest:OutputVerifier.java</font>
<font color=#0000ff>package</font> com.bruceeckel.simpletest;
<font color=#0000ff>import</font> java.util.*;
<font color=#0000ff>import</font> java.io.PrintStream;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> OutputVerifier {
  <font color=#0000ff>private</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> verifyLength(
    <font color=#0000ff>int</font> output, <font color=#0000ff>int</font> expected, <font color=#0000ff>int</font> compare) {
    <font color=#0000ff>if</font>((compare == Test.EXACT &amp;&amp; expected != output)
      || (compare == Test.AT_LEAST &amp;&amp; output &lt; expected))
      <font color=#0000ff>throw</font> <font color=#0000ff>new</font> NumOfLinesException(expected, output);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> verify(List output, List expected) {
    verifyLength(output.size(),expected.size(),Test.EXACT);
    <font color=#0000ff>if</font>(!expected.equals(output)) {
      <font color=#009900>//find the line of mismatch</font>
      ListIterator it1 = expected.listIterator();
      ListIterator it2 = output.listIterator();
      <font color=#0000ff>while</font>(it1.hasNext()
        &amp;&amp; it2.hasNext()
        &amp;&amp; it1.next().equals(it2.next()));
      <font color=#0000ff>throw</font> <font color=#0000ff>new</font> LineMismatchException(
        it1.nextIndex(), it1.previous().toString(),
        it2.previous().toString());
    }
  }

⌨️ 快捷键说明

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