podam icon indicating copy to clipboard operation
podam copied to clipboard

Unused fields in AbstractRandomDataProviderStrategy.addAttributeStrategy() and AbstractRandomDataProviderStrategy.addSpecific(). The Javadoc for these two methods is incorrect as does not specify a correct contract.

Open daivanov opened this issue 11 years ago • 6 comments

Calling AbstractRandomDataProviderStrategy.addAttributeStrategy() and AbstractRandomDataProviderStrategy.addSpecific() has no effect if there is already mapping or attribute strategy present.

Despite JavaDocs specifies correct behavior: http://mtedone.github.io/podam/apidocs/uk/co/jemos/podam/api/AbstractRandomDataProviderStrategy.html#addSpecific-java.lang.Class-java.lang.Class-

https://github.com/mtedone/podam/blob/cf7d7fda9b50bb767ce809bf7d9342fdfad41e1d/src/main/java/uk/co/jemos/podam/api/AbstractRandomDataProviderStrategy.java#L540

https://github.com/mtedone/podam/blob/cf7d7fda9b50bb767ce809bf7d9342fdfad41e1d/src/main/java/uk/co/jemos/podam/api/AbstractRandomDataProviderStrategy.java#L598

Both methods define unused variable aClass and attributeStrategyClass

Class<?> aClass = specificTypes.putIfAbsent(abstractClass, specificClass);
if (null == aClass) {
    aClass = specificClass;
}

Class<AttributeStrategy<?>> attributeStrategyClass = attributeStrategies.putIfAbsent(annotationClass, strategyClass);
if (null == attributeStrategyClass) {
    attributeStrategyClass = strategyClass;
}

daivanov avatar May 23 '15 04:05 daivanov

This is the correct behaviour as if there's already an element present nothing should be done. If replacement is needed them the method signature should be called differently, e.g. Replace atrributeStrategy and replaceSpecific. So the issue should be renamed to: remove unused fields. This is no side effect as a side effect has ripercussions outside method context. Unused fields have no ripercussions outside the method context.

mtedone avatar May 23 '15 12:05 mtedone

Fixed in 5.4.1-SNAPSHOT

mtedone avatar May 23 '15 15:05 mtedone

When I call AbstractRandomDataProviderStrategy.addSpecific() I want to be sure this call takes the effect. Current behavior is incorrect.

daivanov avatar May 25 '15 08:05 daivanov

What do you mean by takes effect? Currently it adds the element if it's not there, otherwise it doesn't add it.

mtedone avatar May 25 '15 08:05 mtedone

This test

@Test
public void testPut() throws Exception {
    ConcurrentHashMap<String, String> map = new ConcurrentHashMap<String, String>();
    map.putIfAbsent("key", "first");
    String value = map.get("key");
    assertEquals("first", value);
    map.putIfAbsent("key", "second");
    value = map.get("key");
    assertEquals("second", value);
}

results in:

Failed tests:   testPut(org.indt.AppTest): expected:<[second]> but was:<[first]>

so that all sequential calls of addSpecific result in no-op, which is obviously undesired behavior. putIfAbsent at least returns value, which caller can check, but addSpecific returns this for chaining, so caller will be left to wonder was this operation successful or not. This is really bad API.

daivanov avatar May 25 '15 12:05 daivanov

That's the correct behaviour. A Map holds only one value per key. So the semantic of addSpecific means add an element to the map. What you're describing is an addOrReplace functionality and if you want that you should write an implementation for it. Add means "Add", it doesn't mean "Replace". If you want to add this functionality, create a new issue and implement it. Do not reopen this issue please.

mtedone avatar May 25 '15 13:05 mtedone