@SuppressWarnings({"ConstantConditions", "unchecked"}) @Test public void testHeaderExclusionOoc() throws Exception { service = createMock(ExecutorService.class); provider = createMock(TranslationProvider.class); callback = createMock(TranslatorCallback.class); replayAll(); Callable task = new TranslateTask(service, provider, TranslationDirection.EnglishToGerman, "Somebody says: ((ALL YOUR BASE ARE BELONG TO US.))", callback); verifyAll(); resetAll(); Future<String> translationFuture = createMock(Future.class); expect(translationFuture.get()).andReturn("ALL DEINE STÜTZPUNKT SIND GEHÖREN UNS.").anyTimes(); TranslateSentenceTask sentenceTask = PowerMock.createMock(TranslateSentenceTask.class); PowerMock.expectStrictNew(TranslateSentenceTask.class, new Class<?>[]{TranslationProvider.class, TranslationDirection.class, String.class}, provider, TranslationDirection.EnglishToGerman, "ALL YOUR BASE ARE BELONG TO US.") .andReturn(sentenceTask); expect(service.submit(sentenceTask)).andReturn(translationFuture); callback.sendTranslation("Somebody says: ((ALL DEINE STÜTZPUNKT SIND GEHÖREN UNS.))"); PowerMock.replay(sentenceTask); PowerMock.replay(TranslateSentenceTask.class); replayAll(); task.call(); PowerMock.verify(sentenceTask); PowerMock.verify(TranslateSentenceTask.class); verifyAll(); }
@Override @Nullable public String call() throws Exception { String header = findHeader(original); String usedText = (header == null) ? original : original.substring(header.length()); boolean foundOocMarkers = false; if (usedText.startsWith("((") && usedText.endsWith("))")) { foundOocMarkers = true; usedText = usedText.substring(2, usedText.length() - 2); } BreakIterator iterator = BreakIterator.getSentenceInstance(Lang.getInstance().getLocale()); iterator.setText(usedText); Collection<Future<String>> translationTasks = new ArrayList<>(); int start = iterator.first(); for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next()) { String line = usedText.substring(start, end).trim(); translationTasks.add(executorService.submit(new TranslateSentenceTask(provider, direction, line))); } StringBuilder resultBuilder = new StringBuilder(); if (header != null) { resultBuilder.append(header); } if (foundOocMarkers) { resultBuilder.append("(("); } for (Future<String> task : translationTasks) { String translated = task.get(); if (translated == null) { callback.sendTranslation(null); return null; } resultBuilder.append(task.get()); resultBuilder.append(' '); } if (resultBuilder.length() == 0) { callback.sendTranslation(null); return null; } resultBuilder.setLength(resultBuilder.length() - 1); if (foundOocMarkers) { resultBuilder.append("))"); } String result = resultBuilder.toString(); callback.sendTranslation(result); return result; }
TranslateTask implements Callable<String> { @Override @Nullable public String call() throws Exception { String header = findHeader(original); String usedText = (header == null) ? original : original.substring(header.length()); boolean foundOocMarkers = false; if (usedText.startsWith("((") && usedText.endsWith("))")) { foundOocMarkers = true; usedText = usedText.substring(2, usedText.length() - 2); } BreakIterator iterator = BreakIterator.getSentenceInstance(Lang.getInstance().getLocale()); iterator.setText(usedText); Collection<Future<String>> translationTasks = new ArrayList<>(); int start = iterator.first(); for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next()) { String line = usedText.substring(start, end).trim(); translationTasks.add(executorService.submit(new TranslateSentenceTask(provider, direction, line))); } StringBuilder resultBuilder = new StringBuilder(); if (header != null) { resultBuilder.append(header); } if (foundOocMarkers) { resultBuilder.append("(("); } for (Future<String> task : translationTasks) { String translated = task.get(); if (translated == null) { callback.sendTranslation(null); return null; } resultBuilder.append(task.get()); resultBuilder.append(' '); } if (resultBuilder.length() == 0) { callback.sendTranslation(null); return null; } resultBuilder.setLength(resultBuilder.length() - 1); if (foundOocMarkers) { resultBuilder.append("))"); } String result = resultBuilder.toString(); callback.sendTranslation(result); return result; } }
TranslateTask implements Callable<String> { @Override @Nullable public String call() throws Exception { String header = findHeader(original); String usedText = (header == null) ? original : original.substring(header.length()); boolean foundOocMarkers = false; if (usedText.startsWith("((") && usedText.endsWith("))")) { foundOocMarkers = true; usedText = usedText.substring(2, usedText.length() - 2); } BreakIterator iterator = BreakIterator.getSentenceInstance(Lang.getInstance().getLocale()); iterator.setText(usedText); Collection<Future<String>> translationTasks = new ArrayList<>(); int start = iterator.first(); for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next()) { String line = usedText.substring(start, end).trim(); translationTasks.add(executorService.submit(new TranslateSentenceTask(provider, direction, line))); } StringBuilder resultBuilder = new StringBuilder(); if (header != null) { resultBuilder.append(header); } if (foundOocMarkers) { resultBuilder.append("(("); } for (Future<String> task : translationTasks) { String translated = task.get(); if (translated == null) { callback.sendTranslation(null); return null; } resultBuilder.append(task.get()); resultBuilder.append(' '); } if (resultBuilder.length() == 0) { callback.sendTranslation(null); return null; } resultBuilder.setLength(resultBuilder.length() - 1); if (foundOocMarkers) { resultBuilder.append("))"); } String result = resultBuilder.toString(); callback.sendTranslation(result); return result; } TranslateTask(@Nonnull ExecutorService executorService, @Nonnull TranslationProvider provider,
                  @Nonnull TranslationDirection direction, @Nonnull String original,
                  @Nonnull TranslatorCallback callback); }
TranslateTask implements Callable<String> { @Override @Nullable public String call() throws Exception { String header = findHeader(original); String usedText = (header == null) ? original : original.substring(header.length()); boolean foundOocMarkers = false; if (usedText.startsWith("((") && usedText.endsWith("))")) { foundOocMarkers = true; usedText = usedText.substring(2, usedText.length() - 2); } BreakIterator iterator = BreakIterator.getSentenceInstance(Lang.getInstance().getLocale()); iterator.setText(usedText); Collection<Future<String>> translationTasks = new ArrayList<>(); int start = iterator.first(); for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next()) { String line = usedText.substring(start, end).trim(); translationTasks.add(executorService.submit(new TranslateSentenceTask(provider, direction, line))); } StringBuilder resultBuilder = new StringBuilder(); if (header != null) { resultBuilder.append(header); } if (foundOocMarkers) { resultBuilder.append("(("); } for (Future<String> task : translationTasks) { String translated = task.get(); if (translated == null) { callback.sendTranslation(null); return null; } resultBuilder.append(task.get()); resultBuilder.append(' '); } if (resultBuilder.length() == 0) { callback.sendTranslation(null); return null; } resultBuilder.setLength(resultBuilder.length() - 1); if (foundOocMarkers) { resultBuilder.append("))"); } String result = resultBuilder.toString(); callback.sendTranslation(result); return result; } TranslateTask(@Nonnull ExecutorService executorService, @Nonnull TranslationProvider provider,
                  @Nonnull TranslationDirection direction, @Nonnull String original,
                  @Nonnull TranslatorCallback callback); @Override @Nullable String call(); }
TranslateTask implements Callable<String> { @Override @Nullable public String call() throws Exception { String header = findHeader(original); String usedText = (header == null) ? original : original.substring(header.length()); boolean foundOocMarkers = false; if (usedText.startsWith("((") && usedText.endsWith("))")) { foundOocMarkers = true; usedText = usedText.substring(2, usedText.length() - 2); } BreakIterator iterator = BreakIterator.getSentenceInstance(Lang.getInstance().getLocale()); iterator.setText(usedText); Collection<Future<String>> translationTasks = new ArrayList<>(); int start = iterator.first(); for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next()) { String line = usedText.substring(start, end).trim(); translationTasks.add(executorService.submit(new TranslateSentenceTask(provider, direction, line))); } StringBuilder resultBuilder = new StringBuilder(); if (header != null) { resultBuilder.append(header); } if (foundOocMarkers) { resultBuilder.append("(("); } for (Future<String> task : translationTasks) { String translated = task.get(); if (translated == null) { callback.sendTranslation(null); return null; } resultBuilder.append(task.get()); resultBuilder.append(' '); } if (resultBuilder.length() == 0) { callback.sendTranslation(null); return null; } resultBuilder.setLength(resultBuilder.length() - 1); if (foundOocMarkers) { resultBuilder.append("))"); } String result = resultBuilder.toString(); callback.sendTranslation(result); return result; } TranslateTask(@Nonnull ExecutorService executorService, @Nonnull TranslationProvider provider,
                  @Nonnull TranslationDirection direction, @Nonnull String original,
                  @Nonnull TranslatorCallback callback); @Override @Nullable String call();  }