/projects/OG-Util/tests/unit/com/opengamma/util/i18n/CountryTest.java

https://github.com/gsteri1/OG-Platform · Java · 251 lines · 183 code · 36 blank · 32 comment · 1 complexity · 3ed5780ec8328f0879bb80903f9a19ac MD5 · raw file

  1. /**
  2. * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
  3. *
  4. * Please see distribution for license.
  5. */
  6. package com.opengamma.util.i18n;
  7. import static org.testng.AssertJUnit.assertEquals;
  8. import static org.testng.AssertJUnit.assertSame;
  9. import static org.testng.AssertJUnit.assertTrue;
  10. import java.io.ByteArrayInputStream;
  11. import java.io.ByteArrayOutputStream;
  12. import java.io.ObjectInputStream;
  13. import java.io.ObjectOutputStream;
  14. import org.testng.annotations.Test;
  15. import com.opengamma.id.ObjectId;
  16. import com.opengamma.id.UniqueId;
  17. /**
  18. * Test Country.
  19. */
  20. @Test
  21. public class CountryTest {
  22. //-----------------------------------------------------------------------
  23. // constants
  24. //-----------------------------------------------------------------------
  25. public void test_constants() {
  26. assertEquals(Country.EU, Country.of("EU"));
  27. assertEquals(Country.BE, Country.of("BE"));
  28. assertEquals(Country.CH, Country.of("CH"));
  29. assertEquals(Country.CZ, Country.of("CZ"));
  30. assertEquals(Country.DE, Country.of("DE"));
  31. assertEquals(Country.DK, Country.of("DK"));
  32. assertEquals(Country.ES, Country.of("ES"));
  33. assertEquals(Country.FI, Country.of("FI"));
  34. assertEquals(Country.FR, Country.of("FR"));
  35. assertEquals(Country.GB, Country.of("GB"));
  36. assertEquals(Country.GR, Country.of("GR"));
  37. assertEquals(Country.HU, Country.of("HU"));
  38. assertEquals(Country.IT, Country.of("IT"));
  39. assertEquals(Country.NL, Country.of("NL"));
  40. assertEquals(Country.NO, Country.of("NO"));
  41. assertEquals(Country.PT, Country.of("PT"));
  42. assertEquals(Country.SK, Country.of("SK"));
  43. assertEquals(Country.PL, Country.of("PL"));
  44. assertEquals(Country.RU, Country.of("RU"));
  45. assertEquals(Country.SE, Country.of("SE"));
  46. assertEquals(Country.AR, Country.of("AR"));
  47. assertEquals(Country.BR, Country.of("BR"));
  48. assertEquals(Country.CA, Country.of("CA"));
  49. assertEquals(Country.CL, Country.of("CL"));
  50. assertEquals(Country.MX, Country.of("MX"));
  51. assertEquals(Country.US, Country.of("US"));
  52. assertEquals(Country.AU, Country.of("AU"));
  53. assertEquals(Country.CN, Country.of("CN"));
  54. assertEquals(Country.HK, Country.of("HK"));
  55. assertEquals(Country.IN, Country.of("IN"));
  56. assertEquals(Country.JP, Country.of("JP"));
  57. assertEquals(Country.NZ, Country.of("NZ"));
  58. assertEquals(Country.TH, Country.of("TH"));
  59. }
  60. //-----------------------------------------------------------------------
  61. // of(String)
  62. //-----------------------------------------------------------------------
  63. public void test_of_String() {
  64. Country test = Country.of("SE");
  65. assertSame(Country.SE, test);
  66. assertEquals("SE", test.getCode());
  67. assertSame(Country.of("SE"), test);
  68. }
  69. public void test_of_String_unknownCountryCreated() {
  70. Country test = Country.of("ZY");
  71. assertEquals("ZY", test.getCode());
  72. assertSame(Country.of("ZY"), test);
  73. }
  74. @Test(expectedExceptions = IllegalArgumentException.class)
  75. public void test_of_String_lowerCase() {
  76. try {
  77. Country.of("gb");
  78. } catch (IllegalArgumentException ex) {
  79. assertEquals("Invalid country code: gb", ex.getMessage());
  80. throw ex;
  81. }
  82. }
  83. @Test(expectedExceptions = IllegalArgumentException.class)
  84. public void test_of_String_empty() {
  85. Country.of("");
  86. }
  87. @Test(expectedExceptions = IllegalArgumentException.class)
  88. public void test_of_String_tooShort() {
  89. Country.of("A");
  90. }
  91. @Test(expectedExceptions = IllegalArgumentException.class)
  92. public void test_of_String_tooLong() {
  93. Country.of("ABC");
  94. }
  95. @Test(expectedExceptions = IllegalArgumentException.class)
  96. public void test_of_String_nullString() {
  97. Country.of((String) null);
  98. }
  99. //-----------------------------------------------------------------------
  100. // parse(String)
  101. //-----------------------------------------------------------------------
  102. public void test_parse_String() {
  103. Country test = Country.parse("GB");
  104. assertEquals("GB", test.getCode());
  105. assertSame(Country.GB, test);
  106. }
  107. public void test_parse_String_unknownCountryCreated() {
  108. Country test = Country.parse("ZX");
  109. assertEquals("ZX", test.getCode());
  110. assertSame(Country.of("ZX"), test);
  111. }
  112. public void test_parse_String_lowerCase() {
  113. Country test = Country.parse("gb");
  114. assertEquals("GB", test.getCode());
  115. assertSame(Country.GB, test);
  116. }
  117. @Test(expectedExceptions = IllegalArgumentException.class)
  118. public void test_parse_String_empty() {
  119. Country.parse("");
  120. }
  121. @Test(expectedExceptions = IllegalArgumentException.class)
  122. public void test_parse_String_tooShort() {
  123. Country.parse("A");
  124. }
  125. @Test(expectedExceptions = IllegalArgumentException.class)
  126. public void test_parse_String_tooLong() {
  127. Country.parse("ABC");
  128. }
  129. @Test(expectedExceptions = IllegalArgumentException.class)
  130. public void test_parse_String_nullString() {
  131. Country.parse((String) null);
  132. }
  133. //-----------------------------------------------------------------------
  134. // Serialisation
  135. //-----------------------------------------------------------------------
  136. public void test_serialization_GB() throws Exception {
  137. Country cu = Country.of("GB");
  138. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  139. ObjectOutputStream oos = new ObjectOutputStream(baos);
  140. oos.writeObject(cu);
  141. oos.close();
  142. ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
  143. Country input = (Country) ois.readObject();
  144. assertSame(input, cu);
  145. }
  146. public void test_serialization_AA() throws Exception {
  147. Country cu = Country.of("AA");
  148. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  149. ObjectOutputStream oos = new ObjectOutputStream(baos);
  150. oos.writeObject(cu);
  151. oos.close();
  152. ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
  153. Country input = (Country) ois.readObject();
  154. assertSame(input, cu);
  155. }
  156. //-----------------------------------------------------------------------
  157. // gets
  158. //-----------------------------------------------------------------------
  159. public void test_gets() {
  160. Country test = Country.of("GB");
  161. assertEquals("GB", test.getCode());
  162. assertEquals(ObjectId.of("CountryISO", "GB"), test.getObjectId());
  163. assertEquals(UniqueId.of("CountryISO", "GB"), test.getUniqueId());
  164. }
  165. //-----------------------------------------------------------------------
  166. // compareTo()
  167. //-----------------------------------------------------------------------
  168. public void test_compareTo() {
  169. Country a = Country.FR;
  170. Country b = Country.GB;
  171. Country c = Country.JP;
  172. assertEquals(a.compareTo(a), 0);
  173. assertEquals(b.compareTo(b), 0);
  174. assertEquals(c.compareTo(c), 0);
  175. assertTrue(a.compareTo(b) < 0);
  176. assertTrue(b.compareTo(a) > 0);
  177. assertTrue(a.compareTo(c) < 0);
  178. assertTrue(c.compareTo(a) > 0);
  179. assertTrue(b.compareTo(c) < 0);
  180. assertTrue(c.compareTo(b) > 0);
  181. }
  182. @Test(expectedExceptions = NullPointerException.class)
  183. public void test_compareTo_null() {
  184. Country.AU.compareTo(null);
  185. }
  186. //-----------------------------------------------------------------------
  187. // equals() hashCode()
  188. //-----------------------------------------------------------------------
  189. public void test_equals_hashCode() {
  190. Country a = Country.GB;
  191. Country b = Country.of("GB");
  192. Country c = Country.FR;
  193. assertEquals(a.equals(a), true);
  194. assertEquals(b.equals(b), true);
  195. assertEquals(c.equals(c), true);
  196. assertEquals(a.equals(b), true);
  197. assertEquals(b.equals(a), true);
  198. assertEquals(a.hashCode() == b.hashCode(), true);
  199. assertEquals(a.equals(c), false);
  200. assertEquals(b.equals(c), false);
  201. }
  202. public void test_equals_false() {
  203. Country a = Country.GB;
  204. assertEquals(a.equals(null), false);
  205. assertEquals(a.equals("String"), false);
  206. assertEquals(a.equals(new Object()), false);
  207. }
  208. //-----------------------------------------------------------------------
  209. // toString()
  210. //-----------------------------------------------------------------------
  211. public void test_toString() {
  212. Country test = Country.GB;
  213. assertEquals(test.toString(), "GB");
  214. }
  215. }