View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import java.io.*;
22  
23  import org.apache.juneau.parser.*;
24  import org.junit.jupiter.api.*;
25  
26  class ParserReader_Test extends TestBase {
27  
28  	//====================================================================================================
29  	// testBasic
30  	//====================================================================================================
31  	@Test void a01_basic() throws Exception {
32  		var t = "01234567890123456789012345678901234567890123456789";
33  
34  		// Min buff size is 20.
35  		var pr = createParserReader(t);
36  		var r = read(pr);
37  		assertEquals(t, r);
38  		pr.close();
39  
40  		pr = createParserReader(t);
41  		pr.read();
42  		pr.unread();
43  		r = read(pr);
44  		assertEquals(t, r);
45  		pr.close();
46  
47  		pr = createParserReader(t);
48  		assertEquals('0', (char)pr.peek());
49  		assertEquals('0', (char)pr.peek());
50  		r = read(pr);
51  		assertEquals(t, r);
52  
53  		var pr2 = createParserReader(t);
54  		pr2.read();
55  		pr2.unread();
56  		assertThrows(IOException.class, pr2::unread);
57  	}
58  
59  	//====================================================================================================
60  	// testMarking
61  	//====================================================================================================
62  	@Test void a02_marking() throws Exception {
63  		var t = "a123456789b123456789c123456789d123456789e123456789f123456789g123456789h123456789i123456789j123456789";
64  		var r = (Object)null;
65  
66  		// Min buff size is 20.
67  		var pr = createParserReader(t);
68  		read(pr, 5);
69  		pr.mark();
70  		read(pr, 10);
71  		r = pr.getMarked();
72  		assertEquals("56789b1234", r);
73  		r = read(pr);
74  		assertEquals("56789c123456789d123456789e123456789f123456789g123456789h123456789i123456789j123456789", r);
75  
76  		// Force doubling of buffer size
77  		pr = createParserReader(t);
78  		read(pr, 5);
79  		pr.mark();
80  		read(pr, 20);
81  		r = pr.getMarked();
82  		assertEquals("56789b123456789c1234", r);
83  		r = read(pr);
84  		assertEquals("56789d123456789e123456789f123456789g123456789h123456789i123456789j123456789", r);
85  	}
86  
87  	//====================================================================================================
88  	// testReadStrings
89  	//====================================================================================================
90  	@Test void a03_readStrings() throws Exception {
91  		var t = "a123456789b123456789c123456789d123456789e123456789f123456789g123456789h123456789i123456789j123456789";
92  
93  		// Min buff size is 20.
94  		var pr = createParserReader(t);
95  		assertEquals("a123456789", pr.read(10));
96  		pr.mark();
97  		assertEquals("b123456789c123456789", pr.read(20));
98  		assertEquals("d123456789e123456789f123456789", pr.read(30));
99  		assertEquals("123456789c123456789d123456789e123456789f12345678", pr.getMarked(1, -1));
100 		assertEquals("g123456789h123456789i123456789j123456789", pr.read(100));
101 		assertEquals("", pr.read(100));
102 		pr.close();
103 	}
104 
105 	//====================================================================================================
106 	// testReplace
107 	//====================================================================================================
108 	@Test void a04_replace() throws Exception {
109 		var t = "a123456789b123456789c123456789d123456789e123456789f123456789g123456789h123456789i123456789j123456789";
110 
111 		// Min buff size is 20.
112 		var pr = createParserReader(t);
113 		assertEquals("a123456789", pr.read(10));
114 		pr.mark();
115 		assertEquals("b123456789", pr.read(10));
116 		pr.replace('x');
117 		assertEquals("c123456789", pr.read(10));
118 		assertEquals("b12345678xc123456789", pr.getMarked());
119 		pr.close();
120 
121 		pr = createParserReader(t);
122 		assertEquals("a123456789", pr.read(10));
123 		pr.mark();
124 		assertEquals("b123456789", pr.read(10));
125 		pr.replace('x', 5);
126 		assertEquals("c123456789", pr.read(10));
127 		assertEquals("b1234xc123456789", pr.getMarked());
128 		pr.close();
129 	}
130 
131 	//====================================================================================================
132 	// testDelete
133 	//====================================================================================================
134 	@Test void a05_delete() throws Exception {
135 		var t = "a123456789b123456789c123456789d123456789e123456789f123456789g123456789h123456789i123456789j123456789";
136 
137 		// Min buff size is 20.
138 		var pr = createParserReader(t);
139 		assertEquals("a123456789", pr.read(10));
140 		pr.mark();
141 		assertEquals("b123456789", pr.read(10));
142 		pr.delete();
143 		assertEquals("c123456789", pr.read(10));
144 		assertEquals("b12345678c123456789", pr.getMarked());
145 		pr.close();
146 
147 		pr = createParserReader(t);
148 		assertEquals("a123456789", pr.read(10));
149 		pr.mark();
150 		assertEquals("b123456789", pr.read(10));
151 		pr.delete(5);
152 		assertEquals("c123456789", pr.read(10));
153 		assertEquals("b1234c123456789", pr.getMarked());
154 		pr.close();
155 	}
156 
157 	//====================================================================================================
158 	// Utility methods
159 	//====================================================================================================
160 
161 	private String read(ParserReader r) throws IOException {
162 		return read(r, Integer.MAX_VALUE);
163 	}
164 
165 	private String read(ParserReader r, int length) throws IOException {
166 		var sb = new StringBuilder();
167 		for (int i = 0; i < length; i++) {
168 			int c = r.read();
169 			if (c == -1)
170 				return sb.toString();
171 			sb.append((char)c);
172 		}
173 		return sb.toString();
174 	}
175 
176 	private ParserReader createParserReader(Object in) throws Exception {
177 		return new ParserReader(new ParserPipe(in));
178 	}
179 }