[jbpm-commits] JBoss JBPM SVN: r4461 - jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/console.
do-not-reply at jboss.org
do-not-reply at jboss.org
Mon Apr 6 18:49:42 EDT 2009
Author: alex.guizar at jboss.com
Date: 2009-04-06 18:49:42 -0400 (Mon, 06 Apr 2009)
New Revision: 4461
Removed:
jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/console/Base64Coder.java
jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/console/HTTP.java
Modified:
jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/console/ConsoleAvailabilityTest.java
Log:
simplify console test
Deleted: jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/console/Base64Coder.java
===================================================================
--- jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/console/Base64Coder.java 2009-04-06 22:22:55 UTC (rev 4460)
+++ jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/console/Base64Coder.java 2009-04-06 22:49:42 UTC (rev 4461)
@@ -1,147 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2006, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jbpm.enterprise.console;
-
-/**
- * A Base64 Encoder/Decoder.
- */
-public class Base64Coder
-{
-
- // Mapping table from 6-bit nibbles to Base64 characters.
- private static char[] map1 = new char[64];
- static {
- int i=0;
- for (char c='A'; c<='Z'; c++) map1[i++] = c;
- for (char c='a'; c<='z'; c++) map1[i++] = c;
- for (char c='0'; c<='9'; c++) map1[i++] = c;
- map1[i++] = '+'; map1[i++] = '/'; }
-
- // Mapping table from Base64 characters to 6-bit nibbles.
- private static byte[] map2 = new byte[128];
- static {
- for (int i=0; i<map2.length; i++) map2[i] = -1;
- for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; }
-
- /**
- * Encodes a string into Base64 format.
- * No blanks or line breaks are inserted.
- * @param s a String to be encoded.
- * @return A String with the Base64 encoded data.
- */
- public static String encodeString (String s) {
- return new String(encode(s.getBytes())); }
-
- /**
- * Encodes a byte array into Base64 format.
- * No blanks or line breaks are inserted.
- * @param in an array containing the data bytes to be encoded.
- * @return A character array with the Base64 encoded data.
- */
- public static char[] encode (byte[] in) {
- return encode(in,in.length); }
-
- /**
- * Encodes a byte array into Base64 format.
- * No blanks or line breaks are inserted.
- * @param in an array containing the data bytes to be encoded.
- * @param iLen number of bytes to process in <code>in</code>.
- * @return A character array with the Base64 encoded data.
- */
- public static char[] encode (byte[] in, int iLen) {
- int oDataLen = (iLen*4+2)/3; // output length without padding
- int oLen = ((iLen+2)/3)*4; // output length including padding
- char[] out = new char[oLen];
- int ip = 0;
- int op = 0;
- while (ip < iLen) {
- int i0 = in[ip++] & 0xff;
- int i1 = ip < iLen ? in[ip++] & 0xff : 0;
- int i2 = ip < iLen ? in[ip++] & 0xff : 0;
- int o0 = i0 >>> 2;
- int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
- int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
- int o3 = i2 & 0x3F;
- out[op++] = map1[o0];
- out[op++] = map1[o1];
- out[op] = op < oDataLen ? map1[o2] : '='; op++;
- out[op] = op < oDataLen ? map1[o3] : '='; op++; }
- return out; }
-
- /**
- * Decodes a string from Base64 format.
- * @param s a Base64 String to be decoded.
- * @return A String containing the decoded data.
- * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
- */
- public static String decodeString (String s) {
- return new String(decode(s)); }
-
- /**
- * Decodes a byte array from Base64 format.
- * @param s a Base64 String to be decoded.
- * @return An array containing the decoded data bytes.
- * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
- */
- public static byte[] decode (String s) {
- return decode(s.toCharArray()); }
-
- /**
- * Decodes a byte array from Base64 format.
- * No blanks or line breaks are allowed within the Base64 encoded data.
- * @param in a character array containing the Base64 encoded data.
- * @return An array containing the decoded data bytes.
- * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
- */
- public static byte[] decode (char[] in) {
- int iLen = in.length;
- if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4.");
- while (iLen > 0 && in[iLen-1] == '=') iLen--;
- int oLen = (iLen*3) / 4;
- byte[] out = new byte[oLen];
- int ip = 0;
- int op = 0;
- while (ip < iLen) {
- int i0 = in[ip++];
- int i1 = in[ip++];
- int i2 = ip < iLen ? in[ip++] : 'A';
- int i3 = ip < iLen ? in[ip++] : 'A';
- if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
- throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
- int b0 = map2[i0];
- int b1 = map2[i1];
- int b2 = map2[i2];
- int b3 = map2[i3];
- if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
- throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
- int o0 = ( b0 <<2) | (b1>>>4);
- int o1 = ((b1 & 0xf)<<4) | (b2>>>2);
- int o2 = ((b2 & 3)<<6) | b3;
- out[op++] = (byte)o0;
- if (op<oLen) out[op++] = (byte)o1;
- if (op<oLen) out[op++] = (byte)o2; }
- return out; }
-
- // Dummy constructor.
- private Base64Coder() {}
-
-} // end class Base64Coder
Modified: jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/console/ConsoleAvailabilityTest.java
===================================================================
--- jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/console/ConsoleAvailabilityTest.java 2009-04-06 22:22:55 UTC (rev 4460)
+++ jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/console/ConsoleAvailabilityTest.java 2009-04-06 22:49:42 UTC (rev 4461)
@@ -21,6 +21,14 @@
*/
package org.jbpm.enterprise.console;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
import junit.framework.TestCase;
/**
@@ -28,20 +36,69 @@
*/
public class ConsoleAvailabilityTest extends TestCase
{
-
/**
* Verify if the console has been deployed successfully
- * @throws Exception
*/
- public void testConsoleDeployment()
- throws Exception
+ public void testConsoleDeployment() throws IOException
{
+ String host = "localhost";
+ int port = 8080;
+
String bindAddress = System.getProperty("jboss.bind.address");
- String host = bindAddress !=null ? bindAddress : "localhost:8080";
- if(host.indexOf(":")==-1) host = (host+":8080"); // default port
+ if (bindAddress != null)
+ {
+ int colonIndex = bindAddress.lastIndexOf(':');
+ if (colonIndex != -1)
+ {
+ host = bindAddress.substring(0, colonIndex);
+ port = Integer.parseInt(bindAddress.substring(colonIndex + 1));
+ }
+ else
+ host = bindAddress;
+ }
- System.out.println("Console URL: " + host);
- String response = HTTP.get("http://"+host +"/jbpm-console", null, true); // no auth
+ URL url = new URL("http", host, port, "/jbpm-console");
+ System.out.println("Console URL: " + url);
+
+ List<String> response = doGet(url);
+ assertEquals(HttpURLConnection.HTTP_OK, Integer.parseInt(response.get(0)));
}
+ /**
+ * Gets the resource pointed by the given URL.
+ * @param url a pointer to a resource
+ * @return response data in the following order:
+ * <
+ * <li>Code</li>
+ * <li>Message</li>
+ * <li>Body</li>
+ * @throws IOException
+ */
+ static List<String> doGet(URL url) throws IOException
+ {
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ try
+ {
+ List<String> response = new ArrayList<String>();
+ response.add(Integer.toString(connection.getResponseCode()));
+ response.add(connection.getResponseMessage());
+
+ BufferedReader connReader = new BufferedReader(new InputStreamReader(connection
+ .getInputStream()));
+ try
+ {
+ for (String line; (line = connReader.readLine()) != null;)
+ response.add(line);
+ }
+ finally
+ {
+ connReader.close();
+ }
+ return response;
+ }
+ finally
+ {
+ connection.disconnect();
+ }
+ }
}
Deleted: jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/console/HTTP.java
===================================================================
--- jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/console/HTTP.java 2009-04-06 22:22:55 UTC (rev 4460)
+++ jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/console/HTTP.java 2009-04-06 22:49:42 UTC (rev 4461)
@@ -1,214 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2006, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jbpm.enterprise.console;
-
-import java.io.*;
-import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.UUID;
-
-/**
- * @author Heiko.Braun <heiko.braun at jboss.com>
- */
-public class HTTP
-{
- public static String post(String urlString, InputStream inputStream, String[] credentials)
- throws Exception
- {
-
- HttpURLConnection conn = null;
- BufferedReader br = null;
- DataOutputStream dos = null;
- DataInputStream inStream = null;
-
- InputStream is = null;
- OutputStream os = null;
- boolean ret = false;
- String StrMessage = "";
-
-
- String lineEnd = "\r\n";
- String twoHyphens = "--";
- String boundary = "*****";
-
-
- int bytesRead, bytesAvailable, bufferSize;
-
- byte[] buffer;
-
- int maxBufferSize = 1*1024*1024;
-
- String responseFromServer = "";
-
- try
- {
- //------------------ CLIENT REQUEST
-
- // open a URL connection to the Servlet
-
- URL url = new URL(urlString);
-
-
- // Open a HTTP connection to the URL
-
- conn = (HttpURLConnection) url.openConnection();
-
- if(credentials!=null)
- applyCredentials(credentials, conn);
-
- // Allow Inputs
- conn.setDoInput(true);
-
- // Allow Outputs
- conn.setDoOutput(true);
-
- // Don't use a cached copy.
- conn.setUseCaches(false);
-
- // Use a post method.
- conn.setRequestMethod("POST");
-
- conn.setRequestProperty("Connection", "Keep-Alive");
-
- conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
-
- dos = new DataOutputStream( conn.getOutputStream() );
-
- dos.writeBytes(twoHyphens + boundary + lineEnd);
- dos.writeBytes("Content-Disposition: form-data; name=\"upload\";"
- + " filename=\"" + UUID.randomUUID().toString() +"\"" + lineEnd);
- dos.writeBytes(lineEnd);
-
- // create a buffer of maximum size
- bytesAvailable = inputStream.available();
- bufferSize = Math.min(bytesAvailable, maxBufferSize);
- buffer = new byte[bufferSize];
-
- // read file and write it into form...
- bytesRead = inputStream.read(buffer, 0, bufferSize);
-
- while (bytesRead > 0)
- {
- dos.write(buffer, 0, bufferSize);
- bytesAvailable = inputStream.available();
- bufferSize = Math.min(bytesAvailable, maxBufferSize);
- bytesRead = inputStream.read(buffer, 0, bufferSize);
- }
-
- // send multipart form data necesssary after file data...
-
- dos.writeBytes(lineEnd);
- dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
-
- // close streams
-
- inputStream.close();
- dos.flush();
- dos.close();
-
- }
- catch (MalformedURLException ex)
- {
- throw ex;
- }
-
- catch (IOException ioe)
- {
- throw ioe;
- }
-
-
- //------------------ read the SERVER RESPONSE
-
- StringBuffer sb = new StringBuffer();
-
- try
- {
- inStream = new DataInputStream ( conn.getInputStream() );
- String str;
- while (( str = inStream.readLine()) != null)
- {
- sb.append(str).append("");
- }
- inStream.close();
-
- }
- catch (IOException ioex)
- {
- System.out.println("From (ServerResponse): "+ioex);
-
- }
-
-
- return sb.toString();
-
- }
-
- private static void applyCredentials(String[] credentials, HttpURLConnection conn)
- {
- String userPassword = credentials[0]+":"+credentials[1];
- String encoding = new String(Base64Coder.encode (userPassword.getBytes()));
- conn.setRequestProperty ("Authorization", "Basic " + encoding);
- }
-
- public static String get(String urlString, String[] credentials, boolean throwServerError)
- {
- StringBuffer sb = new StringBuffer();
- try
- {
- URL url = new URL(urlString);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- if(credentials!=null)
- applyCredentials(credentials, conn);
-
-
- if(throwServerError && conn.getResponseCode()!=200)
- {
- throw new RuntimeException("Server response " + conn.getResponseCode());
- }
-
- BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String str;
-
-
- while ((str = in.readLine()) != null)
- {
- sb.append(str);
- }
-
- in.close();
- }
- catch (MalformedURLException e)
- {
- throw new RuntimeException(e);
- }
- catch (IOException e)
- {
- throw new RuntimeException(e);
- }
-
- return sb.toString();
- }
-}
-
-
More information about the jbpm-commits
mailing list