use of java.util.Currency in project hibernate-orm by hibernate.
the class MonetaryAmountUserType method nullSafeGet.
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
BigDecimal amt = StandardBasicTypes.BIG_DECIMAL.nullSafeGet(rs, names[0], session);
Currency cur = StandardBasicTypes.CURRENCY.nullSafeGet(rs, names[1], session);
if (amt == null)
return null;
return new MonetaryAmount(amt, cur);
}
use of java.util.Currency in project jdk8u_jdk by JetBrains.
the class CurrencyTest method testLocaleMapping.
static void testLocaleMapping() {
// very basic test: most countries have their own currency, and then
// their currency code is an extension of their country code.
Locale[] locales = Locale.getAvailableLocales();
int goodCountries = 0;
int ownCurrencies = 0;
for (int i = 0; i < locales.length; i++) {
Locale locale = locales[i];
if (locale.getCountry().length() == 0) {
boolean gotException = false;
try {
Currency.getInstance(locale);
} catch (IllegalArgumentException e) {
gotException = true;
}
if (!gotException) {
throw new RuntimeException("didn't get specified exception");
}
} else {
goodCountries++;
Currency currency = Currency.getInstance(locale);
if (currency.getCurrencyCode().indexOf(locale.getCountry()) == 0) {
ownCurrencies++;
}
}
}
System.out.println("Countries tested: " + goodCountries + ", own currencies: " + ownCurrencies);
if (ownCurrencies < (goodCountries / 2 + 1)) {
throw new RuntimeException("suspicious: not enough countries have their own currency.");
}
// check a few countries that don't change their currencies too often
String[] country1 = { "US", "CA", "JP", "CN", "SG", "CH" };
String[] currency1 = { "USD", "CAD", "JPY", "CNY", "SGD", "CHF" };
for (int i = 0; i < country1.length; i++) {
checkCountryCurrency(country1[i], currency1[i]);
}
/*
* check currency changes
* In current implementation, there is no data of old currency and transition date at jdk/src/share/classes/java/util/CurrencyData.properties.
* So, all the switch data arrays are empty. In the future, if data of old currency and transition date are necessary for any country, the
* arrays here can be updated so that the program can check the currency switch.
*/
String[] switchOverCtry = {};
String[] switchOverOld = {};
String[] switchOverNew = {};
String[] switchOverTZ = {};
int[] switchOverYear = {};
int[] switchOverMonth = {};
int[] switchOverDay = {};
for (int i = 0; i < switchOverCtry.length; i++) {
TimeZone.setDefault(TimeZone.getTimeZone(switchOverTZ[i]));
Calendar date = new GregorianCalendar(switchOverYear[i], switchOverMonth[i], switchOverDay[i]);
long switchOver = date.getTime().getTime();
boolean switchedOver = System.currentTimeMillis() >= switchOver;
checkCountryCurrency(switchOverCtry[i], switchedOver ? switchOverNew[i] : switchOverOld[i]);
}
// check a country code which doesn't have a currency
checkCountryCurrency("AQ", null);
// check an invalid country code
boolean gotException = false;
try {
Currency.getInstance(new Locale("", "EU"));
} catch (IllegalArgumentException e) {
gotException = true;
}
if (!gotException) {
throw new RuntimeException("didn't get specified exception.");
}
}
use of java.util.Currency in project jdk8u_jdk by JetBrains.
the class CurrencyTest method testSerialization.
static void testSerialization() throws Exception {
Currency currency1 = Currency.getInstance("DEM");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oStream = new ObjectOutputStream(baos);
oStream.writeObject(currency1);
oStream.flush();
byte[] bytes = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream iStream = new ObjectInputStream(bais);
Currency currency2 = (Currency) iStream.readObject();
if (currency1 != currency2) {
throw new RuntimeException("serialization breaks class invariant");
}
}
use of java.util.Currency in project jdk8u_jdk by JetBrains.
the class Bug4512215 method testCountryCurrency.
private static void testCountryCurrency(String country, String currencyCode, int digits) {
testCurrencyDefined(currencyCode, digits);
Currency currency = Currency.getInstance(new Locale("", country));
if (!currency.getCurrencyCode().equals(currencyCode)) {
throw new RuntimeException("[" + country + "] expected: " + currencyCode + "; got: " + currency.getCurrencyCode());
}
}
use of java.util.Currency in project jdk8u_jdk by JetBrains.
the class CurrencyTest method checkCountryCurrency.
static void checkCountryCurrency(String countryCode, String expected) {
Locale locale = new Locale("", countryCode);
Currency currency = Currency.getInstance(locale);
String code = (currency != null) ? currency.getCurrencyCode() : null;
if (!(expected == null ? code == null : expected.equals(code))) {
throw new RuntimeException("Wrong currency for " + locale.getDisplayCountry() + ": expected " + expected + ", got " + code);
}
}
Aggregations