jtulach@61
|
1 |
|
jtulach@61
|
2 |
package org.apidesign.delegatingwriter;
|
jtulach@61
|
3 |
|
jtulach@61
|
4 |
import java.io.BufferedWriter;
|
jtulach@61
|
5 |
import java.io.IOException;
|
jtulach@61
|
6 |
import org.junit.Test;
|
jtulach@61
|
7 |
import static org.junit.Assert.*;
|
jtulach@61
|
8 |
|
jtulach@61
|
9 |
/** Emulates what goes wrong when one wants to process really large character
|
jtulach@61
|
10 |
* sequence.
|
jtulach@61
|
11 |
*
|
jtulach@61
|
12 |
* @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jtulach@61
|
13 |
*/
|
jtulach@61
|
14 |
public class BufferedWriterOnCDImageTest {
|
jtulach@61
|
15 |
|
jtulach@61
|
16 |
public BufferedWriterOnCDImageTest() {
|
jtulach@61
|
17 |
}
|
jtulach@61
|
18 |
|
jtulach@61
|
19 |
@Test
|
jtulach@63
|
20 |
public void testBehaviourOfRealBufferInJDKObviouslyThisIsGoingToThrowOutOfMemoryError() throws IOException {
|
jtulach@61
|
21 |
// BEGIN: writer.countcd
|
jtulach@61
|
22 |
CountingWriter writer = new CountingWriter();
|
jtulach@61
|
23 |
CDSequence cdImage = new CDSequence();
|
jtulach@61
|
24 |
BufferedWriter bufferedWriter = new BufferedWriter(writer);
|
jtulach@61
|
25 |
bufferedWriter.append(cdImage);
|
jtulach@132
|
26 |
assertEquals(
|
jtulach@132
|
27 |
"Correct number of writes delegated",
|
jtulach@132
|
28 |
cdImage.length(), writer.getCharacterCount()
|
jtulach@132
|
29 |
);
|
jtulach@61
|
30 |
// END: writer.countcd
|
jtulach@61
|
31 |
}
|
jtulach@61
|
32 |
|
jtulach@61
|
33 |
@Test
|
jtulach@61
|
34 |
public void testBehaviourOfBufferThatDelegatesToAppend() throws IOException {
|
jtulach@61
|
35 |
CountingWriter writer = new CountingWriter();
|
jtulach@61
|
36 |
CDSequence cdImage = new CDSequence();
|
jtulach@132
|
37 |
BufferedWriter bufferedWriter = new AltBufferedWriter(
|
jtulach@132
|
38 |
writer, AltBufferedWriter.Behaviour.DELEGATE_TO_OUT
|
jtulach@132
|
39 |
);
|
jtulach@61
|
40 |
bufferedWriter.append(cdImage);
|
jtulach@132
|
41 |
assertEquals(
|
jtulach@132
|
42 |
"Correct number of writes delegated",
|
jtulach@132
|
43 |
cdImage.length(),
|
jtulach@132
|
44 |
writer.getCharacterCount()
|
jtulach@132
|
45 |
);
|
jtulach@61
|
46 |
}
|
jtulach@64
|
47 |
|
jtulach@64
|
48 |
@Test
|
jtulach@64
|
49 |
public void testBehaviourWhenDelegatingConditionallyIsOK() throws IOException {
|
jtulach@64
|
50 |
CountingWriter writer = new CountingWriter();
|
jtulach@64
|
51 |
CDSequence cdImage = new CDSequence();
|
jtulach@132
|
52 |
BufferedWriter bufferedWriter = new AltBufferedWriter(
|
jtulach@132
|
53 |
writer, AltBufferedWriter.Behaviour.DELEGATE_CONDITIONALLY
|
jtulach@132
|
54 |
);
|
jtulach@64
|
55 |
bufferedWriter.append(cdImage);
|
jtulach@132
|
56 |
assertEquals(
|
jtulach@132
|
57 |
"Correct number of writes delegated",
|
jtulach@132
|
58 |
cdImage.length(),
|
jtulach@132
|
59 |
writer.getCharacterCount()
|
jtulach@132
|
60 |
);
|
jtulach@64
|
61 |
}
|
jtulach@64
|
62 |
|
jtulach@61
|
63 |
|
jtulach@61
|
64 |
// BEGIN: writer.bigseq
|
jtulach@61
|
65 |
/** A "lazy" sequence of characters, for example one that can represent
|
jtulach@61
|
66 |
* content of a CD, read it lazily, do not fit all into memory at once.
|
jtulach@61
|
67 |
*/
|
jtulach@61
|
68 |
private static final class CDSequence implements CharSequence {
|
jtulach@61
|
69 |
private final int start;
|
jtulach@61
|
70 |
private final int end;
|
jtulach@61
|
71 |
|
jtulach@61
|
72 |
public CDSequence() {
|
jtulach@61
|
73 |
this(0, 647 * 1024 * 1024);
|
jtulach@61
|
74 |
}
|
jtulach@61
|
75 |
|
jtulach@61
|
76 |
private CDSequence(int start, int end) {
|
jtulach@61
|
77 |
this.start = start;
|
jtulach@61
|
78 |
this.end = end;
|
jtulach@61
|
79 |
}
|
jtulach@61
|
80 |
|
jtulach@61
|
81 |
|
jtulach@61
|
82 |
public int length() {
|
jtulach@61
|
83 |
return end - start;
|
jtulach@61
|
84 |
}
|
jtulach@61
|
85 |
// END: writer.bigseq
|
jtulach@61
|
86 |
|
jtulach@61
|
87 |
public char charAt(int index) {
|
jtulach@61
|
88 |
int ch = index % ('Z' - 'A' + 1);
|
jtulach@61
|
89 |
return (char) ('A' + ch);
|
jtulach@61
|
90 |
}
|
jtulach@61
|
91 |
|
jtulach@61
|
92 |
public CharSequence subSequence(int start, int end) {
|
jtulach@61
|
93 |
return new CDSequence(start, end);
|
jtulach@61
|
94 |
}
|
jtulach@61
|
95 |
|
jtulach@61
|
96 |
@Override
|
jtulach@61
|
97 |
public String toString() {
|
jtulach@61
|
98 |
char[] arr = new char[length()];
|
jtulach@61
|
99 |
for (int i = 0; i < length(); i++) {
|
jtulach@61
|
100 |
arr[i] = charAt(i);
|
jtulach@61
|
101 |
}
|
jtulach@61
|
102 |
return new String(arr);
|
jtulach@61
|
103 |
}
|
jtulach@61
|
104 |
|
jtulach@61
|
105 |
|
jtulach@61
|
106 |
} // end of CharSequence
|
jtulach@61
|
107 |
} |