### Question:
ParsedStringPack { @Nullable public String getString(int id) { final String result = strings.get(id); if (result == null) { return loadString(id); } else { return result; } } ParsedStringPack(@NonNull InputStream inputStream, @NonNull List<String> parentLocales); boolean isEmpty(); @Nullable String getString(int id); @Nullable String getQuantityString(int id, Object quantity, @NonNull PluralRules pluralRules);  }### Answer:
@Test public void getString() { String string = parsedStringPack.getString(STRING_ID); assertEquals("你好，世界", string); }
@Test public void getString_MultipleTimes() { String first = parsedStringPack.getString(STRING_ID); assertEquals("你好，世界", first); String second = parsedStringPack.getString(STRING_ID); assertEquals("你好，世界", second); }
@Test public void getString_WithNonexistentId() { String nonexistent = parsedStringPack.getString(EXPECTED_STRINGS.length + 1); assertNull(nonexistent); }
@Test public void getString_onDemandLoadingSameString_calledFromMultipleThreads() throws InterruptedException { int numberOfThreads = 100; String expected = "你好，世界"; ExecutorService service = Executors.newFixedThreadPool(numberOfThreads); CountDownLatch latch = new CountDownLatch(numberOfThreads); Callable<String> getStringTask = () -> { String string = parsedStringPack.getString(STRING_ID); latch.countDown(); return string; }; List<Callable<String>> tasks = new ArrayList<>(); for (int i = 0; i < numberOfThreads; i++) { tasks.add(getStringTask); } List<Future<String>> results = service.invokeAll(tasks, 2, TimeUnit.SECONDS); latch.await(); Thread.sleep(2000); for (int i = 0; i < numberOfThreads; i++) { Future<String> result = results.get(i); assertTrue(!result.isCancelled()); try { assertEquals("Problem fetching correct string", expected, result.get()); } catch (ExecutionException ee) { assertTrue("Exception when fetching string " + ee.toString(), false); } } }
@Test public void getString_onDemandLoadingMultipleStrings_calledFromMultipleThreads() throws InterruptedException { int numberOfThreads = 100; int numStringToCall = EXPECTED_STRINGS.length; ExecutorService service = Executors.newFixedThreadPool(numberOfThreads); CountDownLatch latch = new CountDownLatch(numberOfThreads); List<Callable<List<String>>> tasks = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < numberOfThreads; i++) { final int nextRandomNumber = random.nextInt(numStringToCall - 1) + 1; tasks.add(() -> { List<String> outcome = new ArrayList<>(); outcome.add(EXPECTED_STRINGS[nextRandomNumber]); outcome.add(parsedStringPack.getString(nextRandomNumber)); latch.countDown(); return outcome; }); } List<Future<List<String>>> results = service.invokeAll(tasks, 5, TimeUnit.SECONDS); latch.await(); Thread.sleep(2000); for (int i = 0; i < numberOfThreads; i++) { Future<List<String>> result = results.get(i); assertTrue(!result.isCancelled()); try { List<String> output = result.get(); assertEquals("Problem fetching correct string", output.get(0), output.get(1)); } catch (ExecutionException ee) { assertTrue("Exception when fetching string " + ee.toString(), false); } } }