aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/io/reader/NamedReaderTestCase.java
blob: 671b8a0fa9bd4d324c6f19014f3e22cba2636316 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io.reader;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Method;
import java.nio.CharBuffer;
import java.util.Collections;
import java.util.List;

import com.yahoo.protect.ClassValidator;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * Tests all method of NamedReader.
 *
 * @author bratseth
 * @author Steinar Knutsen
 */
public class NamedReaderTestCase {

    @Test
    public void testIt() {
        StringReader stringReader=new StringReader("hello world");
        NamedReader r=new NamedReader("test1",stringReader);
        assertEquals("test1",r.getName());
        assertEquals("test1",r.toString());
        assertEquals(stringReader,r.getReader());
        NamedReader.closeAll(Collections.singletonList(r));
        NamedReader.closeAll(null); // noop, nor exception
    }

    @Test
    public void testMethodMasking() {
        List<Method> unmaskedMethods = ClassValidator.unmaskedMethodsFromSuperclass(NamedReader.class);
        if (! unmaskedMethods.isEmpty()) {
            fail("Unmasked methods in " + NamedReader.class.getName() + ": " + unmaskedMethods);
        }
    }

    private static class MarkerReader extends Reader {
        static final String READ_CHAR_BUFFER = "com.yahoo.io.reader.NamedReaderTestCase.MarkerReader.read(CharBuffer)";
        static final String READ = "com.yahoo.io.reader.NamedReaderTestCase.MarkerReader.read()";
        static final String READ_CHAR = "com.yahoo.io.reader.NamedReaderTestCase.MarkerReader.read(char[])";
        static final String READ_CHAR_INT_INT = "com.yahoo.io.reader.NamedReaderTestCase.MarkerReader.read(char[], int, int)";
        static final String SKIP_LONG = "com.yahoo.io.reader.NamedReaderTestCase.MarkerReader.skip(long)";
        static final String READY = "com.yahoo.io.reader.NamedReaderTestCase.MarkerReader.ready()";
        static final String MARK_SUPPORTED = "com.yahoo.io.reader.NamedReaderTestCase.MarkerReader.markSupported()";
        static final String MARK_INT = "com.yahoo.io.reader.NamedReaderTestCase.MarkerReader.mark(int)";
        static final String RESET = "com.yahoo.io.reader.NamedReaderTestCase.MarkerReader.reset()";
        static final String CLOSE = "com.yahoo.io.reader.NamedReaderTestCase.MarkerReader.close()";
        String lastMethodHit = null;

        @Override
        public int read(CharBuffer target) throws IOException {
            lastMethodHit = READ_CHAR_BUFFER;
            return 0;
        }

        @Override
        public int read() throws IOException {
            lastMethodHit = READ;
            return -1;
        }

        @Override
        public int read(char[] cbuf) throws IOException {
            lastMethodHit = READ_CHAR;
            return 0;
        }

        @Override
        public int read(char[] cbuf, int off, int len) throws IOException {
            lastMethodHit = READ_CHAR_INT_INT;
            return 0;
        }

        @Override
        public long skip(long n) throws IOException {
            lastMethodHit = SKIP_LONG;
            return 0;
        }

        @Override
        public boolean ready() throws IOException {
            lastMethodHit = READY;
            return false;
        }

        @Override
        public boolean markSupported() {
            lastMethodHit = MARK_SUPPORTED;
            return false;
        }

        @Override
        public void mark(int readAheadLimit) throws IOException {
            lastMethodHit = MARK_INT;
        }

        @Override
        public void reset() throws IOException {
            lastMethodHit = RESET;
        }

        @Override
        public void close() throws IOException {
            lastMethodHit = CLOSE;
        }
    }

    @Test
    public void testAllDelegators() throws IOException {
        MarkerReader m = new MarkerReader();
        NamedReader r = new NamedReader("nalle", m);
        r.read(CharBuffer.allocate(5000));
        assertEquals(MarkerReader.READ_CHAR_BUFFER, m.lastMethodHit);
        r.read();
        assertEquals(MarkerReader.READ, m.lastMethodHit);
        r.read(new char[5]);
        assertEquals(MarkerReader.READ_CHAR, m.lastMethodHit);
        r.read(new char[5], 0, 5);
        assertEquals(MarkerReader.READ_CHAR_INT_INT, m.lastMethodHit);
        r.skip(5L);
        assertEquals(MarkerReader.SKIP_LONG, m.lastMethodHit);
        r.ready();
        assertEquals(MarkerReader.READY, m.lastMethodHit);
        r.markSupported();
        assertEquals(MarkerReader.MARK_SUPPORTED, m.lastMethodHit);
        r.mark(5);
        assertEquals(MarkerReader.MARK_INT, m.lastMethodHit);
        r.reset();
        assertEquals(MarkerReader.RESET, m.lastMethodHit);
        r.close();
        assertEquals(MarkerReader.CLOSE, m.lastMethodHit);
    }

}