Chinaunix

标题: TIJ 3中的OutputVerifier.java和Test.java问题 [打印本页]

作者: nec-tokin    时间: 2008-06-27 14:32
标题: TIJ 3中的OutputVerifier.java和Test.java问题
编译Test.java时的错误信息:
D:\java\com\bruceeckel\simpletest>javac Test.java
Test.java:64: cannot resolve symbol
symbol  : variable OutputVerifier
location: class com.bruceeckel.simpletest.Test
        OutputVerifier.verifyIgnoreOrder(output, exp);
        ^
Test.java:66: cannot resolve symbol
symbol  : variable OutputVerifier
location: class com.bruceeckel.simpletest.Test
        OutputVerifier.verifyAtLeast(output,
        ^
Test.java:69: cannot resolve symbol
symbol  : variable OutputVerifier
location: class com.bruceeckel.simpletest.Test
        OutputVerifier.verify(output, arrayToList(exp));
        ^
3 errors:
编译OutputVerifier.java时的错误提示:
D:\java\com\bruceeckel\simpletest>javac OutputVerifier.java
OutputVerifier.java:11: cannot resolve symbol
symbol  : variable Test
location: class com.bruceeckel.simpletest.OutputVerifier
    if((compare == Test.EXACT && expected != output)
                   ^
OutputVerifier.java:12: cannot resolve symbol
symbol  : variable Test
location: class com.bruceeckel.simpletest.OutputVerifier
      || (compare == Test.AT_LEAST && output < expected))
                     ^
OutputVerifier.java:16: cannot resolve symbol
symbol  : variable Test
location: class com.bruceeckel.simpletest.OutputVerifier
    verifyLength(output.size(),expected.size(),Test.EXACT);
                                               ^
OutputVerifier.java:31: cannot resolve symbol
symbol  : variable Test
location: class com.bruceeckel.simpletest.OutputVerifier
    verifyLength(expected.length,output.size(),Test.EXACT);
                                               ^
OutputVerifier.java:53: cannot resolve symbol
symbol  : variable Test
location: class com.bruceeckel.simpletest.OutputVerifier
      Test.AT_LEAST);
      ^
5 errors
这两个原文件如下:
package com.bruceeckel.simpletest;
import java.util.*;
import java.io.PrintStream;

public class OutputVerifier {
  private static void verifyLength(
    int output, int expected, int compare) {
    if((compare == Test.EXACT && expected != output)
      || (compare == Test.AT_LEAST && output < expected))
      throw new NumOfLinesException(expected, output);
  }
  public static void verify(List output, List expected) {
    verifyLength(output.size(),expected.size(),Test.EXACT);
    if(!expected.equals(output)) {
      //find the line of mismatch
      ListIterator it1 = expected.listIterator();
      ListIterator it2 = output.listIterator();
      while(it1.hasNext()
        && it2.hasNext()
        && it1.next().equals(it2.next()));
      throw new LineMismatchException(
        it1.nextIndex(), it1.previous().toString(),
        it2.previous().toString());
    }
  }
  public static void
  verifyIgnoreOrder(List output, Object[] expected) {
    verifyLength(expected.length,output.size(),Test.EXACT);
    if(!(expected instanceof String[]))
      throw new RuntimeException(
        "IGNORE_ORDER only works with String objects");
    String[] out = new String[output.size()];
    Iterator it = output.iterator();
    for(int i = 0; i < out.length; i++)
      out = it.next().toString();
    Arrays.sort(out);
    Arrays.sort(expected);
    int i =0;
    if(!Arrays.equals(expected, out)) {
      while(expected.equals(out)) {i++;}
      throw new SimpleTestException(
        ((String) out).compareTo(expected) < 0
          ? "output: <" + out + ">"
          : "expected: <" + expected + ">");
    }
  }
  public static void
  verifyAtLeast(List output, List expected) {
    verifyLength(output.size(), expected.size(),
      Test.AT_LEAST);
    if(!output.containsAll(expected)) {
      ListIterator it = expected.listIterator();
      while(output.contains(it.next())) {}
      throw new SimpleTestException(
        "expected: <" + it.previous().toString() + ">");
    }
  }
}
下面这个是Test.java的代码:
import java.io.*;
import java.util.*;
import java.util.regex.*;

public class Test {
  public static final int
    EXACT = 1 << 0,
    AT_LEAST = 1 << 1,
    IGNORE_ORDER = 1 << 2,
    WAIT = 1 << 3;
  private String className;
  private TestStream testStream;
  public Test() {
    className =
      new Throwable().getStackTrace()[1].getClassName();
    testStream = new TestStream(className);
  }
  public static List fileToList(String fname) {
    ArrayList list = new ArrayList();
    try {
      BufferedReader in =
        new BufferedReader(new FileReader(fname));
      try {
        String line;
        while((line = in.readLine()) != null) {
          if(fname.endsWith(".txt"))
            list.add(line);
          else
            list.add(new TestExpression(line));
        }
      } finally {
        in.close();
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return list;
  }
  public static List arrayToList(Object[] array) {
    List l = new ArrayList();
    for(int i = 0; i < array.length; i++) {
      if(array instanceof TestExpression) {
        TestExpression re = (TestExpression)array;
        for(int j = 0; j < re.getNumber(); j++)
          l.add(re);
      } else {
        l.add(new TestExpression(array.toString()));
      }
    }
    return l;
  }
  public void expect(Object[] exp, int flags) {
    if((flags & WAIT) != 0)
      while(testStream.numOfLines < exp.length) {
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          throw new RuntimeException(e);
        }
      }
      List output = fileToList(className + "Output.txt");
      if((flags & IGNORE_ORDER) == IGNORE_ORDER)
        OutputVerifier.verifyIgnoreOrder(output, exp);
      else if((flags & AT_LEAST) == AT_LEAST)
        OutputVerifier.verifyAtLeast(output,
          arrayToList(exp));
      else
        OutputVerifier.verify(output, arrayToList(exp));
    testStream.openOutputFile();
  }
  public void expect(Object[] expected) {
    expect(expected, EXACT);
  }
  public void expect(Object[] expectFirst,
    String fname, int flags) {
    List expected = fileToList(fname);
    for(int i = 0; i < expectFirst.length; i++)
      expected.add(i, expectFirst);
    expect(expected.toArray(), flags);
  }
  public void expect(Object[] expectFirst, String fname) {
    expect(expectFirst, fname, EXACT);
  }
  public void expect(String fname) {
    expect(new Object[] {}, fname, EXACT);
  }
}
好了,其他的类都已经放在com/bruceeckel/simpleTest下面了.
帮我分析这个错误:不胜感激.
作者: nec-tokin    时间: 2008-06-27 15:54
如何没有一个人回复呢?非常郁闷
作者: kakasi    时间: 2008-06-27 15:59
classpath的问题,怎么compile的?
作者: nec-tokin    时间: 2008-06-27 16:35
classpath问题在哪里啊?说也不说清楚
作者: nec-tokin    时间: 2008-06-27 17:13
兄弟们哪,我已经解决了.我现在可以把它详细说明下了以供初学者学习Thanking in java:下载完com包后,在simpletest下面有两个文件test.java和OutputVerifier.java,无论如何编译都不行,想解决这个问题是:首先你在simpletest这个当前目录下建立一个文件夹起名com,然后再在com目录下建一个名为bruceeckel的文件夹,继续simpletest.好了.然后把所有能编译的java文件编译出来的class文件放入你新建立的simpletest下面,关于test.java和OutputVerifier.java的编译只要把test.java也复制到你新建立的simpletest文件夹下面.在编译OutputVerifier.java这个文件就可以了.说的有些罗嗦.大家耐心点看吧.反正学习这个java也需要有耐心.最后感谢大家能看我在这里瞎侃.
作者: nec-tokin    时间: 2008-06-27 21:43
拜托了,既然没有人支持下.本来想把这个压缩文件给给你们,既然这样就算了.有人看没有人支持那就算了,看来大家都知道了,而且都有这个包的文件.看来我是多此一举了.
作者: kakasi    时间: 2008-06-28 23:02
原帖由 nec-tokin 于 2008-6-27 17:35 发表
classpath问题在哪里啊?说也不说清楚


直接用javac和用ide,配置使用classpath的方法不同。
看了你侃侃而谈,真不知该说什么了。tij比较适合oo的入门,若是关于java的基础知识,我推荐java核心技术,名字叫不太准了,好多年了,就是上下两册的那个。
作者: nec-tokin    时间: 2008-06-29 09:23
斑竹谢谢你,我看过了核心技术可是我下的这两本书没有高清版,应该是扫描的.很类.上面好多的扫描的不清楚.如果斑竹有高清的能否共享出来.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2