/** * Creates a new JCas with the given text. The type system is detected automatically using * {@link TypeSystemDescriptionFactory#createTypeSystemDescription()}. Type priorities are * detected automatically using {@link TypePrioritiesFactory#createTypePriorities()}. Indexes are * detected automatically using {@link FsIndexFactory#createFsIndexCollection()}. * * @param aText * the document text to be set in the new JCas. * @return a new JCas * @throws UIMAException * if the JCas could not be initialized */ public static JCas createText(String aText) throws UIMAException { return createText(aText, null); }
@Test public void thatCreatingOrReadingInitialCasForNewDocumentCreatesNewCas() throws Exception { when(importExportService.importCasFromFile(any(File.class), any(Project.class), any())).thenReturn(JCasFactory.createText("Test")); SourceDocument doc = makeSourceDocument(1l, 1l, "test"); JCas cas = sut.createOrReadInitialCas(doc); assertThat(cas).isNotNull(); assertThat(cas.getDocumentText()).isEqualTo("Test"); assertThat(storageService.getCasFile(doc, WebAnnoConst.INITIAL_CAS_PSEUDO_USER)).exists(); }
@Test public void testSplitSentences() throws Exception { JCas jcas = JCasFactory.createText("I am one. I am two.", "en"); ImportExportServiceImpl.splitSentences(jcas); assertEquals(asList("I am one.", "I am two."), toText(select(jcas, Sentence.class))); }
@Test public void thatReadingNonExistentAnnotationCasCreatesNewCas() throws Exception { SourceDocument sourceDocument = makeSourceDocument(1l, 1l, "test"); User user = makeUser(); when(userRepository.get(user.getUsername())).thenReturn(user); when(entityManager.createQuery(anyString(), any())).thenThrow(NoResultException.class); when(importExportService.importCasFromFile(any(File.class), any(Project.class), any())).thenReturn(JCasFactory.createText("Test")); JCas cas = sut.readAnnotationCas(sourceDocument, user.getUsername()); assertThat(cas).isNotNull(); assertThat(cas.getDocumentText()).isEqualTo("Test"); assertThat(storageService.getCasFile(sourceDocument, user.getUsername())).exists(); }
@Test public void testWriteReadExistsDeleteCas() throws Exception { SourceDocument doc = makeSourceDocument(1l, 1l); JCas cas = JCasFactory.createText("This is a test"); String user = "test"; sut.writeCas(doc, cas, user); assertThat(sut.getCasFile(doc, user)).exists(); assertThat(sut.existsCas(doc, user)).isTrue(); JCas cas2 = sut.readCas(doc, user); assertThat(cas2.getDocumentText()).isEqualTo(cas.getDocumentText()); sut.deleteCas(doc, user); assertThat(sut.getCasFile(doc, user)).doesNotExist(); assertThat(sut.existsCas(doc, user)).isFalse(); }
@Test public void testTokenize() throws Exception { JCas jcas = JCasFactory.createText("i am one.i am two.", "en"); new Sentence(jcas, 0, 9).addToIndexes();; new Sentence(jcas, 9, 18).addToIndexes(); ImportExportServiceImpl.tokenize(jcas); assertEquals(asList("i am one.", "i am two."), toText(select(jcas, Sentence.class))); assertEquals(asList("i", "am", "one", ".", "i", "am", "two", "."), toText(select(jcas, Token.class))); } }
@Test public void testReadOrCreateCas() throws Exception { SourceDocument doc = makeSourceDocument(2l, 2l); String user = "test"; JCas cas = sut.readOrCreateCas(doc, user, () -> { try { return JCasFactory.createText("This is a test"); } catch (UIMAException e) { throw new IOException(e); } }); assertThat(sut.getCasFile(doc, user)).exists(); assertThat(sut.existsCas(doc, user)).isTrue(); JCas cas2 = sut.readCas(doc, user); assertThat(cas2.getDocumentText()).isEqualTo(cas.getDocumentText()); sut.deleteCas(doc, user); assertThat(sut.getCasFile(doc, user)).doesNotExist(); assertThat(sut.existsCas(doc, user)).isFalse(); }