Skip to content

Commit

Permalink
changed: numeric rounding for ili10 and ili23 (claeis/ilivalidator#111)
Browse files Browse the repository at this point in the history
- added separate test class for numeric
- added test class to test the numericRounding methode
  • Loading branch information
renato1987 committed Jun 5, 2018
1 parent b9d2087 commit b05c05c
Show file tree
Hide file tree
Showing 6 changed files with 1,033 additions and 235 deletions.
49 changes: 32 additions & 17 deletions src/main/java/ch/interlis/iox_j/validator/Validator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package ch.interlis.iox_j.validator;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -3215,25 +3219,36 @@ private void validateNumericType(String validateType, NumericType type, String v
if(value!=null){
PrecisionDecimal minimum=((NumericType) type).getMinimum();
PrecisionDecimal maximum=((NumericType) type).getMaximum();
BigDecimal rounded = new BigDecimal(
value.toString()).setScale(
value.getExponent(),
BigDecimal.ROUND_HALF_UP);
BigDecimal min_general = new BigDecimal(
minimum.toString()).setScale(
minimum.getExponent(),
BigDecimal.ROUND_HALF_UP);
BigDecimal max_general = new BigDecimal(
maximum.toString()).setScale(
maximum.getExponent(),
BigDecimal.ROUND_HALF_UP);
if (rounded.compareTo (min_general) == -1
|| rounded.compareTo (max_general) == +1){
logMsg(validateType,"value {0} is out of range", valueStr);
}
BigDecimal min_general = new BigDecimal(minimum.toString());
BigDecimal max_general = new BigDecimal(maximum.toString());
BigDecimal valueBigDec = new BigDecimal(value.toString());
int precision= minimum.getAccuracy();
BigDecimal rounded=roundNumeric(precision,valueStr);
if (rounded!=null && (rounded.compareTo(min_general)==-1 || rounded.compareTo(max_general)==+1)){
logMsg(validateType,"value {0} is out of range", rounded.toString());
}
}
}


public static BigDecimal roundNumeric(int precision, String valueStr) {
if(valueStr==null) {
return null;
}
double valueDouble=Double.valueOf(valueStr);
boolean isNegative=valueDouble<0;

BigDecimal value=new BigDecimal(valueStr);
BigDecimal rounded=null;
if(value!=null) {
if(isNegative){
rounded=value.setScale(precision, BigDecimal.ROUND_HALF_DOWN);
}else {
rounded=value.setScale(precision, BigDecimal.ROUND_HALF_UP);
}
}
return rounded;
}

public boolean isValidUuid(String valueStr) {
return valueStr.length() == 36 && valueStr.matches("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}?");
}
Expand Down
8 changes: 6 additions & 2 deletions src/test/data/validator/Datatypes23.ili
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ MODEL Datatypes23
aBoolean : BOOLEAN;

!! NumericType
numericInt : 0 .. 10;
numericDec : 0.0 .. 10.0;
numericInt : -10 .. 10;
numericInt2 : -9 .. 9;
numericInt3 : -199999 .. 199999;
numericDec : -10.0 .. 10.0;
numericDec2 : -0.001 .. 0.001;
numericDec3 : -1.999999999999 .. 1.999999999999;
numericNum : numerictype;
!!numCountClockWise : numericCounterClockwise;
!!numCountClock : numericCounterClock;
Expand Down
102 changes: 83 additions & 19 deletions src/test/java/ch/interlis/iox_j/validator/Datatypes10Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public void text2ZeichensatzContentOk(){
@Test
public void bereichMaximumOk(){
Iom_jObject objTest=new Iom_jObject("Datatypes10.Topic.Table", "o1");
objTest.setattrvalue("bereich", "10.4");
objTest.setattrvalue("bereich", "9.94");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Expand All @@ -210,13 +210,14 @@ public void bereichMaximumOk(){
assertTrue(logger.getErrs().size()==0);
}

// Dezimal 0.0 wird gerundet auf (0).
// -0.4 gerundet = 0. valid.
// -0.5 gerundet = -1. unvalid.
// Dezimal -0.05 wird gerundet auf (0).
// -0.04 gerundet = 0. valid.
// -0.05 gerundet = 0. valid.
// -0.06 gerundet = -1. unvalid.
@Test
public void bereichMinimumOk(){
Iom_jObject objTest=new Iom_jObject("Datatypes10.Topic.Table", "o1");
objTest.setattrvalue("bereich", "-0.4");
objTest.setattrvalue("bereich", "-0.05");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Expand All @@ -235,7 +236,7 @@ public void bereichMinimumOk(){
@Test
public void bereich2MaximumOk(){
Iom_jObject objTest=new Iom_jObject("Datatypes10.Topic.Table", "o1");
objTest.setattrvalue("bereich2", "10.499");
objTest.setattrvalue("bereich2", "9.999");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Expand All @@ -254,7 +255,7 @@ public void bereich2MaximumOk(){
@Test
public void bereich2MinimumOk(){
Iom_jObject objTest=new Iom_jObject("Datatypes10.Topic.Table", "o1");
objTest.setattrvalue("bereich2", "-0.499");
objTest.setattrvalue("bereich2", "0.000");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Expand Down Expand Up @@ -330,7 +331,7 @@ public void aufzaehlungVerschachtelungOk(){
@Test
public void laengeMaximumOk(){
Iom_jObject objTest=new Iom_jObject("Datatypes10.Topic.Table", "o1");
objTest.setattrvalue("laenge", "10.4");
objTest.setattrvalue("laenge", "9.94");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Expand All @@ -349,7 +350,7 @@ public void laengeMaximumOk(){
@Test
public void laengeMinimumOk(){
Iom_jObject objTest=new Iom_jObject("Datatypes10.Topic.Table", "o1");
objTest.setattrvalue("laenge", "0.5");
objTest.setattrvalue("laenge", "0.95");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Expand Down Expand Up @@ -387,7 +388,7 @@ public void laenge2ThirdPositionAfterDotOk(){
@Test
public void flaecheMaximumOk(){
Iom_jObject objTest=new Iom_jObject("Datatypes10.Topic.Table", "o1");
objTest.setattrvalue("flaeche", "10.4");
objTest.setattrvalue("flaeche", "9.94");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Expand All @@ -406,7 +407,7 @@ public void flaecheMaximumOk(){
@Test
public void flaecheMinimumOk(){
Iom_jObject objTest=new Iom_jObject("Datatypes10.Topic.Table", "o1");
objTest.setattrvalue("flaeche", "0.5");
objTest.setattrvalue("flaeche", "0.95");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Expand All @@ -425,7 +426,7 @@ public void flaecheMinimumOk(){
@Test
public void flaeche2ThirdPositionAfterDotOk(){
Iom_jObject objTest=new Iom_jObject("Datatypes10.Topic.Table", "o1");
objTest.setattrvalue("flaeche2", "99.000");
objTest.setattrvalue("flaeche2", "98.999");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Expand All @@ -444,7 +445,7 @@ public void flaeche2ThirdPositionAfterDotOk(){
@Test
public void winkelRadianMaximumOk(){
Iom_jObject objTest=new Iom_jObject("Datatypes10.Topic.Table", "o1");
objTest.setattrvalue("radians", "10.4");
objTest.setattrvalue("radians", "10.0");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Expand All @@ -463,7 +464,7 @@ public void winkelRadianMaximumOk(){
@Test
public void winkelRadianMinimumOk(){
Iom_jObject objTest=new Iom_jObject("Datatypes10.Topic.Table", "o1");
objTest.setattrvalue("radians", "0.5");
objTest.setattrvalue("radians", "1.0");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Expand All @@ -482,7 +483,7 @@ public void winkelRadianMinimumOk(){
@Test
public void winkelGradsMaximumOk(){
Iom_jObject objTest=new Iom_jObject("Datatypes10.Topic.Table", "o1");
objTest.setattrvalue("grads", "10.4");
objTest.setattrvalue("grads", "10.04");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Expand All @@ -501,7 +502,7 @@ public void winkelGradsMaximumOk(){
@Test
public void winkelGradsMinimumOk(){
Iom_jObject objTest=new Iom_jObject("Datatypes10.Topic.Table", "o1");
objTest.setattrvalue("grads", "0.5");
objTest.setattrvalue("grads", "0.95");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Expand All @@ -520,7 +521,7 @@ public void winkelGradsMinimumOk(){
@Test
public void winkelDegreesMaximumOk(){
Iom_jObject objTest=new Iom_jObject("Datatypes10.Topic.Table", "o1");
objTest.setattrvalue("degrees", "10.4");
objTest.setattrvalue("degrees", "10.04");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Expand All @@ -539,7 +540,7 @@ public void winkelDegreesMaximumOk(){
@Test
public void winkelDegreesMinimumOk(){
Iom_jObject objTest=new Iom_jObject("Datatypes10.Topic.Table", "o1");
objTest.setattrvalue("degrees", "0.5");
objTest.setattrvalue("degrees", "0.95");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Expand Down Expand Up @@ -1784,5 +1785,68 @@ public void koord3WrongDimensionsFail(){
assertEquals("Wrong COORD structure, C3 expected", logger.getErrs().get(0).getEventMsg());
}


// eine Fehlermeldung wird erwartet, da 9(4) auf 90 abgerundet werden soll,
// und somit der gueltige Bereich unterschritten wird.
@Test
public void coordType_Rounding_Down_Fail(){
Iom_jObject obj=new Iom_jObject("Datatypes10.Topic.Table", "o1");
IomObject coordValue=obj.addattrobj("koord2", "COORD");
coordValue.setattrvalue("C1", "5.55");
coordValue.setattrvalue("C2", "99.94");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Settings settings=new Settings();
Validator validator=new Validator(td, modelConfig,logger,errFactory,settings);
validator.validate(new StartTransferEvent());
validator.validate(new StartBasketEvent("Datatypes10.Topic","b1"));
validator.validate(new ObjectEvent(obj));
validator.validate(new EndBasketEvent());
validator.validate(new EndTransferEvent());
// Asserts
assertTrue(logger.getErrs().size()==1);
assertEquals("value 99.9 is out of range", logger.getErrs().get(0).getEventMsg());
}

// prueft, ob 9(5) erfolgreich auf 100 aufgerundet wird.
@Test
public void coordType_Rounding_UpFrom5_Ok(){
Iom_jObject obj=new Iom_jObject("Datatypes10.Topic.Table", "o1");
IomObject coordValue=obj.addattrobj("koord2", "COORD");
coordValue.setattrvalue("C1", "5.55");
coordValue.setattrvalue("C2", "99.95");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Settings settings=new Settings();
Validator validator=new Validator(td, modelConfig,logger,errFactory,settings);
validator.validate(new StartTransferEvent());
validator.validate(new StartBasketEvent("Datatypes10.Topic","b1"));
validator.validate(new ObjectEvent(obj));
validator.validate(new EndBasketEvent());
validator.validate(new EndTransferEvent());
// Asserts
assertTrue(logger.getErrs().size()==0);
}

// prueft, ob 9(6) erfolgreich auf 100 aufgerundet wird.
@Test
public void coordType_Rounding_Up_Ok(){
Iom_jObject obj=new Iom_jObject("Datatypes10.Topic.Table", "o1");
IomObject coordValue=obj.addattrobj("koord2", "COORD");
coordValue.setattrvalue("C1", "5.55");
coordValue.setattrvalue("C2", "99.96");
ValidationConfig modelConfig=new ValidationConfig();
LogCollector logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Settings settings=new Settings();
Validator validator=new Validator(td, modelConfig,logger,errFactory,settings);
validator.validate(new StartTransferEvent());
validator.validate(new StartBasketEvent("Datatypes10.Topic","b1"));
validator.validate(new ObjectEvent(obj));
validator.validate(new EndBasketEvent());
validator.validate(new EndTransferEvent());
// Asserts
assertTrue(logger.getErrs().size()==0);
}
}
Loading

0 comments on commit b05c05c

Please sign in to comment.