BLOCK-LEVEL ON ERROR UNDO, THROW.

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

CLASS tests.TestWriteDatasetProperty:
    DEFINE PUBLIC PROPERTY OutputMessage AS LONGCHAR NO-UNDO
    GET.
    PROTECTED SET.
    
    DEFINE TEMP-TABLE tt1
        FIELD f1 AS INTEGER.
    
    DEFINE TEMP-TABLE tt2
        FIELD f2 AS INTEGER.
    
    DEFINE DATASET ds1 FOR tt1, tt2.
    
    @Test.
    METHOD PUBLIC VOID TestPropertyTTWriteJSON():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        CREATE tt1.
        tt1.f1 = 1.
        RELEASE tt1.
        cExpected = '~{"tt1":[~{"f1":1~}]~}'.
        OutputMessage = "".
        TEMP-TABLE tt1:WRITE-JSON ("LONGCHAR":U, OutputMessage, FALSE, "UTF-8":U, TRUE).
        Assert:Equals(cExpected, OutputMessage).
        
        FINALLY:
            FOR EACH tt1:
                DELETE tt1.
            END.
        END FINALLY.
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestPropertyTTWriteXML():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        CREATE tt1.
        tt1.f1 = 1.
        RELEASE tt1.
        cExpected = '<?xml version="1.0"?><tt1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><tt1Row><f1>1</f1></tt1Row></tt1>'.
        OutputMessage = "".
        TEMP-TABLE tt1:WRITE-XML ("LONGCHAR":U, OutputMessage).
        Assert:Equals(cExpected, OutputMessage).
        
        FINALLY:
            FOR EACH tt1:
                DELETE tt1.
            END.
        END FINALLY.
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestPropertyTTWriteXMLSchema():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        CREATE tt1.
        tt1.f1 = 1.
        RELEASE tt1.
        cExpected = '<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:prodata="urn:schemas-progress-com:xml-prodata:0001"><xsd:element name="tt1" prodata:proTempTable="true" prodata:undo="true"><xsd:complexType><xsd:sequence><xsd:element name="tt1Row" minOccurs="0" maxOccurs="unbounded"><xsd:complexType><xsd:sequence><xsd:element name="f1" type="xsd:int" nillable="true"/></xsd:sequence></xsd:complexType></xsd:element></xsd:sequence></xsd:complexType></xsd:element></xsd:schema>'.
        OutputMessage = "".
        TEMP-TABLE tt1:WRITE-XMLSCHEMA ("LONGCHAR":U, OutputMessage).
        Assert:Equals(cExpected, OutputMessage).
        
        FINALLY:
            FOR EACH tt1:
                DELETE tt1.
            END.
        END FINALLY.
    END METHOD.

    @Test.
    METHOD PUBLIC VOID TestPropertyDatasetHandleWriteJSON():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE hdl       AS HANDLE  NO-UNDO.
        hdl = DATASET ds1:HANDLE.
        cExpected = '~{"ds1":~{~}~}'.
        OutputMessage = "".
        hdl:WRITE-JSON ("LONGCHAR":U, OutputMessage, FALSE, "UTF-8":U, TRUE).
        Assert:Equals(cExpected, OutputMessage).
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestPropertyDatasetHandleWriteXML():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE hdl       AS HANDLE  NO-UNDO.
        hdl = DATASET ds1:HANDLE.
        cExpected = '<?xml version="1.0"?><ds1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></ds1>'.
        OutputMessage = "".
        hdl:WRITE-XML ("LONGCHAR":U, OutputMessage).
        Assert:Equals(cExpected, OutputMessage).
    END METHOD.

    @Test.
    METHOD PUBLIC VOID TestPropertyDatasetHandleWriteXMLSchema():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE hdl       AS HANDLE  NO-UNDO.
        hdl = DATASET ds1:HANDLE.
        cExpected = '<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:prodata="urn:schemas-progress-com:xml-prodata:0001"><xsd:element name="ds1" prodata:proDataSet="true"><xsd:complexType><xsd:sequence><xsd:element name="tt1" minOccurs="0" maxOccurs="unbounded" prodata:undo="true"><xsd:complexType><xsd:sequence><xsd:element name="f1" type="xsd:int" nillable="true"/></xsd:sequence></xsd:complexType></xsd:element><xsd:element name="tt2" minOccurs="0" maxOccurs="unbounded" prodata:undo="true"><xsd:complexType><xsd:sequence><xsd:element name="f2" type="xsd:int" nillable="true"/></xsd:sequence></xsd:complexType></xsd:element></xsd:sequence></xsd:complexType></xsd:element></xsd:schema>'.
        OutputMessage = "".
        hdl:WRITE-XMLSCHEMA ("LONGCHAR":U, OutputMessage).
        Assert:Equals(cExpected, OutputMessage).
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestPropertyDatasetWriteJSON():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        cExpected = '~{"ds1":~{~}~}'.
        OutputMessage = "".
        DATASET ds1:WRITE-JSON ("LONGCHAR":U, OutputMessage, FALSE, "UTF-8":U, TRUE).
        Assert:Equals(cExpected, OutputMessage).
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestPropertyDatasetWriteXML():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        cExpected = '<?xml version="1.0"?><ds1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></ds1>'.
        OutputMessage = "".
        DATASET ds1:WRITE-XML ("LONGCHAR":U, OutputMessage).
        Assert:Equals(cExpected, OutputMessage).
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestPropertyDatasetWriteXMLSchema():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        cExpected = '<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:prodata="urn:schemas-progress-com:xml-prodata:0001"><xsd:element name="ds1" prodata:proDataSet="true"><xsd:complexType><xsd:sequence><xsd:element name="tt1" minOccurs="0" maxOccurs="unbounded" prodata:undo="true"><xsd:complexType><xsd:sequence><xsd:element name="f1" type="xsd:int" nillable="true"/></xsd:sequence></xsd:complexType></xsd:element><xsd:element name="tt2" minOccurs="0" maxOccurs="unbounded" prodata:undo="true"><xsd:complexType><xsd:sequence><xsd:element name="f2" type="xsd:int" nillable="true"/></xsd:sequence></xsd:complexType></xsd:element></xsd:sequence></xsd:complexType></xsd:element></xsd:schema>'.
        OutputMessage = "".
        DATASET ds1:WRITE-XMLSCHEMA ("LONGCHAR":U, OutputMessage).
        Assert:Equals(cExpected, OutputMessage).
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestPropertySerializeRow():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        cExpected = '~{"tt1":~{"f1":10~}~}'.
        OutputMessage = "".
        
        CREATE tt1.
        tt1.f1 = 10.
        BUFFER tt1:HANDLE:SERIALIZE-ROW ("JSON", "longchar":U, OutputMessage).
        DELETE tt1.
        
        Assert:Equals(cExpected, OutputMessage).
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestPropertySaxWriterSetOutputDestination():
        DEFINE VARIABLE hParser   AS HANDLE   NO-UNDO.
        OutputMessage = "".
        
        CREATE SAX-WRITER hParser.
        hParser:STRICT = FALSE.
        Assert:IsTrue(hParser:SET-OUTPUT-DESTINATION('LONGCHAR', OutputMessage)).
        Assert:IsTrue(hParser:START-DOCUMENT ()).
        
        Assert:IsTrue(hParser:WRITE-CDATA ('')).
        Assert:IsTrue(hParser:END-DOCUMENT ()).
        
        Assert:Equals('<?xml version="1.0"?><![CDATA[]]>', OutputMessage).
        
        FINALLY:
            DELETE OBJECT hParser NO-ERROR.
        END FINALLY.
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestPropertyXDocumentSave():
        DEFINE VARIABLE hDoc      AS HANDLE NO-UNDO.
        DEFINE VARIABLE lRsp      AS LOGICAL  NO-UNDO.
        
        OutputMessage = "".
        CREATE X-DOCUMENT hDoc.
        hDoc:ENCODING = "UTF-8".
        
        lRsp = hDoc:SAVE("longchar":U, OutputMessage) NO-ERROR.
        Assert:IsTrue(lRsp).
        AssertExt:NotErrorNotWarning().
        Assert:Equals('<?xml version="1.0" encoding="UTF-8"?>~n', OutputMessage).
        
        FINALLY:
            DELETE OBJECT hDoc NO-ERROR.
        END FINALLY.
    END METHOD.

 @Test.
    METHOD PUBLIC VOID TestTTWriteJSON():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE cActual AS LONGCHAR NO-UNDO.
        CREATE tt1.
        tt1.f1 = 1.
        RELEASE tt1.
        cExpected = '~{"tt1":[~{"f1":1~}]~}'.
        cActual = "".
        TEMP-TABLE tt1:WRITE-JSON ("LONGCHAR":U, cActual, FALSE, "UTF-8":U, TRUE).
        Assert:Equals(cExpected, cActual).
        
        FINALLY:
            FOR EACH tt1:
                DELETE tt1.
            END.
        END FINALLY.
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestTTWriteXML():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE cActual AS LONGCHAR NO-UNDO.
        CREATE tt1.
        tt1.f1 = 1.
        RELEASE tt1.
        cExpected = '<?xml version="1.0"?><tt1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><tt1Row><f1>1</f1></tt1Row></tt1>'.
        cActual = "".
        TEMP-TABLE tt1:WRITE-XML ("LONGCHAR":U, cActual).
        Assert:Equals(cExpected, cActual).
        
        FINALLY:
            FOR EACH tt1:
                DELETE tt1.
            END.
        END FINALLY.
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestTTWriteXMLSchema():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE cActual AS LONGCHAR NO-UNDO.
        CREATE tt1.
        tt1.f1 = 1.
        RELEASE tt1.
        cExpected = '<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:prodata="urn:schemas-progress-com:xml-prodata:0001"><xsd:element name="tt1" prodata:proTempTable="true" prodata:undo="true"><xsd:complexType><xsd:sequence><xsd:element name="tt1Row" minOccurs="0" maxOccurs="unbounded"><xsd:complexType><xsd:sequence><xsd:element name="f1" type="xsd:int" nillable="true"/></xsd:sequence></xsd:complexType></xsd:element></xsd:sequence></xsd:complexType></xsd:element></xsd:schema>'.
        cActual = "".
        TEMP-TABLE tt1:WRITE-XMLSCHEMA ("LONGCHAR":U, cActual).
        Assert:Equals(cExpected, cActual).
        
        FINALLY:
            FOR EACH tt1:
                DELETE tt1.
            END.
        END FINALLY.
    END METHOD.

    @Test.
    METHOD PUBLIC VOID TestDatasetHandleWriteJSON():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE cActual AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE hdl       AS HANDLE  NO-UNDO.
        hdl = DATASET ds1:HANDLE.
        cExpected = '~{"ds1":~{~}~}'.
        cActual = "".
        hdl:WRITE-JSON ("LONGCHAR":U, cActual, FALSE, "UTF-8":U, TRUE).
        Assert:Equals(cExpected, cActual).
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestDatasetHandleWriteXML():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE cActual AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE hdl       AS HANDLE  NO-UNDO.
        hdl = DATASET ds1:HANDLE.
        cExpected = '<?xml version="1.0"?><ds1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></ds1>'.
        cActual = "".
        hdl:WRITE-XML ("LONGCHAR":U, cActual).
        Assert:Equals(cExpected, cActual).
    END METHOD.

    @Test.
    METHOD PUBLIC VOID TestDatasetHandleWriteXMLSchema():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE cActual AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE hdl       AS HANDLE  NO-UNDO.
        hdl = DATASET ds1:HANDLE.
        cExpected = '<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:prodata="urn:schemas-progress-com:xml-prodata:0001"><xsd:element name="ds1" prodata:proDataSet="true"><xsd:complexType><xsd:sequence><xsd:element name="tt1" minOccurs="0" maxOccurs="unbounded" prodata:undo="true"><xsd:complexType><xsd:sequence><xsd:element name="f1" type="xsd:int" nillable="true"/></xsd:sequence></xsd:complexType></xsd:element><xsd:element name="tt2" minOccurs="0" maxOccurs="unbounded" prodata:undo="true"><xsd:complexType><xsd:sequence><xsd:element name="f2" type="xsd:int" nillable="true"/></xsd:sequence></xsd:complexType></xsd:element></xsd:sequence></xsd:complexType></xsd:element></xsd:schema>'.
        cActual = "".
        hdl:WRITE-XMLSCHEMA ("LONGCHAR":U, cActual).
        Assert:Equals(cExpected, cActual).
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestDatasetWriteJSON():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE cActual AS LONGCHAR NO-UNDO.
        cExpected = '~{"ds1":~{~}~}'.
        cActual = "".
        DATASET ds1:WRITE-JSON ("LONGCHAR":U, cActual, FALSE, "UTF-8":U, TRUE).
        Assert:Equals(cExpected, cActual).
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestDatasetWriteXML():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE cActual AS LONGCHAR NO-UNDO.
        cExpected = '<?xml version="1.0"?><ds1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></ds1>'.
        cActual = "".
        DATASET ds1:WRITE-XML ("LONGCHAR":U, cActual).
        Assert:Equals(cExpected, cActual).
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestDatasetWriteXMLSchema():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE cActual AS LONGCHAR NO-UNDO.
        cExpected = '<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:prodata="urn:schemas-progress-com:xml-prodata:0001"><xsd:element name="ds1" prodata:proDataSet="true"><xsd:complexType><xsd:sequence><xsd:element name="tt1" minOccurs="0" maxOccurs="unbounded" prodata:undo="true"><xsd:complexType><xsd:sequence><xsd:element name="f1" type="xsd:int" nillable="true"/></xsd:sequence></xsd:complexType></xsd:element><xsd:element name="tt2" minOccurs="0" maxOccurs="unbounded" prodata:undo="true"><xsd:complexType><xsd:sequence><xsd:element name="f2" type="xsd:int" nillable="true"/></xsd:sequence></xsd:complexType></xsd:element></xsd:sequence></xsd:complexType></xsd:element></xsd:schema>'.
        cActual = "".
        DATASET ds1:WRITE-XMLSCHEMA ("LONGCHAR":U, cActual).
        Assert:Equals(cExpected, cActual).
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestSerializeRow():
        DEFINE VARIABLE cExpected AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE cActual AS LONGCHAR NO-UNDO.
        cExpected = '~{"tt1":~{"f1":10~}~}'.
        cActual = "".
        
        CREATE tt1.
        tt1.f1 = 10.
        BUFFER tt1:HANDLE:SERIALIZE-ROW ("JSON", "longchar":U, cActual).
        DELETE tt1.
        
        Assert:Equals(cExpected, cActual).
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestSaxWriterSetOutputDestination():
        DEFINE VARIABLE hParser   AS HANDLE   NO-UNDO.
        DEFINE VARIABLE cActual   AS LONGCHAR NO-UNDO.
        cActual = "".
        
        CREATE SAX-WRITER hParser.
        hParser:STRICT = FALSE.
        Assert:IsTrue(hParser:SET-OUTPUT-DESTINATION('LONGCHAR', cActual)).
        Assert:IsTrue(hParser:START-DOCUMENT ()).
        
        Assert:IsTrue(hParser:WRITE-CDATA ('')).
        Assert:IsTrue(hParser:END-DOCUMENT ()).
        
        Assert:Equals('<?xml version="1.0"?><![CDATA[]]>', cActual).
        
        FINALLY:
            DELETE OBJECT hParser NO-ERROR.
        END FINALLY.
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestXDocumentSave():
        DEFINE VARIABLE hDoc      AS HANDLE NO-UNDO.
        DEFINE VARIABLE cActual   AS LONGCHAR NO-UNDO.
        DEFINE VARIABLE lRsp      AS LOGICAL  NO-UNDO.
        
        cActual = "".
        CREATE X-DOCUMENT hDoc.
        hDoc:ENCODING = "UTF-8".
        
        lRsp = hDoc:SAVE("longchar":U, cActual) NO-ERROR.
        Assert:IsTrue(lRsp).
        AssertExt:NotErrorNotWarning().
        Assert:Equals('<?xml version="1.0" encoding="UTF-8"?>~n', cActual).
        
        FINALLY:
            DELETE OBJECT hDoc NO-ERROR.
        END FINALLY.
    END METHOD.
    
    @Test.
    METHOD PUBLIC VOID TestSaxWriterSetOutputDestinationBeforeEnd():
        DEFINE VARIABLE hParser   AS HANDLE   NO-UNDO.
        DEFINE VARIABLE lRsp      AS LOGICAL  NO-UNDO.
        DEFINE VARIABLE lMessage  AS LONGCHAR NO-UNDO.
        OutputMessage = "".
        
        CREATE SAX-WRITER hParser.
        hParser:STRICT = FALSE.
        Assert:IsTrue(hParser:SET-OUTPUT-DESTINATION('LONGCHAR', lMessage)).
        Assert:IsTrue(hParser:START-DOCUMENT ()).
        
        lRsp = hParser:WRITE-CDATA ('') NO-ERROR.
        AssertExt:NotErrorNotWarning().
        Assert:IsTrue(lRsp).
        Assert:Equals('<?xml version="1.0"?><![CDATA[]]>', lMessage).
        
        FINALLY:
            DELETE OBJECT hParser NO-ERROR.
        END FINALLY.
    END METHOD.
END CLASS.