@Test(expected = ResourceTransactionException.class) public void testResourceTimeoutInvalidatesTransaction() throws Exception { ResourceTransaction transaction = resourceFactory.getTransaction(); transaction.setTransactionTimeout(2); transaction.begin(); Thread.sleep(3000); transaction.commit(); }
@Test public void testResourceTransactionSucceeds() throws Exception { ResourceTransaction transaction = resourceFactory.getTransaction(); Assert.assertFalse(transaction.isStarted()); transaction.begin(); Assert.assertTrue(transaction.isStarted()); Thread.sleep(1000); transaction.commit(); Assert.assertFalse(transaction.isStarted()); }
@Test @SuppressWarnings("unchecked") public void testResourceTransactionCommit() throws IOException { File tempDir = OperatingSystemUtils.createTempDir(); File file = createTempFile(tempDir, false); ResourceTransaction transaction = resourceFactory.getTransaction(); Assert.assertNotNull(transaction); Assert.assertFalse(transaction.isStarted()); transaction.begin(); Assert.assertTrue(transaction.isStarted()); FileResource<?> fileResource = resourceFactory.create(FileResource.class, file); Assert.assertNotNull(fileResource); fileResource.setContents("Hello World"); transaction.commit(); Assert.assertTrue(fileResource.exists()); Assert.assertEquals("Hello World", fileResource.getContents()); }
@Test @SuppressWarnings("unchecked") public void testResourceChangeSet() throws IOException { File tempDir = OperatingSystemUtils.createTempDir(); File file = createTempFile(tempDir, true); ResourceTransaction transaction = resourceFactory.getTransaction(); Assert.assertNotNull(transaction); Assert.assertFalse(transaction.isStarted()); transaction.begin(); Assert.assertTrue(transaction.isStarted()); FileResource<?> fileResource = resourceFactory.create(FileResource.class, file); Assert.assertNotNull(fileResource); fileResource.setContents("Hello World"); Assert.assertEquals("Hello World", fileResource.getContents()); Collection<ResourceEvent> changeSet = transaction.getChangeSet(); Assert.assertEquals(3, changeSet.size()); Iterator<ResourceEvent> iterator = changeSet.iterator(); // Created the file { ResourceEvent event = iterator.next(); Assert.assertThat(event, is(instanceOf(ResourceCreated.class))); Assert.assertEquals(fileResource, event.getResource()); } { ResourceEvent event = iterator.next(); // Modified the Directory Assert.assertThat(event, is(instanceOf(ResourceModified.class))); Assert.assertThat(event.getResource(), is(instanceOf(DirectoryResource.class))); } { ResourceEvent event = iterator.next(); // Modified the File resource Assert.assertThat(event, is(instanceOf(ResourceModified.class))); Assert.assertEquals(fileResource, event.getResource()); } Assert.assertFalse(file.exists()); Assert.assertTrue(fileResource.exists()); transaction.commit(); Assert.assertTrue(fileResource.exists()); Assert.assertTrue(file.exists()); Assert.assertEquals("Hello World", fileResource.getContents()); }
@After public void tearDown() { ResourceTransaction transaction = resourceFactory.getTransaction(); if (transaction.isStarted()) transaction.rollback(); }
@Test(expected = ResourceTransactionException.class) public void testResourceTimeout() { ResourceTransaction transaction = resourceFactory.getTransaction(); transaction.setTransactionTimeout(-100); }