I created that transformation
package org.global
import groovy.transform.CompileStatic
import org.codehaus.groovy.ast.ASTNode
import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.ClassHelper
import org.codehaus.groovy.ast.MethodNode
import org.codehaus.groovy.ast.Parameter
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.SourceUnit
import org.codehaus.groovy.transform.ASTTransformation
import org.codehaus.groovy.transform.GroovyASTTransformation
import org.codehaus.groovy.ast.stmt.BlockStatement
import org.codehaus.groovy.ast.stmt.ExpressionStatement
import org.codehaus.groovy.ast.expr.MethodCallExpression
import org.codehaus.groovy.ast.expr.VariableExpression
import org.codehaus.groovy.ast.expr.ConstantExpression
import org.codehaus.groovy.ast.expr.ArgumentListExpression
#CompileStatic
#GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS)
public class SayHelloTransformation implements ASTTransformation {
public void visit(ASTNode[] astNodes, SourceUnit sourceUnit) {
BlockStatement statement = new BlockStatement()
statement.addStatement(
new ExpressionStatement (
new MethodCallExpression (
new VariableExpression("this"),
new ConstantExpression("println"),
new ArgumentListExpression(
new ConstantExpression("hello world")
)
)
)
)
MethodNode sayHelloMethod = new MethodNode(
"sayHello", 0x0001, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY,
ClassNode.EMPTY_ARRAY, statement
)
sourceUnit.AST.methods.add(sayHelloMethod)
}
}
I compiled it. Then I added META-INF folder with descriptor
org.global.SayHelloTransformation
So the overall structure looks like this
META-INF\
services\
org.codehaus.groovy.transform.ASTTransformation
org\
global\
SayHelloTransformation.class
SayHelloTransformation.groovy
Then I added SomeClass.groovy to the same folder as SayHelloTransformation.groovy
class SomeClass {
}
And test.groovy file
new SomeClass().sayHello()
But I got this exception
Caught: groovy.lang.MissingMethodException: No signature of method: SomeClass.sayHello() is applicable for argument types: (
) values: []
groovy.lang.MissingMethodException: No signature of method: SomeClass.sayHello() is applicable for argument types: () values
: []
at test.run(test.groovy:2)
The whole structure looks like this
META-INF\
services\
org.codehaus.groovy.transform.ASTTransformation
org\
global\
SayHelloTransformation.class
SayHelloTransformation.groovy
SomeClass.class
SomeClass.groovy
test.groovy
Related
I have 2 CustomKeywords, located in the same package in Katalon Studio project. I try to call one custom keyword from the other one. This code isn’t working in this case:
CustomKeywords.'mypack.myclass.mymethod'()
Keyword, which should be called:
package uploadFile
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import java.awt.Robot
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
import java.awt.event.KeyEvent
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable
class upload2Files {
#Keyword
def upload(TestObject to, String filePath , String file , String file2) {
WebUI.click(to)
StringSelection ss = new StringSelection("\""+filePath+"\" " +"\""+ file +"\" "+ file2 );
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL)
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
}
Other keyword, where I try to call it:
(new uploadFile.upload2Files()).upload(findTestObject('Object Repository/validateFile/input_originalFile'), (d_directory.toString() + '\\') + detachedTXT1, (d_directory.toString() + '\\') + detachedTXT2)
Error message:
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: uploadFile.upload2Files.upload() is applicable for argument types: (com.kms.katalon.core.testobject.TestObject, java.lang.String, java.lang.String) values
I will explain on the example "(new packagename.classname()).methodname()"
I have keyword1:
package closeAplication
import...
public class closeApp {
#Keyword
public void cmdAdbCloseApp(String ApplicationID){
String CMDclose = ('adb shell am force-stop ' + ApplicationID)
println ('This CMD Windows command will be executed: ' + CMDclose)
Runtime.getRuntime().exec(CMDclose)
}
}
I will use keyword1 in the test case:
def ApplicationID = (GlobalVariable.ApplicationIDds)
CustomKeywords.'closeAplication.closeApp.cmdAdbCloseApp'(ApplicationID)
I want to write another keyword2 and call inside keyword1:
package runAppInMobile
import ...
public class runAppClass {
#Keyword
public void runApp (String ApplicationID){
new closeAplication.runAppClass().cmdAdbCloseApp(ApplicationID) //here's the call of the above keyword1
Mobile.startExistingApplication(ApplicationID)
}
}
(new packagename.classname()).methodname()
mockito-1.10.19
powermock-mockito-1.7.1
powermock-1.7.4
junit 4.12
I have a class that has multiple constructors (java). Once constructor calls the other. I want to mock only 1 of the constructors (the one that is called from the other). I cannot change the code unfortunately - I am just testing it. Here is the class to be tested:
import java.io.File;
import java.sql.connection;
public class Foo {
public Foo (Connection connection){
this(connection, new File ());
}
public Foo (Connection connection, File file){
// do stuff
}
// other methods
}
Here is the test class I have written:
import java.io.File;
import java.sql.connection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.legacy.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest(Foo.class)
#PowerMockIgnore("javax.management.*")
public class FooTest {
#Test
public void testFoo() throws Exception {
Connection mockConnection = Mockito.mock(Connection.class);
Foo fooObj = Mockito.mock(Foo.class);
PowerMockito.whenNew(Foo.class).withArguments(Matchers.notNull(), Matchers.notNull()).thenReturn(fooObj);
Foo newFooObj = new Foo (mockConnection);
assertNotNull ("newFooObj should not be null", newFooObj);
}
}
The problem is that Foo(Connection) is not being entered. Is there something I am missing?
I tried your code with the latest 1.7.x version of Powermock (1.7.4) and it works as you wanted it to. So you might just need to upgrade a few minor versions.
I'm new to Apache Spark and I'm learning how to use it in java. I would like to define and use an user defined function (udf) and I get the scala.MatchError by returning a java.util.HashMap.
Here is my code for extracting hashtags from a tweets dataset and adding a new column with a map of the hashtag and it's number of occurrences in the respective tweet:
// Open spark session
SparkSession sparkSession = SparkSession.builder().master("local[*]").appName("TwitterAnalyticsExample").getOrCreate();
// Load training data
Dataset<Row> twitterData = sparkSession.read().format("json").load(inputFilePath);
UDF1 extractHashtags = new UDF1<String, Map<String, Integer>>() {
#Override
public Map<String, Integer> call(String tweet) throws Exception {
Map<String, Integer> result = new HashMap<>();
Pattern pattern = Pattern.compile("#\\w*");
Matcher matcher = pattern.matcher(tweet);
while (matcher.find()) {
result.merge(matcher.group(), 1, (v1, v2) -> v1 + v2);
}
return result;
}
};
sparkSession.sqlContext().udf().register("extractHashtags", extractHashtags, DataTypes.StringType);
twitterData.limit(50).select(callUDF("extractHashtags", col("text"))).show(20);
and following imports:
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.api.java.UDF1;
import org.apache.spark.sql.types.DataTypes;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Any hint, what am I doing wrong? Is the return type java.util.Map a problem for UDF? What could I use instead?
I am new to ASM. I have a class file in which I have runtime visible annotations for methods. I want to parse this class file and select the annotation according to specific criteria. I looked into the documentation of ASM and tried with the visibleAnnotation. I can't seem to print the list of annotations of method which I can see in my class files.
My code is as
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.ClassReader;
public class ByteCodeParser {
public static void main(String[] args) throws Exception{
InputStream in=new FileInputStream("sample.class");
ClassReader cr=new ClassReader(in);
ClassNode classNode=new ClassNode();
//ClassNode is a ClassVisitor
cr.accept(classNode, 0);
//
Iterator<MethodNode> i = classNode.methods.iterator();
while(i.hasNext()){
MethodNode mn = i.next();
System.out.println(mn.name+ "" + mn.desc);
System.out.println(mn.visibleAnnotations);
}
}
}
The output is:
<clinit>()V
null
<init>()V
null
MyRandomFunction1()V
[org.objectweb.asm.tree.AnnotationNode#5674cd4d]
MyRandomFunction2()V
[org.objectweb.asm.tree.AnnotationNode#63961c42]
My RandomFunction 1 & 2 has annotations but I can't seem to understand [org.objectweb.asm.tree.AnnotationNode#5674cd4d].
I solved this issue myself, I had to iterate over the annotations which I didn't realize initally.
if (mn.visibleAnnotations != null) {
Iterator<AnnotationNode>j=mn.visibleAnnotations.iterator();
while (j.hasNext()) {
AnnotationNode an=j.next();
System.out.println(an.values);
}
}
I want to create a groovy's bean which takes the parameters of a request and return an appropriate response when the sum of parameter is positive. I want to know how I can load my request in a method of groovy bean
import com.eviware.soapui.support.XmlHolder
import com.eviware.soapui.support.GroovyUtils
import com.eviware.soapui.model.mock.*
import org.apache.commons.lang.StringUtils
import groovy.util.*
import groovy.lang.*
public class Test
{
public int somme ()
{
def holder = new XmlHolder(mockRequest.getRequestContent())
def a = holder["//firstInt:a"]
def b = holder["//secondInt:b"]
return(a + b)
}
public String getResponse ()
{
Test t = new Test ()
if (t.somme() > 0)
{
return "response1"
}
else
{
return "response2"
}
}
}
When I run this code I have error below:
com.eviware.soapui.impl.wsdl.mock.DispatchException: Failed to
dispatch using script; groovy.lang.MissingPropertyException: No such
property: mockRequest for class:Test
Your class is supposed to appear like this.
Note the changes:
Declared mockRequest member variable, pass it as argument to constructor of the class.
Object creation is not required in getResponse() method, instead you may do it in another script as you desired. Like in Mock response's script where mockRequest is available.
import com.eviware.soapui.model.mock.MockRequest
import com.eviware.soapui.support.XmlHolder
public class Test {
MockRequest mockRequest
public Test(MockRequest mockRequest) {
this.mockRequest=mockRequest
}
public int somme () {
def holder=new XmlHolder(mockRequest.getRequestContent())
def a=holder["//firstInt:a"]
def b=holder["//secondInt:b"]
(a + b)
}
public String getResponse () {
somme() ? "response1" : "response2"
}
}