1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.tests.api.request.misc;
14 import java.util.concurrent.atomic.AtomicInteger;
16 import org.junit.Test;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.primitiverequest.Value;
22 import org.simantics.db.common.request.WriteRequest;
23 import org.simantics.db.common.request.WriteResultRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.procedure.Listener;
26 import org.simantics.db.testing.base.ExistingDatabaseTest;
27 import org.simantics.layer0.Layer0;
30 * @author Tuukka Lehtonen
32 public class NoneListenerTest extends ExistingDatabaseTest {
34 static AtomicInteger execs = new AtomicInteger(0);
36 static class TestListener implements Listener<String> {
38 public void execute(String result) {
39 execs.incrementAndGet();
42 public boolean isDisposed() {
46 public void exception(Throwable t) {
50 public int hashCode() {
51 return getClass().hashCode();
54 public boolean equals(Object obj) {
55 return getClass().equals(obj.getClass());
60 public void test() throws Exception {
62 final Session session = getSession();
64 final Resource newResource = session.syncRequest(new WriteResultRequest<Resource>() {
66 public Resource perform(WriteGraph g) throws DatabaseException {
67 Layer0 b = Layer0.getInstance(g);
68 Resource result = g.newResource();
69 g.claim(result, b.InstanceOf, null, b.Entity);
70 g.claim(g.getRootLibrary(), b.ConsistsOf, result);
71 g.claimValue(result, "Test", Bindings.STRING);
76 TestListener l1 = new TestListener();
77 TestListener l2 = new TestListener();
79 session.syncRequest(new Value<String>(newResource, Bindings.STRING), l1);
80 session.syncRequest(new Value<String>(newResource, Bindings.STRING), l2);
82 assertEquals(execs.get(), 2);
84 session.syncRequest(new WriteRequest() {
86 public void perform(WriteGraph graph) throws DatabaseException {
87 graph.claimValue(newResource, "Test2", Bindings.STRING);
91 assertEquals(execs.get(), 3);