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.mail;
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 generateByteBuffer(int length) {
084: ByteBuffer buffer = ByteBuffer.wrap(generateByteArray(length));
085: return buffer;
086: }
087:
088: public static ByteBuffer generateDirectByteBuffer(int length) {
089: byte[] bytes = generateByteArray(length);
090: ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length);
091: buffer.put(bytes);
092: buffer.flip();
093: return buffer;
094: }
095:
096: public static ByteBuffer[] generateDirectByteBufferArray(
097: int elements, int length) {
098: ByteBuffer[] byteBufferArray = new ByteBuffer[elements];
099: for (int i = 0; i < elements; i++) {
100: byteBufferArray[i] = generateDirectByteBuffer(length);
101: }
102:
103: return byteBufferArray;
104: }
105:
106: public static byte[] generateByteArray(int length) {
107:
108: byte[] bytes = new byte[length];
109:
110: int item = OFFSET;
111:
112: for (int i = 0; i < length; i++) {
113: bytes[i] = (byte) item;
114:
115: item++;
116: if (item > (OFFSET + 9)) {
117: item = OFFSET;
118: }
119: }
120:
121: return bytes;
122: }
123:
124: public static byte[] generateByteArray(int length, String delimiter) {
125: byte[] del = delimiter.getBytes();
126: byte[] data = generateByteArray(length);
127:
128: byte[] result = new byte[del.length + data.length];
129: System.arraycopy(data, 0, result, 0, data.length);
130: System.arraycopy(del, 0, result, data.length, del.length);
131: return result;
132: }
133:
134: public static boolean isEquals(byte[] b1, byte[] b2) {
135: if (b1.length != b2.length) {
136: return false;
137: }
138:
139: for (int i = 0; i < b1.length; i++) {
140: if (b1[i] != b2[i]) {
141: return false;
142: }
143: }
144:
145: return true;
146: }
147:
148: public static boolean isEquals(ByteBuffer b1, ByteBuffer b2) {
149: if (b1.limit() != b2.limit()) {
150: return false;
151: }
152:
153: if (b1.position() != b2.position()) {
154: return false;
155: }
156:
157: if (b1.capacity() != b2.capacity()) {
158: return false;
159: }
160:
161: for (int i = 0; i < b1.limit(); i++) {
162: if (b1.get(i) != b2.get(i)) {
163: return false;
164: }
165: }
166:
167: return true;
168: }
169:
170: public static void sleep(int sleepTime) {
171: try {
172: Thread.sleep(sleepTime);
173: } catch (InterruptedException ignore) {
174: }
175: }
176:
177: public static byte[] mergeByteArrays(byte[] b1, byte[] b2) {
178: byte[] result = new byte[b1.length + b2.length];
179: System.arraycopy(b1, 0, result, 0, b1.length);
180: System.arraycopy(b2, 0, result, b1.length, b2.length);
181:
182: return result;
183: }
184:
185: public static byte[] toArray(ByteBuffer buffer) {
186:
187: byte[] array = new byte[buffer.limit() - buffer.position()];
188:
189: if (buffer.hasArray()) {
190: int offset = buffer.arrayOffset();
191: byte[] bufferArray = buffer.array();
192: System.arraycopy(bufferArray, offset, array, 0,
193: array.length);
194:
195: return array;
196: } else {
197: buffer.get(array);
198: return array;
199: }
200: }
201:
202: public static void setLogLevel(Level level) {
203: setLogLevel("org.xsocket", level);
204: }
205:
206: public static void setLogLevel(String namespace, Level level) {
207: Handler[] hdl = Logger.getLogger("").getHandlers();
208: for (Handler handler : hdl) {
209: if (handler instanceof ConsoleHandler) {
210: handler.setFormatter(new LogFormatter());
211: handler.setLevel(level);
212: }
213: }
214:
215: Logger logger = Logger.getLogger(namespace);
216: logger.setLevel(level);
217:
218: }
219:
220: public static void assertTimeout(long elapsed, long min, long max) {
221: System.out.println("elapsed time " + elapsed + " (min=" + min
222: + ", max=" + max + ")");
223: Assert.assertTrue("elapsed time " + elapsed
224: + " out of range (min=" + min + ", max=" + max + ")",
225: (elapsed >= min) && (elapsed <= max));
226: }
227:
228: public static InetAddress getRandomLocalAddress()
229: throws IOException {
230: String hostname = InetAddress.getLocalHost().getHostName();
231: InetAddress[] addresses = InetAddress.getAllByName(hostname);
232:
233: int i = new Random().nextInt();
234: if (i < 0) {
235: i = 0 - i;
236: }
237:
238: i = i % addresses.length;
239:
240: return addresses[i];
241: }
242: }
|