/** * Returns the location of the error and the error message. * * @return the location of the error and the error message */ public String toString() { return location.toString() + getMessage(); }
/** * Runs Ant glob expansion. * * @return * A set of relative file names from the base directory. */ @Nonnull private static String[] glob(File dir, String includes, String excludes, boolean defaultExcludes) throws IOException { if(isAbsolute(includes)) throw new IOException("Expecting Ant GLOB pattern, but saw '"+includes+"'. See http://ant.apache.org/manual/Types/fileset.html for syntax"); FileSet fs = Util.createFileSet(dir,includes,excludes); fs.setDefaultexcludes(defaultExcludes); DirectoryScanner ds; try { ds = fs.getDirectoryScanner(new Project()); } catch (BuildException x) { throw new IOException(x.getMessage()); } String[] files = ds.getIncludedFiles(); return files; }
/** * Performs the actual compilation. */ public boolean compile(Javah javah) throws BuildException { Commandline cmd = setupGcjhCommand(javah); try { Execute.runCommand(javah, cmd.getCommandline()); return true; } catch (BuildException e) { if (!e.getMessage().contains("failed with return code")) { throw e; } } return false; }
/** * Performs the actual compilation. * @param javah the calling javah task. * @return true if the compilation was successful. * @throws BuildException if there is an error. * @since Ant 1.6.3 */ public boolean compile(Javah javah) throws BuildException { Commandline cmd = setupKaffehCommand(javah); try { Execute.runCommand(javah, cmd.getCommandline()); return true; } catch (BuildException e) { if (!e.getMessage().contains("failed with return code")) { throw e; } } return false; }
/** * Add location to build exception. * @param ex the build exception, if the build exception * does not include * @param newLocation the location of the calling task (may be null) * @return a new build exception based in the build exception with * location set to newLocation. If the original exception * did not have a location, just return the build exception */ public static BuildException addLocationToBuildException( BuildException ex, Location newLocation) { if (ex.getLocation() == null || ex.getMessage() == null) { return ex; } String errorMessage = String.format("The following error occurred while executing this line:%n%s%s", ex.getLocation().toString(), ex.getMessage()); if (ex instanceof ExitStatusException) { int exitStatus = ((ExitStatusException) ex).getStatus(); if (newLocation == null) { return new ExitStatusException(errorMessage, exitStatus); } return new ExitStatusException(errorMessage, exitStatus, newLocation); } if (newLocation == null) { return new BuildException(errorMessage, ex); } return new BuildException(errorMessage, ex, newLocation); }
/** * Initialisation routine called after handler creation * with the element name and attributes. This configures * the element with its attributes and sets it up with * its parent container (if any). Nested elements are then * added later as the parser encounters them. * * @param propType Name of the element which caused this handler * to be created. Must not be <code>null</code>. * * @param attrs Attributes of the element which caused this * handler to be created. Must not be <code>null</code>. * * @exception SAXParseException in case of error, such as a * BuildException being thrown during configuration. */ public void init(String propType, AttributeList attrs) throws SAXParseException { try { element = helperImpl.project.createDataType(propType); if (element == null) { throw new BuildException("Unknown data type " + propType); } wrapper = new RuntimeConfigurable(element, propType); wrapper.setAttributes(attrs); target.addDataType(wrapper); } catch (BuildException exc) { throw new SAXParseException(exc.getMessage(), helperImpl.locator, exc); } }
@Override public void execute() { if (resources == null) { throw new BuildException("At least one resource-collection is required"); } Resource currentResource = null; try { for (Resource r : resources) { currentResource = r; try { PermissionUtils.setPermissions(r, permissions, this::posixPermissionsNotSupported); } catch (IOException ioe) { maybeThrowException(ioe, "Failed to set permissions on '%s' due to %s", r, ioe.getMessage()); } } } catch (ClassCastException cce) { maybeThrowException(null, "some specified permissions are not of type PosixFilePermission: %s", StringUtils.join(permissions, ", ")); } catch (SecurityException se) { maybeThrowException(null, "the SecurityManager denies role accessUserInformation or write access for SecurityManager.checkWrite for resource '%s'", currentResource); } catch (BuildException be) { // maybe thrown by callback method this::posixPermissionsNotSupported. maybeThrowException(be, be.getMessage()); } }
@Test public final void testSimultaneousConfiguration() throws IOException { final File file = new File(getPath(CONFIG_FILE)); final URL url = file.toURI().toURL(); final String expected = "Attribute 'config' has already been set"; try { final CheckstyleAntTask antTask = new CheckstyleAntTask(); antTask.setConfig(url.toString()); antTask.setConfig(file.toString()); fail("Exception is expected"); } catch (BuildException ex) { assertEquals("Error message is unexpected", expected, ex.getMessage()); } }
System.err.println("The specified logger class " + loggerClassname + " could not be used because " + e.getMessage()); throw e;
@Test public final void testSetPropertiesNonExistentFile() throws IOException { final CheckstyleAntTask antTask = getCheckstyleAntTask(); antTask.setFile(new File(getPath(FLAWLESS_INPUT))); antTask.setProperties(new File(getPath(NOT_EXISTING_FILE))); try { antTask.execute(); fail("Exception is expected"); } catch (BuildException ex) { assertTrue("Error message is unexpected", ex.getMessage().startsWith("Error loading Properties file")); } }
@Test public void testSetInvalidType() { final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType(); try { formatterType.setValue("foo"); fail("Exception is expected"); } catch (BuildException ex) { assertEquals("Error message is unexpected", "foo is not a legal value for this attribute", ex.getMessage()); } }
@Test public final void testNoFile() throws IOException { final CheckstyleAntTask antTask = getCheckstyleAntTask(); try { antTask.execute(); fail("Exception is expected"); } catch (BuildException ex) { assertEquals("Error message is unexpected", "Must specify at least one of 'file' or nested 'fileset' or 'path'.", ex.getMessage()); } }
@Test public final void testCreateListenerException() throws IOException { final CheckstyleAntTask antTask = getCheckstyleAntTask(); antTask.setFile(new File(getPath(FLAWLESS_INPUT))); final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter(); final File outputFile = new File("target/"); formatter.setTofile(outputFile); antTask.addFormatter(formatter); try { antTask.execute(); fail("Exception is expected"); } catch (BuildException ex) { assertTrue("Error message is unexpected", ex.getMessage().startsWith("Unable to create listeners: formatters")); } }
@Test public final void testMaxWarningExceeded() throws IOException { final CheckstyleAntTask antTask = getCheckstyleAntTask(); antTask.setFile(new File(getPath(WARNING_INPUT))); antTask.setMaxWarnings(0); try { antTask.execute(); fail("Exception is expected"); } catch (BuildException ex) { assertEquals("Error message is unexpected", "Got 0 errors and 1 warnings.", ex.getMessage()); } }
@Test public void testCheckerException() throws IOException { final CheckstyleAntTask antTask = new CheckstyleAntTaskStub(); antTask.setConfig(getPath(CONFIG_FILE)); antTask.setProject(new Project()); antTask.setFile(new File("")); try { antTask.execute(); fail("Exception is expected"); } catch (BuildException ex) { assertTrue("Error message is unexpected", ex.getMessage().startsWith("Unable to process files:")); } }
@Test public final void testNonExistentConfig() throws IOException { final CheckstyleAntTask antTask = new CheckstyleAntTask(); antTask.setConfig(getPath(NOT_EXISTING_FILE)); antTask.setProject(new Project()); antTask.setFile(new File(getPath(FLAWLESS_INPUT))); try { antTask.execute(); fail("Exception is expected"); } catch (BuildException ex) { assertTrue("Error message is unexpected", ex.getMessage().startsWith("Unable to create Root Module: config")); } }
@Test public final void testEmptyConfigFile() throws IOException { final CheckstyleAntTask antTask = new CheckstyleAntTask(); antTask.setConfig(getPath("InputCheckstyleAntTaskEmptyConfig.xml")); antTask.setProject(new Project()); antTask.setFile(new File(getPath(FLAWLESS_INPUT))); try { antTask.execute(); fail("Exception is expected"); } catch (BuildException ex) { assertTrue("Error message is unexpected", ex.getMessage().startsWith("Unable to create Root Module: config")); } }
@Test public final void testNoConfigFile() throws IOException { final CheckstyleAntTask antTask = new CheckstyleAntTask(); antTask.setProject(new Project()); antTask.setFile(new File(getPath(FLAWLESS_INPUT))); try { antTask.execute(); fail("Exception is expected"); } catch (BuildException ex) { assertEquals("Error message is unexpected", "Must specify 'config'.", ex.getMessage()); } }
@Test public final void testCreateListenerExceptionWithXmlLogger() throws IOException { final CheckstyleAntTask antTask = getCheckstyleAntTask(); antTask.setFile(new File(getPath(FLAWLESS_INPUT))); final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter(); final File outputFile = new File("target/"); formatter.setTofile(outputFile); final CheckstyleAntTask.FormatterType formatterType = new CheckstyleAntTask.FormatterType(); formatterType.setValue("xml"); formatter.setType(formatterType); antTask.addFormatter(formatter); try { antTask.execute(); fail("Exception is expected"); } catch (BuildException ex) { assertTrue("Error message is unexpected", ex.getMessage().startsWith("Unable to create listeners: formatters")); } }
/** This test is created to satisfy pitest, it is hard to emulate Reference by Id. */ @Test public void testSetClasspathRef1() { final CheckstyleAntTask antTask = new CheckstyleAntTask(); final Project project = new Project(); antTask.setClasspath(new Path(project, "firstPath")); antTask.setClasspathRef(new Reference(project, "idXX")); try { assertNotNull("Classpath should not be null", Whitebox.getInternalState(antTask, "classpath")); final Path classpath = Whitebox.getInternalState(antTask, "classpath"); classpath.list(); fail("Exception is expected"); } catch (BuildException ex) { assertEquals("unexpected exception message", "Reference idXX not found.", ex.getMessage()); } }