Domanda

I need to to replace in a query a parameter with a project property value. Take this example:

select name from subscribers where phonenb = NEW-CREATE.SUBS.phone and homeaddress = NEW-CREATE.SUBS.address

I should replace everything that starts with the prefix NEW-CREATE.SUBS. with a project property value whose name comes after this prefix. In my example the project property name would be phone

value 0712345678 could be obtained like this: testRunner.testCase.testSuite.project.getPropertyValue("phone")

and address

value England coming from testRunner.testCase.testSuite.project.getPropertyValue("address")

So after this replace my new query should look like:

select name from subscribers where phonenb = "0712345678" and homeaddress = "England"

And this query I would be able to run on the DBConn, first parameter from the file and then store the result.

Can anyone provide me such a groovy code?

Thanks.

È stato utile?

Soluzione

You can use regular expression to identify the property name from the query and then do a replace. The code would look something like this...

def query = "select name from subscribers where phonenb = NEW-CREATE.SUBS.phone and homeaddress = NEW-CREATE.SUBS.address"

def regExp = /NEW-CREATE.SUBS.([a-zA-Z]+)/

matcher = ( query =~ regExp )

matcher.each { match ->
    def prop = testRunner.testCase.testSuite.project.getPropertyValue(match[1])
    //update the query
    query = query.replace("NEW-CREATE.SUBS." + match[1],prop)
}

log.info query

//returns
//Tue Apr 08 11:59:58 ADT 2014:INFO:select name from subscribers where phonenb = 999-999-9999 and homeaddress = nowhere to go

Alternate Solution, Using a prefix stored at project level. The project level property for holding the prefix is called prefix

def query = "select name from subscribers where phonenb = NEW-CREATE.SUBS.phone and homeaddress = NEW-CREATE.SUBS.address"

def prefix = context.expand('${#Project#prefix}') //get prefix from project level properties.

def regExp = /${prefix}([a-zA-Z]+)/

matcher = ( query =~ regExp )

matcher.each { match ->
    def prop = testRunner.testCase.testSuite.project.getPropertyValue(match[1])
    //update the query
    query = query.replace(prefix + match[1],prop)
}

log.info query

//returns
//Tue Apr 08 15:20:51 ADT 2014:INFO:select name from subscribers where phonenb = 999-999-9999 and homeaddress = nowhere to go
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top