[r34x] Bug 294502 - Handling of unknown arguments/targets
diff --git a/ant/org.eclipse.ant.core/src_ant/org/eclipse/ant/internal/core/ant/InternalAntMessages.java b/ant/org.eclipse.ant.core/src_ant/org/eclipse/ant/internal/core/ant/InternalAntMessages.java
index 4987e8f..794d83e 100644
--- a/ant/org.eclipse.ant.core/src_ant/org/eclipse/ant/internal/core/ant/InternalAntMessages.java
+++ b/ant/org.eclipse.ant.core/src_ant/org/eclipse/ant/internal/core/ant/InternalAntMessages.java
@@ -50,6 +50,8 @@
 	public static String InternalAntRunner_ANT_HOME_must_be_set_to_use_Ant_diagnostics_2;
 	public static String InternalAntRunner_Buildfile___0__is_not_a_file_1;
 	public static String InternalAntRunner__find_not_supported;
+
+	public static String InternalAntRunner_0;
 	public static String InternalAntRunner_Error_setting_Ant_task;
 	public static String InternalAntRunner_Missing_Class;
 	public static String InternalAntRunner_157;
diff --git a/ant/org.eclipse.ant.core/src_ant/org/eclipse/ant/internal/core/ant/InternalAntMessages.properties b/ant/org.eclipse.ant.core/src_ant/org/eclipse/ant/internal/core/ant/InternalAntMessages.properties
index 5562d59..ad68ad3 100644
--- a/ant/org.eclipse.ant.core/src_ant/org/eclipse/ant/internal/core/ant/InternalAntMessages.properties
+++ b/ant/org.eclipse.ant.core/src_ant/org/eclipse/ant/internal/core/ant/InternalAntMessages.properties
@@ -43,6 +43,7 @@
 InternalAntRunner_ANT_HOME_must_be_set_to_use_Ant_diagnostics_2=ANT_HOME must be set to use Ant diagnostics
 InternalAntRunner_Buildfile___0__is_not_a_file_1=Buildfile: {0} is not a file
 InternalAntRunner__find_not_supported=-find not supported.\nCan be emulated using Run As > Ant Build located\nin the Run > External Tools menu
+InternalAntRunner_0=Unknown target: {0}
 InternalAntRunner_Error_setting_Ant_task=Error setting Ant task {0}
 InternalAntRunner_Missing_Class=Could not load a class required for parsing targets
 InternalAntRunner_157=-lib not supported\nConfigure the Ant runtime classpath using either the\nglobal Ant runtime classpath or the Ant runtime classpath\n for this particular build
diff --git a/ant/org.eclipse.ant.core/src_ant/org/eclipse/ant/internal/core/ant/InternalAntRunner.java b/ant/org.eclipse.ant.core/src_ant/org/eclipse/ant/internal/core/ant/InternalAntRunner.java
index 12854f0..3206ccf 100644
--- a/ant/org.eclipse.ant.core/src_ant/org/eclipse/ant/internal/core/ant/InternalAntRunner.java
+++ b/ant/org.eclipse.ant.core/src_ant/org/eclipse/ant/internal/core/ant/InternalAntRunner.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2008 IBM Corporation and others.
+ * Copyright (c) 2000, 2009 IBM Corporation and others.
  * Portions Copyright  2000-2007 The Apache Software Foundation
  * All rights reserved. This program and the accompanying materials are made 
  * available under the terms of the Apache Software License v2.0 which 
@@ -25,10 +25,13 @@
 import java.util.Arrays;
 import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.ListIterator;
 import java.util.Map;
 import java.util.Properties;
+import java.util.Set;
 import java.util.Vector;
 
 import org.apache.tools.ant.AntTypeDefinition;
@@ -1068,7 +1071,11 @@
 		if (!commands.isEmpty()) {
 			processUnrecognizedCommands(commands);
 		}
-
+		
+		if (!commands.isEmpty()) {
+			processUnrecognizedTargets(commands);
+		}
+		
 		if (!commands.isEmpty()) {
 			processTargets(commands);
 		}
@@ -1076,6 +1083,35 @@
 		return true;
 	}
 	
+	/**
+	 * Checks for unrecognized targets on the command line and
+	 * removes them.
+	 */
+	private void processUnrecognizedTargets(List commands) {
+		List list = getTargets();
+		Set names = new HashSet();
+		Iterator it = list.iterator();
+		while (it.hasNext()) {
+			Object element = it.next();
+			if (element instanceof List) {
+				List target = (List)element;
+				if (!target.isEmpty()) {
+					names.add(target.get(0));
+				}
+			}
+		}
+		ListIterator iterator = commands.listIterator();
+		
+		while (iterator.hasNext()) {
+			String target = (String) iterator.next();
+			if (!names.contains(target)) {
+				iterator.remove();
+				String message = MessageFormat.format(InternalAntMessages.InternalAntRunner_0, new Object[]{target});
+				logMessage(currentProject, message, Project.MSG_WARN); 
+			}
+		}
+	}
+	
 	/*
 	 * Checks for unrecognized arguments on the command line.
 	 * Since there is no syntactic way to distingush between
@@ -1091,7 +1127,7 @@
 
 		// find the last arg that begins with '-'
 		for (int i = commands.size() - 1; i >= 0; i--) {
-			if (((String) commands.get(0)).startsWith("-")) { //$NON-NLS-1$
+			if (((String) commands.get(i)).startsWith("-")) { //$NON-NLS-1$
 				p = i;
 				break;
 			}
diff --git a/ant/org.eclipse.ant.tests.core/tests/org/eclipse/ant/tests/core/tests/OptionTests.java b/ant/org.eclipse.ant.tests.core/tests/org/eclipse/ant/tests/core/tests/OptionTests.java
index 42048c5..6c7ac3c 100644
--- a/ant/org.eclipse.ant.tests.core/tests/org/eclipse/ant/tests/core/tests/OptionTests.java
+++ b/ant/org.eclipse.ant.tests.core/tests/org/eclipse/ant/tests/core/tests/OptionTests.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2007 IBM Corporation and others.
+ * Copyright (c) 2000, 2009 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -288,14 +288,14 @@
 	
 	/**
 	 * Tests specifying a target at the command line that does not exist.
+	 * 
+	 * This will no longer fail - the default target will be run instead
 	 */
-	public void testSpecifyBadTargetAsArg() {
-		try {
-			run("TestForEcho.xml", new String[]{"echo2"}, false);
-		} catch (CoreException ce) {
-			return;
-		}
-		assertTrue("A core exception should have occurred as the target does not exist", false);
+	public void testSpecifyBadTargetAsArg() throws CoreException {
+		run("TestForEcho.xml", new String[]{"echo2"}, false);
+		assertTrue("Should be an unknown target message", AntTestChecker.getDefault().getLoggedMessage(5).indexOf("Unknown target") >= 0);
+		assertTrue("Should be an unknown target message", AntTestChecker.getDefault().getLoggedMessage(5).indexOf("echo2") >= 0);
+		assertEquals("Should have run the default target & dependents", 5, AntTestChecker.getDefault().getTargetsStartedCount());
 	}
 	
 	/**