/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.db.tests.regression.bugs; import org.junit.Test; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; import org.simantics.db.WriteGraph; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.AssumptionException; import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.ResourceNotFoundException; import org.simantics.db.testing.base.ExistingDatabaseTest; import org.simantics.layer0.Layer0; /** * Tests that exception during write causes canceling of changes. * This tests must be added at least twice in order for the bug to appear. * Even then the bug is not deterministic. */ public class WriteCancelTest3 extends ExistingDatabaseTest { final String resourceName = "WriteCancelTest3"; @Test public void testWriteCancelTest3() throws Exception { final Session session = getSession(); boolean exceptionThrown = false; try { session.syncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); checkThatInstanceDoesNotExist(graph); Resource r1 = graph.newResource(); graph.claim(r1, L0.InstanceOf, null, L0.Library); graph.claimLiteral(r1, L0.HasName, resourceName); Resource root = graph.getResource(ROOT_LIBRARY_URI); graph.claim(root, L0.ConsistsOf, L0.PartOf, r1); checkThatInstanceDoesExist(graph); throw new AssumptionException("Intentional exception during write."); } }); } catch (AssumptionException e) { exceptionThrown = true; if (DEBUG) System.out.println("Catched AssumptionException as excepected:" + e.getMessage()); } catch (Throwable e) { fail("Write transaction threw an unknown exception " + e); } assertTrue("Failed to throw exception as expected.", exceptionThrown); session.sync(new ReadRequest() { @Override public void run(ReadGraph graph) throws DatabaseException { checkThatInstanceDoesNotExist(graph); } }); } private void checkThatInstanceDoesExist(final ReadGraph graph) { try { String uri = ROOT_LIBRARY_URI + "/" + resourceName; Resource r = graph.getResource(uri); if (null == r) fail("Uri " + uri + " has not been defined."); } catch (DatabaseException e) { fail("Unexpected exception during read request: " + e.getMessage()); } } private void checkThatInstanceDoesNotExist(final ReadGraph graph) { try { String uri = ROOT_LIBRARY_URI + "/" + resourceName; Resource r = graph.getResource(uri); if (null != r) fail("Uri " + uri + " has been defined."); } catch (ResourceNotFoundException e) { if (DEBUG) System.out.println("Catched ResourceNotFoundException as expected."); } catch (DatabaseException e) { fail("Unexpected exception during read request: " + e.getMessage()); } } }