Source Code Cross Referenced for TestStringUtil.java in  » Collaboration » poi-3.0.2-beta2 » org » apache » poi » util » 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 » Collaboration » poi 3.0.2 beta2 » org.apache.poi.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* ====================================================================
002:         Licensed to the Apache Software Foundation (ASF) under one or more
003:         contributor license agreements.  See the NOTICE file distributed with
004:         this work for additional information regarding copyright ownership.
005:         The ASF licenses this file to You under the Apache License, Version 2.0
006:         (the "License"); you may not use this file except in compliance with
007:         the License.  You may obtain a copy of the License at
008:
009:         http://www.apache.org/licenses/LICENSE-2.0
010:
011:         Unless required by applicable law or agreed to in writing, software
012:         distributed under the License is distributed on an "AS IS" BASIS,
013:         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         See the License for the specific language governing permissions and
015:         limitations under the License.
016:         ==================================================================== */
017:
018:        package org.apache.poi.util;
019:
020:        import junit.framework.*;
021:
022:        import java.text.NumberFormat;
023:
024:        /**
025:         * Unit test for StringUtil
026:         *
027:         * @author  Marc Johnson (mjohnson at apache dot org
028:         * @author  Glen Stampoultzis (glens at apache.org)
029:         * @author  Sergei Kozello (sergeikozello at mail.ru)
030:         */
031:        public class TestStringUtil extends TestCase {
032:            /**
033:             * Creates new TestStringUtil
034:             *
035:             * @param name
036:             */
037:            public TestStringUtil(String name) {
038:                super (name);
039:            }
040:
041:            /**
042:             * test simple form of getFromUnicode
043:             */
044:            public void testSimpleGetFromUnicode() {
045:                byte[] test_data = new byte[32];
046:                int index = 0;
047:
048:                for (int k = 0; k < 16; k++) {
049:                    test_data[index++] = (byte) 0;
050:                    test_data[index++] = (byte) ('a' + k);
051:                }
052:
053:                assertEquals("abcdefghijklmnop", StringUtil
054:                        .getFromUnicodeBE(test_data));
055:            }
056:
057:            /**
058:             * test simple form of getFromUnicode with symbols with code below and more 127
059:             */
060:            public void testGetFromUnicodeSymbolsWithCodesMoreThan127() {
061:                byte[] test_data = new byte[] { 0x04, 0x22, 0x04, 0x35, 0x04,
062:                        0x41, 0x04, 0x42, 0x00, 0x20, 0x00, 0x74, 0x00, 0x65,
063:                        0x00, 0x73, 0x00, 0x74, };
064:
065:                assertEquals("\u0422\u0435\u0441\u0442 test", StringUtil
066:                        .getFromUnicodeBE(test_data));
067:            }
068:
069:            /**
070:             * test getFromUnicodeHigh for symbols with code below and more 127
071:             */
072:            public void testGetFromUnicodeHighSymbolsWithCodesMoreThan127() {
073:                byte[] test_data = new byte[] { 0x22, 0x04, 0x35, 0x04, 0x41,
074:                        0x04, 0x42, 0x04, 0x20, 0x00, 0x74, 0x00, 0x65, 0x00,
075:                        0x73, 0x00, 0x74, 0x00, };
076:
077:                assertEquals("\u0422\u0435\u0441\u0442 test", StringUtil
078:                        .getFromUnicodeLE(test_data));
079:            }
080:
081:            /**
082:             * Test more complex form of getFromUnicode
083:             */
084:            public void testComplexGetFromUnicode() {
085:                byte[] test_data = new byte[32];
086:                int index = 0;
087:                for (int k = 0; k < 16; k++) {
088:                    test_data[index++] = (byte) 0;
089:                    test_data[index++] = (byte) ('a' + k);
090:                }
091:                assertEquals("abcdefghijklmno", StringUtil.getFromUnicodeBE(
092:                        test_data, 0, 15));
093:                assertEquals("bcdefghijklmnop", StringUtil.getFromUnicodeBE(
094:                        test_data, 2, 15));
095:                try {
096:                    StringUtil.getFromUnicodeBE(test_data, -1, 16);
097:                    fail("Should have caught ArrayIndexOutOfBoundsException");
098:                } catch (ArrayIndexOutOfBoundsException ignored) {
099:                    // as expected
100:                }
101:
102:                try {
103:                    StringUtil.getFromUnicodeBE(test_data, 32, 16);
104:                    fail("Should have caught ArrayIndexOutOfBoundsException");
105:                } catch (ArrayIndexOutOfBoundsException ignored) {
106:                    // as expected
107:                }
108:
109:                try {
110:                    StringUtil.getFromUnicodeBE(test_data, 1, 16);
111:                    fail("Should have caught IllegalArgumentException");
112:                } catch (IllegalArgumentException ignored) {
113:                    // as expected
114:                }
115:
116:                try {
117:                    StringUtil.getFromUnicodeBE(test_data, 1, -1);
118:                    fail("Should have caught IllegalArgumentException");
119:                } catch (IllegalArgumentException ignored) {
120:                    // as expected
121:                }
122:            }
123:
124:            /**
125:             * Test putCompressedUnicode
126:             */
127:            public void testPutCompressedUnicode() throws Exception {
128:                byte[] output = new byte[100];
129:                byte[] expected_output = { (byte) 'H', (byte) 'e', (byte) 'l',
130:                        (byte) 'l', (byte) 'o', (byte) ' ', (byte) 'W',
131:                        (byte) 'o', (byte) 'r', (byte) 'l', (byte) 'd',
132:                        (byte) 0xAE };
133:                String input = new String(expected_output, StringUtil
134:                        .getPreferredEncoding());
135:
136:                StringUtil.putCompressedUnicode(input, output, 0);
137:                for (int j = 0; j < expected_output.length; j++) {
138:                    assertEquals("testing offset " + j, expected_output[j],
139:                            output[j]);
140:                }
141:                StringUtil.putCompressedUnicode(input, output,
142:                        100 - expected_output.length);
143:                for (int j = 0; j < expected_output.length; j++) {
144:                    assertEquals("testing offset " + j, expected_output[j],
145:                            output[100 + j - expected_output.length]);
146:                }
147:                try {
148:                    StringUtil.putCompressedUnicode(input, output,
149:                            101 - expected_output.length);
150:                    fail("Should have caught ArrayIndexOutOfBoundsException");
151:                } catch (ArrayIndexOutOfBoundsException ignored) {
152:                    // as expected
153:                }
154:            }
155:
156:            /**
157:             * Test putUncompressedUnicode
158:             */
159:            public void testPutUncompressedUnicode() {
160:                byte[] output = new byte[100];
161:                String input = "Hello World";
162:                byte[] expected_output = { (byte) 'H', (byte) 0, (byte) 'e',
163:                        (byte) 0, (byte) 'l', (byte) 0, (byte) 'l', (byte) 0,
164:                        (byte) 'o', (byte) 0, (byte) ' ', (byte) 0, (byte) 'W',
165:                        (byte) 0, (byte) 'o', (byte) 0, (byte) 'r', (byte) 0,
166:                        (byte) 'l', (byte) 0, (byte) 'd', (byte) 0 };
167:
168:                StringUtil.putUnicodeLE(input, output, 0);
169:                for (int j = 0; j < expected_output.length; j++) {
170:                    assertEquals("testing offset " + j, expected_output[j],
171:                            output[j]);
172:                }
173:                StringUtil.putUnicodeLE(input, output,
174:                        100 - expected_output.length);
175:                for (int j = 0; j < expected_output.length; j++) {
176:                    assertEquals("testing offset " + j, expected_output[j],
177:                            output[100 + j - expected_output.length]);
178:                }
179:                try {
180:                    StringUtil.putUnicodeLE(input, output,
181:                            101 - expected_output.length);
182:                    fail("Should have caught ArrayIndexOutOfBoundsException");
183:                } catch (ArrayIndexOutOfBoundsException ignored) {
184:                    // as expected
185:                }
186:            }
187:
188:            public void testFormat() throws Exception {
189:                assertEquals("This is a test " + fmt(1.2345, 2, 2), StringUtil
190:                        .format("This is a test %2.2",
191:                                new Object[] { new Double(1.2345) }));
192:                assertEquals("This is a test " + fmt(1.2345, -1, 3), StringUtil
193:                        .format("This is a test %.3",
194:                                new Object[] { new Double(1.2345) }));
195:                assertEquals("This is a great test " + fmt(1.2345, -1, 3),
196:                        StringUtil.format("This is a % test %.3", new Object[] {
197:                                "great", new Double(1.2345) }));
198:                assertEquals("This is a test 1", StringUtil.format(
199:                        "This is a test %", new Object[] { new Integer(1) }));
200:                assertEquals("This is a test 1", StringUtil.format(
201:                        "This is a test %", new Object[] { new Integer(1),
202:                                new Integer(1) }));
203:                assertEquals("This is a test 1.x", StringUtil.format(
204:                        "This is a test %1.x", new Object[] { new Integer(1) }));
205:                assertEquals("This is a test ?missing data?1.x", StringUtil
206:                        .format("This is a test %1.x", new Object[] {}));
207:                assertEquals("This is a test %1.x", StringUtil.format(
208:                        "This is a test \\%1.x", new Object[] {}));
209:            }
210:
211:            private String fmt(double num, int minIntDigits, int maxFracDigitis) {
212:                NumberFormat nf = NumberFormat.getInstance();
213:
214:                if (minIntDigits != -1) {
215:                    nf.setMinimumIntegerDigits(minIntDigits);
216:                }
217:                if (maxFracDigitis != -1) {
218:                    nf.setMaximumFractionDigits(maxFracDigitis);
219:                }
220:
221:                return nf.format(num);
222:            }
223:
224:            /**
225:             * main
226:             *
227:             * @param ignored_args
228:             */
229:            public static void main(String[] ignored_args) {
230:                System.out.println("Testing util.StringUtil functionality");
231:                junit.textui.TestRunner.run(TestStringUtil.class);
232:            }
233:
234:            /**
235:             * @see junit.framework.TestCase#setUp()
236:             */
237:            protected void setUp() throws Exception {
238:                super .setUp();
239:
240:                // System.setProperty()
241:            }
242:
243:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.