use of org.apache.tools.ant.DirectoryScanner in project mdw-designer by CenturyLinkCloud.
the class AutoTestAntTask method runTests.
public void runTests() throws Exception {
if (!baseDir.exists() || !baseDir.isDirectory())
throw new IOException("Directory does not exist: " + baseDir);
ThreadPool threadPool = new ThreadPool(threadCount);
HashMap<String, ProcessVO> processCache = new HashMap<>();
boolean hasGherkin = false;
boolean hasNonGherkin = false;
if (workflowDir != null) {
DirectoryScanner scanner = getDirectoryScanner(workflowDir);
scanner.scan();
String jsonString;
if (testResultsSummaryFile.exists() && testResultsSummaryFile.getName().endsWith(".json")) {
jsonString = new String(Files.readAllBytes(testResultsSummaryFile.toPath()));
if (jsonString != null && !jsonString.isEmpty())
testCaseList = new TestCaseList(workflowDir, new JSONObject(jsonString));
}
if (testCaseList == null) {
testCaseList = new TestCaseList(workflowDir);
testCaseList.setPackageTests(new ArrayList<PackageTests>());
}
String workflowPath = workflowDir.toString().replace('\\', '/');
String[] caseFilePaths = scanner.getIncludedFiles();
for (String caseFilePath : caseFilePaths) {
String casePath = caseFilePath.replace('\\', '/');
int lastSlash = casePath.lastIndexOf('/');
String pkgName = casePath.substring(0, lastSlash).replace('/', '.');
File caseFile = new File(workflowPath + "/" + casePath);
PackageTests pkgTests = testCaseList.getPackageTests(pkgName);
if (pkgTests == null) {
PackageDir pkgDir = new PackageDir(workflowDir, caseFile.getParentFile(), null);
pkgDir.parse();
pkgTests = new PackageTests(pkgDir);
pkgTests.setTestCases(new ArrayList<com.centurylink.mdw.test.TestCase>());
testCaseList.getPackageTests().add(pkgTests);
}
pkgTests.getTestCases().add(new com.centurylink.mdw.test.TestCase(pkgName, new AssetInfo(caseFile)));
TestCase tc = new TestCase(pkgName, caseFile);
if (tc.isGherkin())
hasGherkin = true;
else
hasNonGherkin = true;
testCases.add(tc);
}
} else if (jdbcUrl != null) {
// db asset tests
if (dbAssetTests == null)
throw new BuildException("Attribute dbAssetTests required for non-VCS asset tests");
String[] assets = dbAssetTests.split("\\s?,\\s?");
for (String asset : assets) {
int lastSlash = asset.lastIndexOf('/');
String pkg = asset.substring(0, lastSlash);
String assetName = asset.substring(lastSlash + 1);
String language = RuleSetVO.getLanguage(assetName.substring(assetName.lastIndexOf('.')));
RuleSetVO ruleSet = designerDataAccess.getRuleSet(assetName, language, 0);
ruleSet.setPackageName(pkg);
TestCase tc = new TestCase(pkg, ruleSet);
testCases.add(tc);
}
} else {
throw new BuildException("Missing attribute: workflowDir");
}
if (hasGherkin && hasNonGherkin)
throw new BuildException("Gherkin/non-Gherkin tests require separate task/target executions.");
// gradle
boolean useStdErr = System.getProperty("org.gradle.appname") != null;
// does
// not
// show
// output
// otherwise
LogMessageMonitor monitor = hasGherkin ? null : new LogMessageMonitor(designerDataAccess, oldNamespaces);
if (monitor != null)
monitor.start(true);
for (TestCase testCase : testCases) {
String masterRequestId = user + "-" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
File resultDir = new File(testResultsDir + "/" + testCase.getPrefix());
testCase.prepare();
TestCaseRun run;
if (testCase.isGherkin()) {
// User defined masterRequestId
if (testCase.getMasterRequestId() != null) {
if (testCase.getMasterRequestId().indexOf("${masterRequestId}") != -1) {
masterRequestId = testCase.getMasterRequestId().replace("${masterRequestId}", masterRequestId);
} else
masterRequestId = testCase.getMasterRequestId();
}
testCase.setMasterRequestId(masterRequestId);
run = new GherkinTestCaseRun(testCase, 0, masterRequestId, new DesignerDataAccess(designerDataAccess), monitor, processCache, oldNamespaces, resultDir, stubbing, stubPort);
} else if (testCase.isGroovy())
run = new GroovyTestCaseRun(testCase, 0, masterRequestId, new DesignerDataAccess(designerDataAccess), monitor, processCache, loadTests, true, oldNamespaces, null);
else
run = new TestCaseRun(testCase, 0, masterRequestId, new DesignerDataAccess(designerDataAccess), monitor, processCache, loadTests, true, oldNamespaces);
File executeLog = new File(resultDir.getPath() + "/" + testCase.getCaseName() + ".log");
if (!executeLog.getParentFile().exists() && !executeLog.getParentFile().mkdirs())
throw new IOException("Unable to create test run directory: " + executeLog.getParentFile());
PrintStream log = verbose ? new TeePrintStream(useStdErr ? System.err : System.out, executeLog) : new PrintStream(executeLog);
run.prepareTest(false, resultDir, true, singleServer, stubbing, log);
if (verbose)
log("Test case prepared: " + testCase.getCaseName(), useStdErr ? Project.MSG_ERR : Project.MSG_INFO);
masterRequestRunMap.put(run.getMasterRequestId(), run);
threadPool.execute(run);
updateResults(false);
try {
Thread.sleep(intervalSecs * 1000);
} catch (InterruptedException e) {
}
}
log("All cases prepared. Waiting for completion ...", Project.MSG_INFO);
threadPool.shutdown();
while (!threadPool.isTerminated()) {
updateResults(false);
try {
Thread.sleep(2500);
} catch (InterruptedException e) {
}
}
if (monitor != null)
monitor.shutdown();
updateResults(true);
log("All cases completed.", Project.MSG_ERR);
}
use of org.apache.tools.ant.DirectoryScanner in project ph-schematron by phax.
the class Schematron method _performValidation.
private void _performValidation(@Nonnull final ISchematronResource aSch, @Nonnull final ICommonsList<ResourceCollection> aResCollections, @Nullable final File aSVRLDirectory, final boolean bExpectSuccess) throws BuildException {
// Resolve resourceCollections - pain in the ass
final ICommonsMap<File, DirectoryData> aFiles = new CommonsHashMap<>();
for (final ResourceCollection aResCollection : aResCollections) {
if (!aResCollection.isFilesystemOnly())
_error("Only FileSystem resources are supported.");
else
for (final Resource aRes : aResCollection) {
if (!aRes.isExists()) {
_error("Could not find resource " + aRes.toLongString() + " to copy.");
continue;
}
File baseDir = NULL_FILE_PLACEHOLDER;
String name = aRes.getName();
final FileProvider fp = aRes.as(FileProvider.class);
if (fp != null) {
final FileResource fr = ResourceUtils.asFileResource(fp);
baseDir = _getKeyFile(fr.getBaseDir());
if (baseDir == NULL_FILE_PLACEHOLDER)
name = fr.getFile().getAbsolutePath();
}
if ((aRes.isDirectory() || fp != null) && name != null) {
final DirectoryData aBaseDir = aFiles.computeIfAbsent(_getKeyFile(baseDir), k -> new DirectoryData(k));
if (aRes.isDirectory())
aBaseDir.addDir(name);
else
aBaseDir.addFile(name);
} else
_error("Could not resolve resource " + aRes.toLongString() + " to a file.");
}
}
for (final DirectoryData aBaseDir : aFiles.values()) {
log("Scanning directory " + aBaseDir.getBaseDir() + " for XMLs to be Schematron validated", Project.MSG_DEBUG);
final ICommonsList<String> aIncludes = new CommonsArrayList<>();
aIncludes.addAll(aBaseDir.getFiles());
for (final String sFile : aBaseDir.getDirs()) aIncludes.add(sFile + "/**");
final DirectoryScanner aScanner = new DirectoryScanner();
aScanner.setBasedir(aBaseDir.getBaseDir());
if (aIncludes.isNotEmpty())
aScanner.setIncludes(aIncludes.toArray(new String[0]));
aScanner.setCaseSensitive(true);
aScanner.scan();
final String[] aXMLFilenames = aScanner.getIncludedFiles();
if (aXMLFilenames != null) {
for (final String sXMLFilename : aXMLFilenames) {
final File aXMLFile = new File(aBaseDir.getBaseDir(), sXMLFilename);
// Validate XML file
log("Validating XML file '" + aXMLFile.getPath() + "' against Schematron rules from '" + m_aSchematronFile.getName() + "' expecting " + (bExpectSuccess ? "success" : "failure"), Project.MSG_INFO);
try {
final SchematronOutputType aSOT = aSch.applySchematronValidationToSVRL(TransformSourceFactory.create(aXMLFile));
if (aSVRLDirectory != null) {
// Save SVRL
final File aSVRLFile = new File(aSVRLDirectory, sXMLFilename + ".svrl");
if (!aSVRLFile.getParentFile().mkdirs())
log("Failed to create parent directory of '" + aSVRLFile.getAbsolutePath() + "'!", Project.MSG_ERR);
if (new SVRLMarshaller().write(aSOT, aSVRLFile).isSuccess())
log("Successfully saved SVRL file '" + aSVRLFile.getPath() + "'", Project.MSG_INFO);
else
log("Error saving SVRL file '" + aSVRLFile.getPath() + "'", Project.MSG_ERR);
}
if (false)
System.out.println(new SVRLMarshaller().getAsString(aSOT));
final ICommonsList<AbstractSVRLMessage> aMessages = SVRLHelper.getAllFailedAssertionsAndSuccessfulReports(aSOT);
final int nErrorMessages = aMessages.getCount(x -> x.getFlag().isError());
final int nWarningMessages = aMessages.size() - nErrorMessages;
final String sErrors = nErrorMessages + " Schematron error" + (nErrorMessages == 1 ? "" : "s");
final String sWarnings = nWarningMessages + " Schematron warning" + (nWarningMessages == 1 ? "" : "s");
if (bExpectSuccess) {
// No failed assertions expected
if (nErrorMessages > 0) {
final StringBuilder aMessage = new StringBuilder();
aMessage.append(sErrors + (nWarningMessages > 0 ? " and " + sWarnings : "") + " for XML file '" + aXMLFile.getPath() + "'");
for (final AbstractSVRLMessage aMsg : aMessages) {
aMessage.append("\n ").append(ErrorTextProvider.DEFAULT.getErrorText(aMsg.getAsResourceError(aXMLFile.getPath()), Locale.US));
}
// As at least one error is contained, it's okay to throw an
// exception in case
_error(aMessage.toString());
continue;
}
// Success as expected
log("XML file '" + aXMLFile.getPath() + "' was validated against Schematron '" + aSch.getResource().getPath() + "' and matches the rules" + (nWarningMessages > 0 ? " - only " + sWarnings + " are contained" : ""), Project.MSG_INFO);
} else {
// At least one failed assertions expected
if (nErrorMessages == 0) {
String sMessage = "No Schematron errors for erroneous XML file '" + aXMLFile.getPath() + "'";
if (nWarningMessages > 0)
sMessage += " - only " + sWarnings + " are contained";
_error(sMessage);
continue;
}
// Success as expected
log("XML file '" + aXMLFile.getPath() + "' was validated against Schematron '" + aSch.getResource().getPath() + "' " + sErrors + (nWarningMessages > 0 ? " and " + sWarnings : "") + " were found (as expected)", Project.MSG_INFO);
}
} catch (final BuildException up) {
throw up;
} catch (final Exception ex) {
final String sMessage = "Exception validating XML '" + aXMLFile.getPath() + "' against Schematron rules from '" + m_aSchematronFile.getName() + "'. Technical details: " + ex.getClass().getSimpleName() + " - " + ex.getMessage();
_error(sMessage, ex);
continue;
}
}
}
}
}
use of org.apache.tools.ant.DirectoryScanner in project sql-boot by sql-boot.
the class LocalFileSystem method listFiles.
@Override
public List<File> listFiles(final String mask) {
try {
DirectoryScanner scanner = new DirectoryScanner();
scanner.setIncludes(new String[] { mask });
scanner.setBasedir(this.basedir.replace("\\", "/"));
scanner.setCaseSensitive(false);
scanner.scan();
String[] files = scanner.getIncludedFiles();
List<File> result = new ArrayList<>();
for (String file : files) {
result.add(new SimpleFile(file, FileUtils.readFileToByteArray(new java.io.File(basedir + "/" + file))));
}
return result;
} catch (IOException exception) {
throw new BootException(exception);
}
}
use of org.apache.tools.ant.DirectoryScanner in project ant by apache.
the class Javadoc method doJava14.
// Do java1.4 arguments
private void doJava14(final Commandline toExecute) {
for (final Object element : tags) {
if (element instanceof TagArgument) {
final TagArgument ta = (TagArgument) element;
final File tagDir = ta.getDir(getProject());
if (tagDir == null) {
// The tag element is not used as a fileset,
// but specifies the tag directly.
toExecute.createArgument().setValue("-tag");
toExecute.createArgument().setValue(ta.getParameter());
} else {
// The tag element is used as a
// fileset. Parse all the files and create
// -tag arguments.
final DirectoryScanner tagDefScanner = ta.getDirectoryScanner(getProject());
for (String file : tagDefScanner.getIncludedFiles()) {
final File tagDefFile = new File(tagDir, file);
try (final BufferedReader in = new BufferedReader(new FileReader(tagDefFile))) {
String line;
while ((line = in.readLine()) != null) {
toExecute.createArgument().setValue("-tag");
toExecute.createArgument().setValue(line);
}
} catch (final IOException ioe) {
throw new BuildException("Couldn't read tag file from " + tagDefFile.getAbsolutePath(), ioe);
}
}
}
} else {
final ExtensionInfo tagletInfo = (ExtensionInfo) element;
toExecute.createArgument().setValue("-taglet");
toExecute.createArgument().setValue(tagletInfo.getName());
if (tagletInfo.getPath() != null) {
final Path tagletPath = tagletInfo.getPath().concatSystemClasspath("ignore");
if (!tagletPath.isEmpty()) {
toExecute.createArgument().setValue("-tagletpath");
toExecute.createArgument().setPath(tagletPath);
}
}
}
}
final String sourceArg = source != null ? source : getProject().getProperty(MagicNames.BUILD_JAVAC_SOURCE);
if (sourceArg != null) {
toExecute.createArgument().setValue("-source");
toExecute.createArgument().setValue(sourceArg);
}
if (linksource && doclet == null) {
toExecute.createArgument().setValue("-linksource");
}
if (noqualifier != null && doclet == null) {
toExecute.createArgument().setValue("-noqualifier");
toExecute.createArgument().setValue(noqualifier);
}
}
use of org.apache.tools.ant.DirectoryScanner in project ant by apache.
the class Javadoc method parsePackages.
/**
* Add the directories matched by the nested dirsets to the Vector
* and the base directories of the dirsets to the Path. It also
* handles the packages and excludepackages attributes and
* elements.
*
* @since 1.5
*/
private void parsePackages(final List<String> pn, final Path sp) {
final Set<String> addedPackages = new HashSet<>();
final List<DirSet> dirSets = new ArrayList<>(packageSets);
// and nested excludepackage elements
if (sourcePath != null) {
final PatternSet ps = new PatternSet();
ps.setProject(getProject());
if (packageNames.isEmpty()) {
ps.createInclude().setName("**");
} else {
packageNames.stream().map(PackageName::getName).map(s -> s.replace('.', '/').replaceFirst("\\*$", "**")).forEach(pkg -> ps.createInclude().setName(pkg));
}
excludePackageNames.stream().map(PackageName::getName).map(s -> s.replace('.', '/').replaceFirst("\\*$", "**")).forEach(pkg -> ps.createExclude().setName(pkg));
for (String pathElement : sourcePath.list()) {
final File dir = new File(pathElement);
if (dir.isDirectory()) {
final DirSet ds = new DirSet();
ds.setProject(getProject());
ds.setDefaultexcludes(useDefaultExcludes);
ds.setDir(dir);
ds.createPatternSet().addConfiguredPatternset(ps);
dirSets.add(ds);
} else {
log("Skipping " + pathElement + " since it is no directory.", Project.MSG_WARN);
}
}
}
for (DirSet ds : dirSets) {
final File baseDir = ds.getDir(getProject());
log("scanning " + baseDir + " for packages.", Project.MSG_DEBUG);
final DirectoryScanner dsc = ds.getDirectoryScanner(getProject());
boolean containsPackages = false;
for (String dir : dsc.getIncludedDirectories()) {
// are there any java files in this directory?
final File pd = new File(baseDir, dir);
final String[] files = pd.list((directory, name) -> name.endsWith(".java") || (includeNoSourcePackages && name.equals("package.html")));
if (files.length > 0) {
if ("".equals(dir)) {
log(baseDir + " contains source files in the default package, you must specify them as source files not packages.", Project.MSG_WARN);
} else {
containsPackages = true;
final String packageName = dir.replace(File.separatorChar, '.');
if (!addedPackages.contains(packageName)) {
addedPackages.add(packageName);
pn.add(packageName);
}
}
}
}
if (containsPackages) {
// We don't need to care for duplicates here,
// Path.list does it for us.
sp.createPathElement().setLocation(baseDir);
} else {
log(baseDir + " doesn\'t contain any packages, dropping it.", Project.MSG_VERBOSE);
}
}
}
Aggregations