]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/request/misc/NoneListenerTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / request / misc / NoneListenerTest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.tests.api.request.misc;
13
14 import java.util.concurrent.atomic.AtomicInteger;
15
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;
28
29 /**
30  * @author Tuukka Lehtonen
31  */
32 public class NoneListenerTest extends ExistingDatabaseTest {
33
34         static AtomicInteger execs = new AtomicInteger(0);
35         
36     static class TestListener implements Listener<String> {
37         @Override
38         public void execute(String result) {
39                 execs.incrementAndGet();
40         }
41         @Override
42         public boolean isDisposed() {
43             return false;
44         }
45                 @Override
46                 public void exception(Throwable t) {
47                         t.printStackTrace();
48                 }
49                 @Override
50                 public int hashCode() {
51                         return getClass().hashCode();
52                 }
53                 @Override
54                 public boolean equals(Object obj) {
55                         return getClass().equals(obj.getClass());
56                 }
57     }
58
59         @Test
60     public void test() throws Exception {
61
62         final Session session = getSession();
63
64         final Resource newResource = session.syncRequest(new WriteResultRequest<Resource>() {
65             @Override
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);
72                 return result;
73             }
74         });
75
76         TestListener l1 = new TestListener();
77         TestListener l2 = new TestListener();
78
79         session.syncRequest(new Value<String>(newResource, Bindings.STRING), l1);
80         session.syncRequest(new Value<String>(newResource, Bindings.STRING), l2);
81
82         assertEquals(execs.get(), 2);
83
84         session.syncRequest(new WriteRequest() {
85             @Override
86             public void perform(WriteGraph graph) throws DatabaseException {
87                 graph.claimValue(newResource, "Test2", Bindings.STRING);
88             }
89         });
90         
91         assertEquals(execs.get(), 3);
92
93     }
94 }