SentenceDetector implements Analyzer { public void analyze(Document document) { if (document.getText() == null) throw new IllegalArgumentException("Document text is null."); Span[] spans; synchronized (sentenceDetector) { spans = sentenceDetector.sentPosDetect(document.getText()); } List<Sentence> sentences = new ArrayList<Sentence>(spans.length); for (int i = 0; i < spans.length; i++) { Sentence sentence = new SentenceImpl(spans[i].getStart(), spans[i].getEnd(), document); sentences.add(sentence); } document.setSentences(sentences); } SentenceDetector(SentenceDetectorME sentenceDetector); void analyze(Document document);  }
@Test(expected = IllegalArgumentException.class) public void testAnalyzeNull() { DocumentImpl document = new DocumentImpl(); String text = null; document.setText(text); Span[] spans = new Span[0]; when(mockedSentenceDetector.sentPosDetect(text)).thenReturn(spans); sentenceDetector.analyze(document); }
@Test public void testAnalyze() { DocumentImpl document = new DocumentImpl(); String text = "Este é um teste. Ele contém duas frases."; document.setText(text); Span[] spans = { new Span(0, 16), new Span(17, 40) }; when(mockedSentenceDetector.sentPosDetect(text)).thenReturn(spans); sentenceDetector.analyze(document); assertNotNull(document.getSentences()); assertEquals(2, document.getSentences().size()); assertEquals("Este é um teste.", document.getSentences().get(0) .getText()); assertEquals("Ele contém duas frases.", document.getSentences().get(1) .getText()); }
@Test public void testAnalyzeEmpty() { DocumentImpl document = new DocumentImpl(); String text = ""; document.setText(text); Span[] spans = new Span[0]; when(mockedSentenceDetector.sentPosDetect(text)).thenReturn(spans); sentenceDetector.analyze(document); assertNotNull(document.getSentences()); assertEquals(0, document.getSentences().size()); }