samples/delegatingwriterfinal/src-test2.0/api/usage/twodotzero/BufferedWriterOnCDImageTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:53:06 +0200
changeset 66 8379bb7c0dff
permissions -rw-r--r--
Tests rewritten to new version, just the Writer version 2.0 does not yet implement Appendable
jtulach@66
     1
jtulach@66
     2
package api.usage.twodotzero;
jtulach@66
     3
jtulach@66
     4
import api.Writer;
jtulach@66
     5
import java.io.IOException;
jtulach@66
     6
import java.util.concurrent.atomic.AtomicInteger;
jtulach@66
     7
import org.junit.Test;
jtulach@66
     8
import static org.junit.Assert.*;
jtulach@66
     9
jtulach@66
    10
/** Emulates what goes wrong when one wants to process really large character 
jtulach@66
    11
 * sequence.
jtulach@66
    12
 *
jtulach@66
    13
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@66
    14
 */
jtulach@66
    15
public class BufferedWriterOnCDImageTest {
jtulach@66
    16
    
jtulach@66
    17
    public BufferedWriterOnCDImageTest() {
jtulach@66
    18
    }
jtulach@66
    19
jtulach@66
    20
    @Test
jtulach@66
    21
    public void testBehaviourOfRealBufferInJDKObviouslyThisIsGoingToThrowOutOfMemoryError() throws IOException {
jtulach@66
    22
        AtomicInteger cnt = new AtomicInteger();
jtulach@66
    23
        Writer writer = CountingWriter.create(cnt);
jtulach@66
    24
        CDSequence cdImage = new CDSequence();
jtulach@66
    25
        Writer bufferedWriter = Writer.createBuffered(writer);
jtulach@66
    26
        bufferedWriter.append(cdImage);
jtulach@66
    27
        assertEquals("Correct number of writes delegated", cdImage.length(), cnt.get());
jtulach@66
    28
    }
jtulach@66
    29
jtulach@66
    30
jtulach@66
    31
    /** A "lazy" sequence of characters, for example one that can represent
jtulach@66
    32
     * content of a CD, read it lazily, do not fit all into memory at once.
jtulach@66
    33
     */
jtulach@66
    34
    private static final class CDSequence implements CharSequence {
jtulach@66
    35
        private final int start;
jtulach@66
    36
        private final int end;
jtulach@66
    37
        
jtulach@66
    38
        public CDSequence() {
jtulach@66
    39
            this(0, 647 * 1024 * 1024);
jtulach@66
    40
        }
jtulach@66
    41
jtulach@66
    42
        private CDSequence(int start, int end) {
jtulach@66
    43
            this.start = start;
jtulach@66
    44
            this.end = end;
jtulach@66
    45
        }
jtulach@66
    46
        
jtulach@66
    47
        
jtulach@66
    48
        public int length() {
jtulach@66
    49
            return end - start;
jtulach@66
    50
        }
jtulach@66
    51
jtulach@66
    52
        public char charAt(int index) {
jtulach@66
    53
            int ch = index % ('Z' - 'A' + 1);
jtulach@66
    54
            return (char) ('A' + ch);
jtulach@66
    55
        }
jtulach@66
    56
jtulach@66
    57
        public CharSequence subSequence(int start, int end) {
jtulach@66
    58
            return new CDSequence(start, end);
jtulach@66
    59
        }
jtulach@66
    60
jtulach@66
    61
        @Override
jtulach@66
    62
        public String toString() {
jtulach@66
    63
            char[] arr = new char[length()];
jtulach@66
    64
            for (int i = 0; i < length(); i++) {
jtulach@66
    65
                arr[i] = charAt(i);
jtulach@66
    66
            }
jtulach@66
    67
            return new String(arr);
jtulach@66
    68
        }
jtulach@66
    69
        
jtulach@66
    70
        
jtulach@66
    71
    } // end of CharSequence
jtulach@66
    72
}