Groovy and SoapUI

peterchen-easyli發表於2013-09-27
Groovy
Groovy JDK,Process Management,Regular Expressions, Getting Started Guide
Date
String VS Array
#!/usr/bin/groovy
str = "str1,str2, S333, S566, S88A88G"
def strArray = str.split('[ \t]*,[ \t]*')
println strArray
println strArray.join("-") 

Map
def items = [:]
items["key1"] = "4.00"
items["key2"] = "4.02"
println items
items = items.minus(["key2":"4.02"])
// after v1.8.7 items = items.dropWhile {it.key == "key2"}
println items

Find last modified file(a unix/linux way, it is not compatible with windows)
import java.util.regex.Pattern

def lsDateFormat = "%Y-%m-%d_%H:%M:%S" // for ls command
def dateFormat = "yyyy-MM-dd_HH:mm:ss" // for Date
def lscmd = """ls -lt --time-style=""" + "\"+${lsDateFormat}\" "
lscmd += """/home/Prime/cycles/ | grep .*.csv\$ -m 1 | cut -d ' ' -f 6-7"""

def f = File.createTempFile('bash', '.sh')
f.write("#!/bin/bash\n")
f.append(lscmd)
f.append("\n")
def fpath = f.getAbsolutePath()

"chmod +x ${fpath}".execute()

def cmd = fpath
log.info(cmd)
def proc1 = cmd.execute()
proc1.waitFor()
if ( 0 != proc1.exitValue() ) {
	throw new Exception("Execute ${cmd} failed.")
}
def str = proc1.in.text
f.delete()

def pattern = Pattern.compile('([^ ]{1,}) (.*)')
def matcher = pattern.matcher(str)
def dStr = (new Date()).format(dateFormat)

if (matcher.find()) {
	//count = Integer.valueOf(matcher.group(1));
	dStr = matcher.group(1)
	fileName = matcher.group(2)
	def d = Date.parse(dateFormat, dStr)
	log.info(dStr)
	log.info("Last modified file: ${fileName}. Modified date: ${d}" )
} else {
	log.error("Non file found.")
}

Java
Java API
String

SoapUI
SoapUI API
TestCase,TestRunner

相關文章