EasyMock: can andReturn give a runtime error? - easymock

I'm using EasyMock to mock a class called "Tuple". The Tuple.getString(int i) method is supposed to return the String at position i in the tuple. If that field is not a String, it will give a runtime error.
In order for andReturn to work, I will have to cast the value to String like below:
expect(tuple.getString(i)).andReturn((String) json.get(list[i])).atLeastOnce();
which is not the desired behavior.
For example, if json.get(list[i]) is an integer, I want to give a runtime error instead of casting it to String.
Is there a way to get around this?
Thank you!

So I actually don't have to cast it because JSONObject has a getString() method. I don't know why I didn't think of this earlier.

Related

PowerShell - .count() not working on an array of strings

I'm probably over thinking this, but I have an array of strings and when I enter $[arrayname].count(); I get an error that it failed because system.string doesn't contain a method named 'count'.
I know this works with an array of integers, and I would think there would be an easier way to get how many values are in an array other than doing a forloop.
Below is a simplified example. The example threw the error.
$Test=#("3","2","1")
$Test.count()
Lee_Dailey actually answered this question.
I needed to use the property [.count] not the method [.count()]

Querying database in Grails

The values in database are saved as such
::{"rating1":"2","rating2":"4","rating3":"5","rating4":"0","rating5":"0"},
Now I need to acces the individual values like 2,4,5 etc.
I made a variable "rating" of the Domain Class type and tried accesing as object using (.) operator but it wont work and gives error:
:exception::groovy.lang.MissingPropertyException: No such property: rating1 for class: java.lang.String
, I tried casting to array and list (as Array, as ArrayList, as List) etc but that wont work either.
Casting to List gives exception:exception::org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '{"rating1":"2","rating2":"4","rating3":"5","rating4":"0","rating5":"0"}' with class 'java.lang.String' to class 'java.util.List' .
Accessing like "rating[3]" gives answer "a". Should I use "rating[11]" to get value 2 or is there any way around.
What could be the possible solution. Please help.
You're storing a string, so you only have string operations available. You need to parse that string to get at the attributes individually (JsonSlurper perhaps?).
rating[character-position] is a bad idea IMO.
Maybe transients (check the GORM docs) would be useful.
class YourDomain {
String yourField
String getRating1() { new JsonSlurper().parseText(yourField).rating1 }
}
Maybe. Totally untested, just an idea.

Unterminated string literal error while storing lesson_location

I have this error that doesnt give any indication as to what is the problem:
I'm trying to store this string in lesson_location field:
B^$eNrT0srLywNiLRANpiAsMAknoMKognlwdUi6ERrQuDARmCwmE2EMslOQ9aFaguw6dKyllQxhJcNp TFBTk1iSWhBvYBhvYGAKABXXVRI&#3d;
but it throws SyntaxError: unterminated string literal
when I've modified the way the reload stores the data in ReloadAPIAdaptor.js
from using eval on entire string:
eval("this.cmi.core.lesson_location.cmivalue =\"B^$eNrT0srLywNiLRANpiAsMAknoMKognlwdUi6ERrQuDARmCwmE2EMslOQ9aFaguw6dAzXnJwMozFB TU1ual4pAMimU3Q&#3d;\";");
to evaluate object first:
var o = eval("this." + element);
console.log("o",o);
if(o) o.cmivalue = value;
then it stores data without error,
now I can't modify the code in any lms so this was only to identify if the string can't be stored but it can. Just evil doesnt work so the question is what is in the given string that eval doesnt like and how to fix it.
There is nothing wrong with your string if you're doing the following:
API.SetValue("cmi.core.lesson_location", "B^$eNrT0srLywNiLRANpiAsMAknoMKognlwdUi6ERrQuDARmCwmE2EMslOQ9aFaguw6dKyllQxhJcNp TFBTk1iSWhBvYBhvYGAKABXXVRI&#3d;");
(where API is a reference to the window's API object)
The string is valid as far as SCORM is concerned, and the length falls within the acceptable character limit.
If you're encountering an issue, it might be a bug within the Reload wrapper. Frankly, the code in the Reload wrapper (as found on SourceForge) is TEN years old. It uses eval() and other JavaScript techniques that have been identified as problematic, and are highly discouraged by leading JavaScript developers. Your bug might very well be related to the wrapper's use of eval().
I'd try using a different wrapper and see if it makes a difference.

What is happening in Console.Write() when i put int array as parameter?

Question like in title. What is exactly happening when Write method is invoked? If I have code like this:
int [] t = new int[]{2,1};
Console.Write(t);
is there any posibility that without changing Write method parameter (without adding [0]) number 2 will be displayed? (first element of array) Will this code give the same result in different .NET framework versions?
Why this method doesn't write type of y? (System.Int32[])
No that is not really possible since you would have to redefine the toString() method of Int32[].
Console.Write(t) is simply Console.Write(t.toString()) and t.toString() is Int32[].toString().
The behavior of Int32[].toString() is to simply return the type, in this case System.Int32[].
So to answer your question, no you cannot make the Int32[].toString() return the toString() of the first index.

Flex 4 converting array.length into a string

so I am trying to assign a number to a variable that is dynically generated from a binded array...when i try and assign it and trace it out nothing happens, which means I am obviously doing something wrong but I am not sure? just for fun i decided to bind the data to a label like so...
<s:Label text="{this.dd.selectedViews.length}"/>
and that work and updated properly, but when running in debug mode i got this warning...
warning: unable to bind to property 'length' on class 'Array' (class is not an IEventDispatcher)
so what would be the best method of assigning the array to a variable to use throughout my application
thanks in advance for any help
I'm not really sure what you are asking here, but maybe this will help you out. As your error message says, the Array class is not an IEventDispatcher. What that means is that if you try to use a plain old Array as the source of a data-binding expression, it generally is not going to work.
If you need to bind to an array, you can try using a different class such as ArrayCollection, which supports data binding.

Resources