/test/com/facebook/buck/android/StringResourcesTest.java

https://gitlab.com/smartether/buck · Java · 303 lines · 220 code · 58 blank · 25 comment · 0 complexity · 818ab25a8098799db4446f463629968f MD5 · raw file

  1. /*
  2. * Copyright 2013-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. * not use this file except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations
  14. * under the License.
  15. */
  16. package com.facebook.buck.android;
  17. import com.facebook.buck.android.StringResources.Gender;
  18. import static org.junit.Assert.assertEquals;
  19. import com.google.common.collect.ImmutableList;
  20. import com.google.common.collect.ImmutableMap;
  21. import com.google.common.collect.Maps;
  22. import org.junit.Test;
  23. import java.io.ByteArrayInputStream;
  24. import java.io.DataInputStream;
  25. import java.io.IOException;
  26. import java.util.EnumMap;
  27. import java.util.TreeMap;
  28. public class StringResourcesTest {
  29. private static final EnumMap<Gender, String> genderStrings1 = Maps.newEnumMap(Gender.class);
  30. private static final EnumMap<Gender, String> genderStrings2 = Maps.newEnumMap(Gender.class);
  31. private static final EnumMap<Gender, String> genderStrings3 = Maps.newEnumMap(Gender.class);
  32. private static final EnumMap<Gender, ImmutableMap<String, String>> genderPluralMap1 =
  33. Maps.newEnumMap(Gender.class);
  34. private static final EnumMap<Gender, ImmutableMap<String, String>> genderPluralMap2 =
  35. Maps.newEnumMap(Gender.class);
  36. private static final EnumMap<Gender, ImmutableList<String>> genderArray1 =
  37. Maps.newEnumMap(Gender.class);
  38. private static final EnumMap<Gender, ImmutableList<String>> genderArray2 =
  39. Maps.newEnumMap(Gender.class);
  40. static {
  41. genderStrings1.put(Gender.unknown, "S_one");
  42. genderStrings1.put(Gender.female, "S_one_female");
  43. genderStrings2.put(Gender.unknown, "S_two");
  44. genderStrings3.put(Gender.unknown, "S_ne");
  45. genderStrings3.put(Gender.female, "\\\"S_three\\\"");
  46. // populate first gender plural map
  47. genderPluralMap1.put(
  48. Gender.unknown, ImmutableMap.of(
  49. "one", "P1_one",
  50. "few", "P1_few",
  51. "many", "P1_many"));
  52. genderPluralMap1.put(
  53. Gender.female, ImmutableMap.of(
  54. "one", "P1_one_f1",
  55. "few", "P1_few_f1",
  56. "many", "P1_many_f1"));
  57. genderPluralMap1.put(
  58. Gender.male, ImmutableMap.of(
  59. "one", "P1_one_m2",
  60. "few", "P1_few_m2",
  61. "many", "P1_many_m2"));
  62. // populate second gender plural map
  63. genderPluralMap2.put(
  64. Gender.unknown, ImmutableMap.of(
  65. "zero", "P2_zero",
  66. "two", "P2_two",
  67. "other", "P2_other"));
  68. genderPluralMap2.put(
  69. Gender.female, ImmutableMap.of(
  70. "zero", "P2_zero_f1",
  71. "two", "P2_two_f1",
  72. "other", "P2_other_f1"));
  73. // populate gender arrays
  74. genderArray1.put(Gender.unknown, ImmutableList.of("A1_one", "A1_two"));
  75. genderArray1.put(Gender.female, ImmutableList.of("A1_one_f1", "A1_two_f1"));
  76. genderArray1.put(Gender.male, ImmutableList.of("A1_one_m2", "A1_two_m2"));
  77. genderArray2.put(Gender.unknown, ImmutableList.of("A2_one"));
  78. genderArray2.put(Gender.male, ImmutableList.of("A2_one_m2"));
  79. }
  80. private static final ImmutableMap<Integer, EnumMap<Gender, String>> strings = ImmutableMap.of(
  81. 12345678, genderStrings1,
  82. 12345679, genderStrings2,
  83. 12345680, genderStrings3);
  84. private static final ImmutableMap<Integer, EnumMap<Gender, ImmutableMap<String, String>>>
  85. plurals = ImmutableMap.of(
  86. 12345689, genderPluralMap1,
  87. 12345692, genderPluralMap2);
  88. private static final ImmutableMap<Integer, EnumMap<Gender, ImmutableList<String>>> arrays =
  89. ImmutableMap.of(
  90. 12345694, genderArray1,
  91. 12345699, genderArray2);
  92. @Test
  93. public void testBinaryStream() throws IOException {
  94. TreeMap<Integer, EnumMap<Gender, String>> stringsMap = Maps.newTreeMap();
  95. stringsMap.putAll(strings);
  96. TreeMap<Integer, EnumMap<Gender, ImmutableMap<String, String>>> pluralsMap = Maps.newTreeMap();
  97. pluralsMap.putAll(plurals);
  98. TreeMap<Integer, EnumMap<Gender, ImmutableList<String>>> arraysMap = Maps.newTreeMap();
  99. arraysMap.putAll(arrays);
  100. byte[] binaryOutput = new StringResources(stringsMap, pluralsMap, arraysMap)
  101. .getBinaryFileContent();
  102. verifyBinaryStream(binaryOutput);
  103. }
  104. @Test
  105. public void testUnescapesQuotesAndApostrophes() {
  106. assertEquals("Test",
  107. new String(StringResources.getUnescapedStringBytes("Test")));
  108. assertEquals("\"testing\"",
  109. new String(StringResources.getUnescapedStringBytes("\\\"testing\\\"")));
  110. assertEquals("On a friend's timeline",
  111. new String(StringResources.getUnescapedStringBytes("On a friend\\'s timeline")));
  112. }
  113. private void verifyBinaryStream(byte[] binaryOutput) throws IOException {
  114. DataInputStream stream = new DataInputStream(new ByteArrayInputStream(binaryOutput));
  115. // Version
  116. assertEquals(2, stream.readInt());
  117. // Strings
  118. assertEquals(3, stream.readInt()); // number of strings
  119. assertEquals(12345678, stream.readInt()); // res id of first string
  120. assertEquals(0, stream.readShort()); // delta of id for first element
  121. assertEquals(2, stream.readByte()); // number of genders
  122. assertEquals(0, stream.readByte()); // ordinal of the gender enum for first string
  123. assertEquals(5, stream.readShort()); // length of first string of first element
  124. assertEquals(1, stream.readByte()); // ordinal of the gender enum for the second string
  125. assertEquals(12, stream.readShort()); // length of second string of first element
  126. assertEquals(1, stream.readShort()); // delta for second element
  127. assertEquals(1, stream.readByte()); // number of genders
  128. assertEquals(0, stream.readByte()); // ordinal of the gender enum for first string
  129. assertEquals(5, stream.readShort()); // length of first string
  130. assertEquals(1, stream.readShort()); // delta for third element
  131. assertEquals(2, stream.readByte()); // number of genders
  132. assertEquals(0, stream.readByte()); // ordinal of the gender enum for first string
  133. assertEquals(4, stream.readShort()); // length of first string of first element
  134. assertEquals(1, stream.readByte()); // ordinal of the gender enum for the second string
  135. assertEquals(9, stream.readShort()); // length of second string of first element
  136. // string values
  137. assertEquals("S_one", readStringOfLength(stream, 5));
  138. assertEquals("S_one_female", readStringOfLength(stream, 12));
  139. assertEquals("S_two", readStringOfLength(stream, 5));
  140. assertEquals("S_ne", readStringOfLength(stream, 4));
  141. assertEquals("\"S_three\"", readStringOfLength(stream, 9)); // string should be unescaped.
  142. // Plurals
  143. assertEquals(2, stream.readInt()); // number of plurals
  144. assertEquals(12345689, stream.readInt()); // res Id of the first string
  145. assertEquals(0, stream.readShort()); // delta of id for first element
  146. assertEquals(3, stream.readByte()); // number of genders
  147. assertEquals(0, stream.readByte()); // ordinal of the gender enum for first string
  148. assertEquals(3, stream.readByte()); // number of categories.
  149. assertEquals(1, stream.readByte()); // plural key mapped value of first item
  150. assertEquals(6, stream.readShort()); // length of first item
  151. assertEquals(3, stream.readByte()); // plural key mapped value of second item
  152. assertEquals(6, stream.readShort()); // length of second item
  153. assertEquals(4, stream.readByte()); // plural key mapped value of third item
  154. assertEquals(7, stream.readShort()); // length of third item
  155. assertEquals(1, stream.readByte()); // ordinal of the gender enum for first string
  156. assertEquals(3, stream.readByte()); // number of categories.
  157. assertEquals(1, stream.readByte()); // plural key mapped value of first item
  158. assertEquals(9, stream.readShort()); // length of first item
  159. assertEquals(3, stream.readByte()); // plural key mapped value of second item
  160. assertEquals(9, stream.readShort()); // length of second item
  161. assertEquals(4, stream.readByte()); // plural key mapped value of third item
  162. assertEquals(10, stream.readShort()); // length of third item
  163. assertEquals(2, stream.readByte()); // ordinal of the gender enum for first plural
  164. assertEquals(3, stream.readByte()); // number of categories.
  165. assertEquals(1, stream.readByte()); // plural key mapped value of first item
  166. assertEquals(9, stream.readShort()); // length of first item
  167. assertEquals(3, stream.readByte()); // plural key mapped value of second item
  168. assertEquals(9, stream.readShort()); // length of second item
  169. assertEquals(4, stream.readByte()); // plural key mapped value of third item
  170. assertEquals(10, stream.readShort()); // length of third item
  171. assertEquals(3, stream.readShort()); // delta of id for the second plural
  172. assertEquals(2, stream.readByte()); // number of genders
  173. assertEquals(0, stream.readByte()); // ordinal of the gender enum for first plural
  174. assertEquals(3, stream.readByte()); // number of categories.
  175. assertEquals(0, stream.readByte()); // plural key mapped value of first item
  176. assertEquals(7, stream.readShort()); // length of first item
  177. assertEquals(2, stream.readByte()); // plural key mapped value of second item
  178. assertEquals(6, stream.readShort()); // length of second item
  179. assertEquals(5, stream.readByte()); // plural key mapped value of third item
  180. assertEquals(8, stream.readShort()); // length of third item
  181. assertEquals(1, stream.readByte()); // ordinal of the gender enum for first plural
  182. assertEquals(3, stream.readByte()); // number of categories.
  183. assertEquals(0, stream.readByte()); // plural key mapped value of first item
  184. assertEquals(10, stream.readShort()); // length of first item
  185. assertEquals(2, stream.readByte()); // plural key mapped value of second item
  186. assertEquals(9, stream.readShort()); // length of second item
  187. assertEquals(5, stream.readByte()); // plural key mapped value of third item
  188. assertEquals(11, stream.readShort()); // length of third item
  189. // plural strings
  190. assertEquals("P1_one", readStringOfLength(stream, 6));
  191. assertEquals("P1_few", readStringOfLength(stream, 6));
  192. assertEquals("P1_many", readStringOfLength(stream, 7));
  193. assertEquals("P1_one_f1", readStringOfLength(stream, 9));
  194. assertEquals("P1_few_f1", readStringOfLength(stream, 9));
  195. assertEquals("P1_many_f1", readStringOfLength(stream, 10));
  196. assertEquals("P1_one_m2", readStringOfLength(stream, 9));
  197. assertEquals("P1_few_m2", readStringOfLength(stream, 9));
  198. assertEquals("P1_many_m2", readStringOfLength(stream, 10));
  199. assertEquals("P2_zero", readStringOfLength(stream, 7));
  200. assertEquals("P2_two", readStringOfLength(stream, 6));
  201. assertEquals("P2_other", readStringOfLength(stream, 8));
  202. assertEquals("P2_zero_f1", readStringOfLength(stream, 10));
  203. assertEquals("P2_two_f1", readStringOfLength(stream, 9));
  204. assertEquals("P2_other_f1", readStringOfLength(stream, 11));
  205. // Arrays
  206. assertEquals(2, stream.readInt()); // number of arrays
  207. assertEquals(12345694, stream.readInt()); // res Id of the first string
  208. assertEquals(0, stream.readShort()); // delta of id for first element
  209. assertEquals(3, stream.readByte()); // number of genders
  210. assertEquals(0, stream.readByte()); // ordinal of the gender enum for first array
  211. assertEquals(2, stream.readByte()); // number of array elements.
  212. assertEquals(6, stream.readShort()); // length of first item
  213. assertEquals(6, stream.readShort()); // length of second item
  214. assertEquals(1, stream.readByte()); // ordinal of the gender enum for second array
  215. assertEquals(2, stream.readByte()); // number of array elements.
  216. assertEquals(9, stream.readShort()); // length of first item
  217. assertEquals(9, stream.readShort()); // length of second item
  218. assertEquals(2, stream.readByte()); // ordinal of the gender enum for third array
  219. assertEquals(2, stream.readByte()); // number of array elements.
  220. assertEquals(9, stream.readShort()); // length of first item
  221. assertEquals(9, stream.readShort()); // length of second item
  222. assertEquals(5, stream.readShort()); // delta of id for second element
  223. assertEquals(2, stream.readByte()); // number of genders
  224. assertEquals(0, stream.readByte()); // ordinal of the gender enum for first array
  225. assertEquals(1, stream.readByte()); // number of array elements.
  226. assertEquals(6, stream.readShort()); // length of first item
  227. assertEquals(2, stream.readByte()); // ordinal of the gender enum for second array
  228. assertEquals(1, stream.readByte()); // number of array elements.
  229. assertEquals(9, stream.readShort()); // length of first item
  230. // array strings
  231. assertEquals("A1_one", readStringOfLength(stream, 6));
  232. assertEquals("A1_two", readStringOfLength(stream, 6));
  233. assertEquals("A1_one_f1", readStringOfLength(stream, 9));
  234. assertEquals("A1_two_f1", readStringOfLength(stream, 9));
  235. assertEquals("A1_one_m2", readStringOfLength(stream, 9));
  236. assertEquals("A1_two_m2", readStringOfLength(stream, 9));
  237. assertEquals("A2_one", readStringOfLength(stream, 6));
  238. assertEquals("A2_one_m2", readStringOfLength(stream, 9));
  239. }
  240. private String readStringOfLength(DataInputStream stream, int length) throws IOException {
  241. byte[] data = new byte[length];
  242. assertEquals(length, stream.read(data, 0, length));
  243. return new String(data);
  244. }
  245. }