samples/delegatingwriter/test/org/apidesign/delegatingwriter/BufferedWriterOnCDImageTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:53:03 +0200
changeset 61 59df94cee246
child 62 02ccc701dcbc
permissions -rw-r--r--
Demo for writer throwing an exception; and also writing out CD content
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 java.io.StringWriter;
jtulach@61
     7
import org.junit.Before;
jtulach@61
     8
import org.junit.Test;
jtulach@61
     9
import static org.junit.Assert.*;
jtulach@61
    10
jtulach@61
    11
/** Emulates what goes wrong when one wants to process really large character 
jtulach@61
    12
 * sequence.
jtulach@61
    13
 *
jtulach@61
    14
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@61
    15
 */
jtulach@61
    16
public class BufferedWriterOnCDImageTest {
jtulach@61
    17
    
jtulach@61
    18
    public BufferedWriterOnCDImageTest() {
jtulach@61
    19
    }
jtulach@61
    20
jtulach@61
    21
    @Test
jtulach@61
    22
    public void testBehaviourOfRealBufferInJDKObviouslyThisIsGoingToThrowOutOfMemoryException() throws IOException {
jtulach@61
    23
        // BEGIN: writer.countcd
jtulach@61
    24
        CountingWriter writer = new CountingWriter();
jtulach@61
    25
        CDSequence cdImage = new CDSequence();
jtulach@61
    26
        BufferedWriter bufferedWriter = new BufferedWriter(writer);
jtulach@61
    27
        bufferedWriter.append(cdImage);
jtulach@61
    28
        assertEquals("Correct number of writes delegated", cdImage.length(), writer.getCharacterCount());
jtulach@61
    29
        // END: writer.countcd
jtulach@61
    30
    }
jtulach@61
    31
jtulach@61
    32
    @Test
jtulach@61
    33
    public void testBehaviourOfBufferThatDelegatesToAppend() throws IOException {
jtulach@61
    34
        CountingWriter writer = new CountingWriter();
jtulach@61
    35
        CDSequence cdImage = new CDSequence();
jtulach@61
    36
        BufferedWriter bufferedWriter = new AltBufferedWriter(writer, AltBufferedWriter.Behaviour.DELEGATE_TO_OUT);
jtulach@61
    37
        bufferedWriter.append(cdImage);
jtulach@61
    38
        assertEquals("Correct number of writes delegated", cdImage.length(), writer.getCharacterCount());
jtulach@61
    39
    }
jtulach@61
    40
jtulach@61
    41
// BEGIN: writer.bigseq
jtulach@61
    42
    /** A "lazy" sequence of characters, for example one that can represent
jtulach@61
    43
     * content of a CD, read it lazily, do not fit all into memory at once.
jtulach@61
    44
     */
jtulach@61
    45
    private static final class CDSequence implements CharSequence {
jtulach@61
    46
        private final int start;
jtulach@61
    47
        private final int end;
jtulach@61
    48
        
jtulach@61
    49
        public CDSequence() {
jtulach@61
    50
            this(0, 647 * 1024 * 1024);
jtulach@61
    51
        }
jtulach@61
    52
jtulach@61
    53
        private CDSequence(int start, int end) {
jtulach@61
    54
            this.start = start;
jtulach@61
    55
            this.end = end;
jtulach@61
    56
        }
jtulach@61
    57
        
jtulach@61
    58
        
jtulach@61
    59
        public int length() {
jtulach@61
    60
            return end - start;
jtulach@61
    61
        }
jtulach@61
    62
// END: writer.bigseq
jtulach@61
    63
jtulach@61
    64
        public char charAt(int index) {
jtulach@61
    65
            int ch = index % ('Z' - 'A' + 1);
jtulach@61
    66
            return (char) ('A' + ch);
jtulach@61
    67
        }
jtulach@61
    68
jtulach@61
    69
        public CharSequence subSequence(int start, int end) {
jtulach@61
    70
            return new CDSequence(start, end);
jtulach@61
    71
        }
jtulach@61
    72
jtulach@61
    73
        @Override
jtulach@61
    74
        public String toString() {
jtulach@61
    75
            char[] arr = new char[length()];
jtulach@61
    76
            for (int i = 0; i < length(); i++) {
jtulach@61
    77
                arr[i] = charAt(i);
jtulach@61
    78
            }
jtulach@61
    79
            return new String(arr);
jtulach@61
    80
        }
jtulach@61
    81
        
jtulach@61
    82
        
jtulach@61
    83
    } // end of CharSequence
jtulach@61
    84
}