package org.apache.maven.cli; /* ==================================================================== * Copyright 2001-2005 The Apache Software Foundation. * * Licensed 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. * ==================================================================== */ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Member; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.util.Arrays; import org.apache.commons.cli.ParseException; import org.apache.log4j.Logger; import org.apache.maven.MavenConstants; import junit.framework.TestCase; /** * Test cases for patch in the App class. * * @see org.apache.maven.cli.App * * @author Vincent Siveton vincent.siveton@gmail.com * @version $Id: $ */ public class AppTest extends TestCase { private static final Logger log = Logger.getLogger(AppTest.class); /* * @see TestCase#setUp() */ protected void setUp() throws Exception { super.setUp(); } /* * @see TestCase#tearDown() */ protected void tearDown() throws Exception { super.tearDown(); } /** * Convenience method * * @param clazz * @param methodName * @param member * @param parameterTypes * @return the method defined by methodName */ protected static Method getMethod(Class clazz, String methodName, int member, Class[] parameterTypes) { if (clazz == null) { if (log.isDebugEnabled()) { log.debug("clazz must not be null."); } assertTrue(false); } if (methodName == null) { if (log.isDebugEnabled()) { log.debug("methodName must not be null."); } assertTrue(false); } if (!((member == Member.PUBLIC) || (member == Member.DECLARED))) { if (log.isDebugEnabled()) { log.debug("member value should be Member.PUBLIC or Member.DECLARED"); } assertTrue(false); } Method returnMethod = null; if (member == Member.DECLARED) { Method[] methods = null; try { methods = clazz.getDeclaredMethods(); } catch (SecurityException e) { if (log.isDebugEnabled()) { log.debug("SecurityException", e); } assertTrue(false); } for (int i = 0; i < methods.length; i++) { Method current = methods[i]; if (parameterTypes == null) { parameterTypes = new Class[] {}; } if ((current.getName().equals(methodName)) && (Arrays.asList(current.getParameterTypes()).equals(Arrays.asList(parameterTypes)))) { returnMethod = current; } } } else { try { returnMethod = clazz.getMethod(methodName, parameterTypes); } catch (SecurityException e) { if (log.isDebugEnabled()) { log.debug("SecurityException", e); } assertTrue(false); } catch (NoSuchMethodException e) { if (log.isDebugEnabled()) { log.debug("NoSuchMethodException", e); } assertTrue(false); } } if (returnMethod == null) { if (log.isDebugEnabled()) { log.debug("No such method " + clazz.getPackage().getName() + "." + methodName + argumentTypesToString(parameterTypes)); } assertTrue(false); } return returnMethod; } /** * Convenience method * * @param argTypes * @return Beautify */ private static String argumentTypesToString(Class[] argTypes) { StringBuffer buf = new StringBuffer(); buf.append("("); if (argTypes != null) { for (int i = 0; i < argTypes.length; i++) { if (i > 0) { buf.append(", "); } Class c = argTypes[i]; buf.append((c == null) ? "null" : c.getName()); } } buf.append(")"); return buf.toString(); } /** * Convenience method * * @param method * @param obj * @param args * @return the object invoked by the method */ protected Object getInvoke(Method method, Object obj, Object[] args) { if (method == null) { if (log.isDebugEnabled()) { log.debug("method must not be null."); } assertTrue(false); } if (obj == null) { if (log.isDebugEnabled()) { log.debug("obj must not be null."); } assertTrue(false); } Object returnObj = null; try { // Safety method.setAccessible(true); returnObj = method.invoke(obj, args); } catch (SecurityException e) { if (log.isDebugEnabled()) { log.debug("SecurityException", e); } assertTrue(false); } catch (IllegalAccessException e) { if (log.isDebugEnabled()) { log.debug("IllegalAccessException", e); } assertTrue(false); } catch (IllegalArgumentException e) { if (log.isDebugEnabled()) { log.debug("IllegalArgumentException", e); } assertTrue(false); } catch (NullPointerException e) { if (log.isDebugEnabled()) { log.debug("NullPointerException", e); } assertTrue(false); } catch (ExceptionInInitializerError e) { if (log.isDebugEnabled()) { log.debug("ExceptionInInitializerError", e); } assertTrue(false); } catch (InvocationTargetException e) { // InvocationTargetException wraps the actual exception thrown by the underlying method. // That's the Exception we should deal with. Throwable t = e.getTargetException(); if (log.isDebugEnabled()) { log.debug("InvocationTargetException", t); } assertTrue(false); } return returnObj; } /** * Test the displayBugReportHelp() method * * @see org.apache.maven.cli.App#displayBugReportHelp() */ public void testDisplayBugReportHelp() { Method method = getMethod(App.class, "displayBugReportHelp", Member.DECLARED, null); getInvoke(method, new App(), null); assertTrue(true); } /** * Test the displayDefaultGoal() method * * @see org.apache.maven.cli.App#displayDefaultGoal(String, String, boolean) */ public void testDisplayDefaultGoal() { // Only to prevent an exception System.setProperty(MavenConstants.MAVEN_HOME, System.getProperty( "java.io.tmpdir")); App app = new App(); try { app.initialize(new String[]{}); } catch (MalformedURLException e) { assertTrue(false); } catch (ParseException e) { assertTrue(false); } catch (IOException e) { assertTrue(false); } Method method = getMethod(App.class, "displayDefaultGoal", Member.DECLARED, new Class[]{String.class, String.class, boolean.class}); getInvoke(method, app, new Object[]{"testGoal", "testDescription", Boolean.TRUE}); assertTrue(true); } /** * Test the displayDefaultGoal() method * * @see org.apache.maven.cli.App#displayInfo() */ public void testDisplayInfo() { App app = new App(); try { app.initialize(new String[]{}); } catch (MalformedURLException e) { assertTrue(false); } catch (ParseException e) { assertTrue(false); } catch (IOException e) { assertTrue(false); } Method method = getMethod(App.class, "displayInfo", Member.DECLARED, null); getInvoke(method, app, null); assertTrue(true); } }