Source Code Cross Referenced for QAUtil.java in  » Web-Server » xsocket » org » xsocket » 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 » Web Server » xsocket » org.xsocket 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // $Id: TestUtil.java 1082 2007-03-26 18:03:35Z grro $
002:        /*
003:         *  Copyright (c) xsocket.org, 2006 - 2007. All rights reserved.
004:         *
005:         *  This library is free software; you can redistribute it and/or
006:         *  modify it under the terms of the GNU Lesser General Public
007:         *  License as published by the Free Software Foundation; either
008:         *  version 2.1 of the License, or (at your option) any later version.
009:         *
010:         *  This library is distributed in the hope that it will be useful,
011:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013:         *  Lesser General Public License for more details.
014:         *
015:         *  You should have received a copy of the GNU Lesser General Public
016:         *  License along with this library; if not, write to the Free Software
017:         *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018:         *
019:         * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
020:         * The latest copy of this software may be found on http://www.xsocket.org/
021:         */
022:        package org.xsocket;
023:
024:        import java.io.IOException;
025:        import java.net.InetAddress;
026:        import java.nio.ByteBuffer;
027:        import java.nio.CharBuffer;
028:        import java.nio.charset.Charset;
029:        import java.nio.charset.CharsetEncoder;
030:        import java.util.Random;
031:        import java.util.logging.ConsoleHandler;
032:        import java.util.logging.Handler;
033:        import java.util.logging.Level;
034:        import java.util.logging.Logger;
035:
036:        import org.junit.Assert;
037:
038:        /**
039:         *
040:         * @author grro@xsocket.org
041:         */
042:        public final class QAUtil {
043:
044:            private static String testMail = "Received: from localhost (localhost [127.0.0.1])\r\n"
045:                    + "by Semta.de with ESMTP id 881588961.1153334034540.1900236652.1\r\n"
046:                    + "for feki@semta.de; Mi, 19 Jul 2006 20:34:00 +0200\r\n"
047:                    + "Message-ID: <24807938.01153334039898.JavaMail.grro@127.0.0.1>\r\n"
048:                    + "Date: Wed, 19 Jul 2006 20:33:59 +0200 (CEST)\r\n"
049:                    + "From: feki2 <fekete99@web.de>\r\n"
050:                    + "To: Gregor Roth <feki@semta.de>\r\n"
051:                    + "Subject: Test mail\r\n"
052:                    + "MIME-Version: 1.0\r\n"
053:                    + "Content-Type: multipart/mixed;\r\n"
054:                    + "boundary=\"----=_Part_1_14867177.1153334039707\"\r\n"
055:                    + "\r\n"
056:                    + "This is a multi-part message in MIME format.\r\n"
057:                    + "------=_Part_1_14867177.1153334039707\r\n"
058:                    + "Content-Type: multipart/mixed;\r\n"
059:                    + "boundary=\"----=_Part_0_14158819.1153334039687\"\r\n"
060:                    + "\r\n"
061:                    + "------=_Part_0_14158819.1153334039687\r\n"
062:                    + "Content-Type: text/plain; charset=us-ascii\r\n"
063:                    + "Content-Transfer-Encoding: 7bit\r\n"
064:                    + "\r\n"
065:                    + "Halli Hallo\r\n"
066:                    + "------=_Part_0_14158819.1153334039687\r\n"
067:                    + "------=_Part_1_14867177.1153334039707--";
068:
069:            private static final int OFFSET = 48;
070:
071:            public static ByteBuffer getAsByteBuffer() {
072:                try {
073:                    Charset charset = Charset.forName("ISO-8859-1");
074:                    CharsetEncoder encoder = charset.newEncoder();
075:                    ByteBuffer buf = encoder.encode(CharBuffer.wrap(testMail
076:                            .toCharArray()));
077:                    return buf;
078:                } catch (Exception e) {
079:                    throw new RuntimeException(e.toString());
080:                }
081:            }
082:
083:            public static ByteBuffer generatedByteBuffer(int length) {
084:                ByteBuffer buffer = ByteBuffer.wrap(generatedByteArray(length));
085:                return buffer;
086:            }
087:
088:            public static byte[] generatedByteArray(int length) {
089:
090:                byte[] bytes = new byte[length];
091:
092:                int item = OFFSET;
093:
094:                for (int i = 0; i < length; i++) {
095:                    bytes[i] = (byte) item;
096:
097:                    item++;
098:                    if (item > (OFFSET + 9)) {
099:                        item = OFFSET;
100:                    }
101:                }
102:
103:                return bytes;
104:            }
105:
106:            public static byte[] generatedByteArray(int length, String delimiter) {
107:                byte[] del = delimiter.getBytes();
108:                byte[] data = generatedByteArray(length);
109:
110:                byte[] result = new byte[del.length + data.length];
111:                System.arraycopy(data, 0, result, 0, data.length);
112:                System.arraycopy(del, 0, result, data.length, del.length);
113:                return result;
114:            }
115:
116:            public static boolean isEquals(byte[] b1, byte[] b2) {
117:                if (b1.length != b2.length) {
118:                    return false;
119:                }
120:
121:                for (int i = 0; i < b1.length; i++) {
122:                    if (b1[i] != b2[i]) {
123:                        return false;
124:                    }
125:                }
126:
127:                return true;
128:            }
129:
130:            public static boolean isEquals(ByteBuffer[] b1, ByteBuffer[] b2) {
131:                return isEquals(DataConverter.toByteBuffer(b1), DataConverter
132:                        .toByteBuffer(b2));
133:            }
134:
135:            public static boolean isEquals(ByteBuffer b1, ByteBuffer[] b2) {
136:                return isEquals(b1, DataConverter.toByteBuffer(b2));
137:            }
138:
139:            public static boolean isEquals(ByteBuffer b1, ByteBuffer b2) {
140:                if (b1.limit() != b2.limit()) {
141:                    return false;
142:                }
143:
144:                if (b1.position() != b2.position()) {
145:                    return false;
146:                }
147:
148:                if (b1.capacity() != b2.capacity()) {
149:                    return false;
150:                }
151:
152:                for (int i = 0; i < b1.limit(); i++) {
153:                    if (b1.get(i) != b2.get(i)) {
154:                        return false;
155:                    }
156:                }
157:
158:                return true;
159:            }
160:
161:            public static void sleep(int sleepTime) {
162:                try {
163:                    Thread.sleep(sleepTime);
164:                } catch (InterruptedException ignore) {
165:                }
166:            }
167:
168:            public static byte[] mergeByteArrays(byte[] b1, byte[] b2) {
169:                byte[] result = new byte[b1.length + b2.length];
170:                System.arraycopy(b1, 0, result, 0, b1.length);
171:                System.arraycopy(b2, 0, result, b1.length, b2.length);
172:
173:                return result;
174:            }
175:
176:            public static byte[] toArray(ByteBuffer buffer) {
177:
178:                byte[] array = new byte[buffer.limit() - buffer.position()];
179:
180:                if (buffer.hasArray()) {
181:                    int offset = buffer.arrayOffset();
182:                    byte[] bufferArray = buffer.array();
183:                    System.arraycopy(bufferArray, offset, array, 0,
184:                            array.length);
185:
186:                    return array;
187:                } else {
188:                    buffer.get(array);
189:                    return array;
190:                }
191:            }
192:
193:            public static void setLogLevel(Level level) {
194:                setLogLevel("org.xsocket", level);
195:            }
196:
197:            public static void setLogLevel(String namespace, Level level) {
198:                Handler[] hdl = Logger.getLogger("").getHandlers();
199:                for (Handler handler : hdl) {
200:                    if (handler instanceof  ConsoleHandler) {
201:                        handler.setFormatter(new LogFormatter());
202:                        handler.setLevel(level);
203:                    }
204:                }
205:
206:                Logger logger = Logger.getLogger(namespace);
207:                logger.setLevel(level);
208:
209:            }
210:
211:            public static void assertTimeout(long elapsed, long min, long max) {
212:                System.out.println("elapsed time " + elapsed + " (min=" + min
213:                        + ", max=" + max + ")");
214:                Assert.assertTrue("elapsed time " + elapsed
215:                        + " out of range (min=" + min + ", max=" + max + ")",
216:                        (elapsed >= min) && (elapsed <= max));
217:            }
218:
219:            public static InetAddress getRandomLocalAddress()
220:                    throws IOException {
221:                String hostname = InetAddress.getLocalHost().getHostName();
222:                InetAddress[] addresses = InetAddress.getAllByName(hostname);
223:
224:                int i = new Random().nextInt();
225:                if (i < 0) {
226:                    i = 0 - i;
227:                }
228:
229:                i = i % addresses.length;
230:
231:                return addresses[i];
232:            }
233:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.