Skip to content

Package: Strategy_Test$2

Strategy_Test$2

nameinstructionbranchcomplexitylinemethod
handleNotification(Notification)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
{...}
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * jfaltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.changebroker.test;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.assertTrue;
18: import static org.mockito.Mockito.mock;
19: import static org.mockito.Mockito.when;
20:
21: import java.util.LinkedHashSet;
22: import java.util.Set;
23:
24: import org.eclipse.emf.common.notify.Notification;
25: import org.eclipse.emf.ecore.EAnnotation;
26: import org.eclipse.emf.ecore.EAttribute;
27: import org.eclipse.emf.ecore.EcoreFactory;
28: import org.eclipse.emf.ecore.EcorePackage;
29: import org.eclipse.emf.ecp.changebroker.internal.ChangeBrokerImpl;
30: import org.eclipse.emf.ecp.changebroker.spi.AbstractNotificationProvider;
31: import org.eclipse.emf.ecp.changebroker.spi.ChangeObserver;
32: import org.eclipse.emf.ecp.changebroker.spi.ReadOnlyChangeObserver;
33: import org.junit.Before;
34: import org.junit.Test;
35:
36: /**
37: * Tests all strategies.
38: *
39: * @author jfaltermeier
40: *
41: */
42: public class Strategy_Test {
43:
44:         private ChangeObserver emfObserver;
45:         private ReadOnlyChangeObserver readOnlyEMFObserver;
46:         private ChangeBrokerImpl broker;
47:         private Set<ChangeObserver> observers;
48:         private TestNotificationProvider notificationProvider;
49:
50:         @Before
51:         public void before() {
52:                 broker = new ChangeBrokerImpl();
53:                 observers = new LinkedHashSet<ChangeObserver>();
54:                 emfObserver = new ChangeObserver() {
55:                         @Override
56:                         public void handleNotification(Notification notification) {
57:                                 observers.add(this);
58:                         }
59:                 };
60:                 readOnlyEMFObserver = new ReadOnlyChangeObserver() {
61:                         @Override
62:                         public void handleNotification(Notification notification) {
63:                                 observers.add(this);
64:                         }
65:                 };
66:                 notificationProvider = new TestNotificationProvider();
67:                 broker.addNotificationProvider(notificationProvider);
68:         }
69:
70:         @Test
71:         public void testNoStrategyGetObservers() {
72:                 // setup
73:                 broker.subscribe(emfObserver);
74:                 broker.subscribe(readOnlyEMFObserver);
75:
76:                 // act
77:                 final Notification notification = mock(Notification.class);
78:                 when(notification.getNotifier()).thenReturn(EcoreFactory.eINSTANCE.createEAttribute());
79:                 notifyBroker(notification);
80:
81:                 // assert
82:                 assertEquals(2, observers.size());
83:                 assertTrue(observers.contains(emfObserver));
84:                 assertTrue(observers.contains(readOnlyEMFObserver));
85:         }
86:
87:         @Test
88:         public void testEClassStrategyGetObserversRightEClass() {
89:                 // setup
90:
91:                 broker.subscribeToEClass(emfObserver, EcorePackage.eINSTANCE.getEAttribute());
92:                 broker.subscribeToEClass(readOnlyEMFObserver, EcorePackage.eINSTANCE.getEAttribute());
93:
94:                 // act
95:                 final Notification notification = mock(Notification.class);
96:                 when(notification.getNotifier()).thenReturn(EcoreFactory.eINSTANCE.createEAttribute());
97:                 notifyBroker(notification);
98:
99:                 // assert
100:                 assertEquals(2, observers.size());
101:                 assertTrue(observers.contains(emfObserver));
102:                 assertTrue(observers.contains(readOnlyEMFObserver));
103:         }
104:
105:         @Test
106:         public void testEClassStrategyGetObserversWrongEClass() {
107:                 // setup
108:
109:                 broker.subscribeToEClass(emfObserver, EcorePackage.eINSTANCE.getEAttribute());
110:                 broker.subscribeToEClass(readOnlyEMFObserver, EcorePackage.eINSTANCE.getEAttribute());
111:
112:                 // act
113:                 final Notification notification = mock(Notification.class);
114:                 when(notification.getNotifier()).thenReturn(EcoreFactory.eINSTANCE.createEReference());
115:                 notifyBroker(notification);
116:
117:                 // assert
118:                 assertEquals(0, observers.size());
119:         }
120:
121:         @Test
122:         public void testEClassStrategyGetObserversSuperEClass() {
123:                 // setup
124:
125:                 broker.subscribeToEClass(emfObserver, EcorePackage.eINSTANCE.getEStructuralFeature());
126:                 broker.subscribeToEClass(readOnlyEMFObserver, EcorePackage.eINSTANCE.getEStructuralFeature());
127:
128:                 // act
129:                 final Notification notification = mock(Notification.class);
130:                 when(notification.getNotifier()).thenReturn(EcoreFactory.eINSTANCE.createEReference());
131:                 notifyBroker(notification);
132:
133:                 // assert
134:                 assertEquals(2, observers.size());
135:                 assertTrue(observers.contains(emfObserver));
136:                 assertTrue(observers.contains(readOnlyEMFObserver));
137:         }
138:
139:         @Test
140:         public void testEClassStrategyGetObserversOnlyReadOnlyWrongEClass() {
141:                 // setup
142:
143:                 broker.subscribeToEClass(emfObserver, EcorePackage.eINSTANCE.getEAttribute());
144:                 broker.subscribeToEClass(readOnlyEMFObserver, EcorePackage.eINSTANCE.getEAttribute());
145:
146:                 // act
147:                 final Notification notification = mock(Notification.class);
148:                 when(notification.getNotifier()).thenReturn(EcoreFactory.eINSTANCE.createEReference());
149:                 notifyBroker(notification);
150:
151:                 // assert
152:                 assertEquals(0, observers.size());
153:         }
154:
155:         @Test
156:         public void testContainingEClassStrategyGetObserversRightEClass() {
157:                 // setup
158:
159:                 broker.subscribeToTree(emfObserver, EcorePackage.eINSTANCE.getEAttribute());
160:                 broker.subscribeToTree(readOnlyEMFObserver, EcorePackage.eINSTANCE.getEAttribute());
161:
162:                 // act
163:                 final EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
164:                 final EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
165:                 eAttribute.getEAnnotations().add(eAnnotation);
166:                 final Notification notification = mock(Notification.class);
167:                 when(notification.getNotifier()).thenReturn(eAnnotation);
168:                 notifyBroker(notification);
169:
170:                 // assert
171:                 assertEquals(2, observers.size());
172:                 assertTrue(observers.contains(emfObserver));
173:                 assertTrue(observers.contains(readOnlyEMFObserver));
174:         }
175:
176:         @Test
177:         public void testContainingEClassStrategyGetObserversWrongEClass() {
178:                 // setup
179:
180:                 broker.subscribeToTree(emfObserver, EcorePackage.eINSTANCE.getEReference());
181:                 broker.subscribeToTree(readOnlyEMFObserver, EcorePackage.eINSTANCE.getEReference());
182:
183:                 // act
184:                 final EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
185:                 final EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
186:                 eAttribute.getEAnnotations().add(eAnnotation);
187:                 final Notification notification = mock(Notification.class);
188:                 when(notification.getNotifier()).thenReturn(eAnnotation);
189:                 notifyBroker(notification);
190:
191:                 // assert
192:                 assertEquals(0, observers.size());
193:         }
194:
195:         @Test
196:         public void testContainingEClassStrategyGetObserversSuperEClass() {
197:                 // setup
198:
199:                 broker.subscribeToTree(emfObserver, EcorePackage.eINSTANCE.getEStructuralFeature());
200:                 broker.subscribeToTree(readOnlyEMFObserver, EcorePackage.eINSTANCE.getEStructuralFeature());
201:
202:                 // act
203:                 final EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
204:                 final EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
205:                 eAttribute.getEAnnotations().add(eAnnotation);
206:                 final Notification notification = mock(Notification.class);
207:                 when(notification.getNotifier()).thenReturn(eAnnotation);
208:                 notifyBroker(notification);
209:
210:                 // assert
211:                 assertEquals(2, observers.size());
212:                 assertTrue(observers.contains(emfObserver));
213:                 assertTrue(observers.contains(readOnlyEMFObserver));
214:         }
215:
216:         @Test
217:         public void testContainingEClassStrategyGetObserversOnlyReadOnlyWrongEClass() {
218:                 // setup
219:
220:                 broker.subscribeToTree(emfObserver, EcorePackage.eINSTANCE.getEReference());
221:                 broker.subscribeToTree(readOnlyEMFObserver, EcorePackage.eINSTANCE.getEReference());
222:
223:                 // act
224:                 final EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
225:                 final EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
226:                 eAttribute.getEAnnotations().add(eAnnotation);
227:                 final Notification notification = mock(Notification.class);
228:                 when(notification.getNotifier()).thenReturn(eAnnotation);
229:                 notifyBroker(notification);
230:
231:                 // assert
232:                 assertEquals(0, observers.size());
233:         }
234:
235:         @Test
236:         public void testFeatureStrategyGetObserversRightFeature() {
237:                 // setup
238:
239:                 broker.subscribeToFeature(emfObserver, EcorePackage.eINSTANCE.getEModelElement_EAnnotations());
240:                 broker.subscribeToFeature(readOnlyEMFObserver, EcorePackage.eINSTANCE.getEModelElement_EAnnotations());
241:
242:                 // act
243:                 final Notification notification = mock(Notification.class);
244:                 when(notification.getFeature()).thenReturn(EcorePackage.eINSTANCE.getEModelElement_EAnnotations());
245:                 when(notification.getNotifier()).thenReturn(EcoreFactory.eINSTANCE.createEReference());
246:                 notifyBroker(notification);
247:
248:                 // assert
249:                 assertEquals(2, observers.size());
250:                 assertTrue(observers.contains(emfObserver));
251:                 assertTrue(observers.contains(readOnlyEMFObserver));
252:         }
253:
254:         @Test
255:         public void testFeatureStrategyGetObserversWrongFeature() {
256:                 // setup
257:
258:                 broker.subscribeToFeature(emfObserver, EcorePackage.eINSTANCE.getEModelElement_EAnnotations());
259:                 broker.subscribeToFeature(readOnlyEMFObserver, EcorePackage.eINSTANCE.getEModelElement_EAnnotations());
260:
261:                 // act
262:                 final Notification notification = mock(Notification.class);
263:                 when(notification.getFeature()).thenReturn(EcorePackage.eINSTANCE.getEStructuralFeature_Changeable());
264:                 when(notification.getNotifier()).thenReturn(EcoreFactory.eINSTANCE.createEReference());
265:                 notifyBroker(notification);
266:
267:                 // assert
268:                 assertEquals(0, observers.size());
269:         }
270:
271:         private void notifyBroker(Notification notification) {
272:                 notificationProvider.notifyAll(notification);
273:         }
274:
275:         private class TestNotificationProvider extends AbstractNotificationProvider {
276:                 public void notifyAll(Notification notification) {
277:                         notifyAllReceivers(notification);
278:                 }
279:         }
280:
281: }