USING OpenEdge.Core.Assert FROM PROPATH.
USING support.test.AssertExt FROM PROPATH.

BLOCK-LEVEL ON ERROR UNDO, THROW.

CLASS tests.TestMethodCanFind:
    DEFINE VARIABLE i2 AS INTEGER NO-UNDO INITIAL 0.
    def temp-table tt1 field f1 as int
                       field f2 as int.

    def temp-table tt2 field f2 as int
                       field f3 as int.

    def temp-table tt3 field f1 as int
                       field f2 as int.
    
    define dataset dtt1 for tt1, tt2 
                   data-relation rel1 for tt1, tt2 relation-fields (f2, f2).
    
    DEFINE PROPERTY propGetterNoBlock AS INTEGER GET. SET.
    
    DEFINE PROPERTY propGetterBlock AS INTEGER GET():
        DEFINE BUFFER bpv FOR adaptivept9.
        FIND FIRST adaptivept9 WHERE 1=1 NO-ERROR.
    END.
    SET.
    
    METHOD PUBLIC VOID PopulateBuffers():
        /* POPULATE TT1 TEMP-TABLE */
       create tt1.
       tt1.f1 = 3.
       tt1.f2 = 3.
       release tt1.

       create tt1.
       tt1.f1 = 5.
       tt1.f2 = 5.
       release tt1.
       create tt1.
       tt1.f1 = 7.
       tt1.f2 = 7.
       release tt1.
       
       create tt1.
       tt1.f1 = 8.
       tt1.f2 = 8.
       release tt1.
       
       create tt1.
       tt1.f1 = 9.
       tt1.f2 = 9.
       release tt1.

       create tt1.
       tt1.f1 = 4.
       tt1.f2 = 4.
       release tt1.

       /* POPULATE TT2 TEMP-TABLE */
       create tt2.
       tt2.f2 = 4.
       tt2.f3 = 4.
       release tt2.

       create tt2.
       tt2.f2 = 6.
       tt2.f3 = 6.
       release tt2.
       
       create tt2.
       tt2.f2 = 0.
       tt2.f3 = 0.
       release tt2.
       
       /* SETS THE AUTO-SYNCHRONIZE ATTRIBUTE ON TT1. */
       buffer tt1:AUTO-SYNCHRONIZE = yes.
    END METHOD.
    
    METHOD PUBLIC INTEGER CheckBuffer():
        Assert:IsFalse(AVAILABLE (tt2)).
        RETURN tt1.f2.
    END METHOD.
    
    @Setup.
    METHOD PUBLIC VOID Setup():
        DeleteAllRecords().
        CREATE adaptivept9.
        adaptivept9.k1 = 1.
        adaptivept9.k2 = 2.
        RELEASE adaptivept9.
    END METHOD.
    
    @Cleanup.
    METHOD PUBLIC VOID Cleanup():
        DeleteAllRecords().
    END METHOD.
    
    METHOD PUBLIC VOID DeleteAllRecords():
        FOR EACH adaptivept9:
            DELETE adaptivept9.
        END.
    END METHOD.
    
    METHOD PUBLIC INTEGER GetValue():
        DEFINE BUFFER bpv FOR adaptivept9.
        IF CAN-FIND(FIRST bpv WHERE bpv.k1 = 1) THEN
            RETURN 1.
        RETURN 100.
    END METHOD.
    
    METHOD PUBLIC INTEGER SetValue():
        i2 = 3.
        RETURN i2.
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestBufferModifiedParentMethodAssign():
        DEFINE VARIABLE iVal AS INTEGER NO-UNDO.
        FIND FIRST adaptivept9 WHERE adaptivept9.k1 = 1.
        ASSIGN
            adaptivept9.k2 = 2
            adaptivept9.k1 = 2
            iVal = GetValue().
        Assert:Equals(100, iVal).
    END METHOD.

    @Test.
    METHOD PUBLIC VOID TestBufferModifiedParentMethodAssignValidationError():
        DEFINE VARIABLE iVal AS INTEGER NO-UNDO.
        DEFINE VARIABLE i AS INTEGER NO-UNDO INITIAL 1.
        
        CREATE adaptivept9.
        adaptivept9.k1 = 2.
        adaptivept9.k2 = 2.
        
        FIND FIRST adaptivept9 WHERE adaptivept9.k1 = 1.
        ASSIGN
            adaptivept9.k2 = 2
            adaptivept9.k1 = 2
            iVal = GetValue().
            
        Assert:IsTrue(FALSE).
        
        CATCH aError AS Progress.Lang.Error :
            Assert:Equals(2, aError:NumMessages).
            Assert:Equals(132, aError:GetMessageNum(1)).
            Assert:Equals("** adaptivept9 already exists with k1 2 k2 2. (132)", aError:GetMessage(1)).
            Assert:Equals(13917, aError:GetMessageNum(2)).
            Assert:Equals("Cannot execute user-defined function, method or property accessor 'GetValue' in an ASSIGN statement after an indexing error. (13917)", aError:GetMessage(2)).
            Assert:Equals(1, i).
            Assert:Equals(0, iVal).
        END CATCH.
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestBufferModifiedParentMethodAssignValidationErrorChangesAreReverted():
        DEFINE VARIABLE iVal AS INTEGER NO-UNDO.
        DEFINE VARIABLE i AS INTEGER NO-UNDO INITIAL 1.
        DEFINE VARIABLE errCode AS INTEGER NO-UNDO EXTENT 2.
        DEFINE VARIABLE errMessage AS CHARACTER NO-UNDO EXTENT 2.
        
        ASSIGN
            errCode[1] = 132
            errCode[2] = 13917
            errMessage[1] = "** adaptivept9 already exists with k1 2 k2 2. (132)"
            errMessage[2] = "Cannot execute user-defined function, method or property accessor 'GetValue' in an ASSIGN statement after an indexing error. (13917)".
        
        CREATE adaptivept9.
        adaptivept9.k1 = 2.
        adaptivept9.k2 = 2.
        
        FIND FIRST adaptivept9 WHERE adaptivept9.k1 = 1.
        
        DO ON ERROR UNDO, THROW:
            ASSIGN
                adaptivept9.k2 = 2
                adaptivept9.k1 = 2
                iVal = GetValue().
            Assert:IsTrue(FALSE).
            
            CATCH aError AS Progress.Lang.Error :
                Assert:Equals(2, aError:NumMessages).
                Assert:Equals(132, aError:GetMessageNum(1)).
                Assert:Equals("** adaptivept9 already exists with k1 2 k2 2. (132)", aError:GetMessage(1)).
                Assert:Equals(13917, aError:GetMessageNum(2)).
                Assert:Equals("Cannot execute user-defined function, method or property accessor 'GetValue' in an ASSIGN statement after an indexing error. (13917)", aError:GetMessage(2)).
                Assert:Equals(1, adaptivept9.k1).
            END CATCH.
        END.
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestBufferModifiedParentMethodAssignValidationErrorDynamicFunction():
        DEFINE VARIABLE iVal AS INTEGER NO-UNDO.
        DEFINE VARIABLE i AS INTEGER NO-UNDO INITIAL 1.
        DEFINE VARIABLE errCode AS INTEGER NO-UNDO EXTENT 2.
        DEFINE VARIABLE errMessage AS CHARACTER NO-UNDO EXTENT 2.
        
        ASSIGN
            errCode[1] = 13664
            errCode[2] = 132
            errMessage[1] = "Cannot invoke method 'GetValue' in object of type 'tests.TestMethodCanFind' using DYNAMIC-FUNCTION(). (13664)"
            errMessage[2] = "** adaptivept9 already ex ists with k1 2 k2 2. (132)".
        
        CREATE adaptivept9.
        adaptivept9.k1 = 2.
        adaptivept9.k2 = 2.
        
        FIND FIRST adaptivept9 WHERE adaptivept9.k1 = 1.
        DO ON ERROR UNDO, THROW:
            ASSIGN
                adaptivept9.k2 = 2
                adaptivept9.k1 = 2
                iVal = DYNAMIC-INVOKE (THIS-OBJECT, "GetValue").
            Assert:IsFalse(TRUE).
            CATCH aError AS Progress.Lang.Error :
                Assert:Equals(2, aError:NumMessages).
                Assert:Equals(132, aError:GetMessageNum(1)).
                Assert:Equals("** adaptivept9 already exists with k1 2 k2 2. (132)", aError:GetMessage(1)).
                Assert:Equals(13917, aError:GetMessageNum(2)).
                Assert:Equals("Cannot execute user-defined function, method or property accessor 'GetValue' in an ASSIGN statement after an indexing error. (13917)", aError:GetMessage(2)).
            END CATCH.
        END.
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestBufferModifiedParentMethodAssignValidationErrorPropertyGetterNoBlock():
        DEFINE VARIABLE iVal AS INTEGER NO-UNDO.
        DEFINE VARIABLE i AS INTEGER NO-UNDO INITIAL 1.
        DEFINE VARIABLE errCode AS INTEGER NO-UNDO EXTENT 1.
        DEFINE VARIABLE errMessage AS CHARACTER NO-UNDO EXTENT 1.
        
        ASSIGN
            errCode[1] = 132
            errMessage[1] = "** adaptivept9 already exists with k1 2 k2 2. (132)".
        
        CREATE adaptivept9.
        adaptivept9.k1 = 2.
        adaptivept9.k2 = 2.
        
        FIND FIRST adaptivept9 WHERE adaptivept9.k1 = 1.
        DO ON ERROR UNDO, THROW:
            ASSIGN
                adaptivept9.k2 = 2
                adaptivept9.k1 = 2
                iVal = propGetterNoBlock.
            Assert:IsTrue(FALSE).
            CATCH aError AS Progress.Lang.Error :
                Assert:Equals(1, aError:NumMessages).
                Assert:Equals(132, aError:GetMessageNum(1)).
                Assert:Equals("** adaptivept9 already exists with k1 2 k2 2. (132)", aError:GetMessage(1)).
                Assert:Equals(1, adaptivept9.k1).
            END CATCH.
        END.
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestBufferModifiedParentMethodAssignValidationErrorPropertyGetterBlock():
        DEFINE VARIABLE iVal AS INTEGER NO-UNDO.
        DEFINE VARIABLE i AS INTEGER NO-UNDO INITIAL 1.
        DEFINE VARIABLE errCode AS INTEGER NO-UNDO EXTENT 2.
        DEFINE VARIABLE errMessage AS CHARACTER NO-UNDO EXTENT 2.
        
        ASSIGN
            errCode[1] = 132
            errCode[2] = 13917
            errMessage[1] = "** adaptivept9 already exists with k1 2 k2 2. (132)"
            errMessage[2] = "Cannot execute user-defined function, method or property accessor 'propGetterBlock' in an ASSIGN statement after an indexing error. (13917)".
        
        CREATE adaptivept9.
        adaptivept9.k1 = 2.
        adaptivept9.k2 = 2.
        
        FIND FIRST adaptivept9 WHERE adaptivept9.k1 = 1.
        DO ON ERROR UNDO, THROW:
            ASSIGN
                adaptivept9.k2 = 2
                adaptivept9.k1 = 2
                iVal = propGetterBlock.
            Assert:IsTrue(FALSE).
            CATCH aError AS Progress.Lang.Error :
                Assert:Equals(2, aError:NumMessages).
                Assert:Equals(132, aError:GetMessageNum(1)).
                Assert:Equals("** adaptivept9 already exists with k1 2 k2 2. (132)", aError:GetMessage(1)).
                Assert:Equals(13917, aError:GetMessageNum(2)).
                Assert:Equals("Cannot execute user-defined function, method or property accessor 'propGetterBlock' in an ASSIGN statement after an indexing error. (13917)", aError:GetMessage(2)).
                Assert:Equals(1, adaptivept9.k1).
            END CATCH.
        END.
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestBufferModifiedParentMethodAssignValidationErrorDoLeave():
        DEFINE VARIABLE iVal AS INTEGER NO-UNDO.
        DEFINE VARIABLE i AS INTEGER NO-UNDO INITIAL 1.

        CREATE adaptivept9.
        adaptivept9.k1 = 2.
        adaptivept9.k2 = 2.

        FIND FIRST adaptivept9 WHERE adaptivept9.k1 = 1.

        DO ON ERROR UNDO, LEAVE:
            ASSIGN
                adaptivept9.k2 = 2
                adaptivept9.k1 = 2
                iVal = GetValue()
                i = 2.
        END.
        Assert:Equals(1, i).
        Assert:Equals(0, iVal).
    END METHOD.

    @Test.
    METHOD PUBLIC VOID TestBufferModifiedParentMethodAssignValidationErrorDoLeave2():
        DEFINE VARIABLE iVal AS INTEGER NO-UNDO.
        DEFINE VARIABLE i AS INTEGER NO-UNDO INITIAL 1.
        i2 = 0.

        CREATE adaptivept9.
        adaptivept9.k1 = 2.
        adaptivept9.k2 = 2.

        FIND FIRST adaptivept9 WHERE adaptivept9.k1 = 1.

        DO ON ERROR UNDO, LEAVE:
            ASSIGN
                adaptivept9.k2 = 2
                adaptivept9.k1 = 2
                iVal = GetValue()
                i = SetValue()
                i = 2.
        END.
        Assert:Equals(1, i).
        Assert:Equals(0, i2).
        Assert:Equals(0, iVal).
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestBufferModifiedAutoSynchronize():
        PopulateBuffers().
        FIND FIRST tt1 WHERE tt1.f1 = 3.
        ASSIGN
            tt1.f2 = 11.
            tt1.f2 = CheckBuffer().
        Assert:Equals(11, tt1.f2).
        Assert:IsFalse(AVAILABLE (tt2)).
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestBufferModifiedParentMethodAssignValidationNoError():
        DEFINE VARIABLE iVal AS INTEGER NO-UNDO.
        DEFINE VARIABLE i AS INTEGER NO-UNDO INITIAL 1.
        DEFINE VARIABLE errCode AS INTEGER NO-UNDO EXTENT 2.
        DEFINE VARIABLE errMessage AS CHARACTER NO-UNDO EXTENT 2.
        
        ASSIGN
            errCode[1] = 132
            errCode[2] = 13917
            errMessage[1] = "** adaptivept9 already exists with k1 2 k2 2. (132)"
            errMessage[2] = "Cannot execute user-defined function, method or property accessor 'GetValue' in an ASSIGN statement after an indexing error. (13917)".
        
        CREATE adaptivept9.
        adaptivept9.k1 = 2.
        adaptivept9.k2 = 2.
        
        FIND FIRST adaptivept9 WHERE adaptivept9.k1 = 1.
        ASSIGN
            adaptivept9.k2 = 2
            adaptivept9.k1 = 2
            iVal = GetValue() NO-ERROR.
        AssertExt:Errors(errCode, errMessage).
    END METHOD.
END CLASS.