Skip to content

Commit

Permalink
[Java] Start to introduce property based testing (PBT).
Browse files Browse the repository at this point in the history
In this commit, I've:

1. Added a test dependency on JQwik.

2. Added a JQwik-based generator of arbitrary valid SBE message schemas.
   This generator exercises a lot of possibilities but not all.
   In particular, it is missing the generation of constants, min/max,
   and custom offsets.

3. Added a test that shows the parser parses any arbitrary SBE message
   schema.

Why have I introduced PBT? My aim is to eventually test (an
approximation of) the following property:

```
∀ msgSchema ∈ PossibleMessageSchemas,
  ∀ bytes ∈ PossibleValidValues(msgSchema),
    DtoEncode(DtoDecode(bytes)) = bytes
```

I.e., that `DtoEncode` is the inverse of `DtoDecode` and that it
preserves the information in an encoded message.
  • Loading branch information
ZachBray committed Oct 5, 2023
1 parent 2f657ed commit 59d5a67
Show file tree
Hide file tree
Showing 5 changed files with 848 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,7 @@ rust/Cargo.lock
.DS_Store
/sbe-tool/src/main/golang/uk_co_real_logic_sbe_ir_generated/

# JQwik
*.jqwik-database

/generated/
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def checkstyleVersion = '9.3'
def hamcrestVersion = '2.2'
def mockitoVersion = '4.11.0'
def junitVersion = '5.10.0'
def jqwikVersion = '1.8.0'
def jmhVersion = '1.37'
def agronaVersion = '1.19.2'
def agronaVersionRange = '[1.19.2,2.0[' // allow any release >= 1.19.2 and < 2.0.0
Expand Down Expand Up @@ -248,6 +249,7 @@ project(':sbe-tool') {
testImplementation files('build/classes/java/generated')
testImplementation "org.hamcrest:hamcrest:${hamcrestVersion}"
testImplementation "org.mockito:mockito-core:${mockitoVersion}"
testImplementation "net.jqwik:jqwik:${jqwikVersion}"
testImplementation "org.junit.jupiter:junit-jupiter-params:${junitVersion}"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2013-2023 Real Logic Limited.
*
* Licensed 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
*
* https://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.
*/

package uk.co.real_logic.sbe.properties;

import net.jqwik.api.Arbitrary;
import net.jqwik.api.ForAll;
import net.jqwik.api.Property;
import net.jqwik.api.Provide;
import uk.co.real_logic.sbe.xml.ParserOptions;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import static uk.co.real_logic.sbe.xml.XmlSchemaParser.parse;

public class ParserPropertyTest
{
@Property
void shouldParseAnyValidSchema(@ForAll("schemas") final SchemaDomain.MessageSchema schema) throws Exception
{
final String xml = XmlSchemaWriter.writeString(schema);
try (InputStream in = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)))
{
parse(in, ParserOptions.DEFAULT);
}
}

@Provide
Arbitrary<SchemaDomain.MessageSchema> schemas()
{
return SchemaDomain.MessageSchema.arbitrary();
}
}
Loading

0 comments on commit 59d5a67

Please sign in to comment.