This document explains how to update the C++ and Java version of the MeasureUnit class with new CLDR data.
Code is generated by running MeasureUnitTest.java unit tests, which writes generated code to System.out. Two ways to access this:
Within eclipse:
With ant:
ant checkTest -Dtestclass='com.ibm.icu.dev.test.format.MeasureUnitTest'
out/junit-results/checkTest/html/index.html
After syncing CLDR data with ICU do the following. This documentation assumes that you are updating the MeasureUnit clases for ICU 68.
Check out $GIT_ROOT/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/MeasureUnitTest.java
Open MeasureUnitTest.java.
Find the testZZZ
test, its code should all be commented out. This test will execute last and will run the desired code.
Make sure DRAFT_VERSIONS at top of MeasureUnitTest.java is set correctly. These are the ICU versions that have draft methods.
Change testZZZ
to run generateConstants(“68”); // ICU 68.
Run MeasureUnitTest.java, copy the generated code (see instructions above).
Open MeasureUnit.java: $GIT_ROOT/icu4j/main/classes/core/src/com/ibm/icu/util/MeasureUnit.java
Look for line containing:
// Start generated MeasureUnit constants
Look for line containing:
// End generated MeasureUnit constants
Replace all the generated code in between with the contents of the clipboard
Run the MeasureUnitTest.java to ensure that the new code is backward compatible. These compatibility tests are called something like TestCompatible65
, which tests backward compatibility with ICU 65.
Create a compatibility test for ICU 68. Change testZZZ
to run generateBackwardCompatibilityTest(“68”)
Run tests.
Copy generated test (see instructions above) into MeasureUnitTest.java
Run tests again to ensure that new code is backward compatible with itself
Change testZZZ to run generateCXXHConstants(“68”); // ICU 68
.
Run MeasureUnitTest.java, copy the generated code (see instructions above).
Open $GIT_ROOT/icu4c/source/i18n/unicode/measunit.h. Look for line containing:
// Start generated createXXX methods
Look for line:
// End generated createXXX methods
Replace all the generated code in between with the contents of the clipboard
Change testZZZ to run generateCXXConstants();
Run MeasureUnitTest.java, copy the generated code (see instructions above).
Open $GIT_ROOT/icu4c/source/i18n/measunit.cpp. Look for line containing:
// Start generated code for measunit.cpp
Look for lines
// End generated code for measunit.cpp
Replace all the generated code in between with the contents of the clipboard
./intltest format/MeasureFormatTest
from test/intltest
to ensure new code is backward compatible.testZZZ
in eclipse to run generateCXXBackwardCompatibilityTest(“68”)
TestCompatible65()
These last changes are necessary to permanently record the ICU version number of any new measure units. Without these changes any new functions for this release will be considered new for the next release too.
Change testZZZ
to run updateJAVAVersions(“68”);
Run MeasureUnitTest.java, copy the generated code (see instructions above).
Append the clipboard contents to the values of the JAVA_VERSIONS variable near the top of MeasureUnitTest.java.
Important: what you are copying are just the new functions for the current ICU version, in this case 68. Therefore append, do not replace.
The standard ldml2icu process is used to update ICU‘s resource files (see cldr-icu-readme.txt). CLDR’s units.xml defines conversion rates in terms of some constants defined in unitConstants
.
For efficiency and simplicity, ICU does not read unitConstants
from the resource file. If any new constants are added, some code changes would be needed. This would be caught by testUnitConstantFreshness
unit test in units_test.cpp
.
They are hard-coded:
UnitConverter.java
has the constant names in UnitConverter.Factor.addEntity()
and constant values in UnitConverter.Factor.getConversionRate()
.units_converter.cpp
has the constant names in addSingleFactorConstant()
, with the constant values in double constantsValues[]
in the units_converter.h
header file.