Skip to content

Commit

Permalink
fix errors and add tests for xml deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoAi committed Oct 3, 2023
1 parent bd0c380 commit 4bb63ec
Show file tree
Hide file tree
Showing 25 changed files with 2,361 additions and 378 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ public String getPureClassName()

private List<AddressUse> addresses;

private int addressesSize;

private List<Person> employees;

private int employeesSize;

public String getName()
{
return this.nameSize == 0 ? null : this.name;
Expand Down Expand Up @@ -95,6 +99,7 @@ public void _addressesAdd(AddressUse value)
this.addresses = new ArrayList<AddressUse>();
}
this.addresses.add(value);
this.addressesSize++;
}

public List<Person> getEmployees()
Expand All @@ -109,6 +114,7 @@ void _employeesAddImpl(Person value)
this.employees = new ArrayList<Person>();
}
this.employees.add(value);
this.employeesSize++;
}

public void _employeesAdd(Person value)
Expand All @@ -128,10 +134,6 @@ public List<IDefect> checkMultiplicities()
{
defects.add(BasicDefect.newClassStructureDefect("Invalid multiplicity for ranking: expected [0..1] found [" + this.rankingSize + "]", "meta::external::format::shared::testpack::simple::Firm"));
}
if (this.addresses.size() < 1L)
{
defects.add(BasicDefect.newClassStructureDefect("Invalid multiplicity for addresses: expected [1..*] found [" + this.addresses.size() + "]", "meta::external::format::shared::testpack::simple::Firm"));
}
return defects;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,7 @@ function meta::external::format::shared::executionPlan::platformBinding::legendJ
let itemType = if($javaType->isJavaList(), |$javaType->elementType(), |$javaType)->toUnboxed();

let occurs = occursFromMultiplicity($p.multiplicity);
let getSize = if($javaType->isJavaList(),
| j_this($baseClass)->j_field($fieldName, $javaType)->j_invoke('size', [], javaInt()),
| j_this($baseClass)->j_field($sizeFieldName, javaInt())
);
let getSize = j_this($baseClass)->j_field($sizeFieldName, javaInt());
let lowerBoundClause = $getSize->j_lt($occurs.first);
let upperBoundClause = $getSize->j_gt($occurs.second);
let expectedMultiplicity = $p.multiplicity->printMultiplicity();
Expand Down Expand Up @@ -234,37 +231,29 @@ function meta::external::format::shared::executionPlan::platformBinding::legendJ

let withFieldsAndGetters = $properties->fold(
{p, c|
if($p.javaType->isJavaList(),
{|
$c ->addField(javaField('private', $p.javaType, $p.fieldName))
->addMethod({cls|
let field = j_this($cls)->j_field($p.fieldName);

javaMethod('public', $p.javaType, $p.getterName, [],
j_return(j_conditional($field->j_eq(j_null()), javaCollections()->j_invoke($p.itemType, 'emptyList', [], $p.javaType), $field))
);
})
->addMethods({cls|
$cls->filter(c| $p.property->in($mappedProperties))->map(c| $c->adderMethods($p, $conventions));
});
},
{|
$c ->addField(javaField('private', $p.javaType->toUnboxed(), $p.fieldName))
->addField(javaField('private', javaInt(), $p.sizeFieldName, j_int(0)))
->addMethod({cls|
let field = j_this($cls)->j_field($p.fieldName);
let fieldSize = j_this($cls)->j_field($p.sizeFieldName);

javaMethod('public', $p.javaType, $p.getterName, [],
j_return(j_conditional($fieldSize->j_eq(j_int(0)), j_null(), $field))
);
})
->addMethods({cls|
$cls->filter(c| $p.property->in($mappedProperties))->map(c| $c->adderMethods($p, $conventions));
});

}
let javaType = if($p.javaType->isJavaList(),
|$p.javaType,
|$p.javaType->toUnboxed()
);

let nullValue = if($p.javaType->isJavaList(),
|javaCollections()->j_invoke('emptyList', [], $p.javaType),
|j_null()
);

$c ->addField(javaField('private', $javaType, $p.fieldName))
->addField(javaField('private', javaInt(), $p.sizeFieldName, j_int(0)))
->addMethod({cls|
let field = j_this($cls)->j_field($p.fieldName);
let fieldSize = j_this($cls)->j_field($p.sizeFieldName);

javaMethod('public', $p.javaType, $p.getterName, [],
j_return(j_conditional($fieldSize->j_eq(j_int(0)), $nullValue, $field))
);
})
->addMethods({cls|
$cls->filter(c| $p.property->in($mappedProperties))->map(c| $c->adderMethods($p, $conventions));
});
},
$baseClass
);
Expand Down Expand Up @@ -396,19 +385,19 @@ function <<access.private>> meta::external::format::shared::executionPlan::platf
));


let fieldSize = j_this($jc)->j_field($prop.sizeFieldName);
let impl = $adderValue.create->concatenate(
if($prop.javaType->isJavaList(),
{|
[
j_if($field->j_eq(j_null()),
$field->j_assign(javaArrayList($prop.itemType)->j_new([]))
),
$field->j_invoke('add', $adderValue.access)
]
$field->j_invoke('add', $adderValue.access),
$fieldSize->j_inc()
];
},
{|
let fieldSize = j_this($jc)->j_field($prop.sizeFieldName);

[
j_if($fieldSize->j_eq(j_int(0)),
$field->j_assign($adderValue.access)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function <<access.private>> meta::external::format::xml::executionPlan::platform
{|
let addToType = javaParameterizedType($conventions->className(_AddEnumToObject), [$dataClass, $itemType]);
let simpleTypeHandler = $ctxParam->j_invoke('stringSimpleTypeHandler', [], javaParameterizedType($conventions->className(_SimpleTypeHandler), javaString()));
j_new($addToType, [$getAdder, $simpleTypeHandler, $itemType->j_field('class')]);
j_new($addToType, [$getAdder, $simpleTypeHandler, $itemType->j_field('class'), j_string($propType->elementToPath())]);
},
|
if($itemType == javaBoolean(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void testAttributes()
String expected = ">>>test::gen::LinkId\n" +
"Class {meta::pure::profiles::doc.doc = 'The data type used for link identifiers.'} test::gen::LinkId\n" +
"[\n" +
" c1_length: $this.value->length() <= 255\n" +
" c1_length_LinkId: $this.value->length() <= 255\n" +
"]\n" +
"{\n" +
" id: String[0..1];\n" +
Expand Down Expand Up @@ -208,20 +208,20 @@ public void testValidations()
String expected = ">>>test::gen::ShowValidations\n" +
"Class test::gen::ShowValidations\n" +
"[\n" +
" c1_length: $this.fixedLengthString->forAll(x: String[1]|$x->length() == 12),\n" +
" c2_length: $this.minLengthString->length() >= 5,\n" +
" c3_length: $this.maxLengthString->forAll(x: String[1]|$x->length() <= 20),\n" +
" c4_length: $this.rangeLengthString->forAll(x: String[1]|$x->length() >= 5),\n" +
" c5_length: $this.rangeLengthString->forAll(x: String[1]|$x->length() <= 20),\n" +
" c6_range: $this.minValInteger >= 1,\n" +
" c7_range: $this.minValFloat > 2.4,\n" +
" c8_range: $this.maxValInteger <= 100,\n" +
" c9_range: $this.maxValFloat < 10.12,\n" +
" c10_range: $this.rangeValInteger > 10,\n" +
" c11_range: $this.rangeValInteger <= 100,\n" +
" c12_range: $this.rangeValFloat > 2.7,\n" +
" c13_range: $this.rangeValFloat < 10.99,\n" +
" c14_values: $this.fixedValuesString->in(['AUD', 'USD'])\n" +
" c1_length_ShowValidations: $this.fixedLengthString->forAll(x: String[1]|$x->length() == 12),\n" +
" c2_length_ShowValidations: $this.minLengthString->length() >= 5,\n" +
" c3_length_ShowValidations: $this.maxLengthString->forAll(x: String[1]|$x->length() <= 20),\n" +
" c4_length_ShowValidations: $this.rangeLengthString->forAll(x: String[1]|$x->length() >= 5),\n" +
" c5_length_ShowValidations: $this.rangeLengthString->forAll(x: String[1]|$x->length() <= 20),\n" +
" c6_range_ShowValidations: $this.minValInteger >= 1,\n" +
" c7_range_ShowValidations: $this.minValFloat > 2.4,\n" +
" c8_range_ShowValidations: $this.maxValInteger <= 100,\n" +
" c9_range_ShowValidations: $this.maxValFloat < 10.12,\n" +
" c10_range_ShowValidations: $this.rangeValInteger > 10,\n" +
" c11_range_ShowValidations: $this.rangeValInteger <= 100,\n" +
" c12_range_ShowValidations: $this.rangeValFloat > 2.7,\n" +
" c13_range_ShowValidations: $this.rangeValFloat < 10.99,\n" +
" c14_values_ShowValidations: $this.fixedValuesString->in(['AUD', 'USD'])\n" +
"]\n" +
"{\n" +
" fixedLengthString: String[*];\n" +
Expand Down Expand Up @@ -260,7 +260,7 @@ public void testChoice()
String expected = ">>>test::gen::Choice\n" +
"Class test::gen::Choice\n" +
"[\n" +
" c1_choice: ($this.optionOne->isNotEmpty() && $this.optionTwo->isEmpty()) || ($this.optionOne->isEmpty() && $this.optionTwo->isNotEmpty())\n" +
" c1_choice_Choice: ($this.optionOne->isNotEmpty() && $this.optionTwo->isEmpty()) || ($this.optionOne->isEmpty() && $this.optionTwo->isNotEmpty())\n" +
"]\n" +
"{\n" +
" optionOne: Integer[0..1];\n" +
Expand Down Expand Up @@ -298,7 +298,7 @@ public void testChoiceWithCommonProperty()
String expected = ">>>test::gen::Account\n" +
"Class test::gen::Account\n" +
"[\n" +
" c1_choice: $this.beneficiaryId->isNotEmpty() || ($this.beneficiaryId->isEmpty() && $this.servicerId->isNotEmpty())\n" +
" c1_choice_Account: $this.beneficiaryId->isNotEmpty() || ($this.beneficiaryId->isEmpty() && $this.servicerId->isNotEmpty())\n" +
"]\n" +
"{\n" +
" accountId: String[1];\n" +
Expand Down Expand Up @@ -454,8 +454,8 @@ public void testAttributeWithInlineType()
String expected = ">>>test::gen::AttributeWithInlineType\n" +
"Class test::gen::AttributeWithInlineType\n" +
"[\n" +
" c1_values: $this.fullOrDelta->forAll(x: String[1]|$x->in(['FULL', 'DELTA'])),\n" +
" c2_length: $this.notTooLongString->forAll(x: String[1]|$x->length() <= 500)\n" +
" c1_values_AttributeWithInlineType: $this.fullOrDelta->forAll(x: String[1]|$x->in(['FULL', 'DELTA'])),\n" +
" c2_length_AttributeWithInlineType: $this.notTooLongString->forAll(x: String[1]|$x->length() <= 500)\n" +
"]\n" +
"{\n" +
" fullOrDelta: String[0..1];\n" +
Expand Down Expand Up @@ -709,7 +709,7 @@ public void testExtendComplexTypeWithComplexTypePartiallyResolved()
">>>test::gen::ExceptionType\n" +
"Class test::gen::ExceptionType\n" +
"[\n" +
" c1_values: $this.type->in(['INFO', 'WARN', 'ERROR'])\n" +
" c1_values_ExceptionType: $this.type->in(['INFO', 'WARN', 'ERROR'])\n" +
"]\n" +
"{\n" +
" type: String[1];\n" +
Expand Down Expand Up @@ -847,8 +847,8 @@ public void testInheritedLengthRestriction()
String expected = ">>>test::gen::AccountId\n" +
"Class test::gen::AccountId\n" +
"[\n" +
" c1_length: $this.value->length() >= 1,\n" +
" c2_length: $this.value->length() <= 255\n" +
" c1_length_AccountId: $this.value->length() >= 1,\n" +
" c2_length_AccountId: $this.value->length() <= 255\n" +
"]\n" +
"{\n" +
" value: String[1];\n" +
Expand Down Expand Up @@ -969,6 +969,71 @@ public void testNgm()
assertModelTexts(modelTextsFromResource("ngm-sample/exchangeGenResult.txt"), modelTextsFromContextData(exchModel));
}

@Test
public void testContraintName()
{
String schemaCode = newExternalSchemaSetGrammarBuilder("test::simpleWithConstraint", "XSD")
.withSchemaText(null, "simpleWithConstraint.xsd", "<?xml version='1.0'?>\n" +
"<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n" +
" <xsd:simpleType name=\"NonNegativeDecimal\">\n" +
" <xsd:annotation>\n" +
" <xsd:documentation xml:lang=\"en\">\n" +
" A type defining a number specified as non negative decimal greater than 0 inclusive.\n" +
" </xsd:documentation>\n" +
" </xsd:annotation>\n" +
" <xsd:restriction base=\"xsd:decimal\">\n" +
" <xsd:minInclusive value=\"0\" />\n" +
" </xsd:restriction>\n" +
" </xsd:simpleType>\n" +
" <xsd:complexType name=\"Shape\">\n" +
" <xsd:sequence>\n" +
" <xsd:element name=\"area\" type=\"NonNegativeDecimal\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n" +
" </xsd:sequence>\n" +
" </xsd:complexType>\n" +
" <xsd:complexType name=\"Rectangle\">\n" +
" <xsd:complexContent>\n" +
" <xsd:extension base=\"Shape\">\n" +
" <xsd:sequence>\n" +
" <xsd:element name=\"height\" type=\"NonNegativeDecimal\" minOccurs=\"1\" maxOccurs=\"unbounded\">\n" +
" <xsd:annotation>\n" +
" <xsd:documentation xml:lang=\"en\">\n" +
" One of two dimensions of a rectangle\n" +
" </xsd:documentation>\n" +
" </xsd:annotation>\n" +
" </xsd:element>\n" +
" <xsd:element name=\"width\" type=\"NonNegativeDecimal\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>\n" +
" </xsd:sequence>\n" +
" </xsd:extension>\n" +
" </xsd:complexContent>\n" +
" </xsd:complexType>\n" +
"</xsd:schema>\n")
.build();

PureModelContextData model = generateModel(schemaCode, config("test::simpleWithConstraint", "test::gen", true, true));

String expected = ">>>test::gen::Rectangle\n" +
"Class test::gen::Rectangle extends test::gen::Shape\n" +
"[\n" +
" c1_range_Rectangle: $this.height->forAll(x: Decimal[1]|$x >= 0),\n" +
" c2_range_Rectangle: $this.width->forAll(x: Decimal[1]|$x >= 0)\n" +
"]\n" +
"{\n" +
" {meta::pure::profiles::doc.doc = 'One of two dimensions of a rectangle'} height: Decimal[1..*];\n" +
" width: Decimal[*];\n" +
"}\n" +
"\n" +
">>>test::gen::Shape\n" +
"Class test::gen::Shape\n" +
"[\n" +
" c1_range_Shape: $this.area->forAll(x: Decimal[1]|$x >= 0)\n" +
"]\n" +
"{\n" +
" area: Decimal[*];\n" +
"}";

Assert.assertEquals(modelTextsFromString(expected), modelTextsFromContextData(model));
}

private XsdToModelConfiguration config(String sourceSchemaSet, String targetPackage)
{
return config(sourceSchemaSet, targetPackage, false, false);
Expand Down
Loading

0 comments on commit 4bb63ec

Please sign in to comment.