Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCedric Dumoulin2013-12-10 22:26:50 +0000
committerCedric Dumoulin2014-01-27 13:56:06 +0000
commitaf5bfc666e24b937af21889c13bd5000baccde85 (patch)
tree3767fa5175afb63bca6515adcae5def70db195cb /extraplugins/layers
parent533e71523822faacb0bcf7756e94a5f4c8fe63d3 (diff)
downloadorg.eclipse.papyrus-af5bfc666e24b937af21889c13bd5000baccde85.tar.gz
org.eclipse.papyrus-af5bfc666e24b937af21889c13bd5000baccde85.tar.xz
org.eclipse.papyrus-af5bfc666e24b937af21889c13bd5000baccde85.zip
Add ExprMatcher and NotyfyingList
Diffstat (limited to 'extraplugins/layers')
-rw-r--r--extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ExpressionMatcher.java224
-rw-r--r--extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/util/NotyfyingList.java333
-rw-r--r--extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ExpressionMatcherTest.java262
-rw-r--r--extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/util/FakeNotifyingListListener.java41
-rw-r--r--extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/util/NotyfyingListTest.java447
-rw-r--r--extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/util/TriggeredEventTraces.java131
6 files changed, 1438 insertions, 0 deletions
diff --git a/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ExpressionMatcher.java b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ExpressionMatcher.java
new file mode 100644
index 00000000000..f19b59841c5
--- /dev/null
+++ b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ExpressionMatcher.java
@@ -0,0 +1,224 @@
+/*****************************************************************************
+ * Copyright (c) 2013 Cedric Dumoulin.
+ *
+ *
+ * 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:
+ * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.layers.stackmodel.exprmatcher;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EClassifier;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.query.conditions.eobjects.EObjectCondition;
+import org.eclipse.emf.query.ocl.conditions.BooleanOCLCondition;
+import org.eclipse.emf.query.statements.FROM;
+import org.eclipse.emf.query.statements.IQueryResult;
+import org.eclipse.emf.query.statements.SELECT;
+import org.eclipse.emf.query.statements.WHERE;
+import org.eclipse.gmf.runtime.notation.NotationPackage;
+import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.ocl.ParserException;
+import org.eclipse.ocl.ecore.OCL;
+import org.eclipse.papyrus.layers.stackmodel.LayersException;
+
+/**
+ * This class evaluate its associated expression against the associated models.
+ * It provide a list of elements matching the expression in the model.
+ * It fire some events when the list of matching elements change.
+ * The expression is evaluated each time a change impacting the result occurs in the model.
+ *
+ * It maybe necessary to adjust the listeners on model changes to improve performances.
+ *
+ * @author cedric dumoulin
+ *
+ */
+public class ExpressionMatcher {
+
+ protected String expression;
+
+ /**
+ * List of element matching the expression.
+ * This class maintains the list.
+ */
+ protected List<View> matchingElements;
+
+ /**
+ * List of element used as starting point for search.
+ */
+ protected List<EObject> searchRoots;
+
+ /**
+ * OCL Condition computed from the expr.
+ */
+ protected EObjectCondition condition;
+ protected OCL ocl;
+
+
+ /**
+ * Constructor.
+ *
+ * @param expression
+ * @param searchRoots
+ * @throws LayersException If the Condition can't be computed from the expression.
+ */
+ public ExpressionMatcher(String expression, List<EObject> searchRoots) throws LayersException {
+ this.expression = expression;
+ this.searchRoots = searchRoots;
+ matchingElements = new ArrayList<View>();
+
+ // compute expr
+ computeCondition();
+ refreshMatchingElements();
+ }
+
+ /**
+ * Compute the condition from the expr.
+ */
+ private void computeCondition() throws LayersException {
+ // silently fails if the expr is not set.
+ if( getExpression() == null || getExpression().length() == 0) {
+ return;
+ }
+
+ if( ocl == null) {
+ ocl = OCL.newInstance();
+ }
+ // Create the condition
+ try {
+ condition = new BooleanOCLCondition<EClassifier, EClass, EObject>(
+ ocl.getEnvironment(),
+// "self.oclIsKindOf(Shape)",
+// "self.oclIsKindOf(Shape) and self.oclAsType(Shape).visible = true",
+// "self.oclAsType(Shape).visible = true",
+ getExpression(),
+ NotationPackage.Literals.VIEW
+ );
+ } catch (ParserException e) {
+ // TODO Auto-generated catch block
+ condition = null;
+ throw new LayersException("Can't parse expression", e);
+ }
+
+ }
+
+
+ /**
+ * Recompute the matching elements.
+ * This lead to firing Events (added and removed)
+ */
+ public void refreshMatchingElements() {
+
+
+ if( condition == null) {
+ // If the condition is not set, the list should be empty
+ if( !getMatchingElements().isEmpty()) {
+ resetMatchingElements( Collections.EMPTY_LIST);
+ }
+ return;
+ }
+
+ // Create the OCL statement
+ SELECT statement = new SELECT(SELECT.UNBOUNDED, false,
+ new FROM(getSearchRoots()), new WHERE(condition),
+ new NullProgressMonitor());
+
+ // Execute the OCL statement
+ IQueryResult results = statement.execute();
+
+ /**
+ * Reset the matching elements with the new result.
+ */
+ resetMatchingElements( results);
+ }
+
+ /**
+ * Reset the {@link #matchingElements} and let it contain the specified collection.
+ * This fire added and removed events.
+ *
+ * @param results
+ */
+ private void resetMatchingElements(Collection<?> newElements) {
+ // Compute views to add
+ // This are views in the newElements, but not in the actual list of matchingElement
+ // viewsToAdd = results - getViews()
+ List<View> viewsToAdd = new ArrayList<View>();
+ for( Object o : newElements ) {
+ View v = (View)o;
+ if( !getMatchingElements().contains(v)) {
+ viewsToAdd.add(v);
+ }
+ }
+
+ // Compute views to remove
+ // Their is two ways to compute it:
+ // - viewsToremove = diagramViews - results
+ // - or viewsToremove = getViews() - result
+ // Use the cheaper one.
+ // The computed viewsToRemove list contains also views that are not in the layer,
+ // But this is cheaper than checking for the existence.
+
+// List<View> viewsToRemove = new ArrayList<View>();
+// for( View v : (views.size()<getViews().size()?views:getViews()) ) {
+// if( !results.contains(v)) {
+// viewsToRemove.add(v);
+// }
+// }
+
+ // Do operations
+ getMatchingElements().retainAll(newElements);
+// getViews().removeAll(viewsToRemove);
+ getMatchingElements().addAll(viewsToAdd);
+
+ }
+
+ /**
+ * @return the expression
+ */
+ public String getExpression() {
+ return expression;
+ }
+
+
+ /**
+ * @param expression the expression to set
+ * @throws LayersException If the Condition can't be computed from the expression.
+ */
+ public void setExpression(String expression) throws LayersException {
+ this.expression = expression;
+
+ computeCondition();
+ refreshMatchingElements();
+ }
+
+
+ /**
+ * @return the matchingElements
+ */
+ public List<View> getMatchingElements() {
+ return matchingElements;
+ }
+
+
+ /**
+ * @return the searchRoots
+ */
+ public List<EObject> getSearchRoots() {
+ return searchRoots;
+ }
+
+
+}
diff --git a/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/util/NotyfyingList.java b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/util/NotyfyingList.java
new file mode 100644
index 00000000000..007160bfd95
--- /dev/null
+++ b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/util/NotyfyingList.java
@@ -0,0 +1,333 @@
+/*****************************************************************************
+ * Copyright (c) 2013 Cedric Dumoulin.
+ *
+ *
+ * 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:
+ * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.layers.stackmodel.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.gmf.runtime.notation.View;
+
+import com.google.common.collect.ForwardingList;
+import com.google.common.collect.Lists;
+import com.google.common.eventbus.EventBus;
+
+/**
+ * A delegate list notifying of events on the delegated list.
+ * The following events are notified:
+ * <ul>
+ * <li>elements added</li>
+ * <li>elements removed</li>
+ * <li></li>
+ * <li></li>
+ * </ul>
+ * @author cedric dumoulin
+ *
+ */
+public class NotyfyingList<E> extends ForwardingList<E>{
+
+ protected List<E> delegate;
+
+ protected EventBus eventBus = new EventBus(NotyfyingList.class.getName());
+
+ /**
+ * Constructor.
+ *
+ * @param delegate
+ */
+ public NotyfyingList(List<E> delegate) {
+ this.delegate = delegate;
+ }
+
+ @Override
+ protected List<E> delegate() {
+ return delegate;
+ }
+
+ /**
+ * @return the eventBus
+ */
+ public EventBus getEventBus() {
+ return eventBus;
+ }
+
+ @Override
+ public boolean add(E element) {
+ boolean isModified = delegate().add( element);
+ if( isModified) {
+ eventBus.post(new NotifyingListEvent(element));
+ }
+
+ return isModified;
+ }
+
+ @Override
+ public boolean addAll(Collection<? extends E> collection) {
+ boolean isModified = delegate().addAll(collection);
+ if( isModified) {
+ eventBus.post(new NotifyingListEvent(collection));
+ }
+
+ return isModified;
+ }
+
+ @Override
+ public void add(int index, E element) {
+ delegate().add(index, element);
+ eventBus.post(new NotifyingListEvent(element));
+ }
+
+ @Override
+ public boolean addAll(int index, Collection<? extends E> elements) {
+ // TODO Auto-generated method stub
+ boolean isModified = delegate().addAll(index, elements);
+ if( isModified) {
+ eventBus.post(new NotifyingListEvent(elements));
+ }
+
+ return isModified;
+ }
+
+ @Override
+ public E remove(int index) {
+ E removed = super.remove(index);
+ getEventBus().post( new NotifyingListEvent(null, removed));
+
+ return removed;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean remove(Object object) {
+ boolean isRemoved = super.remove(object);
+ if( isRemoved ) {
+ getEventBus().post( new NotifyingListEvent(null, (E)object));
+ }
+ return isRemoved;
+ }
+
+ /**
+ *
+ * @see com.google.common.collect.ForwardingCollection#removeAll(java.util.Collection)
+ *
+ * @param collection
+ * @return
+ */
+ @Override
+ public boolean removeAll(Collection<?> collection) {
+ // Compute the removed elements (for the events)
+ List<E> removedElements = Lists.newArrayList(delegate());
+ removedElements.retainAll(collection);
+
+ // Do remove
+ boolean isRemoved = super.removeAll(collection);
+ if( isRemoved ) {
+ getEventBus().post( new NotifyingListEvent(null, (Collection<? extends E>) removedElements));
+ }
+ return isRemoved;
+ }
+
+ /**
+ * Reset the collection to the specified collection.
+ * Throw events containing the added and removed elements.
+ *
+ * @param collection
+ * @return
+ */
+ public boolean resetTo(Collection<? extends E> collection ) {
+
+ // Compute removed and added
+ Collection<E> elementsToRemove = new ArrayList<E>();
+ Collection<E> elementsToAdd = new ArrayList<E>();
+
+ // Cached list of attached elements
+ Collection<? extends E> attachedElements = delegate();
+
+ // Compute added and removed elements. Walk both list 2 times.
+ // This could certainly be improved.
+ // TODO improve the algorithm
+
+ // Compute added elements
+ for( E o : collection ) {
+ if( !attachedElements.contains(o)) {
+ elementsToAdd.add(o);
+ continue;
+ }
+ }
+
+ // Compute removed elements
+ for( E o : attachedElements ) {
+ if( !collection.contains(o)) {
+ elementsToRemove.add(o);
+ continue;
+ }
+ }
+
+ // Change the list
+ delegate().clear();
+ delegate().addAll(collection);
+
+ // Fire event
+ boolean isModified = !(elementsToAdd.isEmpty() && elementsToRemove.isEmpty() );
+ if( isModified ) {
+ getEventBus().post( new NotifyingListEvent(elementsToAdd, elementsToRemove));
+ }
+
+ return isModified;
+ }
+
+ /**
+ * Event used to specify that the list is changed
+ * @author cedric dumoulin
+ *
+ */
+ public class NotifyingListEvent {
+
+ Collection<? extends E> addedElements = Collections.emptyList();
+ Collection<? extends E> removedElements = Collections.emptyList();
+
+ /**
+ *
+ * Constructor.
+ *
+ */
+ public NotifyingListEvent() {
+ this.addedElements = Collections.emptyList();
+ this.removedElements = Collections.emptyList();
+ }
+
+ /**
+ *
+ * Constructor.
+ *
+ */
+ public NotifyingListEvent(Collection<? extends E> addedElements) {
+ if( addedElements!=null ) {
+ this.addedElements = addedElements;
+ }
+ else {
+ this.addedElements = Collections.emptyList();
+ }
+
+ this.removedElements = Collections.emptyList();
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param addedElements added elements or null
+ * @param removedElements removed elements or null
+ */
+ public NotifyingListEvent(Collection<? extends E> addedElements, Collection<? extends E> removedElements) {
+ if( addedElements!=null ) {
+ this.addedElements = addedElements;
+ }
+ else {
+ this.addedElements = Collections.emptyList();
+ }
+
+ if( removedElements != null ) {
+ this.removedElements = removedElements;
+ }
+ else {
+ this.removedElements = Collections.emptyList();
+ }
+ }
+
+ /**
+ *
+ * Constructor.
+ *
+ * @param addedElement An element added or null;
+ * @param removedElement An element added or null
+ */
+ public NotifyingListEvent( E addedElement, E removedElement) {
+
+ if( addedElement!=null ) {
+ addedElements = Collections.singletonList(addedElement);
+ }
+ else {
+ addedElements = Collections.emptyList();
+ }
+
+ if( removedElement != null ) {
+ removedElements = Collections.singletonList(removedElement);
+ }
+ else {
+ removedElements = Collections.emptyList();
+ }
+ }
+
+ /**
+ *
+ * Constructor.
+ *
+ * @param addedElement An element added or null;
+ */
+ public NotifyingListEvent( E addedElement) {
+
+ if( addedElement!=null ) {
+ addedElements = Collections.singletonList(addedElement);
+ }
+ else {
+ addedElements = Collections.emptyList();
+ }
+ removedElements = Collections.emptyList();
+ }
+
+ /**
+ * @return the addedElements
+ */
+ public Collection<? extends E> getAddedElements() {
+ return addedElements;
+ }
+
+// /**
+// * @param addedElements the addedElements to set
+// */
+// public void setAddedElements(List<E> addedElements) {
+// this.addedElements = addedElements;
+// }
+//
+// /**
+// * @param addedElements the addedElements to set
+// */
+// public void setAddedElements(E addedElement) {
+// this.addedElements = Collections.singletonList(addedElement);
+// }
+
+ /**
+ * @return the removedElements
+ */
+ public Collection<? extends E> getRemovedElements() {
+ return removedElements;
+ }
+
+// /**
+// * @param removedElements the removedElements to set
+// */
+// public void setRemovedElements(List<E> removedElements) {
+// this.removedElements = removedElements;
+// }
+//
+// /**
+// * @param removedElements the removedElements to set
+// */
+// public void setRemovedElements(E removedElement) {
+// this.removedElements = Collections.singletonList(removedElement);
+// }
+ }
+}
diff --git a/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ExpressionMatcherTest.java b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ExpressionMatcherTest.java
new file mode 100644
index 00000000000..d33830e3ef1
--- /dev/null
+++ b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ExpressionMatcherTest.java
@@ -0,0 +1,262 @@
+/*****************************************************************************
+ * Copyright (c) 2013 Cedric Dumoulin.
+ *
+ *
+ * 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:
+ * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.layers.stackmodel.exprmatcher;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.List;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.papyrus.layers.stackmodel.LayersException;
+import org.junit.After;
+import org.junit.Assume;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Categories.ExcludeCategory;
+
+/**
+ * @author cedric dumoulin
+ *
+ */
+public class ExpressionMatcherTest {
+
+ /**
+ * Name of the plugin that is created.
+ */
+ final protected String PLUGIN_PROJECT_NAME = "org.eclipse.papyrus.layers.stackmodel";
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ /**
+ * @param modelFileName
+ * @param modelNotationKey
+ * @return
+ */
+ private Resource getResource(String modelFileName, String modelNotationKey) {
+ ResourceSet resourceSet = new ResourceSetImpl();
+
+
+ URI uri = URI.createPlatformPluginURI(PLUGIN_PROJECT_NAME+ modelFileName + "." + modelNotationKey, true);
+// URI uri = URI.createPlatformPluginURI("/org.eclipse.papyrus.layers.stackmodel/model/layers.notation", true);
+
+
+ System.out.println("URI=" + uri.toString());
+ Resource resource = resourceSet.getResource(uri, true);
+ return resource;
+ }
+
+ /**
+ * Get the diagram contained in the specified resource.
+ * @param modelFileName
+ * @return
+ */
+ private Diagram getDiagram( String modelFileName ) {
+
+ // Get the Resource containing the diagram
+ EObject root = getResource(modelFileName, "notation").getContents().get(0);
+
+ return (Diagram)root;
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.exprmatcher.ExpressionMatcher#ExpressionMatcher(java.lang.String, java.util.List)}.
+ * @throws LayersException
+ */
+ @Test
+ public void testExpressionMatcher() throws LayersException {
+
+
+ String expression = "self.oclIsKindOf(Shape)";
+// "self.oclIsKindOf(Shape)",
+// "self.oclIsKindOf(Shape) and self.oclAsType(Shape).visible = true",
+// "self.oclAsType(Shape).visible = true",
+
+ String modelFileName = "/test/models/model1";
+ Diagram diagram = getDiagram(modelFileName);
+ List<EObject> searchRoots = diagram.getChildren();
+
+ ExpressionMatcher expressionMatcher = new ExpressionMatcher(expression, searchRoots);
+ assertNotNull("object created", expressionMatcher);
+
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.exprmatcher.ExpressionMatcher#ExpressionMatcher(java.lang.String, java.util.List)}.
+ * @throws LayersException
+ */
+ @Test
+ public void testExpressionMatcherWithWrongExpr() throws LayersException {
+
+ // Wrong expr
+ String expression = "self.o";
+
+ String modelFileName = "/test/models/model1";
+ Diagram diagram = getDiagram(modelFileName);
+ List<EObject> searchRoots = diagram.getChildren();
+
+
+ ExpressionMatcher expressionMatcher;
+ try {
+ expressionMatcher = new ExpressionMatcher(expression, searchRoots);
+ } catch (Exception e) {
+ // shoul have one
+ return;
+ }
+ fail("An exception should be thrown");
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.exprmatcher.ExpressionMatcher#refreshMatchingElements()}.
+ * @throws LayersException
+ */
+ @Test
+ public void testRefreshMatchingElements() throws LayersException {
+ String expression = "self.oclIsKindOf(Shape)";
+// "self.oclIsKindOf(Shape)",
+// "self.oclIsKindOf(Shape) and self.oclAsType(Shape).visible = true",
+// "self.oclAsType(Shape).visible = true",
+
+ String modelFileName = "/test/models/model1";
+ Diagram diagram = getDiagram(modelFileName);
+ List<EObject> searchRoots = diagram.getChildren();
+
+ ExpressionMatcher expressionMatcher = new ExpressionMatcher(expression, searchRoots);
+ assertNotNull("object created", expressionMatcher);
+ assertTrue( "elements founds", expressionMatcher.getMatchingElements().size()>0 );
+
+ // Call refresh
+ expressionMatcher.refreshMatchingElements();
+ assertEquals( "elements founds", 20, expressionMatcher.getMatchingElements().size() );
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.exprmatcher.ExpressionMatcher#getExpression()}.
+ * @throws LayersException
+ */
+ @Test
+ public void testGetExpression() throws LayersException {
+ String expression = "self.oclIsKindOf(Shape)";
+
+ String modelFileName = "/test/models/model1";
+ Diagram diagram = getDiagram(modelFileName);
+ List<EObject> searchRoots = diagram.getChildren();
+
+ ExpressionMatcher expressionMatcher = new ExpressionMatcher(expression, searchRoots);
+
+ assertEquals("expression found", expression, expressionMatcher.getExpression());
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.exprmatcher.ExpressionMatcher#setExpression(java.lang.String)}.
+ * @throws LayersException
+ */
+ @Test
+ public void testSetExpression() throws LayersException {
+ String expression = "self.oclIsKindOf(Shape)";
+// "self.oclIsKindOf(Shape)",
+// "self.oclIsKindOf(Shape) and self.oclAsType(Shape).visible = true",
+// "self.oclAsType(Shape).visible = true",
+
+ String modelFileName = "/test/models/model1";
+ Diagram diagram = getDiagram(modelFileName);
+ List<EObject> searchRoots = diagram.getChildren();
+
+ ExpressionMatcher expressionMatcher = new ExpressionMatcher(expression, searchRoots);
+ assertNotNull("object created", expressionMatcher);
+ assertTrue( "elements founds", expressionMatcher.getMatchingElements().size()>0 );
+
+ // Set the expression
+ expressionMatcher.setExpression("self.oclAsType(Shape).visible = true");
+ assertTrue( "elements founds", expressionMatcher.getMatchingElements().size()>0 );
+
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.exprmatcher.ExpressionMatcher#getMatchingElements()}.
+ * @throws LayersException
+ */
+ @Test
+ public void testGetMatchingElements() throws LayersException {
+ String expression = "self.oclIsKindOf(Shape)";
+// "self.oclIsKindOf(Shape)",
+// "self.oclIsKindOf(Shape) and self.oclAsType(Shape).visible = true",
+// "self.oclAsType(Shape).visible = true",
+
+ String modelFileName = "/test/models/model1";
+ Diagram diagram = getDiagram(modelFileName);
+ List<EObject> searchRoots = diagram.getChildren();
+
+ ExpressionMatcher expressionMatcher = new ExpressionMatcher(expression, searchRoots);
+ assertNotNull("object created", expressionMatcher);
+ assertTrue( "elements founds", expressionMatcher.getMatchingElements().size()>0 );
+ assertEquals( "elements founds", 20, expressionMatcher.getMatchingElements().size() );
+ }
+
+ /**
+ * Test if the resource needed for the test exists.
+ *
+ * @throws ModelMultiException
+ */
+ @Test
+ public void testResourceExist() throws IOException, CoreException {
+
+ String modelNotationKey = "notation";
+ String modelUMLKey = "uml";
+
+ String modelFileName = "/test/models/model1";
+
+ Resource resource = getResource(modelFileName, modelNotationKey);
+
+ assertNotNull("modelFile exist", resource);
+ assertTrue("model is loaded", !resource.getContents().isEmpty());
+ }
+
+ /**
+ * Test if the resource needed for the test exists.
+ *
+ * @throws ModelMultiException
+ */
+ @Test
+ public void testDiagramExist() throws IOException, CoreException {
+
+ String modelFileName = "/test/models/model1";
+ Diagram diagram = getDiagram(modelFileName);
+
+ assertNotNull("Diagram exist", diagram);
+ }
+
+
+}
diff --git a/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/util/FakeNotifyingListListener.java b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/util/FakeNotifyingListListener.java
new file mode 100644
index 00000000000..cfc45543aca
--- /dev/null
+++ b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/util/FakeNotifyingListListener.java
@@ -0,0 +1,41 @@
+/*****************************************************************************
+ * Copyright (c) 2013 Cedric Dumoulin.
+ *
+ *
+ * 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:
+ * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.layers.stackmodel.util;
+
+import com.google.common.eventbus.Subscribe;
+
+/**
+ * A class listening on events, and recording them.
+ *
+ * @author cedric dumoulin
+ *
+ */
+public class FakeNotifyingListListener<E> {
+
+ /**
+ * Trace object tracking events
+ */
+ public TriggeredEventTraces<NotyfyingList<E>.NotifyingListEvent> traces = new TriggeredEventTraces<NotyfyingList<E>.NotifyingListEvent>();
+
+
+ /**
+ * Method listeneing to the list event.
+ * @param event
+ */
+ @Subscribe
+ public void elementAddedToList(NotyfyingList<E>.NotifyingListEvent event) {
+ traces.addTrace("elementAddedToList", event);
+ }
+}
diff --git a/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/util/NotyfyingListTest.java b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/util/NotyfyingListTest.java
new file mode 100644
index 00000000000..5d4b0b4893e
--- /dev/null
+++ b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/util/NotyfyingListTest.java
@@ -0,0 +1,447 @@
+/*****************************************************************************
+ * Copyright (c) 2013 Cedric Dumoulin.
+ *
+ *
+ * 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:
+ * Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.layers.stackmodel.util;
+
+import static org.junit.Assert.*;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.Lists;
+
+/**
+ * @author dumoulin
+ *
+ */
+public class NotyfyingListTest {
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ }
+
+ /**
+ * @throws java.lang.Exception
+ */
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.util.NotyfyingList#NotyfyingList(java.util.List)}.
+ */
+ @Test
+ public void testNotyfyingList() {
+
+ List<String> list = Lists.newArrayList( "red", "orange", "yellow", "green", "blue", "purple");
+
+ NotyfyingList<String> notyfyingList = new NotyfyingList<String>(list);
+
+ assertNotNull("Object created", notyfyingList);
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.util.NotyfyingList#delegate()}.
+ */
+ @Test
+ public void testDelegate() {
+ List<String> list = Lists.newArrayList( "red", "orange", "yellow", "green", "blue", "purple");
+
+ NotyfyingList<String> notyfyingList = new NotyfyingList<String>(list);
+
+ assertSame("delegate is set", list, notyfyingList.delegate() );
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.util.NotyfyingList#getEventBus()}.
+ */
+ @Test
+ public void testGetEventBus() {
+ List<String> list = Lists.newArrayList( "red", "orange", "yellow", "green", "blue", "purple");
+
+ NotyfyingList<String> notyfyingList = new NotyfyingList<String>(list);
+
+ assertNotNull("eventBus found", notyfyingList.getEventBus() );
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.util.NotyfyingList#add(java.lang.Object)}.
+ */
+ @Test
+ public void testAddE() {
+ List<String> list = Lists.newArrayList( "red", "orange", "yellow", "green", "blue", "purple");
+ String addedElement = "purple";
+ int listSizeBefore = list.size();
+ int expectedListSizeAfter = listSizeBefore+1;
+
+ FakeNotifyingListListener<String> notifyingListListener = new FakeNotifyingListListener<String>();
+ NotyfyingList<String> notyfyingList = new NotyfyingList<String>(list);
+
+ notyfyingList.getEventBus().register(notifyingListListener );
+
+ // Action
+ notyfyingList.add(addedElement);
+
+ // Assertions
+ assertSame("event propagated", 1, notifyingListListener.traces.size());
+ assertTrue("event addedElement is correct", notifyingListListener.traces.get(0).notifier.getAddedElements().contains(addedElement));
+
+ Collection<? extends String> eventAddedElements = notifyingListListener.traces.get(0).notifier.getAddedElements();
+ assertEquals("event elements size", 1 , eventAddedElements.size());
+
+ assertEquals("result list size", expectedListSizeAfter , notyfyingList.size());
+ assertEquals("result list size", expectedListSizeAfter , list.size());
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.util.NotyfyingList#addAll(java.util.Collection)}.
+ */
+ @Test
+ public void testAddAllCollectionOfQextendsE() {
+ List<String> list = Lists.newArrayList( "red", "orange", "yellow");
+ List<String> addedElements = Lists.newArrayList( "green", "blue", "purple");
+ int listSizeBefore = list.size();
+ int expectedListSizeAfter = listSizeBefore+addedElements.size();
+
+ NotyfyingList<String> notyfyingList = new NotyfyingList<String>(list);
+ FakeNotifyingListListener<String> notifyingListListener = new FakeNotifyingListListener<String>();
+
+ notyfyingList.getEventBus().register(notifyingListListener );
+
+ // Action
+ notyfyingList.addAll(addedElements);
+
+ assertSame("event propagated", 1, notifyingListListener.traces.size());
+
+ Collection<? extends String> eventAddedElements = notifyingListListener.traces.get(0).notifier.getAddedElements();
+ assertEquals("event elements size", addedElements.size() , eventAddedElements.size());
+
+ assertEquals("result list size", expectedListSizeAfter , notyfyingList.size());
+ assertEquals("result list size", expectedListSizeAfter , list.size());
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.util.NotyfyingList#add(int, java.lang.Object)}.
+ */
+ @Test
+ public void testAddIntE() {
+ List<String> list = Lists.newArrayList( "red", "orange", "yellow", "green", "blue", "purple");
+ String addedElement = "purple";
+ int listSizeBefore = list.size();
+ int expectedListSizeAfter = listSizeBefore+1;
+
+ FakeNotifyingListListener<String> notifyingListListener = new FakeNotifyingListListener<String>();
+ NotyfyingList<String> notyfyingList = new NotyfyingList<String>(list);
+
+ notyfyingList.getEventBus().register(notifyingListListener );
+
+ // Action
+ notyfyingList.add(3, addedElement);
+
+ // Assertions
+ assertSame("event propagated", 1, notifyingListListener.traces.size());
+ assertTrue("event addedElement is correct", notifyingListListener.traces.get(0).notifier.getAddedElements().contains(addedElement));
+
+ Collection<? extends String> eventAddedElements = notifyingListListener.traces.get(0).notifier.getAddedElements();
+ assertEquals("event elements size", 1 , eventAddedElements.size());
+
+ assertEquals("result list size", expectedListSizeAfter , notyfyingList.size());
+ assertEquals("result list size", expectedListSizeAfter , list.size());
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.util.NotyfyingList#addAll(int, java.util.Collection)}.
+ */
+ @Test
+ public void testAddAllIntCollectionOfQextendsE() {
+ List<String> list = Lists.newArrayList( "red", "orange", "yellow");
+ List<String> addedElements = Lists.newArrayList( "green", "blue", "purple");
+ int listSizeBefore = list.size();
+ int expectedListSizeAfter = listSizeBefore+addedElements.size();
+
+ NotyfyingList<String> notyfyingList = new NotyfyingList<String>(list);
+ FakeNotifyingListListener<String> notifyingListListener = new FakeNotifyingListListener<String>();
+
+ notyfyingList.getEventBus().register(notifyingListListener );
+
+ // Action
+ notyfyingList.addAll(2, addedElements);
+
+ assertSame("event propagated", 1, notifyingListListener.traces.size());
+
+ Collection<? extends String> eventAddedElements = notifyingListListener.traces.get(0).notifier.getAddedElements();
+ assertEquals("event elements size", addedElements.size() , eventAddedElements.size());
+
+ assertEquals("result list size", expectedListSizeAfter , notyfyingList.size());
+ assertEquals("result list size", expectedListSizeAfter , list.size());
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.util.NotyfyingList#remove(int)}.
+ */
+ @Test
+ public void testRemoveInt() {
+ List<String> list = Lists.newArrayList( "red", "orange", "yellow", "green", "blue", "purple");
+ String removedElement = "purple";
+ int listSizeBefore = list.size();
+ int expectedListSizeAfter = listSizeBefore-1;
+
+ FakeNotifyingListListener<String> notifyingListListener = new FakeNotifyingListListener<String>();
+ NotyfyingList<String> notyfyingList = new NotyfyingList<String>(list);
+
+ notyfyingList.getEventBus().register(notifyingListListener );
+
+ // Action
+ notyfyingList.remove( removedElement);
+
+ // Assertions
+ assertSame("event propagated", 1, notifyingListListener.traces.size());
+ NotyfyingList<String>.NotifyingListEvent event = notifyingListListener.traces.get(0).notifier;
+ assertTrue("event removesElement is correct", event.getRemovedElements().contains(removedElement));
+
+ assertEquals("addedElement size", 0, event.getAddedElements().size() );
+ assertEquals("removedElement size", 1, event.getRemovedElements().size() );
+
+ assertEquals("result list size", expectedListSizeAfter , notyfyingList.size());
+ assertEquals("result list size", expectedListSizeAfter , list.size());
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.util.NotyfyingList#remove(java.lang.Object)}.
+ */
+ @Test
+ public void testRemoveObject() {
+ List<String> list = Lists.newArrayList( "red", "orange", "yellow", "green", "blue", "purple");
+ int removedIndex = 3;
+ String removedElement = list.get(removedIndex);
+ int listSizeBefore = list.size();
+ int expectedListSizeAfter = listSizeBefore-1;
+
+ FakeNotifyingListListener<String> notifyingListListener = new FakeNotifyingListListener<String>();
+ NotyfyingList<String> notyfyingList = new NotyfyingList<String>(list);
+
+ notyfyingList.getEventBus().register(notifyingListListener );
+
+ // Action
+ notyfyingList.remove( removedIndex);
+
+ // Assertions
+ assertSame("event propagated", 1, notifyingListListener.traces.size());
+ NotyfyingList<String>.NotifyingListEvent event = notifyingListListener.traces.get(0).notifier;
+ assertTrue("event removesElement is correct", event.getRemovedElements().contains(removedElement));
+
+ assertEquals("addedElement size", 0, event.getAddedElements().size() );
+ assertEquals("removedElement size", 1, event.getRemovedElements().size() );
+
+ assertEquals("result list size", expectedListSizeAfter , notyfyingList.size());
+ assertEquals("result list size", expectedListSizeAfter , list.size());
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.util.NotyfyingList#removeAll(java.util.Collection)}.
+ */
+ @Test
+ public void testRemoveAllCollectionOfQ() {
+ List<String> list = Lists.newArrayList( "red", "orange", "yellow", "green", "blue", "purple");
+ List<String> elementsToRemove = Lists.newArrayList( "orange", "green", "purple");
+ int listSizeBefore = list.size();
+ int removedElementsCount = elementsToRemove.size();
+ int expectedListSizeAfter = listSizeBefore-removedElementsCount;
+
+ FakeNotifyingListListener<String> notifyingListListener = new FakeNotifyingListListener<String>();
+ NotyfyingList<String> notyfyingList = new NotyfyingList<String>(list);
+
+ notyfyingList.getEventBus().register(notifyingListListener );
+
+ // Action
+ notyfyingList.removeAll( elementsToRemove);
+
+ // Assertions
+ assertSame("event propagated", 1, notifyingListListener.traces.size());
+
+ NotyfyingList<String>.NotifyingListEvent event = notifyingListListener.traces.get(0).notifier;
+ assertTrue("event removesElement is correct", event.getRemovedElements().containsAll(elementsToRemove));
+
+ assertEquals("addedElement size", 0, event.getAddedElements().size() );
+ assertEquals("removedElement size", removedElementsCount, event.getRemovedElements().size() );
+
+ assertEquals("result list size", expectedListSizeAfter , notyfyingList.size());
+ assertEquals("result list size", expectedListSizeAfter , list.size());
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.util.NotyfyingList#removeAll(java.util.Collection)}.
+ */
+ @Test
+ public void testRemoveAllCollectionOfQWithTooMuchElements() {
+ List<String> list = Lists.newArrayList( "red", "orange", "yellow", "green", "blue", "purple");
+ List<String> elementsToRemove = Lists.newArrayList( "orange", "falseColor1", "green", "purple", "falseColor");
+ int listSizeBefore = list.size();
+ int removedElementsCount = elementsToRemove.size()-2; // 2 = falseColors
+ int expectedListSizeAfter = listSizeBefore-removedElementsCount;
+
+ FakeNotifyingListListener<String> notifyingListListener = new FakeNotifyingListListener<String>();
+ NotyfyingList<String> notyfyingList = new NotyfyingList<String>(list);
+
+ notyfyingList.getEventBus().register(notifyingListListener );
+
+ // Action
+ notyfyingList.removeAll( elementsToRemove);
+
+ // Assertions
+ assertSame("event propagated", 1, notifyingListListener.traces.size());
+
+ NotyfyingList<String>.NotifyingListEvent event = notifyingListListener.traces.get(0).notifier;
+// assertTrue("event removesElement is correct", event.getRemovedElements().containsAll(elementsToRemove));
+ assertTrue("removed elements are from the required list", elementsToRemove.containsAll(event.getRemovedElements()));
+
+ assertEquals("addedElement size", 0, event.getAddedElements().size() );
+ assertEquals("removedElement size", removedElementsCount, event.getRemovedElements().size() );
+
+ assertEquals("result list size", expectedListSizeAfter , notyfyingList.size());
+ assertEquals("result list size", expectedListSizeAfter , list.size());
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.util.NotyfyingList#resetTo(java.util.Collection)}.
+ */
+ @Test
+ public void testResetTo() {
+ List<String> list = Lists.newArrayList( "red", "orange", "yellow", "green", "blue", "purple");
+
+ List<String> listToReset = Lists.newArrayList( "orange", "green", "purple", "falseColor");
+ List<String> expectedRemovedElement = Lists.newArrayList( "red", "yellow", "blue");
+ List<String> expectedAddedElement = Lists.newArrayList( "falseColor");
+
+ int expectedEventCount = 1;
+
+ FakeNotifyingListListener<String> notifyingListListener = new FakeNotifyingListListener<String>();
+ NotyfyingList<String> notyfyingList = new NotyfyingList<String>(list);
+
+ notyfyingList.getEventBus().register(notifyingListListener );
+
+ // Action
+ notyfyingList.resetTo(listToReset);
+
+ // Assertions
+ assertSame("event propagated", expectedEventCount, notifyingListListener.traces.size());
+ assertArrayEquals("List equals", listToReset.toArray(), notyfyingList.toArray());
+
+ NotyfyingList<String>.NotifyingListEvent event = notifyingListListener.traces.get(0).notifier;
+ assertArrayEquals("Removed list ok", expectedRemovedElement.toArray(), event.getRemovedElements().toArray());
+ assertArrayEquals("Added list ok", expectedAddedElement.toArray(), event.getAddedElements().toArray());
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.util.NotyfyingList#resetTo(java.util.Collection)}.
+ */
+ @Test
+ public void testResetToNoAdded() {
+ List<String> list = Lists.newArrayList( "red", "orange", "yellow", "green", "blue", "purple");
+
+ List<String> listToReset = Lists.newArrayList( "orange", "green", "purple");
+ List<String> expectedRemovedElement = Lists.newArrayList( "red", "yellow", "blue");
+ List<String> expectedAddedElement = Collections.emptyList();
+
+ int expectedEventCount = 1;
+
+ FakeNotifyingListListener<String> notifyingListListener = new FakeNotifyingListListener<String>();
+ NotyfyingList<String> notyfyingList = new NotyfyingList<String>(list);
+
+ notyfyingList.getEventBus().register(notifyingListListener );
+
+ // Action
+ notyfyingList.resetTo(listToReset);
+
+ // Assertions
+ assertSame("event propagated", expectedEventCount, notifyingListListener.traces.size());
+ assertArrayEquals("List equals", listToReset.toArray(), notyfyingList.toArray());
+
+ NotyfyingList<String>.NotifyingListEvent event = notifyingListListener.traces.get(0).notifier;
+ assertArrayEquals("Removed list ok", expectedRemovedElement.toArray(), event.getRemovedElements().toArray());
+ assertArrayEquals("Added list ok", expectedAddedElement.toArray(), event.getAddedElements().toArray());
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.util.NotyfyingList#resetTo(java.util.Collection)}.
+ */
+ @Test
+ public void testResetToNoRemoved() {
+ List<String> list = Lists.newArrayList( "red", "orange", "yellow", "green", "blue", "purple");
+
+ List<String> listToReset = Lists.newArrayList( "red", "orange", "yellow", "green", "blue", "purple", "falseColor");
+ List<String> expectedRemovedElement = Collections.emptyList();
+ List<String> expectedAddedElement = Lists.newArrayList( "falseColor");
+
+ int expectedEventCount = 1;
+
+ FakeNotifyingListListener<String> notifyingListListener = new FakeNotifyingListListener<String>();
+ NotyfyingList<String> notyfyingList = new NotyfyingList<String>(list);
+
+ notyfyingList.getEventBus().register(notifyingListListener );
+
+ // Action
+ notyfyingList.resetTo(listToReset);
+
+ // Assertions
+ assertSame("event propagated", expectedEventCount, notifyingListListener.traces.size());
+ assertArrayEquals("List equals", listToReset.toArray(), notyfyingList.toArray());
+
+ NotyfyingList<String>.NotifyingListEvent event = notifyingListListener.traces.get(0).notifier;
+ assertArrayEquals("Removed list ok", expectedRemovedElement.toArray(), event.getRemovedElements().toArray());
+ assertArrayEquals("Added list ok", expectedAddedElement.toArray(), event.getAddedElements().toArray());
+ }
+
+ /**
+ * Test method for {@link org.eclipse.papyrus.layers.stackmodel.util.NotyfyingList#resetTo(java.util.Collection)}.
+ */
+ @Test
+ public void testResetToNoChanges() {
+ List<String> list = Lists.newArrayList( "red", "orange", "yellow", "green", "blue", "purple");
+
+ List<String> listToReset = Lists.newArrayList( "red", "orange", "yellow", "green", "blue", "purple");
+ List<String> expectedRemovedElement = Collections.emptyList();
+ List<String> expectedAddedElement = Collections.emptyList();
+
+ // No change, no events
+ int expectedEventCount = 0;
+
+ FakeNotifyingListListener<String> notifyingListListener = new FakeNotifyingListListener<String>();
+ NotyfyingList<String> notyfyingList = new NotyfyingList<String>(list);
+
+ notyfyingList.getEventBus().register(notifyingListListener );
+
+ // Action
+ notyfyingList.resetTo(listToReset);
+
+ // Assertions
+ assertSame("event propagated", expectedEventCount, notifyingListListener.traces.size());
+ assertArrayEquals("List equals", listToReset.toArray(), notyfyingList.toArray());
+
+// NotyfyingList<String>.NotifyingListEvent event = notifyingListListener.traces.get(0).notifier;
+// assertArrayEquals("Removed list ok", expectedRemovedElement.toArray(), event.getRemovedElements().toArray());
+// assertArrayEquals("Added list ok", expectedAddedElement.toArray(), event.getAddedElements().toArray());
+ }
+
+
+
+}
diff --git a/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/util/TriggeredEventTraces.java b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/util/TriggeredEventTraces.java
new file mode 100644
index 00000000000..9d17708bdd5
--- /dev/null
+++ b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/util/TriggeredEventTraces.java
@@ -0,0 +1,131 @@
+/*******************************************************************************
+ * Copyright (c) 2013 CEA LIST.
+ * 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:
+ * Cedric Dumoulin - cedric.dumoulin@lifl.fr
+ ******************************************************************************/
+package org.eclipse.papyrus.layers.stackmodel.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+/**
+ * This class is used to record a list of traces.
+ * Traces are for event triggered by a method.
+ * This class is for tests purpose.
+ *
+ * @author cedric dumoulin
+ *
+ */
+public class TriggeredEventTraces<E> {
+
+
+ /**
+ * List of recorded events
+ */
+ public List<TriggeredEvent> traces = new ArrayList<TriggeredEvent>();
+
+ /**
+ * Clear all traces.
+ */
+ public void clear() {
+ traces.clear();
+ }
+
+ /**
+ * Add a trace to the list of traces
+ * @param name
+ * @param notification
+ */
+ public void addTrace(String name, E notification) {
+ traces.add(new TriggeredEvent(name, notification));
+ }
+
+ /**
+ * Return true if one of the trace has the specified name.
+ * @param name
+ * @return
+ */
+ public boolean contains(String name) {
+ if( name == null)
+ return false;
+
+ for(TriggeredEvent event : traces) {
+ if(name.equals(event.name))
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Return true if one of the trace has the specified name.
+ * @param name Name of the event to found
+ * @return the first event with the specified name, or null if nothing is found.
+ */
+ public TriggeredEvent getFirstEvent(String name) {
+ if( name == null)
+ return null;
+
+ for(TriggeredEvent event : traces) {
+ if(name.equals(event.name))
+ return event;
+ }
+ return null;
+ }
+
+
+ /**
+ * A record of an event
+ *
+ */
+ public class TriggeredEvent {
+ public String name;
+ public E notifier;
+ public Object object;
+ /**
+ * Constructor.
+ *
+ * @param name
+ * @param notifier
+ */
+ public TriggeredEvent(String name, E notifier) {
+ this.name = name;
+ this.notifier = notifier;
+ }
+ /**
+ * Constructor.
+ *
+ * @param name
+ * @param object
+ */
+ public TriggeredEvent(String name, E notifier, Object object) {
+ this.name = name;
+ this.notifier = notifier;
+ this.object = object;
+ }
+
+ }
+
+
+ /**
+ * Return the number of traces
+ * @return
+ */
+ public int size() {
+ return traces.size();
+ }
+
+ /**
+ * Get the specified trace.
+ * @param i
+ * @return
+ */
+ public TriggeredEvent get(int index) {
+ return traces.get(index);
+ }
+}

Back to the top