/******************************************************************************* * 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.api.readGraph.adapt; import org.junit.Before; import org.junit.Test; import org.simantics.db.AsyncReadGraph; 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.ResourceAsyncRead; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.AdaptionException; import org.simantics.db.exception.DatabaseException; import org.simantics.db.procedure.AsyncProcedure; import org.simantics.db.request.Read; import org.simantics.db.services.GlobalServiceInitializer; import org.simantics.db.testing.base.ExistingDatabaseTest; import org.simantics.layer0.Layer0; /** * Checks that {@link ReadGraph#adapt(org.simantics.db.Resource, Class)}, * {@link ReadGraph#adaptUnique(org.simantics.db.Resource, Class)}, * AsyncReadGraph.forAdapted fail as expected. * *

* Regression test for https://www.simulationsite.net/trac/simantics/ticket/617 * * @author Tuukka Lehtonen */ public class AdaptionFailureTest extends ExistingDatabaseTest { @Override @Before public void setUp() throws Exception { super.setUp(); new GlobalServiceInitializer().initialize(getSession()); } private Resource writeTypeless(Session session) throws DatabaseException { final Resource[] typeless = { null }; session.syncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); Resource tless = graph.newResource(); Resource name = graph.newResource(); graph.claim(name, l0.InstanceOf, null, l0.String); graph.claimValue(name, "A Typeless Resource"); graph.claim(tless, l0.HasName, name); typeless[0] = tless; } }); return typeless[0]; } static abstract class Adapter { String name; public Adapter(String name) { this.name = name; } public abstract T adapt(ReadGraph graph, Resource r, Class clazz) throws DatabaseException; @Override public String toString() { return name; } } private void adaptTester(final Adapter adapter) throws DatabaseException { final Session session = getSession(); final Resource typeless = writeTypeless(session); try { String s = session.syncRequest(new Read() { @Override public String perform(ReadGraph graph) throws DatabaseException { try { return adapter.adapt(graph, typeless, String.class); } catch (AdaptionException e) { throw e; } catch (DatabaseException e) { throw new DatabaseException("AdaptionException expected, got " + e, e); } } }); if (s == null) fail(adapter + " must never return null"); } catch (AdaptionException e) { // Failure with AdaptionException is expected if (DEBUG) System.out.println("Expected failure message: " + e.getMessage()); } } @Test public void testAdapt() throws DatabaseException{ adaptTester(new Adapter("ReadGraph.adapt") { @Override public T adapt(ReadGraph graph, Resource resource, Class clazz) throws DatabaseException { return graph.adapt(resource, clazz); } }); } @Test public void testAdaptUnique() throws DatabaseException{ adaptTester(new Adapter("ReadGraph.adaptUnique") { @Override public T adapt(ReadGraph graph, Resource resource, Class clazz) throws DatabaseException { return graph.adaptUnique(resource, clazz); } }); } @Test public void testforAdapted() throws DatabaseException{ adaptTester(new Adapter("AsyncReadGraph.forAdapted") { @Override public T adapt(ReadGraph graph, Resource resource, final Class clazz) throws DatabaseException { return graph.syncRequest(new ResourceAsyncRead(resource) { @Override public void perform(AsyncReadGraph graph, AsyncProcedure procedure) { graph.forAdapted(resource, clazz, procedure); } }); } }); } @Test public void testforUniqueAdapted() throws DatabaseException{ adaptTester(new Adapter("AsyncReadGraph.forUniqueAdapted") { @Override public T adapt(ReadGraph graph, Resource resource, final Class clazz) throws DatabaseException { return graph.syncRequest(new ResourceAsyncRead(resource) { @Override public void perform(AsyncReadGraph graph, AsyncProcedure procedure) { graph.forUniqueAdapted(resource, clazz, procedure); } }); } }); } }