1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

74433: better error reporting on the command-line

This commit is contained in:
Andrew Ferguson 2007-05-11 16:00:41 +00:00
parent f082faeccd
commit d3ac3402c7
4 changed files with 78 additions and 48 deletions

View file

@ -76,29 +76,21 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
}
public void testBrokenExportProjectProvider1() throws Exception {
try {
File target= File.createTempFile("test", "pdom");
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, TestProjectProvider1.class.getName()
});
fail("Expected exception - IExportProjectProvider implementation returns null for createProject");
} catch(CoreException ce) {
// correct behaviour
}
setExpectedNumberOfLoggedNonOKStatusObjects(1); // IExportProjectProvider implementation returns null for createProject
File target= File.createTempFile("test", "pdom");
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, TestProjectProvider1.class.getName()
});
}
public void testBrokenExportProjectProvider2() throws Exception {
try {
File target= File.createTempFile("test", "pdom");
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, TestProjectProvider2.class.getName()
});
fail("Expected exception - IExportProjectProvider implementation returns null for getLocationConverter");
} catch(CoreException ce) {
// correct behaviour
}
setExpectedNumberOfLoggedNonOKStatusObjects(1); // IExportProjectProvider implementation returns null for getLocationConverter
File target= File.createTempFile("test", "pdom");
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, TestProjectProvider2.class.getName()
});
}
public void testSimpleExportProjectProvider1() throws Exception {
@ -137,33 +129,42 @@ public class GeneratePDOMApplicationTest extends PDOMTestBase {
public void testExternalExportProjectProvider_BadCmdLine1() throws Exception {
File target= File.createTempFile("test", "pdom");
try {
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, ExternalExportProjectProvider.class.getName()
});
assertTrue(target.exists());
fail("Expected failure: -source must be specified");
} catch(CoreException ce) {
// correct behaviour
}
setExpectedNumberOfLoggedNonOKStatusObjects(1); // Expected failure: -source must be specified
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, ExternalExportProjectProvider.class.getName()
});
assertTrue(target.exists());
}
public void testExternalExportProjectProvider_BadCmdLine2() throws Exception {
File target= File.createTempFile("test", "pdom");
TestProjectProvider4 tpp4= new TestProjectProvider4();
ICProject cproject= tpp4.createProject();
try {
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, ExternalExportProjectProvider.class.getName(),
ExternalExportProjectProvider.OPT_SOURCE, cproject.getProject().getLocation().toFile().getAbsolutePath()
});
assertTrue(target.exists());
fail("Expected failure: -id must be specified");
} catch(CoreException ce) {
// correct behaviour
}
setExpectedNumberOfLoggedNonOKStatusObjects(1); // Expected failure: -id must be specified
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, ExternalExportProjectProvider.class.getName(),
ExternalExportProjectProvider.OPT_SOURCE, cproject.getProject().getLocation().toFile().getAbsolutePath()
});
assertTrue(target.exists());
}
public void testExternalExportProjectProvider_BadCmdLine3() throws Exception {
File target= File.createTempFile("test", "pdom");
TestProjectProvider4 tpp4= new TestProjectProvider4();
ICProject cproject= tpp4.createProject();
setExpectedNumberOfLoggedNonOKStatusObjects(1); // Expected failure: -target must be specified
doGenerate(new String[] {
GeneratePDOMApplication.OPT_PROJECTPROVIDER, ExternalExportProjectProvider.class.getName(),
ExternalExportProjectProvider.OPT_SOURCE, cproject.getProject().getLocation().toFile().getAbsolutePath()
});
assertTrue(target.exists());
}
public void testExternalExportProjectProvider() throws Exception {

View file

@ -16,11 +16,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.index.export.Messages;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
/**
* Helper methods for command-line options
@ -43,7 +40,7 @@ public class CLIUtil {
List list = (List) arguments.get(opt);
if(list==null || list.size()!=number) {
String msg= MessageFormat.format(Messages.CLIUtil_OptionParametersMismatch, new Object[] {opt, ""+number}); //$NON-NLS-1$
throw new CoreException(new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, msg));
GeneratePDOMApplication.fail(msg);
}
return list;
}

View file

@ -98,6 +98,6 @@ public class GeneratePDOM implements ISafeRunnable {
}
private void fail(String message) throws CoreException {
throw new CoreException(CCorePlugin.createStatus(message));
GeneratePDOMApplication.fail(message);
}
}

View file

@ -27,8 +27,10 @@ import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.ProgressProvider;
import org.eclipse.equinox.app.IApplication;
@ -45,12 +47,34 @@ public class GeneratePDOMApplication implements IApplication {
public static final String OPT_QUIET= "-quiet"; //$NON-NLS-1$
public static final String OPT_INDEXER_ID= "-indexer"; //$NON-NLS-1$
/**
* Applications needing to fail in an expected way (without stack dump), should throw
* CoreExceptions with this error code.
*/
public static final int ECODE_EXPECTED_FAILURE= 1;
private static Map/*<String,IProjectForExportManager>*/ projectInitializers;
/**
* Starts this application
* @throws CoreException on an unexpected failure
*/
public Object start(IApplicationContext context) throws CoreException {
Object result= IApplication.EXIT_OK;
try {
result= startImpl(context);
} catch(CoreException ce) {
IStatus s= ce.getStatus();
if(s.getCode()==ECODE_EXPECTED_FAILURE) {
output(s.getMessage());
} else {
throw ce;
}
}
return result;
}
private Object startImpl(IApplicationContext context) throws CoreException {
String[] appArgs= (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
Map arguments= CLIUtil.parseToMap(appArgs);
output(Messages.GeneratePDOMApplication_Initializing);
@ -104,8 +128,16 @@ public class GeneratePDOMApplication implements IApplication {
// do nothing
}
private void fail(String message) throws CoreException {
throw new CoreException(CCorePlugin.createStatus(message));
/**
* Causes the appliation to fail in a way that has been anticipated (e.g. a command-line or interface
* contract violation by a extension implementation)
* @param message
* @throws CoreException
*/
public static final void fail(String message) throws CoreException {
IStatus status= new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, ECODE_EXPECTED_FAILURE, message, null);
CCorePlugin.log(status);
throw new CoreException(status);
}
/**