/** * Copyright (c) 2012 - 2018 Data In Motion Consulting. * 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: * Data In Motion Consulting - initial API and implementation */ package org.gecko.runtime.tests; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.File; import java.io.IOException; import java.util.Dictionary; import java.util.Hashtable; import org.gecko.core.tests.AbstractOSGiTest; import org.gecko.core.tests.ServiceChecker; import org.gecko.runtime.boot.GeckoBootConstants; import org.gecko.runtime.resources.GeckoResourcesConstants; import org.gecko.runtime.resources.GeckoResourcesProvider; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.runners.MockitoJUnitRunner; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.InvalidSyntaxException; import org.osgi.service.cm.Configuration; /** * Integration test to test handling of the folder watcher in OSGi environments * @author Mark Hoffmann * */ @RunWith(MockitoJUnitRunner.class) public class ResourcesIntegrationTest extends AbstractOSGiTest { public ResourcesIntegrationTest() { super(FrameworkUtil.getBundle(ResourcesIntegrationTest.class).getBundleContext()); } private File dataFolder; @Override public void doBefore() { String tmp = System.getProperty(GeckoBootConstants.PROP_GECKO_DATA_DIR); dataFolder = new File(tmp); if (!dataFolder.exists()) { dataFolder.mkdirs(); } assertTrue(dataFolder.exists()); } @Override public void doAfter() { assertTrue(dataFolder.exists()); if (dataFolder.listFiles().length > 0) { for (File f : dataFolder.listFiles()) { assertTrue(f.delete()); } } assertTrue(dataFolder.delete()); } @Test public void testResourceFolder() throws InvalidSyntaxException, IOException { Dictionary properties = new Hashtable(); properties.put(GeckoResourcesConstants.RESOURCE_NAME, "mytest"); properties.put(GeckoResourcesConstants.RESOURCE_PATH, "/tmp/geckodata/test"); ServiceChecker providerChecker = createTrackedChecker(GeckoResourcesProvider.class, false); providerChecker.assertCreations(0, false).assertRemovals(0, false); Configuration configuration = createConfigForCleanup(GeckoResourcesConstants.RESOURCES_FACTORY_ID, "test", "?", properties); providerChecker.assertCreations(1, true).assertRemovals(0, false); Configuration c = getConfigAdmin().getFactoryConfiguration(GeckoResourcesConstants.RESOURCES_FACTORY_ID, "test", "?"); c.update(properties); GeckoResourcesProvider provider = providerChecker.getTrackedService(); assertNotNull(provider); assertEquals("mytest", provider.getName()); assertNotNull(provider.getFile()); deleteConfigurationAndRemoveFromCleanup(configuration); providerChecker.assertCreations(1, false).assertRemovals(1, true).immediate(true); } @Test public void testResourceSubstituteFolder() throws InvalidSyntaxException, IOException { Dictionary properties = new Hashtable(); properties.put(GeckoResourcesConstants.RESOURCE_NAME, "mytest"); properties.put(GeckoResourcesConstants.RESOURCE_PATH, "gecko.data.dir/test"); ServiceChecker providerChecker = createTrackedChecker(GeckoResourcesProvider.class, false); providerChecker.assertCreations(0, false).assertRemovals(0, false); Configuration c = createConfigForCleanup(GeckoResourcesConstants.RESOURCES_FACTORY_ID, "test", "?", properties); providerChecker.assertCreations(1, true).assertRemovals(0, false); GeckoResourcesProvider provider = providerChecker.getTrackedService(); assertNotNull(provider); assertEquals("mytest", provider.getName()); assertNotNull(provider.getFile()); deleteConfigurationAndRemoveFromCleanup(c); providerChecker.assertCreations(1, false).assertRemovals(1, true).immediate(true); } }