Optional parameters in cucumber throw error - java

I think I am copying the answer from here, but I still cannot get optional parameters to work. The two steps run independently, I just wanted to try and combine them.
Scenario:
Then(~/^set timeout(?: at (\d+) (min|hr))?$/) { int duration , String units ->
Works for
And set timeout at 30 min
But not for:
And set timeout
Which throws this error
groovy.lang.MissingMethodException: No signature of method: CucumberTestSteps$_run_closure56.doCall() is applicable for argument types: (null, null) values: [null, null]
Possible solutions: doCall(int, java.lang.String), findAll(), findAll()
I've tried several other random locations for '?:' and '?' with no luck. Also several web searches which all come back to that syntax should work.
Cucumber recognizes it as a valid test because when I add
Then(~/^set timeout$/)
It recognises it as a duplicate step
cucumber.runtime.AmbiguousStepDefinitionsException: ✽.Then set timeout(test.feature:57) matches more than one step definition:
^set timeout$ in CucumberTestSteps.groovy:1128
^set timeout(?: at (\d+) (min|hr))?$ in CucumberTestSteps.groovy:1148

I know I'm too late for this answer, but I had the same issue today and was able to resolve it. Hopefully, this answer will help those looking for the solution to this problem. Apparently, in case of optional parameters, it passes null values to the parameters.
The problem happens because your method has an int instead of Integer. In my case, I changed the int to Integer and did a null check before proceeding. That solved the issue.

Related

Jmeter 5.5 error with assertion value type, string not equal to int

I have a test that used to work perfect in jmeter 5.4.1. Then I upgraded to jmeter 5.5 recently and all JSON assertions stopped working.
this is the message I get from all JSON assertions :
Assertion failure message:Value expected to be '8', but found '8'
the value is the same, but I realized that now it checks the type (string != number), but in previous versions, it used to work.
this is my Json assert :
And this is an example of the JSON response :
{
"person": {
"age": "8"
}
}
Anyone knows how to disable the type check or how to make it works like previous versions?
If I change the JSON assertion value like this, it works :
The problem is that I have a lot of tests, and it's better for me to find a different solution, like enable/disable a parameter, or something like that.
Thanks !
It's a side-effect of the bug fix for the JMeter Issue 5543, you might want to report it
Looking into this line:
Object expected = JSONValue.parse(getExpectedValue());
there is no possibility to revert to the previous behaviour via property. You might want to tick "Match as regular expression" box:
however it might break more complex assertions especially if they contain meta characters
So you either need to downgrade and wait for the decision or scan assertion failure messages using JSR223 Assertion and Groovy language and set the assertion result as passed if "expected" and "found" values are equal

Unexpected results from Metaphone algorithm

I am using phonetic matching for different words in Java. i used Soundex but its too crude. i switched to Metaphone and realized it was better. However, when i rigorously tested it. i found weird behaviour. i was to ask whether thats the way metaphone works or am i using it in wrong way. In following example its works fine:-
Metaphone meta = new Metaphone();
if (meta.isMetaphoneEqual("cricket","criket")) System.out.prinlnt("Match 1");
if (meta.isMetaphoneEqual("cricket","criketgame")) System.out.prinlnt("Match 2");
This would Print
Match 1
Mathc 2
Now "cricket" does sound like "criket" but how come "cricket" and "criketgame" are the same. If some one would explain this. it would be of great help.
Your usage is slightly incorrect. A quick investigation of the encoded strings and default maximum code length shows that it is 4, which truncates the end of the longer "criketgame":
System.out.println(meta.getMaxCodeLen());
System.out.println(meta.encode("cricket"));
System.out.println(meta.encode("criket"));
System.out.println(meta.encode("criketgame"));
Output (note "criketgame" is truncated from "KRKTKM" to "KRKT", which matches "cricket"):
4
KRKT
KRKT
KRKT
Solution: Set the maximum code length to something appropriate for your application and the expected input. For example:
meta.setMaxCodeLen(8);
System.out.println(meta.encode("cricket"));
System.out.println(meta.encode("criket"));
System.out.println(meta.encode("criketgame"));
Now outputs:
KRKT
KRKT
KRKTKM
And now your original test gives the expected results:
Metaphone meta = new Metaphone();
meta.setMaxCodeLen(8);
System.out.println(meta.isMetaphoneEqual("cricket","criket"));
System.out.println(meta.isMetaphoneEqual("cricket","criketgame"));
Printing:
true
false
As an aside, you may also want to experiment with DoubleMetaphone, which is an improved version of the algorithm.
By the way, note the caveat from the documentation regarding thread-safety:
The instance field maxCodeLen is mutable but is not volatile, and accesses are not synchronized. If an instance of the class is shared between threads, the caller needs to ensure that suitable synchronization is used to ensure safe publication of the value between threads, and must not invoke setMaxCodeLen(int) after initial setup.

cordova.exec strange behaviour in parameters

Using cordova 2.6, i am calling a plugin using cordova.exec.
The issue is that the parameters if passed in an array element are being cut(truncated from the end by some characters) at the java side.
but if i pass a simple string as a parameter its being passed perfectly fine.
Passed 5714 characters in string and its going without any isues, but passing 450 characters in an array's first element is cutting the array into about 1/4th of its original length(119) .
Then tried the following :
Converted the first array element into string.(Checked the typeof using javascript) and passed but did not help.
Created a substring of the original array and passed static limits to the substring i.e substring(0,4000) etc etc, but to no luck.
Made a clone of the original array and repeated steps 1 and 2 but again no luck.
Could someone tell me where the issue is?
Increased eclipse heap memory and changed to cordova 2.8.1 too, but the same result. :(
I got the reason for the behaviour. Inspected many images , and in my execute method of plugin did the following :
int myLength = args.getString(0).length();
Log.v(TAG,Character.toString(args.getString(0).charAt(myLength-1)));
Got the character as = on the plugin/java side. And an = is inserted in the base64 string at the end of it. It seems that cordova is inserting these delimeters in the base64string.
P.S. :Went through phonegap.js and found that the arguments are being packed as JSON.stringify , and after that could not get more. That how and where the = is inserted.

Mixing Solr range function with additional parameters

I have a range function in a Solr fq that works as expected:
{!frange l=1 u=2}sum(termfreq(tags,'twitter'),termfreq(tags,'facebook'),termfreq(tags,'pinterest'))
However, if I try to further refine it by adding an additional parameter to the end:
{!frange l=1 u=2}sum(termfreq(tags,'twitter'),termfreq(tags,'facebook'),termfreq(tags,'pinterest')) AND (region:"US")
I get the error: org.apache.solr.search.SyntaxError: Unexpected text after function: AND (region:"US")
If I try to prepend an additional parameter:
(region:"US") AND {!frange l=1 u=2}sum(termfreq(tags,'twitter'),termfreq(tags,'facebook'),termfreq(tags,'pinterest'))
I get the error: org.apache.solr.search.SyntaxError: Expected ')' at position 27 in 'sum(termfreq(tags,'twitter''
I've tried wrapping the range portion in additional parenthesis but still having no luck. How can I combine a range function with additional query parameters?
Ok I got solved what I need to. Basically, I was running commands from the Solr admin dashboard. Although I wasn't able to mix the frange command above with other queries in fq, I was able to put my frange command in q and keep the other queries in fq.
q: {!frange l=1 u=2}sum(termfreq(tags,'twitter'),termfreq(tags,'facebook'),termfreq(tags,'pinterest'))
fq: (region:"US")
I answered a similar question here.
My issue is solved, but I'll leave this unanswered for a few days in case someone knows of a better way.
Please check my answer here: Nested functional range query with OR
which solves this problem using query solr feature.
You can also keep adding fq= parameters this is seen as an AND, for example:
&fq={!frange l=1 u=2}sum(termfreq(tags,'twitter'),termfreq(tags,'facebook'),termfreq(tags,'pinterest'))&fq=region:"US"

Java Assert statement usage, exam certification 1Z0-851

I am studing for 1Z0-851 Oracla Java SE 1.6 Certification and I saw this question:
I marked the first alternative as the correct one and failed! "All of the assert statements are used appropriately" and the answer says that the first one assert(x > 0); is incorrect.. the question is why?
The correct answer is this
Appropriate and inappropriate use of assertions
You can place an assertion at any location that you don't expect to be reached normally.
Assertions can be used to validate the parameters passed to a private method. However,
assertions should not be used to validate parameters passed to public methods because a
public method must check its arguments regardless of whether assertions are enabled or
not. However, you can test postconditions with assertions in both public and non-public
methods. Also, assertions should not change the state of a program in any manner.
Src: http://www.freejavaguide.com/java-scjp-part1.pdf
Line 12 is redundant.
if you remove it, the assertion on line 15 will cover the case where x <= 0
To be honest its a strangely worded question but that is all I can see. I am not sure what is meant by appropriately
If you read just the first assert statement -- which should be interpreted as a "precondition" because of its position --, it implies that the function should work properly with any positive int value, which is not true. Therefore, that assertion is misleading.
Starting by go2, it is easy to understand the assert.
The method does nothing, it just asserts your expectation, that x < 0.
The go method, on the other hand, has a switch.
It is good practice to assert false on the default clause, if you absolutely do not expect your program to fall under this clause, ie, under normal circumstances, one of the cases has to be correct.
The only case on the switch expects x to be exactly 2.
So, to sum up, you don't expect x to be greater than 0, as the first assertion says, you expect x to be 2 and nothing else. Thus, the assertion is not used appropriately.
However, as Jeff noted, the case has no break, which means the default will always be executed, leading, in every scenario, to assert false.
Conclusion: The go method should always result in an error, making assert false properly used, while assert x > 0 isn't correct at all.

Categories

Resources