[ 
https://issues.apache.org/jira/browse/GROOVY-10596?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Pavel Golovenkov updated GROOVY-10596:
--------------------------------------
    Description: 
h3. Context

We use Java 8 (slightly different minor versions on local machines and some 
ENVs)
{noformat}
java version "1.8.0_311"{noformat}
{noformat}
java version "1.8.0_321"{noformat}
Issue started happening after Groovy upgrade from 3.0.9 to 3.0.10
h3. (Case 1) Test fails when run using "mvn clean install"

+Tested code sample:+

 
{code:java}
@CompileStatic
class MetainfoHelper {

    public static final String SCHEMA_KEY_IN_JSON = "\$schema"
    public static final String SCHEMA_KEY_IN_DB = "_\$schema"

    public static void convertSchemaKeyForSavingIntoDB(Document document) {
        String schema = document.getString(SCHEMA_KEY_IN_JSON)
        if (schema) {
            document.remove(SCHEMA_KEY_IN_JSON)
            document.put(SCHEMA_KEY_IN_DB, schema)
        }
    }

    public static void convertSchemaKeyAfterReadingFromDB(Document document) {
        String schema = document.getString(SCHEMA_KEY_IN_DB)
        if (schema) {
            document.remove(SCHEMA_KEY_IN_DB)
            document.put(SCHEMA_KEY_IN_JSON, schema)
        }
        document.remove(ID_IN_DB)
    }

} {code}
 

+Test sample:+

 
{code:java}
@CompileStatic
@ExtendWith(MockitoExtension.class)
class MetainfoHelperTest {

    @Test
    void convertSchemaKeyForSavingIntoDBTest() {
        Document document = new Document(MetainfoHelper.SCHEMA_KEY_IN_JSON, 
"http://json-schema.org/draft-04/schema#";)
        MetainfoHelper.convertSchemaKeyForSavingIntoDB(document)
        assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_JSON)).isNull()
        assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_DB)).isNotNull()
    }

    @Test
    void convertSchemaKeyBackFromDB() {
        Document document = new Document(MetainfoHelper.SCHEMA_KEY_IN_DB, 
"http://json-schema.org/draft-04/schema#";)
        MetainfoHelper.convertSchemaKeyAfterReadingFromDB(document)
        assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_DB)).isNull()
        assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_JSON)).isNotNull()
    }

} {code}
 
h3. (Case 2) At some point of time during the runtime the condition with 
non-null object reference started to be evaluated as false

+Affected code part sample:+

 
{code:java}
@CompileStatic
class DbAcquisitionService implements IDistributedAcquisitionService {

    @Override
    <T extends Acquirable> T tryAcquire(Class<T> clazz, Map<String, ?> filter, 
String ownerKey, String ownerInstance, boolean createIfMissing = false) { {
        final updated = dao.findAndModify(query, updateOperations) as T
        systemLogger.debug("tryAcquire - find and modify completed, updated is 
[$updated]")
        if (updated) {
            systemLogger.debug("tryAcquire - updated is true")
            return updated
        } else {
            systemLogger.debug("tryAcquire - updated is not true, can't 
acquire")
            return null
        }
    }

} {code}
+Logs at the time of issue:+

 
{noformat}
2022-04-22 06:23:46.487 - DEBUG - [scheduling-pool-3][80] - 
c.c.k.c.d.DbAcquisitionService - [] : tryAcquire - find and modify completed, 
updated is [BpmReceivedTask(bpKey:kmc, 
taskId:a422a3f0-d37d-4579-81bf-38d7a758ee79)]
2022-04-22 06:23:46.487 - DEBUG - [scheduling-pool-3][80] - 
c.c.k.c.d.DbAcquisitionService - [] : tryAcquire - updated is not true, can't 
acquire{noformat}
h3. Fix we made to resolve the issue

In both cases in order to fix the issue the next change was made:
{code:java}
if (object) {
}{code}
==>
{code:java}
if (object != null) {
}{code}
 

 

Do you have any ideas or suggestions why that could happen?

  was:
h3. Context

We use Java 8 (sligthtly different minor versions on local machines and some 
envs)
{noformat}
java version "1.8.0_311"{noformat}
{noformat}
java version "1.8.0_321"{noformat}
Issue started happening after Groovy upgrade from 3.0.9 to 3.0.10
h3. (Case 1) Test fails when run using "mvn clean install"

+Tested code sample:+

 
{code:java}
@CompileStatic
class MetainfoHelper {

    public static final String SCHEMA_KEY_IN_JSON = "\$schema"
    public static final String SCHEMA_KEY_IN_DB = "_\$schema"

    public static void convertSchemaKeyForSavingIntoDB(Document document) {
        String schema = document.getString(SCHEMA_KEY_IN_JSON)
        if (schema) {
            document.remove(SCHEMA_KEY_IN_JSON)
            document.put(SCHEMA_KEY_IN_DB, schema)
        }
    }

    public static void convertSchemaKeyAfterReadingFromDB(Document document) {
        String schema = document.getString(SCHEMA_KEY_IN_DB)
        if (schema) {
            document.remove(SCHEMA_KEY_IN_DB)
            document.put(SCHEMA_KEY_IN_JSON, schema)
        }
        document.remove(ID_IN_DB)
    }

} {code}
 

+Test sample:+

 
{code:java}
@CompileStatic
@ExtendWith(MockitoExtension.class)
class MetainfoHelperTest {

    @Test
    void convertSchemaKeyForSavingIntoDBTest() {
        Document document = new Document(MetainfoHelper.SCHEMA_KEY_IN_JSON, 
"http://json-schema.org/draft-04/schema#";)
        MetainfoHelper.convertSchemaKeyForSavingIntoDB(document)
        assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_JSON)).isNull()
        assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_DB)).isNotNull()
    }

    @Test
    void convertSchemaKeyBackFromDB() {
        Document document = new Document(MetainfoHelper.SCHEMA_KEY_IN_DB, 
"http://json-schema.org/draft-04/schema#";)
        MetainfoHelper.convertSchemaKeyAfterReadingFromDB(document)
        assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_DB)).isNull()
        assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_JSON)).isNotNull()
    }

} {code}
 
h3. (Case 2) At some point of time during the runtime the condition with 
non-null object reference started to be evaluated as false

+Affected code part sample:+

 
{code:java}
@CompileStatic
class DbAcquisitionService implements IDistributedAcquisitionService {

    @Override
    <T extends Acquirable> T tryAcquire(Class<T> clazz, Map<String, ?> filter, 
String ownerKey, String ownerInstance, boolean createIfMissing = false) { {
        final updated = dao.findAndModify(query, updateOperations) as T
        systemLogger.debug("tryAcquire - find and modify completed, updated is 
[$updated]")
        if (updated) {
            systemLogger.debug("tryAcquire - updated is true")
            return updated
        } else {
            systemLogger.debug("tryAcquire - updated is not true, can't 
acquire")
            return null
        }
    }

} {code}
+Logs at the time of issue:+

 
{noformat}
2022-04-22 06:23:46.487 - DEBUG - [scheduling-pool-3][80] - 
c.c.k.c.d.DbAcquisitionService - [] : tryAcquire - find and modify completed, 
updated is [BpmReceivedTask(bpKey:kmc, 
taskId:a422a3f0-d37d-4579-81bf-38d7a758ee79)]
2022-04-22 06:23:46.487 - DEBUG - [scheduling-pool-3][80] - 
c.c.k.c.d.DbAcquisitionService - [] : tryAcquire - updated is not true, can't 
acquire{noformat}
h3. Fix we made to resolve the issue

In both cases in order to fix the issue the next change was made:
{code:java}
if (object) {
}{code}
==>
{code:java}
if (object != null) {
}{code}
 

 

Do you have any ideas or suggestions why that could happen?


> IF-condition evaluation on the non-null object sometimes doesn't return true
> ----------------------------------------------------------------------------
>
>                 Key: GROOVY-10596
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10596
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 3.0.10
>            Reporter: Pavel Golovenkov
>            Priority: Major
>
> h3. Context
> We use Java 8 (slightly different minor versions on local machines and some 
> ENVs)
> {noformat}
> java version "1.8.0_311"{noformat}
> {noformat}
> java version "1.8.0_321"{noformat}
> Issue started happening after Groovy upgrade from 3.0.9 to 3.0.10
> h3. (Case 1) Test fails when run using "mvn clean install"
> +Tested code sample:+
>  
> {code:java}
> @CompileStatic
> class MetainfoHelper {
>     public static final String SCHEMA_KEY_IN_JSON = "\$schema"
>     public static final String SCHEMA_KEY_IN_DB = "_\$schema"
>     public static void convertSchemaKeyForSavingIntoDB(Document document) {
>         String schema = document.getString(SCHEMA_KEY_IN_JSON)
>         if (schema) {
>             document.remove(SCHEMA_KEY_IN_JSON)
>             document.put(SCHEMA_KEY_IN_DB, schema)
>         }
>     }
>     public static void convertSchemaKeyAfterReadingFromDB(Document document) {
>         String schema = document.getString(SCHEMA_KEY_IN_DB)
>         if (schema) {
>             document.remove(SCHEMA_KEY_IN_DB)
>             document.put(SCHEMA_KEY_IN_JSON, schema)
>         }
>         document.remove(ID_IN_DB)
>     }
> } {code}
>  
> +Test sample:+
>  
> {code:java}
> @CompileStatic
> @ExtendWith(MockitoExtension.class)
> class MetainfoHelperTest {
>     @Test
>     void convertSchemaKeyForSavingIntoDBTest() {
>         Document document = new Document(MetainfoHelper.SCHEMA_KEY_IN_JSON, 
> "http://json-schema.org/draft-04/schema#";)
>         MetainfoHelper.convertSchemaKeyForSavingIntoDB(document)
>         assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_JSON)).isNull()
>         assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_DB)).isNotNull()
>     }
>     @Test
>     void convertSchemaKeyBackFromDB() {
>         Document document = new Document(MetainfoHelper.SCHEMA_KEY_IN_DB, 
> "http://json-schema.org/draft-04/schema#";)
>         MetainfoHelper.convertSchemaKeyAfterReadingFromDB(document)
>         assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_DB)).isNull()
>         
> assertThat(document.get(MetainfoHelper.SCHEMA_KEY_IN_JSON)).isNotNull()
>     }
> } {code}
>  
> h3. (Case 2) At some point of time during the runtime the condition with 
> non-null object reference started to be evaluated as false
> +Affected code part sample:+
>  
> {code:java}
> @CompileStatic
> class DbAcquisitionService implements IDistributedAcquisitionService {
>     @Override
>     <T extends Acquirable> T tryAcquire(Class<T> clazz, Map<String, ?> 
> filter, String ownerKey, String ownerInstance, boolean createIfMissing = 
> false) { {
>         final updated = dao.findAndModify(query, updateOperations) as T
>         systemLogger.debug("tryAcquire - find and modify completed, updated 
> is [$updated]")
>         if (updated) {
>             systemLogger.debug("tryAcquire - updated is true")
>             return updated
>         } else {
>             systemLogger.debug("tryAcquire - updated is not true, can't 
> acquire")
>             return null
>         }
>     }
> } {code}
> +Logs at the time of issue:+
>  
> {noformat}
> 2022-04-22 06:23:46.487 - DEBUG - [scheduling-pool-3][80] - 
> c.c.k.c.d.DbAcquisitionService - [] : tryAcquire - find and modify completed, 
> updated is [BpmReceivedTask(bpKey:kmc, 
> taskId:a422a3f0-d37d-4579-81bf-38d7a758ee79)]
> 2022-04-22 06:23:46.487 - DEBUG - [scheduling-pool-3][80] - 
> c.c.k.c.d.DbAcquisitionService - [] : tryAcquire - updated is not true, can't 
> acquire{noformat}
> h3. Fix we made to resolve the issue
> In both cases in order to fix the issue the next change was made:
> {code:java}
> if (object) {
> }{code}
> ==>
> {code:java}
> if (object != null) {
> }{code}
>  
>  
> Do you have any ideas or suggestions why that could happen?



--
This message was sent by Atlassian Jira
(v8.20.7#820007)

Reply via email to