Skip to content

Package: Labelizer_Test

Labelizer_Test

nameinstructionbranchcomplexitylinemethod
Labelizer_Test()
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
asyncCleanUp()
M: 0 C: 49
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 12
100%
M: 0 C: 1
100%
getLabel_defaultAdapterFactory()
M: 0 C: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
getLabel_registeredAdapterFactory()
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
intern()
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 31
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 8
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2019 Christian W. Damus 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: * Christian W. Damus - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.common.spi;
15:
16: import static org.hamcrest.CoreMatchers.hasItem;
17: import static org.hamcrest.CoreMatchers.is;
18: import static org.hamcrest.CoreMatchers.not;
19: import static org.hamcrest.CoreMatchers.notNullValue;
20: import static org.hamcrest.CoreMatchers.sameInstance;
21: import static org.hamcrest.MatcherAssert.assertThat;
22: import static org.junit.Assume.assumeThat;
23:
24: import java.util.Arrays;
25: import java.util.Collection;
26: import java.util.Map;
27: import java.util.concurrent.TimeUnit;
28:
29: import org.eclipse.emf.common.notify.Adapter;
30: import org.eclipse.emf.common.notify.AdapterFactory;
31: import org.eclipse.emf.ecore.EObject;
32: import org.eclipse.emf.ecore.EPackage;
33: import org.eclipse.emf.ecore.EcorePackage;
34: import org.eclipse.emf.ecore.provider.EAttributeItemProvider;
35: import org.eclipse.emf.ecore.provider.EcoreItemProviderAdapterFactory;
36: import org.eclipse.emf.ecp.common.test.model.TestFactory;
37: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
38: import org.eclipse.emf.edit.provider.IItemLabelProvider;
39: import org.eclipse.emf.edit.provider.ReflectiveItemProvider;
40: import org.junit.Rule;
41: import org.junit.Test;
42: import org.junit.rules.TestRule;
43: import org.junit.rules.Timeout;
44:
45: /**
46: * Unit tests for the {@link Labelizer} API.
47: */
48: public class Labelizer_Test {
49:
50:         @Rule
51:         public final TestRule timeout = new Timeout(5, TimeUnit.SECONDS);
52:
53:         static {
54:                 // Ensure that the item-provider adapter factory registry has what our test needs
55:                 final Collection<?> types = Arrays.asList(EcorePackage.eINSTANCE, IItemLabelProvider.class);
56:                 ComposedAdapterFactory.Descriptor desc = ComposedAdapterFactory.Descriptor.Registry.INSTANCE
57:                         .getDescriptor(types);
58:•                if (desc == null) {
59:                         desc = new ComposedAdapterFactory.Descriptor() {
60:
61:                                 @Override
62:                                 public AdapterFactory createAdapterFactory() {
63:                                         return new EcoreItemProviderAdapterFactory();
64:                                 }
65:                         };
66:
67:                         @SuppressWarnings("unchecked")
68:                         final Map<Collection<?>, ComposedAdapterFactory.Descriptor> registryMap = (Map<Collection<?>, ComposedAdapterFactory.Descriptor>) ComposedAdapterFactory.Descriptor.Registry.INSTANCE;
69:                         registryMap.put(types, desc);
70:                 }
71:         }
72:
73:         /**
74:          * Initializes me.
75:          */
76:         public Labelizer_Test() {
77:                 super();
78:         }
79:
80:         @Test
81:         public void intern() {
82:                 final Labelizer instance1 = Labelizer.get(EcorePackage.eINSTANCE);
83:                 final Labelizer instance2 = Labelizer.get(EcorePackage.eINSTANCE);
84:
85:                 assertThat("Non-unique instances", instance2, sameInstance(instance1));
86:         }
87:
88:         @Test
89:         public void getLabel_registeredAdapterFactory() {
90:                 final EObject object = EcorePackage.Literals.EATTRIBUTE__ID;
91:                 final Labelizer labelizer = Labelizer.get(object);
92:                 final String actualLabel = labelizer.getLabel(object);
93:                 final String expectedLabel = new EAttributeItemProvider(null).getText(object);
94:
95:                 assertThat(actualLabel, is(expectedLabel));
96:         }
97:
98:         @Test
99:         public void getLabel_defaultAdapterFactory() {
100:                 // We don't have an EMF.Edit implementation for these
101:                 final EObject object = TestFactory.eINSTANCE.createTest1();
102:                 final Labelizer labelizer = Labelizer.get(object);
103:                 final String actualLabel = labelizer.getLabel(object);
104:                 final String expectedLabel = new ReflectiveItemProvider(null).getText(object);
105:
106:                 assertThat(actualLabel, is(expectedLabel));
107:         }
108:
109:         @Test
110:         public void asyncCleanUp() throws Exception {
111:                 final EPackage ePackage = EcorePackage.eINSTANCE;
112:                 Labelizer.get(ePackage).getLabel(ePackage);
113:
114:                 final Adapter itemProvider = ePackage.eAdapters().stream().filter(IItemLabelProvider.class::isInstance)
115:                         .findFirst().orElse(null);
116:                 assumeThat("No item-provider adapter attached", itemProvider, notNullValue());
117:
118:•                for (int i = 0; i < 15; i++) {
119:•                        if (!ePackage.eAdapters().contains(itemProvider)) {
120:                                 break;
121:                         }
122:
123:                         System.gc();
124:                         Thread.sleep(500L);
125:                 }
126:
127:                 assertThat("Item providers not disposed", ePackage.eAdapters(), not(hasItem(itemProvider)));
128:         }
129:
130: }