| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * 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: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.infogrid.services.tests; |
| |
| import static org.junit.Assert.assertEquals; |
| import static org.junit.Assert.assertNotNull; |
| import static org.junit.Assert.assertNull; |
| |
| import java.util.List; |
| |
| import org.eclipse.emf.ecore.util.EcoreUtil; |
| import org.eclipse.xtext.common.types.JvmGenericType; |
| import org.eclipse.xtext.common.types.JvmParameterizedTypeReference; |
| import org.eclipse.xtext.common.types.TypesFactory; |
| import org.junit.Before; |
| import org.junit.Test; |
| import org.knowhowlab.osgi.testing.utils.BundleUtils; |
| import org.knowhowlab.osgi.testing.utils.ServiceUtils; |
| import org.eclipse.osbp.infogrid.api.IGridSourceCache; |
| import org.eclipse.osbp.infogrid.model.gridsource.CxGridSource; |
| import org.eclipse.osbp.infogrid.model.gridsource.CxGridSourceFactory; |
| import org.eclipse.osbp.infogrid.services.tests.data.TestClass1; |
| import org.eclipse.osbp.infogrid.services.tests.data.TestClass2; |
| import org.osgi.framework.BundleContext; |
| import org.osgi.framework.FrameworkUtil; |
| |
| public class GridSourceCacheTest { |
| |
| private BundleContext bc; |
| private IGridSourceCache cache; |
| |
| @Before |
| public void setup() throws Exception { |
| bc = getBundleContext(); |
| BundleUtils.startBundleAsync(bc, "org.eclipse.osbp.infogrid.model"); |
| BundleUtils.startBundleAsync(bc, "org.eclipse.osbp.infogrid.services"); |
| |
| cache = ServiceUtils.getService(getBundleContext(), |
| IGridSourceCache.class); |
| assertNotNull(cache); |
| } |
| |
| private BundleContext getBundleContext() { |
| return FrameworkUtil.getBundle(getClass()).getBundleContext(); |
| } |
| |
| @Test |
| public void test_IdNull() { |
| assertNull(cache.getSource(null)); |
| } |
| |
| @Test |
| public void test_ClassNull() { |
| List<CxGridSource> result = cache.getSources(null); |
| assertEquals(0, result.size()); |
| } |
| |
| @Test |
| public void test_AddRemoveModel_getById() { |
| |
| CxGridSource cxSource = CxGridSourceFactory.eINSTANCE |
| .createCxGridSource(); |
| CxGridSource cxSource2 = CxGridSourceFactory.eINSTANCE |
| .createCxGridSource(); |
| cxSource2.setId("test2"); |
| |
| // add remove with empty id |
| cache.registerSource(cxSource); |
| cache.unregisterSource(cxSource); |
| |
| // add test1 |
| cxSource.setId("test1"); |
| cache.registerSource(cxSource); |
| |
| CxGridSource result = cache.getSource("test1"); |
| assertNotNull(result); |
| assertEquals("test1", result.getId()); |
| |
| cache.unregisterSource(cxSource); |
| result = cache.getSource("test1"); |
| assertNull(result); |
| |
| cache.registerSource(cxSource); |
| cache.registerSource(cxSource2); |
| |
| assertEquals("test1", cache.getSource("test1").getId()); |
| assertEquals("test2", cache.getSource("test2").getId()); |
| |
| cache.unregisterSource(cxSource); |
| } |
| |
| @Test |
| public void test_AddRemoveModel_getByClass() { |
| |
| JvmGenericType jvmType1 = TypesFactory.eINSTANCE.createJvmGenericType(); |
| jvmType1.setSimpleName("TestClass1"); |
| jvmType1.setPackageName("org.eclipse.osbp.infogrid.services.tests.data"); |
| |
| JvmParameterizedTypeReference jvmType1Ref = TypesFactory.eINSTANCE |
| .createJvmParameterizedTypeReference(); |
| jvmType1Ref.setType(jvmType1); |
| |
| JvmGenericType jvmType2 = TypesFactory.eINSTANCE.createJvmGenericType(); |
| jvmType2.setSimpleName("TestClass2"); |
| jvmType2.setPackageName("org.eclipse.osbp.infogrid.services.tests.data"); |
| |
| JvmParameterizedTypeReference jvmType2Ref = TypesFactory.eINSTANCE |
| .createJvmParameterizedTypeReference(); |
| jvmType2Ref.setType(jvmType2); |
| |
| CxGridSource cxSource = CxGridSourceFactory.eINSTANCE |
| .createCxGridSource(); |
| cxSource.setId("test1"); |
| cxSource.setRootType(jvmType1Ref); |
| cxSource.setRootTypeFQN(jvmType1Ref.getQualifiedName()); |
| CxGridSource cxSource2 = CxGridSourceFactory.eINSTANCE |
| .createCxGridSource(); |
| cxSource2.setId("test2"); |
| cxSource2.setRootType(jvmType2Ref); |
| cxSource2.setRootTypeFQN(jvmType2Ref.getQualifiedName()); |
| CxGridSource cxSource3 = CxGridSourceFactory.eINSTANCE |
| .createCxGridSource(); |
| cxSource3.setId("test3"); |
| cxSource3.setRootType(EcoreUtil.copy(jvmType1Ref)); |
| cxSource3.setRootTypeFQN(jvmType1Ref.getQualifiedName()); |
| |
| // only register source1 |
| cache.registerSource(cxSource); |
| |
| List<CxGridSource> result = cache.getSources(TestClass1.class); |
| assertEquals(1, result.size()); |
| assertEquals("test1", result.get(0).getId()); |
| |
| result = cache.getSources(TestClass2.class); |
| assertEquals(0, result.size()); |
| |
| // unregister source1 |
| cache.unregisterSource(cxSource); |
| result = cache.getSources(TestClass1.class); |
| assertEquals(0, result.size()); |
| |
| // register all sources |
| cache.registerSource(cxSource); |
| cache.registerSource(cxSource2); |
| cache.registerSource(cxSource3); |
| |
| // 2 descriptors are available |
| result = cache.getSources(TestClass1.class); |
| assertEquals(2, result.size()); |
| assertEquals("test1", result.get(0).getId()); |
| assertEquals("test3", result.get(1).getId()); |
| |
| } |
| |
| } |