openhab / openhab1-addons

Add-ons for openHAB 1.x
Eclipse Public License 2.0
3.43k stars 1.7k forks source link

[KNX.Test] Test failed for system with default Europe/Warsaw timezone #5422

Open slawekjaranowski opened 6 years ago

slawekjaranowski commented 6 years ago

Expected Behavior

Pass tests during build on system with default Europe/Warsaw timezone.

Current Behavior

Failed tests: 
  KNXCoreTypeMapperTest.testTypeMappingDateTime_19_001:1655->testToDPTValue:2079 KNXCoreTypeMapper.toDPTValue() test failed for datapoint type "19.001" expected:<1900-01-01 01:02:03> but was:<null>

Possible Solution

On system with default Europe/Warsaw timezone we can run:

mvn clean install -Dtycho.testArgLine="-Duser.timezone=UTC"

Steps to Reproduce (for bugs)

Build project on system with default Europe/Warsaw timezone or run:

mvn clean install -Dtycho.testArgLine="-Duser.timezone=Europe/Warsaw"

9037568 commented 6 years ago

There's a chance this is a bug in Java itself. Long story...

After some rather painful debugging, I find that the testToType() method is returning null here because knxCoreTypeMapper.toType() is returning null here.

Backtracing those leads to this call to translatorDateTime.getValueMilliseconds(), which is throwing a KNXFormatException:

tuwien.auto.calimero.exception.KNXFormatException: invalid calendar value: MINUTE: 2 -> 26                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
        at tuwien.auto.calimero.dptxlator.DPTXlatorDateTime.fromDPTMilliseconds(DPTXlatorDateTime.java:738)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
        at tuwien.auto.calimero.dptxlator.DPTXlatorDateTime.getValueMilliseconds(DPTXlatorDateTime.java:446)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
        at org.openhab.binding.knx.internal.dpt.KNXCoreTypeMapper.toType(KNXCoreTypeMapper.java:477)

I wrote a simple little Java program to test the date conversion that the binding is wanting to do:

        Calendar cal = Calendar.getInstance();
        cal.set(1900, 0, 1, 1, 2, 3);

        String formatted = String.format(Locale.US, "%1$ta, %1$tT", cal);
        System.out.println("Formatted: " + formatted);

In the default timezone here, this results in the output Formatted: Mon, 01:02:03. In the Europe/Warsaw timezone, this results in the output Formatted: Mon, 01:26:03.

There's a chance we're doing something wrong in setting up the calendar object, but I haven't been able to spot anything so far.

9037568 commented 6 years ago

After a bit more review, I now believe this is a bug in the binding after all. There are a number of places where the code assumes the system's locale is Locale.US, when presumably it should be using the system's default locale instead.