How to run Groovy's main class and Test class in Gradle

Source: Internet
Author: User
Tags sybase

The complete Gradle Small project: Centralization of password management


Here is the configuration example build.gradle:

apply plugin:  ' Groovy ' repositories {mavenlocal ()     mavencentral ()} dependencies {    compile  ' org.codehaus.groovy:groovy-all:2.3.7 ' compile  ' org.apache.ant:ant:1.9.4 '     testCompile  ' junit:junit:4.11 ' testcompile  ' commons-io:commons-io:2.2 '}sourcesets {    main {         groovy {srcdirs = ['./src/main/groovy ']include  ' Main.groovy '          }    }    test {         groovy {             srcdirs = ['./src/test/groovy ']        }     }}task runscript (type: javaexec)  {  description  ' run  Groovy script '   //&nbsP Set main property to name of groovy script class.  main  =  ' Main '   // Set classpath for running the Groovy  script.  classpath = sourcesets.main.runtimeclasspath}defaulttasks  ' RunScript '

Main.groovy

import groovy.util.nodeimport groovy.xml.xmlutilpublic class main{public static  Final string line_separator = system.getproperty ("Line.separator") boolean  Extractpassword (node root, def map) {Boolean update = falsestring beanid  string propertynamefor (Def entry: map.entryset ()) {Beanid = entry.key.split ('_') [0]propertyname = entry.key.split ('_') [ -1]def node = root.find{ it.] @id " == beanid }.find{ it." @name " == propertyname }string password = node.attribute (" value ") if (  password ==~ /\$\{.*?\}/ ) {println  "It ' s already a place-holder of  spring style. skip. " Continue}node. " @value " =  ' ${'  + entry.key +  '} ' entry.value = passwordupdate =  true//println xmlutil.serialize (node)}return uPdate}void savexml (String filename, node xml) {def writer = new  FileWriter (FileName) Def printer = new xmlnodeprinter (New printwriter (writer)) Printer.preservewhitespace = trueprinter.print (XML)}void savesstsconfiguration (String  Filename, def map) {file file = new file (fileName) properties props =  new properties () File.withinputstream{ stream ->    props.load ( Stream)    }boolean update = falsemap.entryset (). Each{ entry->if (props[ Entry.key] == null) {if ( ! ( entry.value ==~ /\$\{.*?\}/)   {file.append (line_separator +  "${entry.key}=${ Entry.value} ") update = true}}}if (update) {file.append (line_separator)}}static main (args) {main  obj = new main () string filename =  "./src/main/resources/spring-config.xml" def map = ["Database_password": ", " Sybase_password ":" "]file file = new file ( FileName) Node root = new xmlparser (). ParseText (File.gettext ()) boolean update =  obj.extractpassword (ROOT, MAP) if (update) {New antbuilder (). Copy ( file:fileName,  tofile:filename +  "_bak") Obj.savexml (filename, root) string sstsconfiguration =  " ./src/main/resources/ssts.configuration "New antbuilder (). Copy ( file:sstsconfiguration, tofile: sstsconfiguration +  "_bak") obj.savesstsconfiguration (Sstsconfiguration, map)}else{println  "No update and no replication."} Println map}}

Maintest.groovy

import org.junit.*import static org.junit.assert.*import  org.apache.commons.io.fileutilsimport groovy.util.antbuilderimport groovy.xml.xmlutilimport  groovy.util.nodeimport org.apache.commons.io.fileutilsclass maintest {private obj =  nullstatic final String input =  ' <beans xmlns= '/http Www.springframework.org/schema/beans " xsi:schemalocation=" Http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd " xmlns:xsi=" http://www.w3.org/2001 /xmlschema-instance ">  <bean id=" Database " class=" Org.apache.commons.dbcp.BasicDataSource ">    <property name=" Driverclassname "  value= "com.mysql.jdbc.Driver"/>    <property name= "url"  value= "JDBC: MYSQL://LOCALHOST:3306/TEST?CHARACTERENCODING=GBK "/>    <property name="Username " value=" root "/>    <property name=" password " value=" sa "/>   </bean>  <bean id= "Sybase"  class= " Org.apache.commons.dbcp.BasicDataSource ">    <property name=" Driverclassname "  value= "com.mysql.jdbc.Driver"/>    <property name= "url"  value= "JDBC: MYSQL://LOCALHOST:3306/TEST?CHARACTERENCODING=GBK "/>    <property name=" Username " value=" root "/>    <property name=" password " value=" Ind_ Suezssts "/>  </bean></beans>" static final string target =  "' <beans xmlns=" Http://www.springframework.org/schema/beans " xsi:schemalocation="/HTTP/ Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd "  xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" >  <bean id= "Database"  class= "Org.apache.commons.dbcp.BasicDataSource" >     <property name= "Driverclassname"  value= "Com.mysql.jdbc.Driver"/>     <property name= "url"  value= "JDBC:MYSQL://LOCALHOST:3306/TEST?CHARACTERENCODING=GBK"/>     <property name= "username"  value= "root"/>    <property  name= "Password"  value= "${database_password}"/>  </bean>  <bean  id= "Sybase"  class= "Org.apache.commons.dbcp.BasicDataSource" >    <property  name= "Driverclassname"  value= "Com.mysql.jdbc.Driver"/>    <property  Name= "url"  value= "JDBC:MYSQL://LOCALHOST:3306/TEST?CHARACTERENCODING=GBK"/>     <property name= "username"  value= "root"/>    <property name= " Password " value=" ${sybasE_password} "/>  </bean></beans>" static def map = null     static node root static node xml@beforeclasspublic static  void enter () {} @Beforepublic  void setup () {Root = new xmlparser (). ParseText ( Input) Xml = new xmlparser (). ParseText (target) obj = new main () map = [" Database_password ":" ", " Sybase_password ":" "]} @Testpublic  void extractpasswordreturntrue () { Boolean result = obj.extractpassword (Root, map) def mymap = ["Database_ Password ":" sa ", " Sybase_password ":" Ind_suezssts "]asserttrue resultassertequals mymap, Mapassertequals xmlutil.serialize (XML),  xmlutil.serialize (Root)} @Testpublic  void  Extractpasswordreturnfalse () {Node myxml = new xmlparser (). ParseText (target) boolean  Result = obj.extractpassword (Xml, map) def mymap = ["Database_password": ", " Sybase_password ":"]assertfalse  Resultassertequals mymap,mapassertequals xmlutil.serialize (myXML),  xmlutil.serialize (XML)}@ Testpublic void savexml () {//string filename, node xmlstring filename =   "./src/test/resources/test-a.xml" New file (fileName). Delete () Obj.savexml (Filename, xml) Asserttrue new file (FileName). Exists () Node myxml = new xmlparser (). ParseText (New  file (FileName). GetText ()) assertequals xmlutil.serialize (myXML),  xmlutil.serialize (XML)}//void  savesstsconfiguration (string filename, def map) {@Testpublic  void  Savesstsconfiguration () {string filename =  "./src/test/resources/ssts.configuration.test" String  fileTarget =  "./src/test/resources/ssts.configuration.target" New file (fileName). Write ("") Boolean result = obj.extractpassword (Root,  map) obj.savesstsconfiguration (filename, map) assertequals (Fileutils.readlines (New File ( FileName),  fileutils.readlines (New file (Filetarget)));}}


How to run Groovy's main class and Test class in Gradle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.