001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.nio.tests.java.nio;
019:
020: import java.nio.Buffer;
021: import java.nio.InvalidMarkException;
022:
023: import junit.framework.TestCase;
024:
025: /**
026: * Test a java.nio.Buffer instance.
027: */
028: public class BufferTest extends TestCase {
029:
030: public static void testBufferInstance(Buffer buf) {
031: // save state
032: int oldPosition = buf.position();
033: int oldLimit = buf.limit();
034:
035: testCapacity(buf);
036: testClear(buf);
037: testFlip(buf);
038: testHasRemaining(buf);
039: testIsReadOnly(buf);
040: testLimit(buf);
041: testLimitint(buf);
042: testMark(buf);
043: testPosition(buf);
044: testPositionint(buf);
045: testRemaining(buf);
046: testReset(buf);
047: testRewind(buf);
048:
049: // check state, should not change
050: assertEquals(buf.position(), oldPosition);
051: assertEquals(buf.limit(), oldLimit);
052: }
053:
054: public static void testCapacity(Buffer buf) {
055: assertTrue(0 <= buf.position() && buf.position() <= buf.limit()
056: && buf.limit() <= buf.capacity());
057: }
058:
059: public static void testClear(Buffer buf) {
060: // save state
061: int oldPosition = buf.position();
062: int oldLimit = buf.limit();
063:
064: Buffer ret = buf.clear();
065: assertSame(ret, buf);
066: assertEquals(buf.position(), 0);
067: assertEquals(buf.limit(), buf.capacity());
068: try {
069: buf.reset();
070: fail("Should throw Exception"); //$NON-NLS-1$
071: } catch (InvalidMarkException e) {
072: // expected
073: }
074:
075: // restore state
076: buf.limit(oldLimit);
077: buf.position(oldPosition);
078: }
079:
080: public static void testFlip(Buffer buf) {
081: // save state
082: int oldPosition = buf.position();
083: int oldLimit = buf.limit();
084:
085: Buffer ret = buf.flip();
086: assertSame(ret, buf);
087: assertEquals(buf.position(), 0);
088: assertEquals(buf.limit(), oldPosition);
089: try {
090: buf.reset();
091: fail("Should throw Exception"); //$NON-NLS-1$
092: } catch (InvalidMarkException e) {
093: // expected
094: }
095:
096: // restore state
097: buf.limit(oldLimit);
098: buf.position(oldPosition);
099: }
100:
101: public static void testHasRemaining(Buffer buf) {
102: // save state
103: int oldPosition = buf.position();
104: int oldLimit = buf.limit();
105:
106: assertEquals(buf.hasRemaining(), buf.position() < buf.limit());
107: buf.position(buf.limit());
108: assertFalse(buf.hasRemaining());
109:
110: // restore state
111: buf.limit(oldLimit);
112: buf.position(oldPosition);
113: }
114:
115: public static void testIsReadOnly(Buffer buf) {
116: buf.isReadOnly();
117: }
118:
119: /*
120: * Class under test for int limit()
121: */
122: public static void testLimit(Buffer buf) {
123: assertTrue(0 <= buf.position() && buf.position() <= buf.limit()
124: && buf.limit() <= buf.capacity());
125: }
126:
127: /*
128: * Class under test for Buffer limit(int)
129: */
130: public static void testLimitint(Buffer buf) {
131: // save state
132: int oldPosition = buf.position();
133: int oldLimit = buf.limit();
134:
135: Buffer ret = buf.limit(buf.limit());
136: assertSame(ret, buf);
137:
138: buf.mark();
139: buf.limit(buf.capacity());
140: assertEquals(buf.limit(), buf.capacity());
141: // position should not change
142: assertEquals(buf.position(), oldPosition);
143: // mark should be valid
144: buf.reset();
145:
146: if (buf.capacity() > 0) {
147: buf.limit(buf.capacity());
148: buf.position(buf.capacity());
149: buf.mark();
150: buf.limit(buf.capacity() - 1);
151: // position should be the new limit
152: assertEquals(buf.position(), buf.limit());
153: // mark should be invalid
154: try {
155: buf.reset();
156: fail("Should throw Exception"); //$NON-NLS-1$
157: } catch (InvalidMarkException e) {
158: // expected
159: }
160: }
161:
162: try {
163: buf.limit(-1);
164: fail("Should throw Exception"); //$NON-NLS-1$
165: } catch (IllegalArgumentException e) {
166: // expected
167: }
168: try {
169: buf.limit(buf.capacity() + 1);
170: fail("Should throw Exception"); //$NON-NLS-1$
171: } catch (IllegalArgumentException e) {
172: // expected
173: }
174:
175: // restore state
176: buf.limit(oldLimit);
177: buf.position(oldPosition);
178: }
179:
180: public static void testMark(Buffer buf) {
181: // save state
182: int oldPosition = buf.position();
183: int oldLimit = buf.limit();
184:
185: Buffer ret = buf.mark();
186: assertSame(ret, buf);
187:
188: buf.mark();
189: buf.position(buf.limit());
190: buf.reset();
191: assertEquals(buf.position(), oldPosition);
192:
193: buf.mark();
194: buf.position(buf.limit());
195: buf.reset();
196: assertEquals(buf.position(), oldPosition);
197:
198: // restore state
199: buf.limit(oldLimit);
200: buf.position(oldPosition);
201: }
202:
203: /*
204: * Class under test for int position()
205: */
206: public static void testPosition(Buffer buf) {
207: assertTrue(0 <= buf.position() && buf.position() <= buf.limit()
208: && buf.limit() <= buf.capacity());
209: }
210:
211: /*
212: * Class under test for Buffer position(int)
213: */
214: public static void testPositionint(Buffer buf) {
215: // save state
216: int oldPosition = buf.position();
217: int oldLimit = buf.limit();
218:
219: try {
220: buf.position(-1);
221: fail("Should throw Exception"); //$NON-NLS-1$
222: } catch (IllegalArgumentException e) {
223: // expected
224: }
225: try {
226: buf.position(buf.limit() + 1);
227: fail("Should throw Exception"); //$NON-NLS-1$
228: } catch (IllegalArgumentException e) {
229: // expected
230: }
231:
232: buf.mark();
233: buf.position(buf.position());
234: buf.reset();
235: assertEquals(buf.position(), oldPosition);
236:
237: buf.position(0);
238: assertEquals(buf.position(), 0);
239: buf.position(buf.limit());
240: assertEquals(buf.position(), buf.limit());
241:
242: if (buf.capacity() > 0) {
243: buf.limit(buf.capacity());
244: buf.position(buf.limit());
245: buf.mark();
246: buf.position(buf.limit() - 1);
247: assertEquals(buf.position(), buf.limit() - 1);
248: // mark should be invalid
249: try {
250: buf.reset();
251: fail("Should throw Exception"); //$NON-NLS-1$
252: } catch (InvalidMarkException e) {
253: // expected
254: }
255: }
256:
257: Buffer ret = buf.position(0);
258: assertSame(ret, buf);
259:
260: // restore state
261: buf.limit(oldLimit);
262: buf.position(oldPosition);
263: }
264:
265: public static void testRemaining(Buffer buf) {
266: assertEquals(buf.remaining(), buf.limit() - buf.position());
267: }
268:
269: public static void testReset(Buffer buf) {
270: // save state
271: int oldPosition = buf.position();
272: int oldLimit = buf.limit();
273:
274: buf.mark();
275: buf.position(buf.limit());
276: buf.reset();
277: assertEquals(buf.position(), oldPosition);
278:
279: buf.mark();
280: buf.position(buf.limit());
281: buf.reset();
282: assertEquals(buf.position(), oldPosition);
283:
284: Buffer ret = buf.reset();
285: assertSame(ret, buf);
286:
287: buf.clear();
288: try {
289: buf.reset();
290: fail("Should throw Exception"); //$NON-NLS-1$
291: } catch (InvalidMarkException e) {
292: // expected
293: }
294:
295: // restore state
296: buf.limit(oldLimit);
297: buf.position(oldPosition);
298: }
299:
300: public static void testRewind(Buffer buf) {
301: // save state
302: int oldPosition = buf.position();
303: int oldLimit = buf.limit();
304:
305: Buffer ret = buf.rewind();
306: assertEquals(buf.position(), 0);
307: assertSame(ret, buf);
308: try {
309: buf.reset();
310: fail("Should throw Exception"); //$NON-NLS-1$
311: } catch (InvalidMarkException e) {
312: // expected
313: }
314:
315: // restore state
316: buf.limit(oldLimit);
317: buf.position(oldPosition);
318: }
319:
320: public void testNothing() {
321: // to remove JUnit warning
322: }
323:
324: }
|