package RoxyTestUtils import groovy.io.FileType import org.gradle.api.Project import org.gradle.api.tasks.TaskAction import org.gradle.api.DefaultTask class RoxyWrapper{ Project rootProject boolean mlUseRoxyTestCode boolean mlTestRestPortSet String buildDir String testResultsDir String testReportDir static final String mlModulesDir = "src/main/ml-modules/root" static final String mlConfigDir = "src/main/ml-config/" static final String dbConfigDir = mlConfigDir + "databases" static final String serverConfigDir = mlConfigDir + "servers" static final String roxyTestCodeDir = "src/roxyTestCode" static final String testConfigDir = "src/roxyTestConfig" static final String testDbConfig = "test-content-db.json" static final String testServerConfig = "test-server.json" RoxyWrapper(Project p){ rootProject = p mlUseRoxyTestCode = (p.findProperty("mlRoxyTestPort") != null) mlTestRestPortSet = (p.findProperty("mlTestRestPort") != null) buildDir = p.findProperty("buildDir") testResultsDir = buildDir + "/test-results/test" testReportDir = buildDir + "/reports" } static void recursivelyCreateDirectory(String dirName){ File f = new File(dirName) f.mkdirs() } static void copyDirectory(String dirNameFrom, String dirNameTo){ def dirFrom = new File(dirNameFrom) def dirTo = new File(dirNameTo) if (!dirTo.exists()){ dirTo.mkdir(); } // copying the daughter files dirFrom.eachFile(FileType.FILES){File source -> File target = new File(dirTo,source.getName()); target.bytes = source.bytes; } // copying the daughter dirs - recursion dirFrom.eachFile(FileType.DIRECTORIES){File source -> File target = new File(dirTo,source.getName()); copyDirectory(source.path, target.path) } } static void deleteDirectory(dirNameFrom, dirNameTo){ def dirFrom = new File(dirNameFrom) def dirTo = new File(dirNameTo) // deleting the daughter files dirFrom.eachFile(FileType.FILES){File source -> File target = new File(dirTo,source.getName()); target.delete() } // deleting the daughter dirs - recursion dirFrom.eachFile(FileType.DIRECTORIES){File source -> File target = new File(dirTo,source.getName()); deleteDirectory(source.path, target.path) target.delete() } } static void assembleRoxyTestConfig(){ println "Running assembleRoxyTestConfig" RoxyTestUtils.RoxyWrapper.recursivelyCreateDirectory(serverConfigDir) new File(serverConfigDir + "/" + testServerConfig).bytes = new File(testConfigDir + "/" + testServerConfig).bytes //RoxyTestUtils.RoxyWrapper.recursivelyCreateDirectory(dbConfigDir) //new File(dbConfigDir + "/" + testDbConfig).bytes = new File(testConfigDir + "/" + testDbConfig).bytes } static void removeRoxyTestConfig(){ println "Running removeRoxyTestConfig" new File(dbConfigDir + "/" + testDbConfig).delete() new File(serverConfigDir + "/" + testServerConfig).delete() } String roxyTestURL(){ return "http://" + rootProject.findProperty("mlHost") + ":" + rootProject.findProperty("mlRoxyTestPort") + "/test/default.xqy" } void saveTestData(text,String suiteName){ new File(testResultsDir, "TEST-" + suiteName + ".xml").write(text) } static void showTestData(text,String suiteName){ def xml =new XmlSlurper() .parseText(text) .declareNamespace(t:"http://marklogic.com/roxy/test") def header = sprintf("Suite : %-20.20s Passed : %4d Failed : %4d",suiteName,xml.@passed.toInteger(),xml.@failed.toInteger()) println header println "=" * header.length() println "" for(item in xml."t:test"){ def success = (item."t:result".findAll{node -> node.@type == "success"}).size() def failure = (item."t:result".findAll{node -> node.@type == "fail"}).size() def testName = item.@name.text() - ~/\.xqy$/ println sprintf("Test Set : %-20.20s Passed : %4d Failed : %4d",testName,success,failure) } println "" } static parseTestList(text){ def list =new XmlSlurper() .parseText(text) .declareNamespace(t:"http://marklogic.com/roxy/test") return list."t:suite".@path } } class RoxyTestCodeTask extends DefaultTask{ RoxyWrapper roxyWrapper @TaskAction def action(){ if(roxyWrapper.mlUseRoxyTestCode){ println "Using roxy test code" RoxyWrapper.recursivelyCreateDirectory(RoxyWrapper.mlModulesDir) RoxyWrapper.copyDirectory(RoxyWrapper.roxyTestCodeDir,RoxyWrapper.mlModulesDir) } else{ println "Not using roxy test code" RoxyWrapper.deleteDirectory(RoxyWrapper.roxyTestCodeDir,RoxyWrapper.mlModulesDir) } } } class RoxyTestConfigTask extends DefaultTask{ RoxyWrapper roxyWrapper @TaskAction def action(){ if(roxyWrapper.mlUseRoxyTestCode){ if(roxyWrapper.mlTestRestPortSet) RoxyWrapper.assembleRoxyTestConfig() else println "You need to set mlTestRestPort in order to use the roxy test feature - it enables the test content database" } else{ RoxyWrapper.removeRoxyTestConfig() } } } class RoxyTestConfigForUndeployTask extends DefaultTask{ @TaskAction def action(){ RoxyWrapper.assembleRoxyTestConfig() } } class JUnitTestReport{ RoxyWrapper roxyWrapper JUnitTestReport(w){roxyWrapper = w} def execute(){ def testReportDir = new File(roxyWrapper.testReportDir) testReportDir.deleteDir() testReportDir.mkdirs() roxyWrapper.rootProject.ant.taskdef( name: 'junitreport', classname: 'org.apache.tools.ant.taskdefs.optional.junit.XMLResultAggregator', classpath: roxyWrapper.rootProject.configurations.antjunit.asPath ) roxyWrapper.rootProject.ant.junitreport(toDir: roxyWrapper.testReportDir) { fileset ( dir: roxyWrapper.testResultsDir ) report ( format:"frames", todir: roxyWrapper.testReportDir ) } } }