Return-Path: Delivered-To: apmail-incubator-ace-commits-archive@minotaur.apache.org Received: (qmail 10534 invoked from network); 30 Apr 2010 18:54:21 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 30 Apr 2010 18:54:21 -0000 Received: (qmail 93991 invoked by uid 500); 30 Apr 2010 18:54:21 -0000 Delivered-To: apmail-incubator-ace-commits-archive@incubator.apache.org Received: (qmail 93971 invoked by uid 500); 30 Apr 2010 18:54:21 -0000 Mailing-List: contact ace-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: ace-dev@incubator.apache.org Delivered-To: mailing list ace-commits@incubator.apache.org Received: (qmail 93963 invoked by uid 99); 30 Apr 2010 18:54:21 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 30 Apr 2010 18:54:21 +0000 X-ASF-Spam-Status: No, hits=-1939.7 required=10.0 tests=ALL_TRUSTED,AWL X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 30 Apr 2010 18:54:19 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id B9F6A23888E3; Fri, 30 Apr 2010 18:53:29 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r939777 - in /incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test: ./ utils/ utils/FileUtils.java utils/NetUtils.java utils/TestUtils.java Date: Fri, 30 Apr 2010 18:53:29 -0000 To: ace-commits@incubator.apache.org From: marrs@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100430185329.B9F6A23888E3@eris.apache.org> Author: marrs Date: Fri Apr 30 18:53:29 2010 New Revision: 939777 URL: http://svn.apache.org/viewvc?rev=939777&view=rev Log: Added some test utilities. Added: incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/ incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/ incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/FileUtils.java incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/NetUtils.java incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/TestUtils.java Added: incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/FileUtils.java URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/FileUtils.java?rev=939777&view=auto ============================================================================== --- incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/FileUtils.java (added) +++ incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/FileUtils.java Fri Apr 30 18:53:29 2010 @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.ace.test.utils; + +import java.io.File; +import java.io.IOException; + +public class FileUtils { + + /** + * Convenience method for creating temp files. + * It creates a temp file, and then deletes it. This is done so the same (unique) filename can be used to create a directory. + * + * If you use null as the baseDirectoryName, a tempfile is created in the platform specific temp directory. + * @throws IOException + */ + public static File createTempFile(File baseDirectory) throws IOException { + return createTempFile(baseDirectory, ""); + } + + public static File createTempFile(File baseDirectory, String extension) throws IOException { + File tempFile = File.createTempFile("test", extension, baseDirectory); + tempFile.delete(); + return tempFile; + } + + /** + * Remove the given directory and all it's files and subdirectories + * @param directory the name of the directory to remove + */ + public static void removeDirectoryWithContent(File directory) { + if ((directory == null) || !directory.exists()) { + return; + } + File[] filesAndSubDirs = directory.listFiles(); + for (int i=0; i < filesAndSubDirs.length; i++) { + File file = filesAndSubDirs[i]; + if (file.isDirectory()) { + removeDirectoryWithContent(file); + } + // else just remove the file + file.delete(); + } + directory.delete(); + } +} Added: incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/NetUtils.java URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/NetUtils.java?rev=939777&view=auto ============================================================================== --- incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/NetUtils.java (added) +++ incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/NetUtils.java Fri Apr 30 18:53:29 2010 @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.ace.test.utils; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * Class containing utility methods concerning network related stuff. + */ +public class NetUtils { + + /** + * Waits for a HTTP URL to become 'available', will retry every 100 milliseconds until it is available or timeout + * has been exceeded. Available in this context means the specified status code is returned when accessing the URL. + * + * @param url HTTP URL that should be tested for availability. + * @param responseCode The response code to be expected on the specified URL when it is available. + * @param timeout Amount of milliseconds to keep trying to access the URL. + * @return True if the response of the URL has the specified status code within the specified timeout delay, false otherwise. + * @throws IllegalArgumentException If the specified URL does not use the HTTP protocol. + */ + public static boolean waitForURL(URL url, int responseCode, int timeout) { + long deadline = System.currentTimeMillis() + timeout; + while (System.currentTimeMillis() < deadline) { + try { + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.connect(); + if (connection.getResponseCode() == responseCode) { + return true; + } + } catch (ClassCastException cce) { + throw new IllegalArgumentException("Expected url to be an HTTP url, not: " + url.toString(), cce); + } + catch (IOException ioe) { + // retry + } + try { + Thread.sleep(100); + } + catch (InterruptedException ie) { + return false; + } + } + return false; + } +} Added: incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/TestUtils.java URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/TestUtils.java?rev=939777&view=auto ============================================================================== --- incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/TestUtils.java (added) +++ incubator/ace/trunk/ace-util/src/main/java/org/apache/ace/test/utils/TestUtils.java Fri Apr 30 18:53:29 2010 @@ -0,0 +1,166 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.ace.test.utils; + +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +/** + * Utility class that injects dependencies. Can be used to unit test service implementations. + */ +public class TestUtils { + public static final String UNIT = "unit"; + public static final String INTEGRATION = "integration"; + public static final String SMOKE = "smoke"; + public static final String PERFORMANCE = "performance"; + public static final String UI = "ui"; + public static final String BROKEN = "broken"; + /** + * Configures an object to use a null object for the specified service interface. + * + * @param object the object + * @param iface the service interface + */ + public static void configureObject(Object object, Class iface) { + configureObject(object, iface, createNullObject(iface)); + } + + /** + * Creates a null object for a service interface. + * + * @param iface the service interface + * @return a null object + */ + @SuppressWarnings("unchecked") + public static T createNullObject(Class iface) { + return (T) Proxy.newProxyInstance(iface.getClassLoader(), new Class[] { iface }, new NullObject()); + } + + /** + * Wraps the given handler in an adapter that will try to pass on received invocations to the hander if that has + * an applicable methods else it defaults to a NullObject. + * + * @param iface the service interface + * @param handler the handler to pass invocations to. + * @return an adapter that will try to pass on received invocations to the given handler + */ + @SuppressWarnings("unchecked") + public static T createMockObjectAdapter(Class iface, final Object handler) { + return (T) Proxy.newProxyInstance(iface.getClassLoader(), new Class[] { iface }, new NullObject() { + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + try { + Method bridge = handler.getClass().getMethod(method.getName(), method.getParameterTypes()); + bridge.setAccessible(true); + return bridge.invoke(handler, args); + } + catch (NoSuchMethodException ex) { + return super.invoke(proxy, method, args); + } + catch (InvocationTargetException ex) { + throw ex.getCause(); + } + } + }); + } + + /** + * Configures an object to use a specific implementation for the specified service interface. + * + * @param object the object + * @param iface the service interface + * @param instance the implementation + */ + @SuppressWarnings("unchecked") + public static void configureObject(Object object, Class iface, Object instance) { + Class serviceClazz = object.getClass(); + + while (serviceClazz != null) { + Field[] fields = serviceClazz.getDeclaredFields(); + AccessibleObject.setAccessible(fields, true); + for (int j = 0; j < fields.length; j++) { + if (fields[j].getType().equals(iface)) { + try { + // synchronized makes sure the field is actually written to immediately + synchronized (new Object()) { + fields[j].set(object, instance); + } + } + catch (Exception e) { + throw new IllegalStateException("Could not set field " + fields[j].getName() + " on " + object); + } + } + } + serviceClazz = serviceClazz.getSuperclass(); + } + } + + static class NullObject implements InvocationHandler { + private static final Boolean DEFAULT_BOOLEAN = Boolean.FALSE; + + private static final Byte DEFAULT_BYTE = new Byte((byte) 0); + + private static final Short DEFAULT_SHORT = new Short((short) 0); + + private static final Integer DEFAULT_INT = new Integer(0); + + private static final Long DEFAULT_LONG = new Long(0); + + private static final Float DEFAULT_FLOAT = new Float(0.0f); + + private static final Double DEFAULT_DOUBLE = new Double(0.0); + + /** + * Invokes a method on this null object. The method will return a default value without doing anything. + */ + @SuppressWarnings("unchecked") + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + Class returnType = method.getReturnType(); + if (returnType.equals(Boolean.class) || returnType.equals(Boolean.TYPE)) { + return DEFAULT_BOOLEAN; + } + else if (returnType.equals(Byte.class) || returnType.equals(Byte.TYPE)) { + return DEFAULT_BYTE; + } + else if (returnType.equals(Short.class) || returnType.equals(Short.TYPE)) { + return DEFAULT_SHORT; + } + else if (returnType.equals(Integer.class) || returnType.equals(Integer.TYPE)) { + return DEFAULT_INT; + } + else if (returnType.equals(Long.class) || returnType.equals(Long.TYPE)) { + return DEFAULT_LONG; + } + else if (returnType.equals(Float.class) || returnType.equals(Float.TYPE)) { + return DEFAULT_FLOAT; + } + else if (returnType.equals(Double.class) || returnType.equals(Double.TYPE)) { + return DEFAULT_DOUBLE; + } + else { + return null; + } + } + } +}