diff --git a/ballerina/build.gradle b/ballerina/build.gradle index a2fcf0d..8c7e155 100644 --- a/ballerina/build.gradle +++ b/ballerina/build.gradle @@ -63,10 +63,6 @@ ballerina { langVersion = ballerinaLangVersion } -configurations { - externalJars -} - task updateTomlFiles { doLast { def newConfig = ballerinaTomlFilePlaceHolder.text.replace("@project.version@", project.version) @@ -113,6 +109,8 @@ task deleteDependencyTomlFiles { } } + +updateTomlFiles.dependsOn unpackStdLibs build.dependsOn "generatePomFileForMavenPublication" build.dependsOn ":${packageName}-native:build" build.dependsOn deleteDependencyTomlFiles diff --git a/ballerina/tests/stream_default_namspace_test.bal b/ballerina/tests/stream_default_namspace_test.bal index dde58dc..6ecd25b 100644 --- a/ballerina/tests/stream_default_namspace_test.bal +++ b/ballerina/tests/stream_default_namspace_test.bal @@ -110,42 +110,159 @@ type InvoiceFull record { CustomersFull customers; }; +# Test default namespace with a record including all fields using annotations. +# +# + return - return error on failure, otherwise nil. @test:Config function testDefaultNamespaceInvoiceFull() returns error? { stream dataStream = check io:fileReadBlocksAsStream("tests/resources/default_namespaced_invoice.xml"); InvoiceFull invoice = check fromXmlStringWithType(dataStream); + test:assertEquals(invoice.length(), 2, "Invoice count mismatched"); + test:assertEquals(invoice.products.length(), 1, "Products count mismatched"); + test:assertEquals(invoice.products.product.length(), 2, "Products/product count mismatched"); + test:assertEquals(invoice.customers.length(), 1, "Customers count mismatched"); + test:assertEquals(invoice.customers.customer.length(), 2, "Customers/customer count mismatched"); + + test:assertEquals(invoice.products.product[0].length(), 5, "Product 1 field count mismatched"); + test:assertEquals(invoice.products.product[0].id, 1); + test:assertEquals(invoice.products.product[0].name, "Product 1"); + test:assertEquals(invoice.products.product[0].description, string `This is the description for + Product 1. + `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); + test:assertEquals(invoice.products.product[0].price.\#content, 57.70d); + test:assertEquals(invoice.products.product[0].price.currency, "USD"); + test:assertEquals(invoice.products.product[0].category, "Home and Garden"); + + test:assertEquals(invoice.products.product[1].length(), 5, "Product 2 field count mismatched"); + test:assertEquals(invoice.products.product[1].id, 2); + test:assertEquals(invoice.products.product[1].name, "Product 2"); + test:assertEquals(invoice.products.product[1].description, string `This is the description for + Product 2. + `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); + test:assertEquals(invoice.products.product[1].price.\#content, 6312.36d); + test:assertEquals(invoice.products.product[1].price.currency, "LKR"); + test:assertEquals(invoice.products.product[1].category, "Books"); + + test:assertEquals(invoice.customers.customer[0].length(), 5, "Customer 1 field count mismatched"); + test:assertEquals(invoice.customers.customer[0].id, "C001"); + test:assertEquals(invoice.customers.customer[0].name, "John Doe"); + test:assertEquals(invoice.customers.customer[0].email, "john@example.com"); + test:assertEquals(invoice.customers.customer[0].phone, "569-5052"); + test:assertEquals(invoice.customers.customer[0].address.length(), 4, "Customer 1 address field count mismatched"); + test:assertEquals(invoice.customers.customer[0].address.street, "MZ738SI4DV St."); + test:assertEquals(invoice.customers.customer[0].address.city, "Newport"); + test:assertEquals(invoice.customers.customer[0].address.state, "NY"); + test:assertEquals(invoice.customers.customer[0].address.zip, 19140); + + test:assertEquals(invoice.customers.customer[1].length(), 5, "Customer 2 field count mismatched"); + test:assertEquals(invoice.customers.customer[1].id, "C002"); + test:assertEquals(invoice.customers.customer[1].name, "Jane Doe"); + test:assertEquals(invoice.customers.customer[1].email, "jane@example.com"); + test:assertEquals(invoice.customers.customer[1].phone, "674-2864"); + test:assertEquals(invoice.customers.customer[1].address.length(), 4, "Customer 2 address field count mismatched"); + test:assertEquals(invoice.customers.customer[1].address.street, "ZI0TGK3BKG St."); + test:assertEquals(invoice.customers.customer[1].address.city, "Otherville"); + test:assertEquals(invoice.customers.customer[1].address.state, "CA"); + test:assertEquals(invoice.customers.customer[1].address.zip, 77855); +} + +type ProductFullPlain record { + string description; + PriceFullPlain price; + string category; + int id; + string name; +}; + +type PriceFullPlain record { + decimal \#content; + string currency; +}; + +type ProductsFullPlain record { + ProductFullPlain[] product; +}; + +type AddressFullPlain record { + string street; + string city; + string state; + int zip; +}; + +type CustomerFullPlain record { + string name; + string email; + string phone; + AddressFullPlain address; + string id; +}; + +type CustomersFullPlain record { + CustomerFullPlain[] customer; +}; + +type InvoiceFullPlain record { + ProductsFullPlain products; + CustomersFullPlain customers; +}; + +# Test default namespace with a record including all fields without using any annotations. +# +# + return - return error on failure, otherwise nil. +@test:Config +function testDefaultNamespaceInvoiceFullPlain() returns error? { + stream dataStream = check io:fileReadBlocksAsStream("tests/resources/default_namespaced_invoice.xml"); + InvoiceFullPlain invoice = check fromXmlStringWithType(dataStream); + + test:assertEquals(invoice.length(), 2, "Invoice count mismatched"); + test:assertEquals(invoice.products.length(), 1, "Products count mismatched"); + test:assertEquals(invoice.products.product.length(), 2, "Products/product count mismatched"); + test:assertEquals(invoice.customers.length(), 1, "Customers count mismatched"); + test:assertEquals(invoice.customers.customer.length(), 2, "Customers/customer count mismatched"); + + test:assertEquals(invoice.products.product[0].length(), 5, "Product 1 field count mismatched"); test:assertEquals(invoice.products.product[0].id, 1); test:assertEquals(invoice.products.product[0].name, "Product 1"); test:assertEquals(invoice.products.product[0].description, string `This is the description for Product 1. `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); test:assertEquals(invoice.products.product[0].price.\#content, 57.70d); test:assertEquals(invoice.products.product[0].price.currency, "USD"); test:assertEquals(invoice.products.product[0].category, "Home and Garden"); + test:assertEquals(invoice.products.product[1].length(), 5, "Product 2 field count mismatched"); test:assertEquals(invoice.products.product[1].id, 2); test:assertEquals(invoice.products.product[1].name, "Product 2"); test:assertEquals(invoice.products.product[1].description, string `This is the description for Product 2. `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); test:assertEquals(invoice.products.product[1].price.\#content, 6312.36d); test:assertEquals(invoice.products.product[1].price.currency, "LKR"); test:assertEquals(invoice.products.product[1].category, "Books"); + test:assertEquals(invoice.customers.customer[0].length(), 5, "Customer 1 field count mismatched"); test:assertEquals(invoice.customers.customer[0].id, "C001"); test:assertEquals(invoice.customers.customer[0].name, "John Doe"); test:assertEquals(invoice.customers.customer[0].email, "john@example.com"); test:assertEquals(invoice.customers.customer[0].phone, "569-5052"); + test:assertEquals(invoice.customers.customer[0].address.length(), 4, "Customer 1 address field count mismatched"); test:assertEquals(invoice.customers.customer[0].address.street, "MZ738SI4DV St."); test:assertEquals(invoice.customers.customer[0].address.city, "Newport"); test:assertEquals(invoice.customers.customer[0].address.state, "NY"); test:assertEquals(invoice.customers.customer[0].address.zip, 19140); + test:assertEquals(invoice.customers.customer[1].length(), 5, "Customer 2 field count mismatched"); test:assertEquals(invoice.customers.customer[1].id, "C002"); test:assertEquals(invoice.customers.customer[1].name, "Jane Doe"); test:assertEquals(invoice.customers.customer[1].email, "jane@example.com"); test:assertEquals(invoice.customers.customer[1].phone, "674-2864"); + test:assertEquals(invoice.customers.customer[1].address.length(), 4, "Customer 2 address field count mismatched"); test:assertEquals(invoice.customers.customer[1].address.street, "ZI0TGK3BKG St."); test:assertEquals(invoice.customers.customer[1].address.city, "Otherville"); test:assertEquals(invoice.customers.customer[1].address.state, "CA"); diff --git a/ballerina/tests/stream_large_file_test.bal b/ballerina/tests/stream_large_file_test.bal new file mode 100644 index 0000000..41ea903 --- /dev/null +++ b/ballerina/tests/stream_large_file_test.bal @@ -0,0 +1,161 @@ +// Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +import ballerina/io; +import ballerina/test; + +@Namespace { + uri: "http://www.example.com/invoice", + prefix: "inv" +} +@Name { + value: "invoice" +} +type SimpleInvoiceNS record {| + @Namespace { + uri: "http://www.example.com/invoice", + prefix: "inv" + } + SimpleCustomersNS customers; +|}; + +type SimpleCustomersNS record {| + @Namespace { + uri: "http://www.example.com/customers", + prefix: "customers" + } + @Name { + value: "customer" + } + SimpleCustomerNS[10] cust; +|}; + +type SimpleCustomerNS record {| + int id; + string name; + @Namespace { + uri: "http://www.example.com/customers", + prefix: "customers" + } + string city; +|}; + +const LARGE_XML_FILE = "build//resources//large_data.xml"; + +# Test reading large xml file with namespaces. +# + return - return error on failure, else nil. +@test:Config { + enable: false +} +function testLargeFileStreamWithNamespace() returns error? { + stream dataStream = check io:fileReadBlocksAsStream(LARGE_XML_FILE); + SimpleInvoiceNS invoice = check fromXmlStringWithType(dataStream); + + test:assertEquals(invoice.customers.length(), 1, "Invalid number of customers fields"); + test:assertEquals(invoice.customers.cust.length(), 10, "Invalid customers/customer length"); + test:assertEquals(invoice.customers.cust[0].length(), 3, "Invalid customers/customer/[0] fields"); + + test:assertEquals(invoice.customers.cust[0].id, 0, "Invalid customers/customer/[0]/id"); + test:assertEquals(invoice.customers.cust[0].name, "Customer0", "Invalid customers/customer/[0]/name"); + test:assertEquals(invoice.customers.cust[0].city, "City0", "Invalid customers/customer/[0]/city"); + + test:assertEquals(invoice.customers.cust[9].id, 9, "Invalid customers/customer/[9]/id"); + test:assertEquals(invoice.customers.cust[9].name, "Customer9", "Invalid customers/customer/[9]/name"); + test:assertEquals(invoice.customers.cust[9].city, "City9", "Invalid customers/customer/[9]/city"); +} + +type SimpleInvoice record {| + SimpleCustomers customers; +|}; + +type SimpleCustomers record {| + SimpleCustomer[] customer; +|}; + +type SimpleCustomer record {| + int id; + string name; + string city; +|}; + +# Test reading large xml file without considering namespaces. +# + return - return error on failure, else nil. +@test:Config { + enable: false +} +function testLargeFileStream() returns error? { + stream dataStream = check io:fileReadBlocksAsStream(LARGE_XML_FILE); + SimpleInvoice invoice = check fromXmlStringWithType(dataStream); + + test:assertEquals(invoice.customers.length(), 1, "Invalid number of customers fields"); + test:assertEquals(invoice.customers.customer.length(), 100000, "Invalid customers/customer length"); + test:assertEquals(invoice.customers.customer[0].length(), 3, "Invalid customers/customer/[0] fields"); + + test:assertEquals(invoice.customers.customer[0].id, 0, "Invalid customers/customer/[0]/id"); + test:assertEquals(invoice.customers.customer[0].name, "Customer0", "Invalid customers/customer/[0]/name"); + test:assertEquals(invoice.customers.customer[0].city, "City0", "Invalid customers/customer/[0]/city"); + + test:assertEquals(invoice.customers.customer[9].id, 9, "Invalid customers/customer/[9]/id"); + test:assertEquals(invoice.customers.customer[9].name, "Customer9", "Invalid customers/customer/[9]/name"); + test:assertEquals(invoice.customers.customer[9].city, "City9", "Invalid customers/customer/[9]/city"); +} + +@test:BeforeSuite +function createLargeFile() returns error? { + + io:WritableByteChannel wbc = check io:openWritableFile(LARGE_XML_FILE); + _ = check wbc.write(string ``.toBytes(), 0); + _ = check wbc.write("\n".toBytes(), 0); + foreach int i in 0 ... 100000 { + _ = check wbc.write(createProduct(i).toString().toBytes(), 0); + } + _ = check wbc.write("\n".toBytes(), 0); + _ = check wbc.write("\n".toBytes(), 0); + foreach int i in 0 ... 100000 { + _ = check wbc.write(createCustomer(i).toString().toBytes(), 0); + } + _ = check wbc.write("\n".toBytes(), 0); + _ = check wbc.write("\n".toBytes(), 0); +} + +xmlns "http://www.example.com/products" as pro; +xmlns "http://www.example.com/customers" as customers; + +function createProduct(int id) returns xml { + string name = "Product" + id.toString(); + string description = "This is a " + name; + int price = 100 + id; + string category = "category" + id.toString(); + xml product = xml ` + ${description} + ${price} + ${category} + `; + return product; +} + +function createCustomer(int id) returns xml { + string name = "Customer" + id.toString(); + string address = "Address" + id.toString(); + string city = "City" + id.toString(); + string country = "Country" + id.toString(); + xml customer = xml ` + ${address} + ${name}@example.com + ${city} + ${country} + `; + return customer; +} diff --git a/ballerina/tests/stream_namespace_test.bal b/ballerina/tests/stream_namespace_test.bal index c4dc298..f62f4a0 100644 --- a/ballerina/tests/stream_namespace_test.bal +++ b/ballerina/tests/stream_namespace_test.bal @@ -126,45 +126,120 @@ type InvoiceFullNS record { CustomersFull customers; }; +# Test namespaces & prefixes with a record including all fields with annotations. +# +# + return - return error on failure, otherwise nil. @test:Config function testNamespaceInvoiceFull() returns error? { stream dataStream = check io:fileReadBlocksAsStream("tests/resources/default_namespaced_invoice.xml"); - InvoiceFull invoice = check fromXmlStringWithType(dataStream); + InvoiceFullNS invoice = check fromXmlStringWithType(dataStream); + test:assertEquals(invoice.length(), 2, "Invoice count mismatched"); + test:assertEquals(invoice.products.length(), 1, "Products count mismatched"); + test:assertEquals(invoice.products.product.length(), 2, "Products/product count mismatched"); + test:assertEquals(invoice.customers.length(), 1, "Customers count mismatched"); + test:assertEquals(invoice.customers.customer.length(), 2, "Customers/customer count mismatched"); + + test:assertEquals(invoice.products.product[0].length(), 5, "Product 1 field count mismatched"); test:assertEquals(invoice.products.product[0].id, 1); test:assertEquals(invoice.products.product[0].name, "Product 1"); test:assertEquals(invoice.products.product[0].description, string `This is the description for Product 1. `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); test:assertEquals(invoice.products.product[0].price.\#content, 57.70d); test:assertEquals(invoice.products.product[0].price.currency, "USD"); test:assertEquals(invoice.products.product[0].category, "Home and Garden"); + test:assertEquals(invoice.products.product[1].length(), 5, "Product 2 field count mismatched"); test:assertEquals(invoice.products.product[1].id, 2); test:assertEquals(invoice.products.product[1].name, "Product 2"); test:assertEquals(invoice.products.product[1].description, string `This is the description for Product 2. `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); test:assertEquals(invoice.products.product[1].price.\#content, 6312.36d); test:assertEquals(invoice.products.product[1].price.currency, "LKR"); test:assertEquals(invoice.products.product[1].category, "Books"); + test:assertEquals(invoice.customers.customer[0].length(), 5, "Customer 1 field count mismatched"); test:assertEquals(invoice.customers.customer[0].id, "C001"); test:assertEquals(invoice.customers.customer[0].name, "John Doe"); test:assertEquals(invoice.customers.customer[0].email, "john@example.com"); test:assertEquals(invoice.customers.customer[0].phone, "569-5052"); + test:assertEquals(invoice.customers.customer[0].address.length(), 4, "Customer 1 address field count mismatched"); test:assertEquals(invoice.customers.customer[0].address.street, "MZ738SI4DV St."); test:assertEquals(invoice.customers.customer[0].address.city, "Newport"); test:assertEquals(invoice.customers.customer[0].address.state, "NY"); test:assertEquals(invoice.customers.customer[0].address.zip, 19140); + test:assertEquals(invoice.customers.customer[1].length(), 5, "Customer 2 field count mismatched"); test:assertEquals(invoice.customers.customer[1].id, "C002"); test:assertEquals(invoice.customers.customer[1].name, "Jane Doe"); test:assertEquals(invoice.customers.customer[1].email, "jane@example.com"); test:assertEquals(invoice.customers.customer[1].phone, "674-2864"); + test:assertEquals(invoice.customers.customer[1].address.length(), 4, "Customer 2 address field count mismatched"); test:assertEquals(invoice.customers.customer[1].address.street, "ZI0TGK3BKG St."); test:assertEquals(invoice.customers.customer[1].address.city, "Otherville"); test:assertEquals(invoice.customers.customer[1].address.state, "CA"); test:assertEquals(invoice.customers.customer[1].address.zip, 77855); } +# Test namespaces & prefixes with a record including all fields without using any annotations. +# +# + return - return error on failure, otherwise nil. +@test:Config +function testNamespaceInvoiceFullPlain() returns error? { + stream dataStream = check io:fileReadBlocksAsStream("tests/resources/default_namespaced_invoice.xml"); + InvoiceFullPlain invoice = check fromXmlStringWithType(dataStream); + + test:assertEquals(invoice.length(), 2, "Invoice count mismatched"); + test:assertEquals(invoice.products.length(), 1, "Products count mismatched"); + test:assertEquals(invoice.products.product.length(), 2, "Products/product count mismatched"); + test:assertEquals(invoice.customers.length(), 1, "Customers count mismatched"); + test:assertEquals(invoice.customers.customer.length(), 2, "Customers/customer count mismatched"); + + test:assertEquals(invoice.products.product[0].length(), 5, "Product 1 field count mismatched"); + test:assertEquals(invoice.products.product[0].id, 1); + test:assertEquals(invoice.products.product[0].name, "Product 1"); + test:assertEquals(invoice.products.product[0].description, string `This is the description for + Product 1. + `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); + test:assertEquals(invoice.products.product[0].price.\#content, 57.70d); + test:assertEquals(invoice.products.product[0].price.currency, "USD"); + test:assertEquals(invoice.products.product[0].category, "Home and Garden"); + + test:assertEquals(invoice.products.product[1].length(), 5, "Product 2 field count mismatched"); + test:assertEquals(invoice.products.product[1].id, 2); + test:assertEquals(invoice.products.product[1].name, "Product 2"); + test:assertEquals(invoice.products.product[1].description, string `This is the description for + Product 2. + `); + test:assertEquals(invoice.products.product[0].price.length(), 2, "Price 1 price field count mismatched"); + test:assertEquals(invoice.products.product[1].price.\#content, 6312.36d); + test:assertEquals(invoice.products.product[1].price.currency, "LKR"); + test:assertEquals(invoice.products.product[1].category, "Books"); + + test:assertEquals(invoice.customers.customer[0].length(), 5, "Customer 1 field count mismatched"); + test:assertEquals(invoice.customers.customer[0].id, "C001"); + test:assertEquals(invoice.customers.customer[0].name, "John Doe"); + test:assertEquals(invoice.customers.customer[0].email, "john@example.com"); + test:assertEquals(invoice.customers.customer[0].phone, "569-5052"); + test:assertEquals(invoice.customers.customer[0].address.length(), 4, "Customer 1 address field count mismatched"); + test:assertEquals(invoice.customers.customer[0].address.street, "MZ738SI4DV St."); + test:assertEquals(invoice.customers.customer[0].address.city, "Newport"); + test:assertEquals(invoice.customers.customer[0].address.state, "NY"); + test:assertEquals(invoice.customers.customer[0].address.zip, 19140); + + test:assertEquals(invoice.customers.customer[1].length(), 5, "Customer 2 field count mismatched"); + test:assertEquals(invoice.customers.customer[1].id, "C002"); + test:assertEquals(invoice.customers.customer[1].name, "Jane Doe"); + test:assertEquals(invoice.customers.customer[1].email, "jane@example.com"); + test:assertEquals(invoice.customers.customer[1].phone, "674-2864"); + test:assertEquals(invoice.customers.customer[1].address.length(), 4, "Customer 2 address field count mismatched"); + test:assertEquals(invoice.customers.customer[1].address.street, "ZI0TGK3BKG St."); + test:assertEquals(invoice.customers.customer[1].address.city, "Otherville"); + test:assertEquals(invoice.customers.customer[1].address.state, "CA"); + test:assertEquals(invoice.customers.customer[1].address.zip, 77855); +} diff --git a/build.gradle b/build.gradle index 52eb523..761065c 100644 --- a/build.gradle +++ b/build.gradle @@ -59,6 +59,23 @@ allprojects { } } +subprojects { + + configurations { + ballerinaStdLibs + jbalTools + } + + dependencies { + jbalTools ("org.ballerinalang:jballerina-tools:${ballerinaLangVersion}") { + transitive = false + } + + /* Standard libraries */ + ballerinaStdLibs "io.ballerina.stdlib:io-ballerina:${stdlibIOVersion}" + } +} + def moduleVersion = project.version.replace("-SNAPSHOT", "") release { diff --git a/gradle.properties b/gradle.properties index e064fba..1e512f4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,3 +12,4 @@ githubJohnrengelmanShadowVersion=8.1.1 underCouchDownloadVersion=4.0.4 researchgateReleaseVersion=2.8.0 ballerinaGradlePluginVersion=2.0.1 +stdlibIOVersion=1.6.0 \ No newline at end of file