File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\test\java\jenkins\util\MarkFindingOutputStreamTest.java.html

1 package jenkins.util;
2
3 import junit.framework.TestCase;
4
5 import java.io.ByteArrayOutputStream;
6 import java.io.IOException;
7 import java.io.OutputStream;
8
9 /**
10 * @author Kohsuke Kawaguchi
11 */
12 public class MarkFindingOutputStreamTest extends TestCase {
13 String mark = MarkFindingOutputStream.MARK;
14 String markHead = mark.substring(0, 5);
15 String markTail = mark.substring(5);
16
17 ByteArrayOutputStream baos = new ByteArrayOutputStream();
18 MarkCountingOutputStream m = new MarkCountingOutputStream(baos);
19
20 public void testFindTwice() throws IOException {
21 write("foo"+mark+"bar"+mark);
22 assertCount(2);
23 assertOutput("foobar");
24 }
25
26 public void testPartialMatchTurnsOutToBeWrongIn2ndWrite() throws IOException {
27 write("bar"+markHead);
28 assertOutput("bar"); // at this point we should just see 'bar'
29
30 write("foo"); // this needs to write what was held off during the first write
31 assertCount(0);
32 assertOutput("bar"+markHead+"foo");
33 }
34
35 /**
36 * If a stream closes without completing a match, the partial match should be sent to the output.
37 */
38 public void testCloseInTheMiddle() throws IOException {
39 write("foo"+ markHead);
40 m.close();
41 assertCount(0);
42 assertOutput("foo"+ markHead);
43 }
44
45 public void testOneByOne() throws IOException {
46 m.write('1');
47 writeOneByOne(mark);
48 m.write('2');
49 assertCount(1);
50 assertOutput("12");
51 }
52
53 public void testWriteOneHoldOff() throws IOException {
54 writeOneByOne(markHead);
55 assertOutput("");
56 writeOneByOne("x");
57 assertOutput(markHead+"x");
58 assertCount(0);
59 }
60
61
62 private void assertOutput(String s) throws IOException {
63 assertEquals(s,baos.toString("UTF-8"));
64 }
65
66 private void assertCount(int n) {
67 assertEquals(n,m.count);
68 }
69
70 private void write(String s) throws IOException {
71 m.write(s.getBytes("UTF-8"));
72 }
73
74 private void writeOneByOne(String s) throws IOException {
75 for (int i=0; i< s.length(); i++)
76 m.write(s.charAt(i));
77 }
78
79 class MarkCountingOutputStream extends MarkFindingOutputStream {
80 int count = 0;
81
82 MarkCountingOutputStream(OutputStream base) {
83 super(base);
84 }
85
86 @Override
87 protected void onMarkFound() {
88 count++;
89 }
90 }
91 }
92