测试两个StreamingOutput是等于

时间:2014-09-24 06:13:36

标签: java jax-rs

如何检查两个StreamingOutput的内容是否等于?

equal()比较对象引用。

谷歌似乎也不知道如何做到这一点,也许我的路径完全错误

@Test
    public void testStreamingDemo()
    {
        Stock stock = new Stock("SPY", "hello");

        StreamingOutputDemo instance = new StreamingOutputDemo();
        StreamingOutput expResult = new StreamingOutput()
        {

            @Override
            public void write(OutputStream output) throws IOException, WebApplicationException
            {
                PrintStream ps = new PrintStream(output);
                ps.println(stock.getName());
                ps.flush();
            }
        };

        StreamingOutput result = instance.streamingDemo(stock);

        assertTrue(expResult.equals(result));
    }

public class StreamingOutputDemo
{

    public StreamingOutput streamingDemo(Stock st) {

        return new StreamingOutput()
        {
            @Override
            public void write(OutputStream output) throws IOException, WebApplicationException
            {
                PrintStream pt = new PrintStream(output);
                pt.println(st.getName());
                pt.flush();
            }
        };
    }

}

1 个答案:

答案 0 :(得分:0)

每当我们使用OutputStream创建一个对象时,我们将获得每个流对象的新副本。因此,比较流对象没有意义。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class StreamComparsion {
public static void main(String[] args) {
    try {
            OutputStream outputStream = new FileOutputStream(new File("C:/Test.xml"));
            OutputStream outputStream1 = new FileOutputStream(new File("C:/Test.xml"));
            System.out.println(outputStream.equals(outputStream1)); // Your Expectation
            System.out.println(outputStream1.toString());
            System.out.println(outputStream.toString());
            System.out.println(outputStream1.hashCode());
            System.out.println(outputStream.hashCode());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

  }

}

输出:

假    java.io.FileOutputStream@6bbc4459    java.io.FileOutputStream@152b6651    1807500377    355165777