EasyMock - expect anyObject except some - easymock

Let's say I have a method like this:
foo (A a, B b)
I want to set expectation so that anyObject is expected except some, like the imaginary code below:
expect(mockedObject).foo(anyObject(A.class), anyObject(B.class)).andReturn(something).anyTimes();
expect(mockedObject).foo(new A("1"), new B("1")).andReturn(something).times(0);
expect(mockedObject).foo(new A("2"), new B("2")).andReturn(something).times(0);
expect(mockedObject).foo(new A("3"), new B("3")).andReturn(something).times(0);
However there is no times(0) in EasyMock. I can create a chain of EasyMock.or() and EasyMock.not() but it's going to be dirty when there are a lot of unexpected objects.
On Mockito I can easily specify
verify(mockedObject, never()).foo(new A("1"), new B("1"));
What's the easiest way on EasyMock to do the same thing as above?

It is something missing indeed.
Right now, the easier is to capture() all your parameters and then check that no invalid permutation was used.

Related

Create a method that convert a List<SObject> to a Map<SObjectField, SObject> with SObjectField as method parameter, any suggestions?

global with sharing class test1 {
global Map<SObjectField, SObject> ConvertMap(List<SObject> listToConvert){
List<SObject> listToConvert = new List<SObject>;
Map<SObjectField, SObject> mapTest = new Map<SObjectField, SObject>;
mapTest.putAll(listToConvert);
return mapTest;
}
I've written this code, but it doesn't respect the request 'cause I can't put SObjectField as method parameter since the Map won't recognize the variable.
Does anyone have suggestions on how to do it?
Thank you in advance.
Do you really need SobjectField as map keys? the special class? If you're fine with strings as keys then there's a builtin, getPopulatedFieldsAsMap()
If you absolutely need SobjectField (I don't even know if it's serializable and can be passed to integrations, LWC etc)... You'd have to loop and marry the results to something like
Schema.describeSObjects(new List<String>{'Account'})[0].fields.getMap()

How to find which element failed comparison between arrays in Kotlin?

I'm writing automated tests for one site. There's a page with all items added to the cart. Maximum items is 58. Instead of verification of each element one by one I decided to create 2 arrays filled with strings: 1 with correct names : String and 1 with names : String I got from the site. Then I compare those 2 arrays with contentEquals.
If that comparison fails, how do I know which element exactly caused comparison fail?
Short simple of what I have now:
#Test
fun verifyNamesOfAddedItems () {
val getAllElementsNames = arrayOf(materials.text, element2.text,
element3.text...)
val correctElementsNames = arrayOf("name1", "name2", "name3"...)
val areArraysEqual = getAllElementsNames contentEquals correctElementsNames
if (!areArraysEqual) {
assert(false)
} else {
assert(true)
}
}
This test fails if 2 arrays are not the same but it doesn't show me the details, so is there a way to see more details of fail, e.g. element that failed comparison?
Thanks.
I recommend using a matcher library like Hamcrest or AssertJ in tests. They provide much better error messages for cases like this. In this case with Hamcrest it would be:
import org.hamcrest.Matchers.*
assertThat(getAllElementsNames, contains(*correctElementsNames))
// or just
assertThat(getAllElementsNames, contains("name1", "name2", "name3", ...))
There are also matcher libraries made specifically for Kotlin: https://github.com/kotlintest/kotlintest, https://yobriefca.se/expect.kt/, https://github.com/winterbe/expekt, https://github.com/MarkusAmshove/Kluent, probably more. Tests using them should be even more readable, but I haven't tried any of them. Look at their documentation and examples and pick the one you like.
You need to find the intersection between the two collections. Intersection will be the common elements. After than removing the intersection collection from the collection you want to perform the test will give you the complementary elements.
val intersection = getAllElementsNames.intersect(correctElementsNames)
getAllElementsNames.removeAll(intersection)

OWL-API Intersection of more concepts

I'm new at OWL-API.
I need to represent an intersection of N concepts
So, intersectionOf (C1, C2, ..... CN).
IntersectionOf has two arguments, but how can I do the general purpose solution?
Is it good enough to build an HashSet and then put it into the arguments?
OWLDataFactory has methods for obtaining an intersection that accept collections - usually sets- of class expressions. I believe that's what you're after.
Just like Ignazio said, but with code:
Java Code:
OWLDataFactory factory = manager.getOWLDataFactory();
Set<OWLClassExpression> mySet = new HashSet<OWLClassExpression>();
factory.getOWLObjectIntersectionOf(mySet);
HTH

Accessing the methods of a preprocessor name dataset in Progress 4GL

I'm trying to access the methods of a dataset in Progress, where the dataset is defined as a preprocessor item. I'm just learning 4GL... maybe this isn't even possible? Here is the scenario in code:
/*My Procedure*/
{Receipt/Receipt_ds.i}
def var hReceipt as handle no-undo.
def var hDataSet as handle no-undo.
run Receipt/Receipt.p persistent set hReceipt.
run GetData in hReceipt ({&input-output_dataset_ReceiptDataSet}).
/* do some stuff */
/* get the handle to the dataset??? Obvious syntax issue here. */
hDataSet = DATASET {&input-output_dataset_ReceiptDataSet}:HANDLE.
/* Empty the DataSet (this is what I want to do)*/
hDataSet:EMPTY-DATASET().
and here's my include file:
/*Receipt/Recipt_ds.i*/
define dataset ReceiptDataSet for
ttRcvHead,
ttRcvDtl,
data-relation for ttRcvHead, ttRcvDtl relation-fields(
stuff, stuff
).
&global-define input-output_dataset_ReceiptDataSet input-output dataset ReceiptDataSet
Clearly my code does not have the correct syntax as mentioned in my comment. Does anyone know what the right way of doing this would be?
This piece:
hDataSet = DATASET {&input-output_dataset_ReceiptDataSet}:HANDLE
is doing this:
hDataSet = DATASET input-output dataset ReceiptDataSet:HANDLE
which isn't working as you've discerned. You need to get to this form instead:
hDataSet = DATASET ReceiptDataSet:HANDLE
If you put a
&GLOBAL-DEFINE pdsName ReceiptDataSet
in your include file and then referenced that where appropriate, then this construct would work:
hDataSet = DATASET {&pdsName}:HANDLE
For starters you need to define a pre-processor before you attempt to use it.
Not at the end of the file.
The next thing that comes to mind is why? Why are you trying to use a pre-processor for this purpose? It clearly isn't to make the code any shorter or more understandable. One reason might be because your code snippet is some sort of common template but that doesn't seem to be the case here.

Idiomatic List Wrapper

In Google App Engine, I make lists of referenced properties much like this:
class Referenced(BaseModel):
name = db.StringProperty()
class Thing(BaseModel):
foo_keys = db.ListProperty(db.Key)
def __getattr__(self, attrname):
if attrname == 'foos':
return Referenced.get(self.foo_keys)
else:
return BaseModel.__getattr__(self, attrname)
This way, someone can have a Thing and say thing.foos and get something legitimate out of it. The problem comes when somebody says thing.foos.append(x). This will not save the added property because the underlying list of keys remains unchanged. So I quickly wrote this solution to make it easy to append keys to a list:
class KeyBackedList(list):
def __init__(self, key_class, key_list):
list.__init__(self, key_class.get(key_list))
self.key_class = key_class
self.key_list = key_list
def append(self, value):
self.key_list.append(value.key())
list.append(self, value)
class Thing(BaseModel):
foo_keys = db.ListProperty(db.Key)
def __getattr__(self, attrname):
if attrname == 'foos':
return KeyBackedList(Thing, self.foo_keys)
else:
return BaseModel.__getattr__(self, attrname)
This is great for proof-of-concept, in that it works exactly as expected when calling append. However, I would never give this to other people, since they might mutate the list in other ways (thing[1:9] = whatevs or thing.sort()). Sure, I could go define all the __setslice__ and whatnot, but that seems to leave me open for obnoxious bugs. However, that is the best solution I can come up with.
Is there a better way to do what I am trying to do (something in the Python library perhaps)? Or am I going about this the wrong way and trying to make things too smooth?
If you want to modify things like this, you shouldn't be changing __getattr__ on the model; instead, you should write a custom Property class.
As you've observed, though, creating a workable 'ReferenceListProperty' is difficult and involved, and there are many subtle edge cases. I would recommend sticking with the list of keys, and fetching the referenced entities in your code when needed.

Resources