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 distributedcache;
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: if (b1.limit() != b2.limit()) {
132: return false;
133: }
134:
135: if (b1.position() != b2.position()) {
136: return false;
137: }
138:
139: if (b1.capacity() != b2.capacity()) {
140: return false;
141: }
142:
143: for (int i = 0; i < b1.limit(); i++) {
144: if (b1.get(i) != b2.get(i)) {
145: return false;
146: }
147: }
148:
149: return true;
150: }
151:
152: public static void sleep(int sleepTime) {
153: try {
154: Thread.sleep(sleepTime);
155: } catch (InterruptedException ignore) {
156: }
157: }
158:
159: public static byte[] mergeByteArrays(byte[] b1, byte[] b2) {
160: byte[] result = new byte[b1.length + b2.length];
161: System.arraycopy(b1, 0, result, 0, b1.length);
162: System.arraycopy(b2, 0, result, b1.length, b2.length);
163:
164: return result;
165: }
166:
167: public static byte[] toArray(ByteBuffer buffer) {
168:
169: byte[] array = new byte[buffer.limit() - buffer.position()];
170:
171: if (buffer.hasArray()) {
172: int offset = buffer.arrayOffset();
173: byte[] bufferArray = buffer.array();
174: System.arraycopy(bufferArray, offset, array, 0,
175: array.length);
176:
177: return array;
178: } else {
179: buffer.get(array);
180: return array;
181: }
182: }
183:
184: public static void setLogLevel(Level level) {
185: setLogLevel("org.xsocket", level);
186: }
187:
188: public static void setLogLevel(String namespace, Level level) {
189: Handler[] hdl = Logger.getLogger("").getHandlers();
190: for (Handler handler : hdl) {
191: if (handler instanceof ConsoleHandler) {
192: handler.setFormatter(new LogFormatter());
193: handler.setLevel(level);
194: }
195: }
196:
197: Logger logger = Logger.getLogger(namespace);
198: logger.setLevel(level);
199:
200: }
201:
202: public static void assertTimeout(long elapsed, long min, long max) {
203: System.out.println("elapsed time " + elapsed + " (min=" + min
204: + ", max=" + max + ")");
205: Assert.assertTrue("elapsed time " + elapsed
206: + " out of range (min=" + min + ", max=" + max + ")",
207: (elapsed >= min) && (elapsed <= max));
208: }
209:
210: public static InetAddress getRandomLocalAddress()
211: throws IOException {
212: String hostname = InetAddress.getLocalHost().getHostName();
213: InetAddress[] addresses = InetAddress.getAllByName(hostname);
214:
215: int i = new Random().nextInt();
216: if (i < 0) {
217: i = 0 - i;
218: }
219:
220: i = i % addresses.length;
221:
222: return addresses[i];
223: }
224: }
|