use of de.tudarmstadt.ukp.clarin.webanno.api.casstorage.CasAccessMode.EXCLUSIVE_WRITE_ACCESS in project webanno by webanno.
the class CasStorageServiceImplTest method testRestorationOfCasWhenSaveFails.
@Test
public void testRestorationOfCasWhenSaveFails() throws Exception {
try (CasStorageSession casStorageSession = openNested(true)) {
// Setup fixture
SourceDocument doc = makeSourceDocument(6l, 6l, "test");
String user = "test";
File casFile = sut.getCasFile(doc, user);
long casFileSize;
long casFileLastModified;
try (CasStorageSession session = openNested(true)) {
createCasFile(doc, user, "This is a test");
assertThat(casFile).exists();
casFileSize = casFile.length();
casFileLastModified = casFile.lastModified();
}
CAS mainCas = sut.readCas(doc, user, EXCLUSIVE_WRITE_ACCESS);
// Wrap the CAS in a proxy so that UIMA cannot serialize it
CAS guardedCas = (CAS) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { CAS.class }, (proxy, method, args) -> method.invoke(mainCas, args));
assertThatExceptionOfType(IOException.class).as("Saving fails because UIMA cannot cast the proxied CAS to something serializable").isThrownBy(() -> sut.writeCas(doc, guardedCas, user)).withRootCauseInstanceOf(ClassCastException.class);
assertThat(casFile).exists().hasSize(casFileSize);
assertThat(casFile.lastModified()).isEqualTo(casFileLastModified);
assertThat(new File(casFile.getParentFile(), user + ".ser.old")).doesNotExist();
}
}
Aggregations