Source Code Cross Referenced for IoBufferTest.java in  » Net » mina-2.0.0-M1 » org » apache » mina » common » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » mina 2.0.0 M1 » org.apache.mina.common 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         *  Licensed to the Apache Software Foundation (ASF) under one
0003:         *  or more contributor license agreements.  See the NOTICE file
0004:         *  distributed with this work for additional information
0005:         *  regarding copyright ownership.  The ASF licenses this file
0006:         *  to you under the Apache License, Version 2.0 (the
0007:         *  "License"); you may not use this file except in compliance
0008:         *  with the License.  You may obtain a copy of the License at
0009:         *
0010:         *    http://www.apache.org/licenses/LICENSE-2.0
0011:         *
0012:         *  Unless required by applicable law or agreed to in writing,
0013:         *  software distributed under the License is distributed on an
0014:         *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015:         *  KIND, either express or implied.  See the License for the
0016:         *  specific language governing permissions and limitations
0017:         *  under the License.
0018:         *
0019:         */
0020:        package org.apache.mina.common;
0021:
0022:        import java.nio.BufferOverflowException;
0023:        import java.nio.ByteBuffer;
0024:        import java.nio.ByteOrder;
0025:        import java.nio.ReadOnlyBufferException;
0026:        import java.nio.charset.CharacterCodingException;
0027:        import java.nio.charset.Charset;
0028:        import java.nio.charset.CharsetDecoder;
0029:        import java.nio.charset.CharsetEncoder;
0030:        import java.util.ArrayList;
0031:        import java.util.Date;
0032:        import java.util.EnumSet;
0033:        import java.util.List;
0034:
0035:        import junit.framework.Assert;
0036:        import junit.framework.TestCase;
0037:
0038:        /**
0039:         * Tests {@link IoBuffer}.
0040:         *
0041:         * @author The Apache MINA Project (dev@mina.apache.org)
0042:         * @version $Rev: 595517 $, $Date: 2007-11-15 18:31:56 -0700 (Thu, 15 Nov 2007) $
0043:         */
0044:        public class IoBufferTest extends TestCase {
0045:
0046:            public static void main(String[] args) {
0047:                junit.textui.TestRunner.run(IoBufferTest.class);
0048:            }
0049:
0050:            @Override
0051:            protected void setUp() throws Exception {
0052:            }
0053:
0054:            @Override
0055:            protected void tearDown() throws Exception {
0056:            }
0057:
0058:            public void testAllocate() throws Exception {
0059:                for (int i = 10; i < 1048576 * 2; i = i * 11 / 10) // increase by 10%
0060:                {
0061:                    IoBuffer buf = IoBuffer.allocate(i);
0062:                    Assert.assertEquals(0, buf.position());
0063:                    Assert.assertEquals(buf.capacity(), buf.remaining());
0064:                    Assert.assertTrue(buf.capacity() >= i);
0065:                    Assert.assertTrue(buf.capacity() < i * 2);
0066:                }
0067:            }
0068:
0069:            public void testAutoExpand() throws Exception {
0070:                IoBuffer buf = IoBuffer.allocate(1);
0071:
0072:                buf.put((byte) 0);
0073:                try {
0074:                    buf.put((byte) 0);
0075:                    Assert.fail();
0076:                } catch (BufferOverflowException e) {
0077:                    // ignore
0078:                }
0079:
0080:                buf.setAutoExpand(true);
0081:                buf.put((byte) 0);
0082:                Assert.assertEquals(2, buf.position());
0083:                Assert.assertEquals(2, buf.limit());
0084:                Assert.assertEquals(2, buf.capacity());
0085:
0086:                buf.setAutoExpand(false);
0087:                try {
0088:                    buf.put(3, (byte) 0);
0089:                    Assert.fail();
0090:                } catch (IndexOutOfBoundsException e) {
0091:                    // ignore
0092:                }
0093:
0094:                buf.setAutoExpand(true);
0095:                buf.put(3, (byte) 0);
0096:                Assert.assertEquals(2, buf.position());
0097:                Assert.assertEquals(4, buf.limit());
0098:                Assert.assertEquals(4, buf.capacity());
0099:            }
0100:
0101:            public void testAutoExpandMark() throws Exception {
0102:                IoBuffer buf = IoBuffer.allocate(4).setAutoExpand(true);
0103:
0104:                buf.put((byte) 0);
0105:                buf.put((byte) 0);
0106:                buf.put((byte) 0);
0107:
0108:                // Position should be 3 when we reset this buffer.
0109:                buf.mark();
0110:
0111:                // Overflow it
0112:                buf.put((byte) 0);
0113:                buf.put((byte) 0);
0114:
0115:                Assert.assertEquals(5, buf.position());
0116:                buf.reset();
0117:                Assert.assertEquals(3, buf.position());
0118:            }
0119:
0120:            public void testAutoShrink() throws Exception {
0121:                IoBuffer buf = IoBuffer.allocate(8).setAutoShrink(true);
0122:
0123:                // Make sure the buffer doesn't shrink too much (less than the initial
0124:                // capacity.)
0125:                buf.sweep((byte) 1);
0126:                buf.fill(7);
0127:                buf.compact();
0128:                Assert.assertEquals(8, buf.capacity());
0129:                Assert.assertEquals(1, buf.position());
0130:                Assert.assertEquals(8, buf.limit());
0131:                buf.clear();
0132:                Assert.assertEquals(1, buf.get());
0133:
0134:                // Expand the buffer.
0135:                buf.capacity(32).clear();
0136:                Assert.assertEquals(32, buf.capacity());
0137:
0138:                // Make sure the buffer shrinks when only 1/4 is being used.
0139:                buf.sweep((byte) 1);
0140:                buf.fill(24);
0141:                buf.compact();
0142:                Assert.assertEquals(16, buf.capacity());
0143:                Assert.assertEquals(8, buf.position());
0144:                Assert.assertEquals(16, buf.limit());
0145:                buf.clear();
0146:                for (int i = 0; i < 8; i++) {
0147:                    Assert.assertEquals(1, buf.get());
0148:                }
0149:
0150:                // Expand the buffer.
0151:                buf.capacity(32).clear();
0152:                Assert.assertEquals(32, buf.capacity());
0153:
0154:                // Make sure the buffer shrinks when only 1/8 is being used.
0155:                buf.sweep((byte) 1);
0156:                buf.fill(28);
0157:                buf.compact();
0158:                Assert.assertEquals(8, buf.capacity());
0159:                Assert.assertEquals(4, buf.position());
0160:                Assert.assertEquals(8, buf.limit());
0161:                buf.clear();
0162:                for (int i = 0; i < 4; i++) {
0163:                    Assert.assertEquals(1, buf.get());
0164:                }
0165:
0166:                // Expand the buffer.
0167:                buf.capacity(32).clear();
0168:                Assert.assertEquals(32, buf.capacity());
0169:
0170:                // Make sure the buffer shrinks when 0 byte is being used.
0171:                buf.fill(32);
0172:                buf.compact();
0173:                Assert.assertEquals(8, buf.capacity());
0174:                Assert.assertEquals(0, buf.position());
0175:                Assert.assertEquals(8, buf.limit());
0176:
0177:                // Expand the buffer.
0178:                buf.capacity(32).clear();
0179:                Assert.assertEquals(32, buf.capacity());
0180:
0181:                // Make sure the buffer doesn't shrink when more than 1/4 is being used.
0182:                buf.sweep((byte) 1);
0183:                buf.fill(23);
0184:                buf.compact();
0185:                Assert.assertEquals(32, buf.capacity());
0186:                Assert.assertEquals(9, buf.position());
0187:                Assert.assertEquals(32, buf.limit());
0188:                buf.clear();
0189:                for (int i = 0; i < 9; i++) {
0190:                    Assert.assertEquals(1, buf.get());
0191:                }
0192:            }
0193:
0194:            public void testGetString() throws Exception {
0195:                IoBuffer buf = IoBuffer.allocate(16);
0196:                CharsetDecoder decoder;
0197:
0198:                Charset charset = Charset.forName("UTF-8");
0199:                buf.clear();
0200:                buf.putString("hello", charset.newEncoder());
0201:                buf.put((byte) 0);
0202:                buf.flip();
0203:                Assert.assertEquals("hello", buf
0204:                        .getString(charset.newDecoder()));
0205:
0206:                buf.clear();
0207:                buf.putString("hello", charset.newEncoder());
0208:                buf.flip();
0209:                Assert.assertEquals("hello", buf
0210:                        .getString(charset.newDecoder()));
0211:
0212:                decoder = Charset.forName("ISO-8859-1").newDecoder();
0213:                buf.clear();
0214:                buf.put((byte) 'A');
0215:                buf.put((byte) 'B');
0216:                buf.put((byte) 'C');
0217:                buf.put((byte) 0);
0218:
0219:                buf.position(0);
0220:                Assert.assertEquals("ABC", buf.getString(decoder));
0221:                Assert.assertEquals(4, buf.position());
0222:
0223:                buf.position(0);
0224:                buf.limit(1);
0225:                Assert.assertEquals("A", buf.getString(decoder));
0226:                Assert.assertEquals(1, buf.position());
0227:
0228:                buf.clear();
0229:                Assert.assertEquals("ABC", buf.getString(10, decoder));
0230:                Assert.assertEquals(10, buf.position());
0231:
0232:                buf.clear();
0233:                Assert.assertEquals("A", buf.getString(1, decoder));
0234:                Assert.assertEquals(1, buf.position());
0235:
0236:                // Test a trailing garbage
0237:                buf.clear();
0238:                buf.put((byte) 'A');
0239:                buf.put((byte) 'B');
0240:                buf.put((byte) 0);
0241:                buf.put((byte) 'C');
0242:                buf.position(0);
0243:                Assert.assertEquals("AB", buf.getString(4, decoder));
0244:                Assert.assertEquals(4, buf.position());
0245:
0246:                buf.clear();
0247:                buf.fillAndReset(buf.limit());
0248:                decoder = Charset.forName("UTF-16").newDecoder();
0249:                buf.put((byte) 0);
0250:                buf.put((byte) 'A');
0251:                buf.put((byte) 0);
0252:                buf.put((byte) 'B');
0253:                buf.put((byte) 0);
0254:                buf.put((byte) 'C');
0255:                buf.put((byte) 0);
0256:                buf.put((byte) 0);
0257:
0258:                buf.position(0);
0259:                Assert.assertEquals("ABC", buf.getString(decoder));
0260:                Assert.assertEquals(8, buf.position());
0261:
0262:                buf.position(0);
0263:                buf.limit(2);
0264:                Assert.assertEquals("A", buf.getString(decoder));
0265:                Assert.assertEquals(2, buf.position());
0266:
0267:                buf.position(0);
0268:                buf.limit(3);
0269:                Assert.assertEquals("A", buf.getString(decoder));
0270:                Assert.assertEquals(2, buf.position());
0271:
0272:                buf.clear();
0273:                Assert.assertEquals("ABC", buf.getString(10, decoder));
0274:                Assert.assertEquals(10, buf.position());
0275:
0276:                buf.clear();
0277:                Assert.assertEquals("A", buf.getString(2, decoder));
0278:                Assert.assertEquals(2, buf.position());
0279:
0280:                buf.clear();
0281:                try {
0282:                    buf.getString(1, decoder);
0283:                    Assert.fail();
0284:                } catch (IllegalArgumentException e) {
0285:                    // ignore
0286:                }
0287:
0288:                // Test getting strings from an empty buffer.
0289:                buf.clear();
0290:                buf.limit(0);
0291:                Assert.assertEquals("", buf.getString(decoder));
0292:                Assert.assertEquals("", buf.getString(2, decoder));
0293:
0294:                // Test getting strings from non-empty buffer which is filled with 0x00
0295:                buf.clear();
0296:                buf.putInt(0);
0297:                buf.clear();
0298:                buf.limit(4);
0299:                Assert.assertEquals("", buf.getString(decoder));
0300:                Assert.assertEquals(2, buf.position());
0301:                Assert.assertEquals(4, buf.limit());
0302:
0303:                buf.position(0);
0304:                Assert.assertEquals("", buf.getString(2, decoder));
0305:                Assert.assertEquals(2, buf.position());
0306:                Assert.assertEquals(4, buf.limit());
0307:            }
0308:
0309:            public void testGetStringWithFailure() throws Exception {
0310:                String test = "\u30b3\u30e1\u30f3\u30c8\u7de8\u96c6";
0311:                IoBuffer buffer = IoBuffer.wrap(test.getBytes("Shift_JIS"));
0312:
0313:                // Make sure the limit doesn't change when an exception arose.
0314:                int oldLimit = buffer.limit();
0315:                int oldPos = buffer.position();
0316:                try {
0317:                    buffer.getString(3, Charset.forName("ASCII").newDecoder());
0318:                    Assert.fail();
0319:                } catch (Exception e) {
0320:                    Assert.assertEquals(oldLimit, buffer.limit());
0321:                    Assert.assertEquals(oldPos, buffer.position());
0322:                }
0323:
0324:                try {
0325:                    buffer.getString(Charset.forName("ASCII").newDecoder());
0326:                    Assert.fail();
0327:                } catch (Exception e) {
0328:                    Assert.assertEquals(oldLimit, buffer.limit());
0329:                    Assert.assertEquals(oldPos, buffer.position());
0330:                }
0331:            }
0332:
0333:            public void testPutString() throws Exception {
0334:                CharsetEncoder encoder;
0335:                IoBuffer buf = IoBuffer.allocate(16);
0336:                encoder = Charset.forName("ISO-8859-1").newEncoder();
0337:
0338:                buf.putString("ABC", encoder);
0339:                Assert.assertEquals(3, buf.position());
0340:                buf.clear();
0341:                Assert.assertEquals('A', buf.get(0));
0342:                Assert.assertEquals('B', buf.get(1));
0343:                Assert.assertEquals('C', buf.get(2));
0344:
0345:                buf.putString("D", 5, encoder);
0346:                Assert.assertEquals(5, buf.position());
0347:                buf.clear();
0348:                Assert.assertEquals('D', buf.get(0));
0349:                Assert.assertEquals(0, buf.get(1));
0350:
0351:                buf.putString("EFG", 2, encoder);
0352:                Assert.assertEquals(2, buf.position());
0353:                buf.clear();
0354:                Assert.assertEquals('E', buf.get(0));
0355:                Assert.assertEquals('F', buf.get(1));
0356:                Assert.assertEquals('C', buf.get(2)); // C may not be overwritten
0357:
0358:                // UTF-16: We specify byte order to omit BOM.
0359:                encoder = Charset.forName("UTF-16BE").newEncoder();
0360:                buf.clear();
0361:
0362:                buf.putString("ABC", encoder);
0363:                Assert.assertEquals(6, buf.position());
0364:                buf.clear();
0365:
0366:                Assert.assertEquals(0, buf.get(0));
0367:                Assert.assertEquals('A', buf.get(1));
0368:                Assert.assertEquals(0, buf.get(2));
0369:                Assert.assertEquals('B', buf.get(3));
0370:                Assert.assertEquals(0, buf.get(4));
0371:                Assert.assertEquals('C', buf.get(5));
0372:
0373:                buf.putString("D", 10, encoder);
0374:                Assert.assertEquals(10, buf.position());
0375:                buf.clear();
0376:                Assert.assertEquals(0, buf.get(0));
0377:                Assert.assertEquals('D', buf.get(1));
0378:                Assert.assertEquals(0, buf.get(2));
0379:                Assert.assertEquals(0, buf.get(3));
0380:
0381:                buf.putString("EFG", 4, encoder);
0382:                Assert.assertEquals(4, buf.position());
0383:                buf.clear();
0384:                Assert.assertEquals(0, buf.get(0));
0385:                Assert.assertEquals('E', buf.get(1));
0386:                Assert.assertEquals(0, buf.get(2));
0387:                Assert.assertEquals('F', buf.get(3));
0388:                Assert.assertEquals(0, buf.get(4)); // C may not be overwritten
0389:                Assert.assertEquals('C', buf.get(5)); // C may not be overwritten
0390:
0391:                // Test putting an emptry string
0392:                buf.putString("", encoder);
0393:                Assert.assertEquals(0, buf.position());
0394:                buf.putString("", 4, encoder);
0395:                Assert.assertEquals(4, buf.position());
0396:                Assert.assertEquals(0, buf.get(0));
0397:                Assert.assertEquals(0, buf.get(1));
0398:            }
0399:
0400:            public void testGetPrefixedString() throws Exception {
0401:                IoBuffer buf = IoBuffer.allocate(16);
0402:                CharsetEncoder encoder;
0403:                CharsetDecoder decoder;
0404:                encoder = Charset.forName("ISO-8859-1").newEncoder();
0405:                decoder = Charset.forName("ISO-8859-1").newDecoder();
0406:
0407:                buf.putShort((short) 3);
0408:                buf.putString("ABCD", encoder);
0409:                buf.clear();
0410:                Assert.assertEquals("ABC", buf.getPrefixedString(decoder));
0411:            }
0412:
0413:            public void testPutPrefixedString() throws Exception {
0414:                CharsetEncoder encoder;
0415:                IoBuffer buf = IoBuffer.allocate(16);
0416:                buf.fillAndReset(buf.remaining());
0417:                encoder = Charset.forName("ISO-8859-1").newEncoder();
0418:
0419:                // Without autoExpand
0420:                buf.putPrefixedString("ABC", encoder);
0421:                Assert.assertEquals(5, buf.position());
0422:                Assert.assertEquals(0, buf.get(0));
0423:                Assert.assertEquals(3, buf.get(1));
0424:                Assert.assertEquals('A', buf.get(2));
0425:                Assert.assertEquals('B', buf.get(3));
0426:                Assert.assertEquals('C', buf.get(4));
0427:
0428:                buf.clear();
0429:                try {
0430:                    buf.putPrefixedString("123456789012345", encoder);
0431:                    Assert.fail();
0432:                } catch (BufferOverflowException e) {
0433:                    // OK
0434:                }
0435:
0436:                // With autoExpand
0437:                buf.clear();
0438:                buf.setAutoExpand(true);
0439:                buf.putPrefixedString("123456789012345", encoder);
0440:                Assert.assertEquals(17, buf.position());
0441:                Assert.assertEquals(0, buf.get(0));
0442:                Assert.assertEquals(15, buf.get(1));
0443:                Assert.assertEquals('1', buf.get(2));
0444:                Assert.assertEquals('2', buf.get(3));
0445:                Assert.assertEquals('3', buf.get(4));
0446:                Assert.assertEquals('4', buf.get(5));
0447:                Assert.assertEquals('5', buf.get(6));
0448:                Assert.assertEquals('6', buf.get(7));
0449:                Assert.assertEquals('7', buf.get(8));
0450:                Assert.assertEquals('8', buf.get(9));
0451:                Assert.assertEquals('9', buf.get(10));
0452:                Assert.assertEquals('0', buf.get(11));
0453:                Assert.assertEquals('1', buf.get(12));
0454:                Assert.assertEquals('2', buf.get(13));
0455:                Assert.assertEquals('3', buf.get(14));
0456:                Assert.assertEquals('4', buf.get(15));
0457:                Assert.assertEquals('5', buf.get(16));
0458:            }
0459:
0460:            public void testPutPrefixedStringWithPrefixLength()
0461:                    throws Exception {
0462:                CharsetEncoder encoder = Charset.forName("ISO-8859-1")
0463:                        .newEncoder();
0464:                IoBuffer buf = IoBuffer.allocate(16).sweep()
0465:                        .setAutoExpand(true);
0466:
0467:                buf.putPrefixedString("A", 1, encoder);
0468:                Assert.assertEquals(2, buf.position());
0469:                Assert.assertEquals(1, buf.get(0));
0470:                Assert.assertEquals('A', buf.get(1));
0471:
0472:                buf.sweep();
0473:                buf.putPrefixedString("A", 2, encoder);
0474:                Assert.assertEquals(3, buf.position());
0475:                Assert.assertEquals(0, buf.get(0));
0476:                Assert.assertEquals(1, buf.get(1));
0477:                Assert.assertEquals('A', buf.get(2));
0478:
0479:                buf.sweep();
0480:                buf.putPrefixedString("A", 4, encoder);
0481:                Assert.assertEquals(5, buf.position());
0482:                Assert.assertEquals(0, buf.get(0));
0483:                Assert.assertEquals(0, buf.get(1));
0484:                Assert.assertEquals(0, buf.get(2));
0485:                Assert.assertEquals(1, buf.get(3));
0486:                Assert.assertEquals('A', buf.get(4));
0487:            }
0488:
0489:            public void testPutPrefixedStringWithPadding() throws Exception {
0490:                CharsetEncoder encoder = Charset.forName("ISO-8859-1")
0491:                        .newEncoder();
0492:                IoBuffer buf = IoBuffer.allocate(16).sweep()
0493:                        .setAutoExpand(true);
0494:
0495:                buf.putPrefixedString("A", 1, 2, (byte) 32, encoder);
0496:                Assert.assertEquals(3, buf.position());
0497:                Assert.assertEquals(2, buf.get(0));
0498:                Assert.assertEquals('A', buf.get(1));
0499:                Assert.assertEquals(' ', buf.get(2));
0500:
0501:                buf.sweep();
0502:                buf.putPrefixedString("A", 1, 4, (byte) 32, encoder);
0503:                Assert.assertEquals(5, buf.position());
0504:                Assert.assertEquals(4, buf.get(0));
0505:                Assert.assertEquals('A', buf.get(1));
0506:                Assert.assertEquals(' ', buf.get(2));
0507:                Assert.assertEquals(' ', buf.get(3));
0508:                Assert.assertEquals(' ', buf.get(4));
0509:            }
0510:
0511:            public void testWideUtf8Characters() throws Exception {
0512:                Runnable r = new Runnable() {
0513:                    public void run() {
0514:                        IoBuffer buffer = IoBuffer.allocate(1);
0515:                        buffer.setAutoExpand(true);
0516:
0517:                        Charset charset = Charset.forName("UTF-8");
0518:
0519:                        CharsetEncoder encoder = charset.newEncoder();
0520:
0521:                        for (int i = 0; i < 5; i++) {
0522:                            try {
0523:                                buffer.putString("\u89d2", encoder);
0524:                            } catch (CharacterCodingException e) {
0525:                                fail(e.getMessage());
0526:                            }
0527:                        }
0528:                    }
0529:                };
0530:
0531:                Thread t = new Thread(r);
0532:                t.setDaemon(true);
0533:                t.start();
0534:
0535:                for (int i = 0; i < 50; i++) {
0536:                    Thread.sleep(100);
0537:                    if (!t.isAlive()) {
0538:                        break;
0539:                    }
0540:                }
0541:
0542:                if (t.isAlive()) {
0543:                    t.interrupt();
0544:
0545:                    fail("Went into endless loop trying to encode character");
0546:                }
0547:            }
0548:
0549:            public void testObjectSerialization() throws Exception {
0550:                IoBuffer buf = IoBuffer.allocate(16);
0551:                buf.setAutoExpand(true);
0552:                List<Object> o = new ArrayList<Object>();
0553:                o.add(new Date());
0554:                o.add(long.class);
0555:
0556:                // Test writing an object.
0557:                buf.putObject(o);
0558:
0559:                // Test reading an object.
0560:                buf.clear();
0561:                Object o2 = buf.getObject();
0562:                Assert.assertEquals(o, o2);
0563:
0564:                // This assertion is just to make sure that deserialization occurred.
0565:                Assert.assertNotSame(o, o2);
0566:            }
0567:
0568:            public void testSweepWithZeros() throws Exception {
0569:                IoBuffer buf = IoBuffer.allocate(4);
0570:                buf.putInt(0xdeadbeef);
0571:                buf.clear();
0572:                Assert.assertEquals(0xdeadbeef, buf.getInt());
0573:                Assert.assertEquals(4, buf.position());
0574:                Assert.assertEquals(4, buf.limit());
0575:
0576:                buf.sweep();
0577:                Assert.assertEquals(0, buf.position());
0578:                Assert.assertEquals(4, buf.limit());
0579:                Assert.assertEquals(0x0, buf.getInt());
0580:            }
0581:
0582:            public void testSweepNonZeros() throws Exception {
0583:                IoBuffer buf = IoBuffer.allocate(4);
0584:                buf.putInt(0xdeadbeef);
0585:                buf.clear();
0586:                Assert.assertEquals(0xdeadbeef, buf.getInt());
0587:                Assert.assertEquals(4, buf.position());
0588:                Assert.assertEquals(4, buf.limit());
0589:
0590:                buf.sweep((byte) 0x45);
0591:                Assert.assertEquals(0, buf.position());
0592:                Assert.assertEquals(4, buf.limit());
0593:                Assert.assertEquals(0x45454545, buf.getInt());
0594:            }
0595:
0596:            public void testWrapNioBuffer() throws Exception {
0597:                ByteBuffer nioBuf = ByteBuffer.allocate(10);
0598:                nioBuf.position(3);
0599:                nioBuf.limit(7);
0600:
0601:                IoBuffer buf = IoBuffer.wrap(nioBuf);
0602:                Assert.assertEquals(3, buf.position());
0603:                Assert.assertEquals(7, buf.limit());
0604:                Assert.assertEquals(10, buf.capacity());
0605:            }
0606:
0607:            public void testWrapSubArray() throws Exception {
0608:                byte[] array = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
0609:
0610:                IoBuffer buf = IoBuffer.wrap(array, 3, 4);
0611:                Assert.assertEquals(3, buf.position());
0612:                Assert.assertEquals(7, buf.limit());
0613:                Assert.assertEquals(10, buf.capacity());
0614:
0615:                buf.clear();
0616:                Assert.assertEquals(0, buf.position());
0617:                Assert.assertEquals(10, buf.limit());
0618:                Assert.assertEquals(10, buf.capacity());
0619:            }
0620:
0621:            public void testDuplicate() throws Exception {
0622:                IoBuffer original;
0623:                IoBuffer duplicate;
0624:
0625:                // Test if the buffer is duplicated correctly.
0626:                original = IoBuffer.allocate(16).sweep();
0627:                original.position(4);
0628:                original.limit(10);
0629:                duplicate = original.duplicate();
0630:                original.put(4, (byte) 127);
0631:                Assert.assertEquals(4, duplicate.position());
0632:                Assert.assertEquals(10, duplicate.limit());
0633:                Assert.assertEquals(16, duplicate.capacity());
0634:                Assert.assertNotSame(original.buf(), duplicate.buf());
0635:                Assert.assertSame(original.buf().array(), duplicate.buf()
0636:                        .array());
0637:                Assert.assertEquals(127, duplicate.get(4));
0638:
0639:                // Test a duplicate of a duplicate.
0640:                original = IoBuffer.allocate(16);
0641:                duplicate = original.duplicate().duplicate();
0642:                Assert.assertNotSame(original.buf(), duplicate.buf());
0643:                Assert.assertSame(original.buf().array(), duplicate.buf()
0644:                        .array());
0645:
0646:                // Try to expand.
0647:                original = IoBuffer.allocate(16);
0648:                original.setAutoExpand(true);
0649:                duplicate = original.duplicate();
0650:                Assert.assertFalse(original.isAutoExpand());
0651:
0652:                try {
0653:                    original.setAutoExpand(true);
0654:                    Assert.fail();
0655:                } catch (IllegalStateException e) {
0656:                    // OK
0657:                }
0658:
0659:                try {
0660:                    duplicate.setAutoExpand(true);
0661:                    Assert.fail();
0662:                } catch (IllegalStateException e) {
0663:                    // OK
0664:                }
0665:            }
0666:
0667:            public void testSlice() throws Exception {
0668:                IoBuffer original;
0669:                IoBuffer slice;
0670:
0671:                // Test if the buffer is sliced correctly.
0672:                original = IoBuffer.allocate(16).sweep();
0673:                original.position(4);
0674:                original.limit(10);
0675:                slice = original.slice();
0676:                original.put(4, (byte) 127);
0677:                Assert.assertEquals(0, slice.position());
0678:                Assert.assertEquals(6, slice.limit());
0679:                Assert.assertEquals(6, slice.capacity());
0680:                Assert.assertNotSame(original.buf(), slice.buf());
0681:                Assert.assertEquals(127, slice.get(0));
0682:            }
0683:
0684:            public void testReadOnlyBuffer() throws Exception {
0685:                IoBuffer original;
0686:                IoBuffer duplicate;
0687:
0688:                // Test if the buffer is duplicated correctly.
0689:                original = IoBuffer.allocate(16).sweep();
0690:                original.position(4);
0691:                original.limit(10);
0692:                duplicate = original.asReadOnlyBuffer();
0693:                original.put(4, (byte) 127);
0694:                Assert.assertEquals(4, duplicate.position());
0695:                Assert.assertEquals(10, duplicate.limit());
0696:                Assert.assertEquals(16, duplicate.capacity());
0697:                Assert.assertNotSame(original.buf(), duplicate.buf());
0698:                Assert.assertEquals(127, duplicate.get(4));
0699:
0700:                // Try to expand.
0701:                try {
0702:                    original = IoBuffer.allocate(16);
0703:                    duplicate = original.asReadOnlyBuffer();
0704:                    duplicate.putString(
0705:                            "A very very very very looooooong string", Charset
0706:                                    .forName("ISO-8859-1").newEncoder());
0707:                    Assert.fail();
0708:                } catch (ReadOnlyBufferException e) {
0709:                    // OK
0710:                }
0711:            }
0712:
0713:            public void testGetUnsigned() throws Exception {
0714:                IoBuffer buf = IoBuffer.allocate(16);
0715:                buf.put((byte) 0xA4);
0716:                buf.put((byte) 0xD0);
0717:                buf.put((byte) 0xB3);
0718:                buf.put((byte) 0xCD);
0719:                buf.flip();
0720:
0721:                buf.order(ByteOrder.LITTLE_ENDIAN);
0722:
0723:                buf.mark();
0724:                Assert.assertEquals(0xA4, buf.getUnsigned());
0725:                buf.reset();
0726:                Assert.assertEquals(0xD0A4, buf.getUnsignedShort());
0727:                buf.reset();
0728:                Assert.assertEquals(0xCDB3D0A4L, buf.getUnsignedInt());
0729:            }
0730:
0731:            public void testIndexOf() throws Exception {
0732:                boolean direct = false;
0733:                for (int i = 0; i < 2; i++, direct = !direct) {
0734:                    IoBuffer buf = IoBuffer.allocate(16, direct);
0735:                    buf.put((byte) 0x1);
0736:                    buf.put((byte) 0x2);
0737:                    buf.put((byte) 0x3);
0738:                    buf.put((byte) 0x4);
0739:                    buf.put((byte) 0x1);
0740:                    buf.put((byte) 0x2);
0741:                    buf.put((byte) 0x3);
0742:                    buf.put((byte) 0x4);
0743:                    buf.position(2);
0744:                    buf.limit(5);
0745:
0746:                    Assert.assertEquals(4, buf.indexOf((byte) 0x1));
0747:                    Assert.assertEquals(-1, buf.indexOf((byte) 0x2));
0748:                    Assert.assertEquals(2, buf.indexOf((byte) 0x3));
0749:                    Assert.assertEquals(3, buf.indexOf((byte) 0x4));
0750:                }
0751:            }
0752:
0753:            // We need an enum with 64 values
0754:            private static enum TestEnum {
0755:                E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18, E19, E20, E21, E22, E23, E24, E25, E26, E27, E28, E29, E30, E31, E32, E33, E34, E35, E36, E37, E38, E39, E40, E41, E42, E43, E44, E45, E46, E77, E48, E49, E50, E51, E52, E53, E54, E55, E56, E57, E58, E59, E60, E61, E62, E63, E64
0756:            }
0757:
0758:            private static enum TooBigEnum {
0759:                E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18, E19, E20, E21, E22, E23, E24, E25, E26, E27, E28, E29, E30, E31, E32, E33, E34, E35, E36, E37, E38, E39, E40, E41, E42, E43, E44, E45, E46, E77, E48, E49, E50, E51, E52, E53, E54, E55, E56, E57, E58, E59, E60, E61, E62, E63, E64, E65
0760:            }
0761:
0762:            public void testPutEnumSet() {
0763:                IoBuffer buf = IoBuffer.allocate(8);
0764:
0765:                // Test empty set
0766:                buf.putEnumSet(EnumSet.noneOf(TestEnum.class));
0767:                buf.flip();
0768:                assertEquals(0, buf.get());
0769:
0770:                buf.clear();
0771:                buf.putEnumSetShort(EnumSet.noneOf(TestEnum.class));
0772:                buf.flip();
0773:                assertEquals(0, buf.getShort());
0774:
0775:                buf.clear();
0776:                buf.putEnumSetInt(EnumSet.noneOf(TestEnum.class));
0777:                buf.flip();
0778:                assertEquals(0, buf.getInt());
0779:
0780:                buf.clear();
0781:                buf.putEnumSetLong(EnumSet.noneOf(TestEnum.class));
0782:                buf.flip();
0783:                assertEquals(0, buf.getLong());
0784:
0785:                // Test complete set
0786:                buf.clear();
0787:                buf.putEnumSet(EnumSet.range(TestEnum.E1, TestEnum.E8));
0788:                buf.flip();
0789:                assertEquals((byte) -1, buf.get());
0790:
0791:                buf.clear();
0792:                buf.putEnumSetShort(EnumSet.range(TestEnum.E1, TestEnum.E16));
0793:                buf.flip();
0794:                assertEquals((short) -1, buf.getShort());
0795:
0796:                buf.clear();
0797:                buf.putEnumSetInt(EnumSet.range(TestEnum.E1, TestEnum.E32));
0798:                buf.flip();
0799:                assertEquals(-1, buf.getInt());
0800:
0801:                buf.clear();
0802:                buf.putEnumSetLong(EnumSet.allOf(TestEnum.class));
0803:                buf.flip();
0804:                assertEquals(-1L, buf.getLong());
0805:
0806:                // Test high bit set
0807:                buf.clear();
0808:                buf.putEnumSet(EnumSet.of(TestEnum.E8));
0809:                buf.flip();
0810:                assertEquals(Byte.MIN_VALUE, buf.get());
0811:
0812:                buf.clear();
0813:                buf.putEnumSetShort(EnumSet.of(TestEnum.E16));
0814:                buf.flip();
0815:                assertEquals(Short.MIN_VALUE, buf.getShort());
0816:
0817:                buf.clear();
0818:                buf.putEnumSetInt(EnumSet.of(TestEnum.E32));
0819:                buf.flip();
0820:                assertEquals(Integer.MIN_VALUE, buf.getInt());
0821:
0822:                buf.clear();
0823:                buf.putEnumSetLong(EnumSet.of(TestEnum.E64));
0824:                buf.flip();
0825:                assertEquals(Long.MIN_VALUE, buf.getLong());
0826:
0827:                // Test high low bits set
0828:                buf.clear();
0829:                buf.putEnumSet(EnumSet.of(TestEnum.E1, TestEnum.E8));
0830:                buf.flip();
0831:                assertEquals(Byte.MIN_VALUE + 1, buf.get());
0832:
0833:                buf.clear();
0834:                buf.putEnumSetShort(EnumSet.of(TestEnum.E1, TestEnum.E16));
0835:                buf.flip();
0836:                assertEquals(Short.MIN_VALUE + 1, buf.getShort());
0837:
0838:                buf.clear();
0839:                buf.putEnumSetInt(EnumSet.of(TestEnum.E1, TestEnum.E32));
0840:                buf.flip();
0841:                assertEquals(Integer.MIN_VALUE + 1, buf.getInt());
0842:
0843:                buf.clear();
0844:                buf.putEnumSetLong(EnumSet.of(TestEnum.E1, TestEnum.E64));
0845:                buf.flip();
0846:                assertEquals(Long.MIN_VALUE + 1, buf.getLong());
0847:            }
0848:
0849:            public void testGetEnumSet() {
0850:                IoBuffer buf = IoBuffer.allocate(8);
0851:
0852:                // Test empty set
0853:                buf.put((byte) 0);
0854:                buf.flip();
0855:                assertEquals(EnumSet.noneOf(TestEnum.class), buf
0856:                        .getEnumSet(TestEnum.class));
0857:
0858:                buf.clear();
0859:                buf.putShort((short) 0);
0860:                buf.flip();
0861:                assertEquals(EnumSet.noneOf(TestEnum.class), buf
0862:                        .getEnumSet(TestEnum.class));
0863:
0864:                buf.clear();
0865:                buf.putInt(0);
0866:                buf.flip();
0867:                assertEquals(EnumSet.noneOf(TestEnum.class), buf
0868:                        .getEnumSet(TestEnum.class));
0869:
0870:                buf.clear();
0871:                buf.putLong(0L);
0872:                buf.flip();
0873:                assertEquals(EnumSet.noneOf(TestEnum.class), buf
0874:                        .getEnumSet(TestEnum.class));
0875:
0876:                // Test complete set
0877:                buf.clear();
0878:                buf.put((byte) -1);
0879:                buf.flip();
0880:                assertEquals(EnumSet.range(TestEnum.E1, TestEnum.E8), buf
0881:                        .getEnumSet(TestEnum.class));
0882:
0883:                buf.clear();
0884:                buf.putShort((short) -1);
0885:                buf.flip();
0886:                assertEquals(EnumSet.range(TestEnum.E1, TestEnum.E16), buf
0887:                        .getEnumSetShort(TestEnum.class));
0888:
0889:                buf.clear();
0890:                buf.putInt(-1);
0891:                buf.flip();
0892:                assertEquals(EnumSet.range(TestEnum.E1, TestEnum.E32), buf
0893:                        .getEnumSetInt(TestEnum.class));
0894:
0895:                buf.clear();
0896:                buf.putLong(-1L);
0897:                buf.flip();
0898:                assertEquals(EnumSet.allOf(TestEnum.class), buf
0899:                        .getEnumSetLong(TestEnum.class));
0900:
0901:                // Test high bit set
0902:                buf.clear();
0903:                buf.put(Byte.MIN_VALUE);
0904:                buf.flip();
0905:                assertEquals(EnumSet.of(TestEnum.E8), buf
0906:                        .getEnumSet(TestEnum.class));
0907:
0908:                buf.clear();
0909:                buf.putShort(Short.MIN_VALUE);
0910:                buf.flip();
0911:                assertEquals(EnumSet.of(TestEnum.E16), buf
0912:                        .getEnumSetShort(TestEnum.class));
0913:
0914:                buf.clear();
0915:                buf.putInt(Integer.MIN_VALUE);
0916:                buf.flip();
0917:                assertEquals(EnumSet.of(TestEnum.E32), buf
0918:                        .getEnumSetInt(TestEnum.class));
0919:
0920:                buf.clear();
0921:                buf.putLong(Long.MIN_VALUE);
0922:                buf.flip();
0923:                assertEquals(EnumSet.of(TestEnum.E64), buf
0924:                        .getEnumSetLong(TestEnum.class));
0925:
0926:                // Test high low bits set
0927:                buf.clear();
0928:                byte b = Byte.MIN_VALUE + 1;
0929:                buf.put(b);
0930:                buf.flip();
0931:                assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E8), buf
0932:                        .getEnumSet(TestEnum.class));
0933:
0934:                buf.clear();
0935:                short s = Short.MIN_VALUE + 1;
0936:                buf.putShort(s);
0937:                buf.flip();
0938:                assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E16), buf
0939:                        .getEnumSetShort(TestEnum.class));
0940:
0941:                buf.clear();
0942:                buf.putInt(Integer.MIN_VALUE + 1);
0943:                buf.flip();
0944:                assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E32), buf
0945:                        .getEnumSetInt(TestEnum.class));
0946:
0947:                buf.clear();
0948:                buf.putLong(Long.MIN_VALUE + 1);
0949:                buf.flip();
0950:                assertEquals(EnumSet.of(TestEnum.E1, TestEnum.E64), buf
0951:                        .getEnumSetLong(TestEnum.class));
0952:            }
0953:
0954:            public void testBitVectorOverFlow() {
0955:                IoBuffer buf = IoBuffer.allocate(8);
0956:                try {
0957:                    buf.putEnumSet(EnumSet.of(TestEnum.E9));
0958:                    fail("Should have thrown IllegalArgumentException");
0959:                } catch (IllegalArgumentException e) {
0960:                    // pass
0961:                }
0962:
0963:                try {
0964:                    buf.putEnumSetShort(EnumSet.of(TestEnum.E17));
0965:                    fail("Should have thrown IllegalArgumentException");
0966:                } catch (IllegalArgumentException e) {
0967:                    // pass
0968:                }
0969:
0970:                try {
0971:                    buf.putEnumSetInt(EnumSet.of(TestEnum.E33));
0972:                    fail("Should have thrown IllegalArgumentException");
0973:                } catch (IllegalArgumentException e) {
0974:                    // pass
0975:                }
0976:
0977:                try {
0978:                    buf.putEnumSetLong(EnumSet.of(TooBigEnum.E65));
0979:                    fail("Should have thrown IllegalArgumentException");
0980:                } catch (IllegalArgumentException e) {
0981:                    // pass
0982:                }
0983:            }
0984:
0985:            public void testGetPutEnum() {
0986:                IoBuffer buf = IoBuffer.allocate(4);
0987:
0988:                buf.putEnum(TestEnum.E64);
0989:                buf.flip();
0990:                assertEquals(TestEnum.E64, buf.getEnum(TestEnum.class));
0991:
0992:                buf.clear();
0993:                buf.putEnumShort(TestEnum.E64);
0994:                buf.flip();
0995:                assertEquals(TestEnum.E64, buf.getEnumShort(TestEnum.class));
0996:
0997:                buf.clear();
0998:                buf.putEnumInt(TestEnum.E64);
0999:                buf.flip();
1000:                assertEquals(TestEnum.E64, buf.getEnumInt(TestEnum.class));
1001:            }
1002:
1003:            public void testGetMediumInt() {
1004:                IoBuffer buf = IoBuffer.allocate(3);
1005:
1006:                buf.put((byte) 0x01);
1007:                buf.put((byte) 0x02);
1008:                buf.put((byte) 0x03);
1009:                assertEquals(3, buf.position());
1010:
1011:                buf.flip();
1012:                assertEquals(0x010203, buf.getMediumInt());
1013:                assertEquals(0x010203, buf.getMediumInt(0));
1014:                buf.flip();
1015:                assertEquals(0x010203, buf.getUnsignedMediumInt());
1016:                assertEquals(0x010203, buf.getUnsignedMediumInt(0));
1017:                buf.flip();
1018:                assertEquals(0x010203, buf.getUnsignedMediumInt());
1019:                buf.flip().order(ByteOrder.LITTLE_ENDIAN);
1020:                assertEquals(0x030201, buf.getMediumInt());
1021:                assertEquals(0x030201, buf.getMediumInt(0));
1022:
1023:                // Test max medium int
1024:                buf.flip().order(ByteOrder.BIG_ENDIAN);
1025:                buf.put((byte) 0x7f);
1026:                buf.put((byte) 0xff);
1027:                buf.put((byte) 0xff);
1028:                buf.flip();
1029:                assertEquals(0x7fffff, buf.getMediumInt());
1030:                assertEquals(0x7fffff, buf.getMediumInt(0));
1031:
1032:                // Test negative number
1033:                buf.flip().order(ByteOrder.BIG_ENDIAN);
1034:                buf.put((byte) 0xff);
1035:                buf.put((byte) 0x02);
1036:                buf.put((byte) 0x03);
1037:                buf.flip();
1038:
1039:                assertEquals(0xffff0203, buf.getMediumInt());
1040:                assertEquals(0xffff0203, buf.getMediumInt(0));
1041:                buf.flip();
1042:
1043:                assertEquals(0x00ff0203, buf.getUnsignedMediumInt());
1044:                assertEquals(0x00ff0203, buf.getUnsignedMediumInt(0));
1045:            }
1046:
1047:            public void testPutMediumInt() {
1048:                IoBuffer buf = IoBuffer.allocate(3);
1049:
1050:                checkMediumInt(buf, 0);
1051:                checkMediumInt(buf, 1);
1052:                checkMediumInt(buf, -1);
1053:                checkMediumInt(buf, 0x7fffff);
1054:            }
1055:
1056:            private void checkMediumInt(IoBuffer buf, int x) {
1057:                buf.putMediumInt(x);
1058:                assertEquals(3, buf.position());
1059:                buf.flip();
1060:                assertEquals(x, buf.getMediumInt());
1061:                assertEquals(3, buf.position());
1062:
1063:                buf.putMediumInt(0, x);
1064:                assertEquals(3, buf.position());
1065:                assertEquals(x, buf.getMediumInt(0));
1066:
1067:                buf.flip();
1068:            }
1069:
1070:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.