Source Code Cross Referenced for BinaryCodecTest.java in  » Library » Apache-common-codec » org » apache » commons » codec » binary » 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 » Library » Apache common codec » org.apache.commons.codec.binary 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Copyright 2001-2004 The Apache Software Foundation.
0003:         * 
0004:         * Licensed under the Apache License, Version 2.0 (the "License");
0005:         * you may not use this file except in compliance with the License.
0006:         * You may obtain a copy of the License at
0007:         * 
0008:         *      http://www.apache.org/licenses/LICENSE-2.0
0009:         * 
0010:         * Unless required by applicable law or agreed to in writing, software
0011:         * distributed under the License is distributed on an "AS IS" BASIS,
0012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013:         * See the License for the specific language governing permissions and
0014:         * limitations under the License.
0015:         */
0016:
0017:        package org.apache.commons.codec.binary;
0018:
0019:        import junit.framework.TestCase;
0020:        import org.apache.commons.codec.DecoderException;
0021:        import org.apache.commons.codec.EncoderException;
0022:
0023:        /**
0024:         * TestCase for BinaryCodec class.
0025:         * 
0026:         * @author Apache Software Foundation
0027:         * @version $Id: BinaryCodecTest.java,v 1.1 2004/03/29 23:04:41 ggregory Exp $
0028:         */
0029:        public class BinaryCodecTest extends TestCase {
0030:            /** mask with bit zero based index 0 raised */
0031:            private static final int BIT_0 = 0x01;
0032:
0033:            /** mask with bit zero based index 0 raised */
0034:            private static final int BIT_1 = 0x02;
0035:
0036:            /** mask with bit zero based index 0 raised */
0037:            private static final int BIT_2 = 0x04;
0038:
0039:            /** mask with bit zero based index 0 raised */
0040:            private static final int BIT_3 = 0x08;
0041:
0042:            /** mask with bit zero based index 0 raised */
0043:            private static final int BIT_4 = 0x10;
0044:
0045:            /** mask with bit zero based index 0 raised */
0046:            private static final int BIT_5 = 0x20;
0047:
0048:            /** mask with bit zero based index 0 raised */
0049:            private static final int BIT_6 = 0x40;
0050:
0051:            /** mask with bit zero based index 0 raised */
0052:            private static final int BIT_7 = 0x80;
0053:
0054:            /** an instance of the binary codec */
0055:            BinaryCodec instance = null;
0056:
0057:            /*
0058:             * @see TestCase#setUp()
0059:             */
0060:            protected void setUp() throws Exception {
0061:                super .setUp();
0062:                this .instance = new BinaryCodec();
0063:            }
0064:
0065:            /*
0066:             * @see TestCase#tearDown()
0067:             */
0068:            protected void tearDown() throws Exception {
0069:                super .tearDown();
0070:                this .instance = null;
0071:            }
0072:
0073:            /**
0074:             * Constructor for BinaryTest.
0075:             * 
0076:             * @param arg0
0077:             */
0078:            public BinaryCodecTest(String arg0) {
0079:                super (arg0);
0080:            }
0081:
0082:            // ------------------------------------------------------------------------
0083:            //
0084:            // Test decode(Object)
0085:            //
0086:            // ------------------------------------------------------------------------
0087:            /**
0088:             * Tests for Object decode(Object)
0089:             */
0090:            public void testDecodeObjectException() {
0091:                try {
0092:                    this .instance.decode(new Object());
0093:                } catch (DecoderException e) {
0094:                    // all is well.
0095:                    return;
0096:                }
0097:                fail("Expected DecoderException");
0098:            }
0099:
0100:            /**
0101:             * Tests for Object decode(Object)
0102:             */
0103:            public void testDecodeObject() throws Exception {
0104:                byte[] bits;
0105:                // With a single raw binary
0106:                bits = new byte[1];
0107:                assertDecodeObject(bits, "00000000");
0108:                bits = new byte[1];
0109:                bits[0] = BIT_0;
0110:                assertDecodeObject(bits, "00000001");
0111:                bits = new byte[1];
0112:                bits[0] = BIT_0 | BIT_1;
0113:                assertDecodeObject(bits, "00000011");
0114:                bits = new byte[1];
0115:                bits[0] = BIT_0 | BIT_1 | BIT_2;
0116:                assertDecodeObject(bits, "00000111");
0117:                bits = new byte[1];
0118:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0119:                assertDecodeObject(bits, "00001111");
0120:                bits = new byte[1];
0121:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0122:                assertDecodeObject(bits, "00011111");
0123:                bits = new byte[1];
0124:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0125:                assertDecodeObject(bits, "00111111");
0126:                bits = new byte[1];
0127:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0128:                assertDecodeObject(bits, "01111111");
0129:                bits = new byte[1];
0130:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0131:                        | BIT_6 | BIT_7);
0132:                assertDecodeObject(bits, "11111111");
0133:                // With a two raw binaries
0134:                bits = new byte[2];
0135:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0136:                        | BIT_6 | BIT_7);
0137:                assertDecodeObject(bits, "0000000011111111");
0138:                bits = new byte[2];
0139:                bits[1] = BIT_0;
0140:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0141:                        | BIT_6 | BIT_7);
0142:                assertDecodeObject(bits, "0000000111111111");
0143:                bits = new byte[2];
0144:                bits[1] = BIT_0 | BIT_1;
0145:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0146:                        | BIT_6 | BIT_7);
0147:                assertDecodeObject(bits, "0000001111111111");
0148:                bits = new byte[2];
0149:                bits[1] = BIT_0 | BIT_1 | BIT_2;
0150:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0151:                        | BIT_6 | BIT_7);
0152:                assertDecodeObject(bits, "0000011111111111");
0153:                bits = new byte[2];
0154:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0155:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0156:                        | BIT_6 | BIT_7);
0157:                assertDecodeObject(bits, "0000111111111111");
0158:                bits = new byte[2];
0159:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0160:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0161:                        | BIT_6 | BIT_7);
0162:                assertDecodeObject(bits, "0001111111111111");
0163:                bits = new byte[2];
0164:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0165:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0166:                        | BIT_6 | BIT_7);
0167:                assertDecodeObject(bits, "0011111111111111");
0168:                bits = new byte[2];
0169:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0170:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0171:                        | BIT_6 | BIT_7);
0172:                assertDecodeObject(bits, "0111111111111111");
0173:                bits = new byte[2];
0174:                bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0175:                        | BIT_6 | BIT_7);
0176:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0177:                        | BIT_6 | BIT_7);
0178:                assertDecodeObject(bits, "1111111111111111");
0179:                assertDecodeObject(new byte[0], null);
0180:            }
0181:
0182:            // ------------------------------------------------------------------------
0183:            //
0184:            // Test decode(byte[])
0185:            //
0186:            // ------------------------------------------------------------------------
0187:            /**
0188:             * Utility used to assert the encoded and decoded values.
0189:             * 
0190:             * @param bits
0191:             *                  the pre-encoded data
0192:             * @param encodeMe
0193:             *                  data to encode and compare
0194:             */
0195:            void assertDecodeObject(byte[] bits, String encodeMe)
0196:                    throws DecoderException {
0197:                byte[] decoded;
0198:                decoded = (byte[]) instance.decode(encodeMe);
0199:                assertEquals(new String(bits), new String(decoded));
0200:                if (encodeMe == null) {
0201:                    decoded = instance.decode((byte[]) null);
0202:                } else {
0203:                    decoded = (byte[]) instance.decode((Object) encodeMe
0204:                            .getBytes());
0205:                }
0206:                assertEquals(new String(bits), new String(decoded));
0207:                if (encodeMe == null) {
0208:                    decoded = (byte[]) instance.decode((char[]) null);
0209:                } else {
0210:                    decoded = (byte[]) instance.decode(encodeMe.toCharArray());
0211:                }
0212:                assertEquals(new String(bits), new String(decoded));
0213:            }
0214:
0215:            /*
0216:             * Tests for byte[] decode(byte[])
0217:             */
0218:            public void testDecodebyteArray() {
0219:                // With a single raw binary
0220:                byte[] bits = new byte[1];
0221:                byte[] decoded = instance.decode("00000000".getBytes());
0222:                assertEquals(new String(bits), new String(decoded));
0223:                bits = new byte[1];
0224:                bits[0] = BIT_0;
0225:                decoded = instance.decode("00000001".getBytes());
0226:                assertEquals(new String(bits), new String(decoded));
0227:                bits = new byte[1];
0228:                bits[0] = BIT_0 | BIT_1;
0229:                decoded = instance.decode("00000011".getBytes());
0230:                assertEquals(new String(bits), new String(decoded));
0231:                bits = new byte[1];
0232:                bits[0] = BIT_0 | BIT_1 | BIT_2;
0233:                decoded = instance.decode("00000111".getBytes());
0234:                assertEquals(new String(bits), new String(decoded));
0235:                bits = new byte[1];
0236:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0237:                decoded = instance.decode("00001111".getBytes());
0238:                assertEquals(new String(bits), new String(decoded));
0239:                bits = new byte[1];
0240:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0241:                decoded = instance.decode("00011111".getBytes());
0242:                assertEquals(new String(bits), new String(decoded));
0243:                bits = new byte[1];
0244:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0245:                decoded = instance.decode("00111111".getBytes());
0246:                assertEquals(new String(bits), new String(decoded));
0247:                bits = new byte[1];
0248:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0249:                decoded = instance.decode("01111111".getBytes());
0250:                assertEquals(new String(bits), new String(decoded));
0251:                bits = new byte[1];
0252:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0253:                        | BIT_6 | BIT_7);
0254:                decoded = instance.decode("11111111".getBytes());
0255:                assertEquals(new String(bits), new String(decoded));
0256:                // With a two raw binaries
0257:                bits = new byte[2];
0258:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0259:                        | BIT_6 | BIT_7);
0260:                decoded = instance.decode("0000000011111111".getBytes());
0261:                assertEquals(new String(bits), new String(decoded));
0262:                bits = new byte[2];
0263:                bits[1] = BIT_0;
0264:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0265:                        | BIT_6 | BIT_7);
0266:                decoded = instance.decode("0000000111111111".getBytes());
0267:                assertEquals(new String(bits), new String(decoded));
0268:                bits = new byte[2];
0269:                bits[1] = BIT_0 | BIT_1;
0270:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0271:                        | BIT_6 | BIT_7);
0272:                decoded = instance.decode("0000001111111111".getBytes());
0273:                assertEquals(new String(bits), new String(decoded));
0274:                bits = new byte[2];
0275:                bits[1] = BIT_0 | BIT_1 | BIT_2;
0276:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0277:                        | BIT_6 | BIT_7);
0278:                decoded = instance.decode("0000011111111111".getBytes());
0279:                assertEquals(new String(bits), new String(decoded));
0280:                bits = new byte[2];
0281:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0282:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0283:                        | BIT_6 | BIT_7);
0284:                decoded = instance.decode("0000111111111111".getBytes());
0285:                assertEquals(new String(bits), new String(decoded));
0286:                bits = new byte[2];
0287:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0288:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0289:                        | BIT_6 | BIT_7);
0290:                decoded = instance.decode("0001111111111111".getBytes());
0291:                assertEquals(new String(bits), new String(decoded));
0292:                bits = new byte[2];
0293:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0294:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0295:                        | BIT_6 | BIT_7);
0296:                decoded = instance.decode("0011111111111111".getBytes());
0297:                assertEquals(new String(bits), new String(decoded));
0298:                bits = new byte[2];
0299:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0300:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0301:                        | BIT_6 | BIT_7);
0302:                decoded = instance.decode("0111111111111111".getBytes());
0303:                assertEquals(new String(bits), new String(decoded));
0304:                bits = new byte[2];
0305:                bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0306:                        | BIT_6 | BIT_7);
0307:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0308:                        | BIT_6 | BIT_7);
0309:                decoded = instance.decode("1111111111111111".getBytes());
0310:                assertEquals(new String(bits), new String(decoded));
0311:            }
0312:
0313:            // ------------------------------------------------------------------------
0314:            //
0315:            // Test toByteArray(String)
0316:            //
0317:            // ------------------------------------------------------------------------
0318:            /*
0319:             * Tests for byte[] toByteArray(String)
0320:             */
0321:            public void testToByteArrayFromString() {
0322:                // With a single raw binary
0323:                byte[] bits = new byte[1];
0324:                byte[] decoded = instance.toByteArray("00000000");
0325:                assertEquals(new String(bits), new String(decoded));
0326:                bits = new byte[1];
0327:                bits[0] = BIT_0;
0328:                decoded = instance.toByteArray("00000001");
0329:                assertEquals(new String(bits), new String(decoded));
0330:                bits = new byte[1];
0331:                bits[0] = BIT_0 | BIT_1;
0332:                decoded = instance.toByteArray("00000011");
0333:                assertEquals(new String(bits), new String(decoded));
0334:                bits = new byte[1];
0335:                bits[0] = BIT_0 | BIT_1 | BIT_2;
0336:                decoded = instance.toByteArray("00000111");
0337:                assertEquals(new String(bits), new String(decoded));
0338:                bits = new byte[1];
0339:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0340:                decoded = instance.toByteArray("00001111");
0341:                assertEquals(new String(bits), new String(decoded));
0342:                bits = new byte[1];
0343:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0344:                decoded = instance.toByteArray("00011111");
0345:                assertEquals(new String(bits), new String(decoded));
0346:                bits = new byte[1];
0347:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0348:                decoded = instance.toByteArray("00111111");
0349:                assertEquals(new String(bits), new String(decoded));
0350:                bits = new byte[1];
0351:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0352:                decoded = instance.toByteArray("01111111");
0353:                assertEquals(new String(bits), new String(decoded));
0354:                bits = new byte[1];
0355:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0356:                        | BIT_6 | BIT_7);
0357:                decoded = instance.toByteArray("11111111");
0358:                assertEquals(new String(bits), new String(decoded));
0359:                // With a two raw binaries
0360:                bits = new byte[2];
0361:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0362:                        | BIT_6 | BIT_7);
0363:                decoded = instance.toByteArray("0000000011111111");
0364:                assertEquals(new String(bits), new String(decoded));
0365:                bits = new byte[2];
0366:                bits[1] = BIT_0;
0367:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0368:                        | BIT_6 | BIT_7);
0369:                decoded = instance.toByteArray("0000000111111111");
0370:                assertEquals(new String(bits), new String(decoded));
0371:                bits = new byte[2];
0372:                bits[1] = BIT_0 | BIT_1;
0373:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0374:                        | BIT_6 | BIT_7);
0375:                decoded = instance.toByteArray("0000001111111111");
0376:                assertEquals(new String(bits), new String(decoded));
0377:                bits = new byte[2];
0378:                bits[1] = BIT_0 | BIT_1 | BIT_2;
0379:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0380:                        | BIT_6 | BIT_7);
0381:                decoded = instance.toByteArray("0000011111111111");
0382:                assertEquals(new String(bits), new String(decoded));
0383:                bits = new byte[2];
0384:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0385:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0386:                        | BIT_6 | BIT_7);
0387:                decoded = instance.toByteArray("0000111111111111");
0388:                assertEquals(new String(bits), new String(decoded));
0389:                bits = new byte[2];
0390:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0391:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0392:                        | BIT_6 | BIT_7);
0393:                decoded = instance.toByteArray("0001111111111111");
0394:                assertEquals(new String(bits), new String(decoded));
0395:                bits = new byte[2];
0396:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0397:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0398:                        | BIT_6 | BIT_7);
0399:                decoded = instance.toByteArray("0011111111111111");
0400:                assertEquals(new String(bits), new String(decoded));
0401:                bits = new byte[2];
0402:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0403:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0404:                        | BIT_6 | BIT_7);
0405:                decoded = instance.toByteArray("0111111111111111");
0406:                assertEquals(new String(bits), new String(decoded));
0407:                bits = new byte[2];
0408:                bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0409:                        | BIT_6 | BIT_7);
0410:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0411:                        | BIT_6 | BIT_7);
0412:                decoded = instance.toByteArray("1111111111111111");
0413:                assertEquals(new String(bits), new String(decoded));
0414:                assertEquals(0, instance.toByteArray((String) null).length);
0415:            }
0416:
0417:            // ------------------------------------------------------------------------
0418:            //
0419:            // Test fromAscii(char[])
0420:            //
0421:            // ------------------------------------------------------------------------
0422:            /*
0423:             * Tests for byte[] fromAscii(char[])
0424:             */
0425:            public void testFromAsciicharArray() {
0426:                // With a single raw binary
0427:                byte[] bits = new byte[1];
0428:                byte[] decoded = BinaryCodec
0429:                        .fromAscii("00000000".toCharArray());
0430:                assertEquals(new String(bits), new String(decoded));
0431:                bits = new byte[1];
0432:                bits[0] = BIT_0;
0433:                decoded = BinaryCodec.fromAscii("00000001".toCharArray());
0434:                assertEquals(new String(bits), new String(decoded));
0435:                bits = new byte[1];
0436:                bits[0] = BIT_0 | BIT_1;
0437:                decoded = BinaryCodec.fromAscii("00000011".toCharArray());
0438:                assertEquals(new String(bits), new String(decoded));
0439:                bits = new byte[1];
0440:                bits[0] = BIT_0 | BIT_1 | BIT_2;
0441:                decoded = BinaryCodec.fromAscii("00000111".toCharArray());
0442:                assertEquals(new String(bits), new String(decoded));
0443:                bits = new byte[1];
0444:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0445:                decoded = BinaryCodec.fromAscii("00001111".toCharArray());
0446:                assertEquals(new String(bits), new String(decoded));
0447:                bits = new byte[1];
0448:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0449:                decoded = BinaryCodec.fromAscii("00011111".toCharArray());
0450:                assertEquals(new String(bits), new String(decoded));
0451:                bits = new byte[1];
0452:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0453:                decoded = BinaryCodec.fromAscii("00111111".toCharArray());
0454:                assertEquals(new String(bits), new String(decoded));
0455:                bits = new byte[1];
0456:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0457:                decoded = BinaryCodec.fromAscii("01111111".toCharArray());
0458:                assertEquals(new String(bits), new String(decoded));
0459:                bits = new byte[1];
0460:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0461:                        | BIT_6 | BIT_7);
0462:                decoded = BinaryCodec.fromAscii("11111111".toCharArray());
0463:                assertEquals(new String(bits), new String(decoded));
0464:                // With a two raw binaries
0465:                bits = new byte[2];
0466:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0467:                        | BIT_6 | BIT_7);
0468:                decoded = BinaryCodec.fromAscii("0000000011111111"
0469:                        .toCharArray());
0470:                assertEquals(new String(bits), new String(decoded));
0471:                bits = new byte[2];
0472:                bits[1] = BIT_0;
0473:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0474:                        | BIT_6 | BIT_7);
0475:                decoded = BinaryCodec.fromAscii("0000000111111111"
0476:                        .toCharArray());
0477:                assertEquals(new String(bits), new String(decoded));
0478:                bits = new byte[2];
0479:                bits[1] = BIT_0 | BIT_1;
0480:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0481:                        | BIT_6 | BIT_7);
0482:                decoded = BinaryCodec.fromAscii("0000001111111111"
0483:                        .toCharArray());
0484:                assertEquals(new String(bits), new String(decoded));
0485:                bits = new byte[2];
0486:                bits[1] = BIT_0 | BIT_1 | BIT_2;
0487:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0488:                        | BIT_6 | BIT_7);
0489:                decoded = BinaryCodec.fromAscii("0000011111111111"
0490:                        .toCharArray());
0491:                assertEquals(new String(bits), new String(decoded));
0492:                bits = new byte[2];
0493:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0494:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0495:                        | BIT_6 | BIT_7);
0496:                decoded = BinaryCodec.fromAscii("0000111111111111"
0497:                        .toCharArray());
0498:                assertEquals(new String(bits), new String(decoded));
0499:                bits = new byte[2];
0500:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0501:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0502:                        | BIT_6 | BIT_7);
0503:                decoded = BinaryCodec.fromAscii("0001111111111111"
0504:                        .toCharArray());
0505:                assertEquals(new String(bits), new String(decoded));
0506:                bits = new byte[2];
0507:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0508:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0509:                        | BIT_6 | BIT_7);
0510:                decoded = BinaryCodec.fromAscii("0011111111111111"
0511:                        .toCharArray());
0512:                assertEquals(new String(bits), new String(decoded));
0513:                bits = new byte[2];
0514:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0515:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0516:                        | BIT_6 | BIT_7);
0517:                decoded = BinaryCodec.fromAscii("0111111111111111"
0518:                        .toCharArray());
0519:                assertEquals(new String(bits), new String(decoded));
0520:                bits = new byte[2];
0521:                bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0522:                        | BIT_6 | BIT_7);
0523:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0524:                        | BIT_6 | BIT_7);
0525:                decoded = BinaryCodec.fromAscii("1111111111111111"
0526:                        .toCharArray());
0527:                assertEquals(new String(bits), new String(decoded));
0528:                assertEquals(0, BinaryCodec.fromAscii((char[]) null).length);
0529:            }
0530:
0531:            // ------------------------------------------------------------------------
0532:            //
0533:            // Test fromAscii(byte[])
0534:            //
0535:            // ------------------------------------------------------------------------
0536:            /*
0537:             * Tests for byte[] fromAscii(byte[])
0538:             */
0539:            public void testFromAsciibyteArray() {
0540:                // With a single raw binary
0541:                byte[] bits = new byte[1];
0542:                byte[] decoded = BinaryCodec.fromAscii("00000000".getBytes());
0543:                assertEquals(new String(bits), new String(decoded));
0544:                bits = new byte[1];
0545:                bits[0] = BIT_0;
0546:                decoded = BinaryCodec.fromAscii("00000001".getBytes());
0547:                assertEquals(new String(bits), new String(decoded));
0548:                bits = new byte[1];
0549:                bits[0] = BIT_0 | BIT_1;
0550:                decoded = BinaryCodec.fromAscii("00000011".getBytes());
0551:                assertEquals(new String(bits), new String(decoded));
0552:                bits = new byte[1];
0553:                bits[0] = BIT_0 | BIT_1 | BIT_2;
0554:                decoded = BinaryCodec.fromAscii("00000111".getBytes());
0555:                assertEquals(new String(bits), new String(decoded));
0556:                bits = new byte[1];
0557:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0558:                decoded = BinaryCodec.fromAscii("00001111".getBytes());
0559:                assertEquals(new String(bits), new String(decoded));
0560:                bits = new byte[1];
0561:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0562:                decoded = BinaryCodec.fromAscii("00011111".getBytes());
0563:                assertEquals(new String(bits), new String(decoded));
0564:                bits = new byte[1];
0565:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0566:                decoded = BinaryCodec.fromAscii("00111111".getBytes());
0567:                assertEquals(new String(bits), new String(decoded));
0568:                bits = new byte[1];
0569:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0570:                decoded = BinaryCodec.fromAscii("01111111".getBytes());
0571:                assertEquals(new String(bits), new String(decoded));
0572:                bits = new byte[1];
0573:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0574:                        | BIT_6 | BIT_7);
0575:                decoded = BinaryCodec.fromAscii("11111111".getBytes());
0576:                assertEquals(new String(bits), new String(decoded));
0577:                // With a two raw binaries
0578:                bits = new byte[2];
0579:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0580:                        | BIT_6 | BIT_7);
0581:                decoded = BinaryCodec.fromAscii("0000000011111111".getBytes());
0582:                assertEquals(new String(bits), new String(decoded));
0583:                bits = new byte[2];
0584:                bits[1] = BIT_0;
0585:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0586:                        | BIT_6 | BIT_7);
0587:                decoded = BinaryCodec.fromAscii("0000000111111111".getBytes());
0588:                assertEquals(new String(bits), new String(decoded));
0589:                bits = new byte[2];
0590:                bits[1] = BIT_0 | BIT_1;
0591:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0592:                        | BIT_6 | BIT_7);
0593:                decoded = BinaryCodec.fromAscii("0000001111111111".getBytes());
0594:                assertEquals(new String(bits), new String(decoded));
0595:                bits = new byte[2];
0596:                bits[1] = BIT_0 | BIT_1 | BIT_2;
0597:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0598:                        | BIT_6 | BIT_7);
0599:                decoded = BinaryCodec.fromAscii("0000011111111111".getBytes());
0600:                assertEquals(new String(bits), new String(decoded));
0601:                bits = new byte[2];
0602:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0603:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0604:                        | BIT_6 | BIT_7);
0605:                decoded = BinaryCodec.fromAscii("0000111111111111".getBytes());
0606:                assertEquals(new String(bits), new String(decoded));
0607:                bits = new byte[2];
0608:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0609:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0610:                        | BIT_6 | BIT_7);
0611:                decoded = BinaryCodec.fromAscii("0001111111111111".getBytes());
0612:                assertEquals(new String(bits), new String(decoded));
0613:                bits = new byte[2];
0614:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0615:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0616:                        | BIT_6 | BIT_7);
0617:                decoded = BinaryCodec.fromAscii("0011111111111111".getBytes());
0618:                assertEquals(new String(bits), new String(decoded));
0619:                bits = new byte[2];
0620:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0621:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0622:                        | BIT_6 | BIT_7);
0623:                decoded = BinaryCodec.fromAscii("0111111111111111".getBytes());
0624:                assertEquals(new String(bits), new String(decoded));
0625:                bits = new byte[2];
0626:                bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0627:                        | BIT_6 | BIT_7);
0628:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0629:                        | BIT_6 | BIT_7);
0630:                decoded = BinaryCodec.fromAscii("1111111111111111".getBytes());
0631:                assertEquals(new String(bits), new String(decoded));
0632:                assertEquals(0, BinaryCodec.fromAscii((byte[]) null).length);
0633:            }
0634:
0635:            // ------------------------------------------------------------------------
0636:            //
0637:            // Test encode(byte[])
0638:            //
0639:            // ------------------------------------------------------------------------
0640:            /*
0641:             * Tests for byte[] encode(byte[])
0642:             */
0643:            public void testEncodebyteArray() {
0644:                // With a single raw binary
0645:                byte[] bits = new byte[1];
0646:                String l_encoded = new String(instance.encode(bits));
0647:                assertEquals("00000000", l_encoded);
0648:                bits = new byte[1];
0649:                bits[0] = BIT_0;
0650:                l_encoded = new String(instance.encode(bits));
0651:                assertEquals("00000001", l_encoded);
0652:                bits = new byte[1];
0653:                bits[0] = BIT_0 | BIT_1;
0654:                l_encoded = new String(instance.encode(bits));
0655:                assertEquals("00000011", l_encoded);
0656:                bits = new byte[1];
0657:                bits[0] = BIT_0 | BIT_1 | BIT_2;
0658:                l_encoded = new String(instance.encode(bits));
0659:                assertEquals("00000111", l_encoded);
0660:                bits = new byte[1];
0661:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0662:                l_encoded = new String(instance.encode(bits));
0663:                assertEquals("00001111", l_encoded);
0664:                bits = new byte[1];
0665:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0666:                l_encoded = new String(instance.encode(bits));
0667:                assertEquals("00011111", l_encoded);
0668:                bits = new byte[1];
0669:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0670:                l_encoded = new String(instance.encode(bits));
0671:                assertEquals("00111111", l_encoded);
0672:                bits = new byte[1];
0673:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0674:                l_encoded = new String(instance.encode(bits));
0675:                assertEquals("01111111", l_encoded);
0676:                bits = new byte[1];
0677:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0678:                        | BIT_6 | BIT_7);
0679:                l_encoded = new String(instance.encode(bits));
0680:                assertEquals("11111111", l_encoded);
0681:                // With a two raw binaries
0682:                bits = new byte[2];
0683:                l_encoded = new String(instance.encode(bits));
0684:                assertEquals("0000000000000000", l_encoded);
0685:                bits = new byte[2];
0686:                bits[0] = BIT_0;
0687:                l_encoded = new String(instance.encode(bits));
0688:                assertEquals("0000000000000001", l_encoded);
0689:                bits = new byte[2];
0690:                bits[0] = BIT_0 | BIT_1;
0691:                l_encoded = new String(instance.encode(bits));
0692:                assertEquals("0000000000000011", l_encoded);
0693:                bits = new byte[2];
0694:                bits[0] = BIT_0 | BIT_1 | BIT_2;
0695:                l_encoded = new String(instance.encode(bits));
0696:                assertEquals("0000000000000111", l_encoded);
0697:                bits = new byte[2];
0698:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0699:                l_encoded = new String(instance.encode(bits));
0700:                assertEquals("0000000000001111", l_encoded);
0701:                bits = new byte[2];
0702:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0703:                l_encoded = new String(instance.encode(bits));
0704:                assertEquals("0000000000011111", l_encoded);
0705:                bits = new byte[2];
0706:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0707:                l_encoded = new String(instance.encode(bits));
0708:                assertEquals("0000000000111111", l_encoded);
0709:                bits = new byte[2];
0710:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0711:                l_encoded = new String(instance.encode(bits));
0712:                assertEquals("0000000001111111", l_encoded);
0713:                bits = new byte[2];
0714:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0715:                        | BIT_6 | BIT_7);
0716:                l_encoded = new String(instance.encode(bits));
0717:                assertEquals("0000000011111111", l_encoded);
0718:                // work on the other byte now
0719:                bits = new byte[2];
0720:                bits[1] = BIT_0;
0721:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0722:                        | BIT_6 | BIT_7);
0723:                l_encoded = new String(instance.encode(bits));
0724:                assertEquals("0000000111111111", l_encoded);
0725:                bits = new byte[2];
0726:                bits[1] = BIT_0 | BIT_1;
0727:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0728:                        | BIT_6 | BIT_7);
0729:                l_encoded = new String(instance.encode(bits));
0730:                assertEquals("0000001111111111", l_encoded);
0731:                bits = new byte[2];
0732:                bits[1] = BIT_0 | BIT_1 | BIT_2;
0733:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0734:                        | BIT_6 | BIT_7);
0735:                l_encoded = new String(instance.encode(bits));
0736:                assertEquals("0000011111111111", l_encoded);
0737:                bits = new byte[2];
0738:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0739:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0740:                        | BIT_6 | BIT_7);
0741:                l_encoded = new String(instance.encode(bits));
0742:                assertEquals("0000111111111111", l_encoded);
0743:                bits = new byte[2];
0744:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0745:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0746:                        | BIT_6 | BIT_7);
0747:                l_encoded = new String(instance.encode(bits));
0748:                assertEquals("0001111111111111", l_encoded);
0749:                bits = new byte[2];
0750:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0751:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0752:                        | BIT_6 | BIT_7);
0753:                l_encoded = new String(instance.encode(bits));
0754:                assertEquals("0011111111111111", l_encoded);
0755:                bits = new byte[2];
0756:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0757:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0758:                        | BIT_6 | BIT_7);
0759:                l_encoded = new String(instance.encode(bits));
0760:                assertEquals("0111111111111111", l_encoded);
0761:                bits = new byte[2];
0762:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0763:                        | BIT_6 | BIT_7);
0764:                bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0765:                        | BIT_6 | BIT_7);
0766:                l_encoded = new String(instance.encode(bits));
0767:                assertEquals("1111111111111111", l_encoded);
0768:                assertEquals(0, instance.encode((byte[]) null).length);
0769:            }
0770:
0771:            // ------------------------------------------------------------------------
0772:            //
0773:            // Test toAsciiBytes
0774:            //
0775:            // ------------------------------------------------------------------------
0776:            public void testToAsciiBytes() {
0777:                // With a single raw binary
0778:                byte[] bits = new byte[1];
0779:                String l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0780:                assertEquals("00000000", l_encoded);
0781:                bits = new byte[1];
0782:                bits[0] = BIT_0;
0783:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0784:                assertEquals("00000001", l_encoded);
0785:                bits = new byte[1];
0786:                bits[0] = BIT_0 | BIT_1;
0787:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0788:                assertEquals("00000011", l_encoded);
0789:                bits = new byte[1];
0790:                bits[0] = BIT_0 | BIT_1 | BIT_2;
0791:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0792:                assertEquals("00000111", l_encoded);
0793:                bits = new byte[1];
0794:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0795:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0796:                assertEquals("00001111", l_encoded);
0797:                bits = new byte[1];
0798:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0799:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0800:                assertEquals("00011111", l_encoded);
0801:                bits = new byte[1];
0802:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0803:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0804:                assertEquals("00111111", l_encoded);
0805:                bits = new byte[1];
0806:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0807:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0808:                assertEquals("01111111", l_encoded);
0809:                bits = new byte[1];
0810:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0811:                        | BIT_6 | BIT_7);
0812:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0813:                assertEquals("11111111", l_encoded);
0814:                // With a two raw binaries
0815:                bits = new byte[2];
0816:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0817:                assertEquals("0000000000000000", l_encoded);
0818:                bits = new byte[2];
0819:                bits[0] = BIT_0;
0820:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0821:                assertEquals("0000000000000001", l_encoded);
0822:                bits = new byte[2];
0823:                bits[0] = BIT_0 | BIT_1;
0824:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0825:                assertEquals("0000000000000011", l_encoded);
0826:                bits = new byte[2];
0827:                bits[0] = BIT_0 | BIT_1 | BIT_2;
0828:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0829:                assertEquals("0000000000000111", l_encoded);
0830:                bits = new byte[2];
0831:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0832:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0833:                assertEquals("0000000000001111", l_encoded);
0834:                bits = new byte[2];
0835:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0836:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0837:                assertEquals("0000000000011111", l_encoded);
0838:                bits = new byte[2];
0839:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0840:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0841:                assertEquals("0000000000111111", l_encoded);
0842:                bits = new byte[2];
0843:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0844:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0845:                assertEquals("0000000001111111", l_encoded);
0846:                bits = new byte[2];
0847:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0848:                        | BIT_6 | BIT_7);
0849:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0850:                assertEquals("0000000011111111", l_encoded);
0851:                // work on the other byte now
0852:                bits = new byte[2];
0853:                bits[1] = BIT_0;
0854:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0855:                        | BIT_6 | BIT_7);
0856:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0857:                assertEquals("0000000111111111", l_encoded);
0858:                bits = new byte[2];
0859:                bits[1] = BIT_0 | BIT_1;
0860:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0861:                        | BIT_6 | BIT_7);
0862:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0863:                assertEquals("0000001111111111", l_encoded);
0864:                bits = new byte[2];
0865:                bits[1] = BIT_0 | BIT_1 | BIT_2;
0866:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0867:                        | BIT_6 | BIT_7);
0868:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0869:                assertEquals("0000011111111111", l_encoded);
0870:                bits = new byte[2];
0871:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0872:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0873:                        | BIT_6 | BIT_7);
0874:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0875:                assertEquals("0000111111111111", l_encoded);
0876:                bits = new byte[2];
0877:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0878:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0879:                        | BIT_6 | BIT_7);
0880:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0881:                assertEquals("0001111111111111", l_encoded);
0882:                bits = new byte[2];
0883:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0884:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0885:                        | BIT_6 | BIT_7);
0886:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0887:                assertEquals("0011111111111111", l_encoded);
0888:                bits = new byte[2];
0889:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0890:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0891:                        | BIT_6 | BIT_7);
0892:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0893:                assertEquals("0111111111111111", l_encoded);
0894:                bits = new byte[2];
0895:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0896:                        | BIT_6 | BIT_7);
0897:                bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0898:                        | BIT_6 | BIT_7);
0899:                l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
0900:                assertEquals("1111111111111111", l_encoded);
0901:                assertEquals(0, BinaryCodec.toAsciiBytes((byte[]) null).length);
0902:            }
0903:
0904:            // ------------------------------------------------------------------------
0905:            //
0906:            // Test toAsciiChars
0907:            //
0908:            // ------------------------------------------------------------------------
0909:            public void testToAsciiChars() {
0910:                // With a single raw binary
0911:                byte[] bits = new byte[1];
0912:                String l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0913:                assertEquals("00000000", l_encoded);
0914:                bits = new byte[1];
0915:                bits[0] = BIT_0;
0916:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0917:                assertEquals("00000001", l_encoded);
0918:                bits = new byte[1];
0919:                bits[0] = BIT_0 | BIT_1;
0920:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0921:                assertEquals("00000011", l_encoded);
0922:                bits = new byte[1];
0923:                bits[0] = BIT_0 | BIT_1 | BIT_2;
0924:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0925:                assertEquals("00000111", l_encoded);
0926:                bits = new byte[1];
0927:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0928:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0929:                assertEquals("00001111", l_encoded);
0930:                bits = new byte[1];
0931:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0932:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0933:                assertEquals("00011111", l_encoded);
0934:                bits = new byte[1];
0935:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0936:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0937:                assertEquals("00111111", l_encoded);
0938:                bits = new byte[1];
0939:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0940:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0941:                assertEquals("01111111", l_encoded);
0942:                bits = new byte[1];
0943:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0944:                        | BIT_6 | BIT_7);
0945:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0946:                assertEquals("11111111", l_encoded);
0947:                // With a two raw binaries
0948:                bits = new byte[2];
0949:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0950:                assertEquals("0000000000000000", l_encoded);
0951:                bits = new byte[2];
0952:                bits[0] = BIT_0;
0953:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0954:                assertEquals("0000000000000001", l_encoded);
0955:                bits = new byte[2];
0956:                bits[0] = BIT_0 | BIT_1;
0957:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0958:                assertEquals("0000000000000011", l_encoded);
0959:                bits = new byte[2];
0960:                bits[0] = BIT_0 | BIT_1 | BIT_2;
0961:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0962:                assertEquals("0000000000000111", l_encoded);
0963:                bits = new byte[2];
0964:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
0965:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0966:                assertEquals("0000000000001111", l_encoded);
0967:                bits = new byte[2];
0968:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
0969:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0970:                assertEquals("0000000000011111", l_encoded);
0971:                bits = new byte[2];
0972:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
0973:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0974:                assertEquals("0000000000111111", l_encoded);
0975:                bits = new byte[2];
0976:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
0977:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0978:                assertEquals("0000000001111111", l_encoded);
0979:                bits = new byte[2];
0980:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0981:                        | BIT_6 | BIT_7);
0982:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0983:                assertEquals("0000000011111111", l_encoded);
0984:                // work on the other byte now
0985:                bits = new byte[2];
0986:                bits[1] = BIT_0;
0987:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0988:                        | BIT_6 | BIT_7);
0989:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0990:                assertEquals("0000000111111111", l_encoded);
0991:                bits = new byte[2];
0992:                bits[1] = BIT_0 | BIT_1;
0993:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
0994:                        | BIT_6 | BIT_7);
0995:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
0996:                assertEquals("0000001111111111", l_encoded);
0997:                bits = new byte[2];
0998:                bits[1] = BIT_0 | BIT_1 | BIT_2;
0999:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1000:                        | BIT_6 | BIT_7);
1001:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
1002:                assertEquals("0000011111111111", l_encoded);
1003:                bits = new byte[2];
1004:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
1005:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1006:                        | BIT_6 | BIT_7);
1007:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
1008:                assertEquals("0000111111111111", l_encoded);
1009:                bits = new byte[2];
1010:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
1011:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1012:                        | BIT_6 | BIT_7);
1013:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
1014:                assertEquals("0001111111111111", l_encoded);
1015:                bits = new byte[2];
1016:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
1017:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1018:                        | BIT_6 | BIT_7);
1019:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
1020:                assertEquals("0011111111111111", l_encoded);
1021:                bits = new byte[2];
1022:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
1023:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1024:                        | BIT_6 | BIT_7);
1025:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
1026:                assertEquals("0111111111111111", l_encoded);
1027:                bits = new byte[2];
1028:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1029:                        | BIT_6 | BIT_7);
1030:                bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1031:                        | BIT_6 | BIT_7);
1032:                l_encoded = new String(BinaryCodec.toAsciiChars(bits));
1033:                assertEquals("1111111111111111", l_encoded);
1034:                assertEquals(0, BinaryCodec.toAsciiChars((byte[]) null).length);
1035:            }
1036:
1037:            // ------------------------------------------------------------------------
1038:            //
1039:            // Test toAsciiString
1040:            //
1041:            // ------------------------------------------------------------------------
1042:            /**
1043:             * Tests the toAsciiString(byte[]) method
1044:             */
1045:            public void testToAsciiString() {
1046:                // With a single raw binary
1047:                byte[] bits = new byte[1];
1048:                String l_encoded = BinaryCodec.toAsciiString(bits);
1049:                assertEquals("00000000", l_encoded);
1050:                bits = new byte[1];
1051:                bits[0] = BIT_0;
1052:                l_encoded = BinaryCodec.toAsciiString(bits);
1053:                assertEquals("00000001", l_encoded);
1054:                bits = new byte[1];
1055:                bits[0] = BIT_0 | BIT_1;
1056:                l_encoded = BinaryCodec.toAsciiString(bits);
1057:                assertEquals("00000011", l_encoded);
1058:                bits = new byte[1];
1059:                bits[0] = BIT_0 | BIT_1 | BIT_2;
1060:                l_encoded = BinaryCodec.toAsciiString(bits);
1061:                assertEquals("00000111", l_encoded);
1062:                bits = new byte[1];
1063:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
1064:                l_encoded = BinaryCodec.toAsciiString(bits);
1065:                assertEquals("00001111", l_encoded);
1066:                bits = new byte[1];
1067:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
1068:                l_encoded = BinaryCodec.toAsciiString(bits);
1069:                assertEquals("00011111", l_encoded);
1070:                bits = new byte[1];
1071:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
1072:                l_encoded = BinaryCodec.toAsciiString(bits);
1073:                assertEquals("00111111", l_encoded);
1074:                bits = new byte[1];
1075:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
1076:                l_encoded = BinaryCodec.toAsciiString(bits);
1077:                assertEquals("01111111", l_encoded);
1078:                bits = new byte[1];
1079:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1080:                        | BIT_6 | BIT_7);
1081:                l_encoded = BinaryCodec.toAsciiString(bits);
1082:                assertEquals("11111111", l_encoded);
1083:                // With a two raw binaries
1084:                bits = new byte[2];
1085:                l_encoded = BinaryCodec.toAsciiString(bits);
1086:                assertEquals("0000000000000000", l_encoded);
1087:                bits = new byte[2];
1088:                bits[0] = BIT_0;
1089:                l_encoded = BinaryCodec.toAsciiString(bits);
1090:                assertEquals("0000000000000001", l_encoded);
1091:                bits = new byte[2];
1092:                bits[0] = BIT_0 | BIT_1;
1093:                l_encoded = BinaryCodec.toAsciiString(bits);
1094:                assertEquals("0000000000000011", l_encoded);
1095:                bits = new byte[2];
1096:                bits[0] = BIT_0 | BIT_1 | BIT_2;
1097:                l_encoded = BinaryCodec.toAsciiString(bits);
1098:                assertEquals("0000000000000111", l_encoded);
1099:                bits = new byte[2];
1100:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
1101:                l_encoded = BinaryCodec.toAsciiString(bits);
1102:                assertEquals("0000000000001111", l_encoded);
1103:                bits = new byte[2];
1104:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
1105:                l_encoded = BinaryCodec.toAsciiString(bits);
1106:                assertEquals("0000000000011111", l_encoded);
1107:                bits = new byte[2];
1108:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
1109:                l_encoded = BinaryCodec.toAsciiString(bits);
1110:                assertEquals("0000000000111111", l_encoded);
1111:                bits = new byte[2];
1112:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
1113:                l_encoded = BinaryCodec.toAsciiString(bits);
1114:                assertEquals("0000000001111111", l_encoded);
1115:                bits = new byte[2];
1116:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1117:                        | BIT_6 | BIT_7);
1118:                l_encoded = BinaryCodec.toAsciiString(bits);
1119:                assertEquals("0000000011111111", l_encoded);
1120:                // work on the other byte now
1121:                bits = new byte[2];
1122:                bits[1] = BIT_0;
1123:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1124:                        | BIT_6 | BIT_7);
1125:                l_encoded = BinaryCodec.toAsciiString(bits);
1126:                assertEquals("0000000111111111", l_encoded);
1127:                bits = new byte[2];
1128:                bits[1] = BIT_0 | BIT_1;
1129:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1130:                        | BIT_6 | BIT_7);
1131:                l_encoded = BinaryCodec.toAsciiString(bits);
1132:                assertEquals("0000001111111111", l_encoded);
1133:                bits = new byte[2];
1134:                bits[1] = BIT_0 | BIT_1 | BIT_2;
1135:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1136:                        | BIT_6 | BIT_7);
1137:                l_encoded = BinaryCodec.toAsciiString(bits);
1138:                assertEquals("0000011111111111", l_encoded);
1139:                bits = new byte[2];
1140:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
1141:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1142:                        | BIT_6 | BIT_7);
1143:                l_encoded = BinaryCodec.toAsciiString(bits);
1144:                assertEquals("0000111111111111", l_encoded);
1145:                bits = new byte[2];
1146:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
1147:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1148:                        | BIT_6 | BIT_7);
1149:                l_encoded = BinaryCodec.toAsciiString(bits);
1150:                assertEquals("0001111111111111", l_encoded);
1151:                bits = new byte[2];
1152:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
1153:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1154:                        | BIT_6 | BIT_7);
1155:                l_encoded = BinaryCodec.toAsciiString(bits);
1156:                assertEquals("0011111111111111", l_encoded);
1157:                bits = new byte[2];
1158:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
1159:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1160:                        | BIT_6 | BIT_7);
1161:                l_encoded = BinaryCodec.toAsciiString(bits);
1162:                assertEquals("0111111111111111", l_encoded);
1163:                bits = new byte[2];
1164:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1165:                        | BIT_6 | BIT_7);
1166:                bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1167:                        | BIT_6 | BIT_7);
1168:                l_encoded = BinaryCodec.toAsciiString(bits);
1169:                assertEquals("1111111111111111", l_encoded);
1170:            }
1171:
1172:            // ------------------------------------------------------------------------
1173:            //
1174:            // Test encode(Object)
1175:            //
1176:            // ------------------------------------------------------------------------
1177:            /*
1178:             * Tests for Object encode(Object)
1179:             */
1180:            public void testEncodeObjectNull() throws Exception {
1181:                Object obj = new byte[0];
1182:                assertEquals(0, ((char[]) instance.encode(obj)).length);
1183:            }
1184:
1185:            /*
1186:             * Tests for Object encode(Object)
1187:             */
1188:            public void testEncodeObjectException() {
1189:                try {
1190:                    instance.encode("");
1191:                } catch (EncoderException e) {
1192:                    // all is well.
1193:                    return;
1194:                }
1195:                fail("Expected EncoderException");
1196:            }
1197:
1198:            /*
1199:             * Tests for Object encode(Object)
1200:             */
1201:            public void testEncodeObject() throws Exception {
1202:                // With a single raw binary
1203:                byte[] bits = new byte[1];
1204:                String l_encoded = new String((char[]) instance
1205:                        .encode((Object) bits));
1206:                assertEquals("00000000", l_encoded);
1207:                bits = new byte[1];
1208:                bits[0] = BIT_0;
1209:                l_encoded = new String((char[]) instance.encode((Object) bits));
1210:                assertEquals("00000001", l_encoded);
1211:                bits = new byte[1];
1212:                bits[0] = BIT_0 | BIT_1;
1213:                l_encoded = new String((char[]) instance.encode((Object) bits));
1214:                assertEquals("00000011", l_encoded);
1215:                bits = new byte[1];
1216:                bits[0] = BIT_0 | BIT_1 | BIT_2;
1217:                l_encoded = new String((char[]) instance.encode((Object) bits));
1218:                assertEquals("00000111", l_encoded);
1219:                bits = new byte[1];
1220:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
1221:                l_encoded = new String((char[]) instance.encode((Object) bits));
1222:                assertEquals("00001111", l_encoded);
1223:                bits = new byte[1];
1224:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
1225:                l_encoded = new String((char[]) instance.encode((Object) bits));
1226:                assertEquals("00011111", l_encoded);
1227:                bits = new byte[1];
1228:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
1229:                l_encoded = new String((char[]) instance.encode((Object) bits));
1230:                assertEquals("00111111", l_encoded);
1231:                bits = new byte[1];
1232:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
1233:                l_encoded = new String((char[]) instance.encode((Object) bits));
1234:                assertEquals("01111111", l_encoded);
1235:                bits = new byte[1];
1236:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1237:                        | BIT_6 | BIT_7);
1238:                l_encoded = new String((char[]) instance.encode((Object) bits));
1239:                assertEquals("11111111", l_encoded);
1240:                // With a two raw binaries
1241:                bits = new byte[2];
1242:                l_encoded = new String((char[]) instance.encode((Object) bits));
1243:                assertEquals("0000000000000000", l_encoded);
1244:                bits = new byte[2];
1245:                bits[0] = BIT_0;
1246:                l_encoded = new String((char[]) instance.encode((Object) bits));
1247:                assertEquals("0000000000000001", l_encoded);
1248:                bits = new byte[2];
1249:                bits[0] = BIT_0 | BIT_1;
1250:                l_encoded = new String((char[]) instance.encode((Object) bits));
1251:                assertEquals("0000000000000011", l_encoded);
1252:                bits = new byte[2];
1253:                bits[0] = BIT_0 | BIT_1 | BIT_2;
1254:                l_encoded = new String((char[]) instance.encode((Object) bits));
1255:                assertEquals("0000000000000111", l_encoded);
1256:                bits = new byte[2];
1257:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
1258:                l_encoded = new String((char[]) instance.encode((Object) bits));
1259:                assertEquals("0000000000001111", l_encoded);
1260:                bits = new byte[2];
1261:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
1262:                l_encoded = new String((char[]) instance.encode((Object) bits));
1263:                assertEquals("0000000000011111", l_encoded);
1264:                bits = new byte[2];
1265:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
1266:                l_encoded = new String((char[]) instance.encode((Object) bits));
1267:                assertEquals("0000000000111111", l_encoded);
1268:                bits = new byte[2];
1269:                bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
1270:                l_encoded = new String((char[]) instance.encode((Object) bits));
1271:                assertEquals("0000000001111111", l_encoded);
1272:                bits = new byte[2];
1273:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1274:                        | BIT_6 | BIT_7);
1275:                l_encoded = new String((char[]) instance.encode((Object) bits));
1276:                assertEquals("0000000011111111", l_encoded);
1277:                // work on the other byte now
1278:                bits = new byte[2];
1279:                bits[1] = BIT_0;
1280:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1281:                        | BIT_6 | BIT_7);
1282:                l_encoded = new String((char[]) instance.encode((Object) bits));
1283:                assertEquals("0000000111111111", l_encoded);
1284:                bits = new byte[2];
1285:                bits[1] = BIT_0 | BIT_1;
1286:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1287:                        | BIT_6 | BIT_7);
1288:                l_encoded = new String((char[]) instance.encode((Object) bits));
1289:                assertEquals("0000001111111111", l_encoded);
1290:                bits = new byte[2];
1291:                bits[1] = BIT_0 | BIT_1 | BIT_2;
1292:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1293:                        | BIT_6 | BIT_7);
1294:                l_encoded = new String((char[]) instance.encode((Object) bits));
1295:                assertEquals("0000011111111111", l_encoded);
1296:                bits = new byte[2];
1297:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
1298:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1299:                        | BIT_6 | BIT_7);
1300:                l_encoded = new String((char[]) instance.encode((Object) bits));
1301:                assertEquals("0000111111111111", l_encoded);
1302:                bits = new byte[2];
1303:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
1304:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1305:                        | BIT_6 | BIT_7);
1306:                l_encoded = new String((char[]) instance.encode((Object) bits));
1307:                assertEquals("0001111111111111", l_encoded);
1308:                bits = new byte[2];
1309:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
1310:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1311:                        | BIT_6 | BIT_7);
1312:                l_encoded = new String((char[]) instance.encode((Object) bits));
1313:                assertEquals("0011111111111111", l_encoded);
1314:                bits = new byte[2];
1315:                bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
1316:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1317:                        | BIT_6 | BIT_7);
1318:                l_encoded = new String((char[]) instance.encode((Object) bits));
1319:                assertEquals("0111111111111111", l_encoded);
1320:                bits = new byte[2];
1321:                bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1322:                        | BIT_6 | BIT_7);
1323:                bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5
1324:                        | BIT_6 | BIT_7);
1325:                l_encoded = new String((char[]) instance.encode((Object) bits));
1326:                assertEquals("1111111111111111", l_encoded);
1327:            }
1328:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.