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