Skip to content

Package: EMFKeyAttributeValueProperty_Test

EMFKeyAttributeValueProperty_Test

nameinstructionbranchcomplexitylinemethod
EMFKeyAttributeValueProperty_Test()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
setUp()
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
tearDown()
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
testDoGetValueEObjectKeyPresent()
M: 0 C: 88
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 20
100%
M: 0 C: 1
100%
testDoGetValueEmptyList()
M: 0 C: 38
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
testDoGetValueKeyNotPresent()
M: 0 C: 69
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 16
100%
M: 0 C: 1
100%
testDoGetValueKeyPresent()
M: 0 C: 100
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 23
100%
M: 0 C: 1
100%
testDoSetValueNewValueDatabindingFails()
M: 5 C: 48
91%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 9
82%
M: 0 C: 1
100%
testDoSetValueNewValueDoesntContainKey()
M: 5 C: 58
92%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 13
87%
M: 0 C: 1
100%
testDoSetValueNoOldKeyPresent()
M: 0 C: 119
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 28
100%
M: 0 C: 1
100%
testDoSetValueOldKeyPresent()
M: 0 C: 119
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 28
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2016 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: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.core.services.databinding.keyattribute;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.assertFalse;
18: import static org.junit.Assert.assertTrue;
19: import static org.mockito.Mockito.mock;
20: import static org.mockito.Mockito.when;
21:
22: import org.eclipse.emf.common.command.BasicCommandStack;
23: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
24: import org.eclipse.emf.ecore.InternalEObject;
25: import org.eclipse.emf.ecp.view.spi.model.VFeaturePathDomainModelReference;
26: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
27: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
28: import org.eclipse.emf.edit.domain.EditingDomain;
29: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
30: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.B;
31: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.C;
32: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.D;
33: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.TestFactory;
34: import org.eclipse.emfforms.core.services.databinding.testmodel.test.model.TestPackage;
35: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
36: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
37: import org.junit.After;
38: import org.junit.Before;
39: import org.junit.Test;
40:
41: /**
42: * JUnit test cases for {@link EMFKeyAttributeValueProperty}.
43: *
44: * @author Lucas Koehler
45: *
46: */
47: public class EMFKeyAttributeValueProperty_Test {
48:
49:         private static final String WRONG_TEST_KEY = "WrongTestKey"; //$NON-NLS-1$
50:         private static final String CORRECT_TEST_KEY = "CorrectTestKey"; //$NON-NLS-1$
51:
52:         /**
53:          * @throws java.lang.Exception
54:          */
55:         @Before
56:         public void setUp() throws Exception {
57:         }
58:
59:         /**
60:          * @throws java.lang.Exception
61:          */
62:         @After
63:         public void tearDown() throws Exception {
64:         }
65:
66:         /**
67:          * Test method for
68:          * {@link org.eclipse.emfforms.internal.core.services.databinding.keyattribute.EMFKeyAttributeValueProperty#doGetValue(java.lang.Object)}
69:          * .
70:          *
71:          * @throws DatabindingFailedException
72:          */
73:         @Test
74:         public void testDoGetValueKeyPresent() throws DatabindingFailedException {
75:                 final B b = TestFactory.eINSTANCE.createB();
76:                 final C cWithoutKey = TestFactory.eINSTANCE.createC();
77:                 final C cWithKey = TestFactory.eINSTANCE.createC();
78:                 final D dWithKey = TestFactory.eINSTANCE.createD();
79:                 final D dWithoutKey = TestFactory.eINSTANCE.createD();
80:
81:                 dWithKey.setX(CORRECT_TEST_KEY);
82:                 dWithoutKey.setX(WRONG_TEST_KEY);
83:
84:                 b.getCList().add(cWithoutKey);
85:                 b.getCList().add(cWithKey);
86:                 cWithKey.setD(dWithKey);
87:                 cWithoutKey.setD(dWithoutKey);
88:
89:                 final VFeaturePathDomainModelReference keyDMR = VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
90:                 keyDMR.getDomainModelEReferencePath().add(TestPackage.eINSTANCE.getC_D());
91:                 keyDMR.setDomainModelEFeature(TestPackage.eINSTANCE.getD_X());
92:
93:                 final EMFFormsDatabindingEMF databinding = mock(EMFFormsDatabindingEMF.class);
94:                 final Setting settingWithKey = ((InternalEObject) dWithKey).eSetting(TestPackage.eINSTANCE.getD_X());
95:                 final Setting settingWithoutKey = ((InternalEObject) dWithoutKey).eSetting(TestPackage.eINSTANCE.getD_X());
96:
97:                 when(databinding.getSetting(keyDMR, cWithKey)).thenReturn(settingWithKey);
98:                 when(databinding.getSetting(keyDMR, cWithoutKey)).thenReturn(settingWithoutKey);
99:
100:                 final EMFKeyAttributeValueProperty valueProperty = new EMFKeyAttributeValueProperty(mock(EditingDomain.class),
101:                         databinding, keyDMR, CORRECT_TEST_KEY, TestPackage.eINSTANCE.getB_CList());
102:
103:                 assertEquals(cWithKey, valueProperty.getValue(b));
104:         }
105:
106:         /**
107:          * Test method for
108:          * {@link org.eclipse.emfforms.internal.core.services.databinding.keyattribute.EMFKeyAttributeValueProperty#doGetValue(java.lang.Object)}
109:          * .
110:          *
111:          * @throws DatabindingFailedException
112:          */
113:         @Test
114:         public void testDoGetValueKeyNotPresent() throws DatabindingFailedException {
115:                 final B b = TestFactory.eINSTANCE.createB();
116:                 final C cWithoutKey = TestFactory.eINSTANCE.createC();
117:                 final D dWithoutKey = TestFactory.eINSTANCE.createD();
118:
119:                 dWithoutKey.setX(WRONG_TEST_KEY);
120:
121:                 b.getCList().add(cWithoutKey);
122:                 cWithoutKey.setD(dWithoutKey);
123:
124:                 final VFeaturePathDomainModelReference keyDMR = VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
125:                 keyDMR.getDomainModelEReferencePath().add(TestPackage.eINSTANCE.getC_D());
126:                 keyDMR.setDomainModelEFeature(TestPackage.eINSTANCE.getD_X());
127:
128:                 final EMFFormsDatabindingEMF databinding = mock(EMFFormsDatabindingEMF.class);
129:                 final Setting settingWithoutKey = ((InternalEObject) dWithoutKey).eSetting(TestPackage.eINSTANCE.getD_X());
130:
131:                 when(databinding.getSetting(keyDMR, cWithoutKey)).thenReturn(settingWithoutKey);
132:
133:                 final EMFKeyAttributeValueProperty valueProperty = new EMFKeyAttributeValueProperty(mock(EditingDomain.class),
134:                         databinding, keyDMR, CORRECT_TEST_KEY, TestPackage.eINSTANCE.getB_CList());
135:
136:                 assertEquals(null, valueProperty.getValue(b));
137:         }
138:
139:         /**
140:          * Test method for
141:          * {@link org.eclipse.emfforms.internal.core.services.databinding.keyattribute.EMFKeyAttributeValueProperty#doGetValue(java.lang.Object)}
142:          * .
143:          *
144:          * @throws DatabindingFailedException
145:          */
146:         @Test
147:         public void testDoGetValueEmptyList() throws DatabindingFailedException {
148:                 final B b = TestFactory.eINSTANCE.createB();
149:
150:                 final VFeaturePathDomainModelReference keyDMR = VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
151:                 keyDMR.getDomainModelEReferencePath().add(TestPackage.eINSTANCE.getC_D());
152:                 keyDMR.setDomainModelEFeature(TestPackage.eINSTANCE.getD_X());
153:
154:                 final EMFFormsDatabindingEMF databinding = mock(EMFFormsDatabindingEMF.class);
155:
156:                 final EMFKeyAttributeValueProperty valueProperty = new EMFKeyAttributeValueProperty(mock(EditingDomain.class),
157:                         databinding, keyDMR, CORRECT_TEST_KEY, TestPackage.eINSTANCE.getB_CList());
158:
159:                 assertEquals(null, valueProperty.getValue(b));
160:         }
161:
162:         /**
163:          * Test method for
164:          * {@link org.eclipse.emfforms.internal.core.services.databinding.keyattribute.EMFKeyAttributeValueProperty#doGetValue(java.lang.Object)}
165:          * .
166:          *
167:          * @throws DatabindingFailedException
168:          */
169:         @Test
170:         public void testDoGetValueEObjectKeyPresent() throws DatabindingFailedException {
171:                 final B b = TestFactory.eINSTANCE.createB();
172:                 final C cWithoutKey = TestFactory.eINSTANCE.createC();
173:                 final C cWithKey = TestFactory.eINSTANCE.createC();
174:                 final D dKey = TestFactory.eINSTANCE.createD();
175:                 final D dNoKey = TestFactory.eINSTANCE.createD();
176:
177:                 b.getCList().add(cWithoutKey);
178:                 b.getCList().add(cWithKey);
179:                 cWithKey.setD(dKey);
180:                 cWithoutKey.setD(dNoKey);
181:
182:                 final VFeaturePathDomainModelReference keyDMR = VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
183:                 keyDMR.setDomainModelEFeature(TestPackage.eINSTANCE.getC_D());
184:
185:                 final EMFFormsDatabindingEMF databinding = mock(EMFFormsDatabindingEMF.class);
186:                 final Setting settingWithKey = ((InternalEObject) cWithKey).eSetting(TestPackage.eINSTANCE.getC_D());
187:                 final Setting settingWithoutKey = ((InternalEObject) cWithoutKey).eSetting(TestPackage.eINSTANCE.getC_D());
188:
189:                 when(databinding.getSetting(keyDMR, cWithKey)).thenReturn(settingWithKey);
190:                 when(databinding.getSetting(keyDMR, cWithoutKey)).thenReturn(settingWithoutKey);
191:
192:                 final EMFKeyAttributeValueProperty valueProperty = new EMFKeyAttributeValueProperty(mock(EditingDomain.class),
193:                         databinding, keyDMR, dKey, TestPackage.eINSTANCE.getB_CList());
194:
195:                 assertEquals(cWithKey, valueProperty.getValue(b));
196:         }
197:
198:         /**
199:          * Test method for
200:          * {@link org.eclipse.emfforms.internal.core.services.databinding.keyattribute.EMFKeyAttributeValueProperty#doSetValue(java.lang.Object, java.lang.Object)}
201:          * .
202:          *
203:          * @throws DatabindingFailedException
204:          */
205:         @Test(expected = IllegalArgumentException.class)
206:         public void testDoSetValueNewValueDoesntContainKey() throws DatabindingFailedException {
207:                 final B b = TestFactory.eINSTANCE.createB();
208:                 final C cWithoutKey = TestFactory.eINSTANCE.createC();
209:                 final D dWithoutKey = TestFactory.eINSTANCE.createD();
210:
211:                 dWithoutKey.setX(WRONG_TEST_KEY);
212:
213:                 cWithoutKey.setD(dWithoutKey);
214:
215:                 final VFeaturePathDomainModelReference keyDMR = VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
216:                 keyDMR.getDomainModelEReferencePath().add(TestPackage.eINSTANCE.getC_D());
217:                 keyDMR.setDomainModelEFeature(TestPackage.eINSTANCE.getD_X());
218:
219:                 final EMFFormsDatabindingEMF databinding = mock(EMFFormsDatabindingEMF.class);
220:                 final Setting settingWithoutKey = ((InternalEObject) dWithoutKey).eSetting(TestPackage.eINSTANCE.getD_X());
221:
222:                 when(databinding.getSetting(keyDMR, cWithoutKey)).thenReturn(settingWithoutKey);
223:
224:                 final EMFKeyAttributeValueProperty valueProperty = new EMFKeyAttributeValueProperty(mock(EditingDomain.class),
225:                         databinding, keyDMR, CORRECT_TEST_KEY, TestPackage.eINSTANCE.getB_CList());
226:
227:                 valueProperty.setValue(b, cWithoutKey);
228:         }
229:
230:         /**
231:          * Test method for
232:          * {@link org.eclipse.emfforms.internal.core.services.databinding.keyattribute.EMFKeyAttributeValueProperty#doSetValue(java.lang.Object, java.lang.Object)}
233:          * .
234:          *
235:          * @throws DatabindingFailedException
236:          */
237:         @SuppressWarnings("unchecked")
238:         @Test(expected = IllegalArgumentException.class)
239:         public void testDoSetValueNewValueDatabindingFails() throws DatabindingFailedException {
240:                 final B b = TestFactory.eINSTANCE.createB();
241:                 final C cFailedDatabinding = TestFactory.eINSTANCE.createC();
242:
243:                 final VFeaturePathDomainModelReference keyDMR = VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
244:                 keyDMR.getDomainModelEReferencePath().add(TestPackage.eINSTANCE.getC_D());
245:                 keyDMR.setDomainModelEFeature(TestPackage.eINSTANCE.getD_X());
246:
247:                 final EMFFormsDatabindingEMF databinding = mock(EMFFormsDatabindingEMF.class);
248:
249:                 when(databinding.getSetting(keyDMR, cFailedDatabinding)).thenThrow(DatabindingFailedException.class);
250:
251:                 final EMFKeyAttributeValueProperty valueProperty = new EMFKeyAttributeValueProperty(mock(EditingDomain.class),
252:                         databinding, keyDMR, CORRECT_TEST_KEY, TestPackage.eINSTANCE.getB_CList());
253:
254:                 valueProperty.setValue(b, cFailedDatabinding);
255:         }
256:
257:         /**
258:          * Test method for
259:          * {@link org.eclipse.emfforms.internal.core.services.databinding.keyattribute.EMFKeyAttributeValueProperty#doSetValue(java.lang.Object, java.lang.Object)}
260:          * .
261:          *
262:          * @throws DatabindingFailedException
263:          */
264:         @Test
265:         public void testDoSetValueNoOldKeyPresent() throws DatabindingFailedException {
266:                 final AdapterFactoryEditingDomain editingDomain = new AdapterFactoryEditingDomain(
267:                         new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE),
268:                         new BasicCommandStack());
269:
270:                 final B b = TestFactory.eINSTANCE.createB();
271:                 final C cOldWithoutKey = TestFactory.eINSTANCE.createC();
272:                 final C cNewWithKey = TestFactory.eINSTANCE.createC();
273:                 final D dWithKey = TestFactory.eINSTANCE.createD();
274:                 final D dWithoutKey = TestFactory.eINSTANCE.createD();
275:
276:                 dWithKey.setX(CORRECT_TEST_KEY);
277:                 dWithoutKey.setX(WRONG_TEST_KEY);
278:
279:                 b.getCList().add(cOldWithoutKey);
280:                 cNewWithKey.setD(dWithKey);
281:                 cOldWithoutKey.setD(dWithoutKey);
282:
283:                 final VFeaturePathDomainModelReference keyDMR = VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
284:                 keyDMR.getDomainModelEReferencePath().add(TestPackage.eINSTANCE.getC_D());
285:                 keyDMR.setDomainModelEFeature(TestPackage.eINSTANCE.getD_X());
286:
287:                 final EMFFormsDatabindingEMF databinding = mock(EMFFormsDatabindingEMF.class);
288:                 final Setting settingWithKey = ((InternalEObject) dWithKey).eSetting(TestPackage.eINSTANCE.getD_X());
289:                 final Setting settingWithoutKey = ((InternalEObject) dWithoutKey).eSetting(TestPackage.eINSTANCE.getD_X());
290:
291:                 when(databinding.getSetting(keyDMR, cNewWithKey)).thenReturn(settingWithKey);
292:                 when(databinding.getSetting(keyDMR, cOldWithoutKey)).thenReturn(settingWithoutKey);
293:
294:                 final EMFKeyAttributeValueProperty valueProperty = new EMFKeyAttributeValueProperty(editingDomain,
295:                         databinding, keyDMR, CORRECT_TEST_KEY, TestPackage.eINSTANCE.getB_CList());
296:
297:                 valueProperty.doSetValue(b, cNewWithKey);
298:
299:                 assertEquals(2, b.getCList().size());
300:                 assertTrue(b.getCList().contains(cOldWithoutKey));
301:                 assertTrue(b.getCList().contains(cNewWithKey));
302:         }
303:
304:         /**
305:          * Test method for
306:          * {@link org.eclipse.emfforms.internal.core.services.databinding.keyattribute.EMFKeyAttributeValueProperty#doSetValue(java.lang.Object, java.lang.Object)}
307:          * .
308:          *
309:          * @throws DatabindingFailedException
310:          */
311:         @Test
312:         public void testDoSetValueOldKeyPresent() throws DatabindingFailedException {
313:                 final AdapterFactoryEditingDomain editingDomain = new AdapterFactoryEditingDomain(
314:                         new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE),
315:                         new BasicCommandStack());
316:
317:                 final B b = TestFactory.eINSTANCE.createB();
318:                 final C cOldWithKey = TestFactory.eINSTANCE.createC();
319:                 final C cNewWithKey = TestFactory.eINSTANCE.createC();
320:                 final D dOldWithKey = TestFactory.eINSTANCE.createD();
321:                 final D dNewWithKey = TestFactory.eINSTANCE.createD();
322:
323:                 dOldWithKey.setX(CORRECT_TEST_KEY);
324:                 dNewWithKey.setX(CORRECT_TEST_KEY);
325:
326:                 b.getCList().add(cOldWithKey);
327:                 cNewWithKey.setD(dNewWithKey);
328:                 cOldWithKey.setD(dOldWithKey);
329:
330:                 final VFeaturePathDomainModelReference keyDMR = VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
331:                 keyDMR.getDomainModelEReferencePath().add(TestPackage.eINSTANCE.getC_D());
332:                 keyDMR.setDomainModelEFeature(TestPackage.eINSTANCE.getD_X());
333:
334:                 final EMFFormsDatabindingEMF databinding = mock(EMFFormsDatabindingEMF.class);
335:                 final Setting settingNewWithKey = ((InternalEObject) dNewWithKey).eSetting(TestPackage.eINSTANCE.getD_X());
336:                 final Setting settingOldWithKey = ((InternalEObject) dOldWithKey).eSetting(TestPackage.eINSTANCE.getD_X());
337:
338:                 when(databinding.getSetting(keyDMR, cNewWithKey)).thenReturn(settingNewWithKey);
339:                 when(databinding.getSetting(keyDMR, cOldWithKey)).thenReturn(settingOldWithKey);
340:
341:                 final EMFKeyAttributeValueProperty valueProperty = new EMFKeyAttributeValueProperty(editingDomain,
342:                         databinding, keyDMR, CORRECT_TEST_KEY, TestPackage.eINSTANCE.getB_CList());
343:
344:                 valueProperty.doSetValue(b, cNewWithKey);
345:
346:                 assertEquals(1, b.getCList().size());
347:                 assertFalse(b.getCList().contains(cOldWithKey));
348:                 assertTrue(b.getCList().contains(cNewWithKey));
349:         }
350: }