Java tutorial
/*************************************************************************** * Copyright 2012 TXT e-solutions SpA * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * This work was performed within the IoT_at_Work Project * and partially funded by the European Commission's * 7th Framework Programme under the research area ICT-2009.1.3 * Internet of Things and enterprise environments. * * Authors: * Salvatore Piccione (TXT e-solutions SpA) * * Contributors: * Domenico Rotondi (TXT e-solutions SpA) **************************************************************************/ package it.txt.ens.client.subscriber.printer.osgi.test; import static org.junit.Assert.assertNotNull; import static org.ops4j.pax.exam.CoreOptions.frameworkProperty; import static org.ops4j.pax.exam.CoreOptions.junitBundles; import static org.ops4j.pax.exam.CoreOptions.mavenBundle; import static org.ops4j.pax.exam.CoreOptions.repositories; import static org.ops4j.pax.exam.CoreOptions.systemPackage; import static org.ops4j.pax.exam.CoreOptions.systemProperty; import static org.ops4j.pax.exam.OptionUtils.expand; import it.txt.ens.client.subscriber.printer.ENSEventPrinter; import it.txt.ens.client.subscriber.printer.osgi.FilterAttributes; import it.txt.ens.client.subscriber.printer.osgi.RegisteredServices; import it.txt.ens.core.util.converter.MapConverter; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Dictionary; import java.util.List; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.inject.Inject; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.ops4j.pax.exam.Option; import org.ops4j.pax.exam.junit.ExamReactorStrategy; import org.ops4j.pax.exam.junit.JUnit4TestRunner; import org.ops4j.pax.exam.spi.reactors.AllConfinedStagedReactorFactory; import org.osgi.framework.BundleContext; import org.osgi.framework.Filter; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; import org.osgi.service.cm.Configuration; import org.osgi.service.cm.ConfigurationAdmin; import org.osgi.service.cm.ConfigurationException; import org.osgi.util.tracker.ServiceTracker; import org.springframework.ldap.filter.AndFilter; import org.springframework.ldap.filter.EqualsFilter; //import it.txt.ens.client.subscriber.printer.osgi.EventPrinterListenersMSF; /** * @author Salvatore Piccione (TXT e-solutions SpA - salvatore.piccione AT txtgroup.com) * @contributor Domenico Rotondi (TXT e-solutions SpA - domenico.rotondi AT txtgroup.com) * */ @RunWith(JUnit4TestRunner.class) @ExamReactorStrategy(AllConfinedStagedReactorFactory.class) public class ENSEventPrinterTest { @Inject private BundleContext context; private List<Properties> properties; private List<ServiceReference<ENSEventPrinter>> serviceReferences; private ServiceReference<ConfigurationAdmin> configAdminSR; private ConfigurationAdmin configAdmin; private static final Logger LOGGER = Logger.getLogger(ENSEventPrinterTest.class.getName()); private static final String PROPERTIES_DIR_PATH = "D:\\IoT@Work_DEV\\ENS\\ens-subscriber-test\\src\\test\\resources\\"; private static final String PROPERTY_NAME_FORMAT = "ENSSubscriber-PrinterApp-%1$s.properties"; private static final long TIMEOUT = 50000; @org.ops4j.pax.exam.junit.Configuration public static Option[] configure() throws MalformedURLException { return expand(mavenBundle("org.osgi", "org.osgi.compendium", "4.3.0"), mavenBundle("org.apache.felix", "org.apache.felix.configadmin", "1.6.0"), mavenBundle("javax.xml.stream", "com.springsource.javax.xml.stream", "1.0.1"), mavenBundle("org.apache.xmlcommons", "com.springsource.org.apache.xmlcommons", "1.3.4"), mavenBundle("com.sun.xml", "com.springsource.com.sun.xml.fastinfoset", "1.2.2"), mavenBundle("javax.activation", "com.springsource.javax.activation", "1.1.1"), mavenBundle("javax.xml.bind", "com.springsource.javax.xml.bind", "2.2.0"), mavenBundle("org.jvnet.staxex", "com.springsource.org.jvnet.staxex", "1.0.0"), mavenBundle("org.apache.xml", "com.springsource.org.apache.xml.resolver", "1.2.0"), mavenBundle("javax.xml.crypto", "com.springsource.javax.xml.crypto", "1.4.3"), mavenBundle("org.apache.xalan", "com.springsource.org.apache.xalan", "2.7.1"), mavenBundle("org.apache.xalan", "com.springsource.org.apache.xml.serializer", "2.7.1"), mavenBundle("org.bouncycastle", "com.springsource.org.bouncycastle.jce", "1.39.0"), mavenBundle("com.sun.xml", "com.springsource.com.sun.xml.bind", "2.2.0"), mavenBundle("org.apache.commons", "com.springsource.org.apache.commons.lang", "2.6.0"), mavenBundle("org.apache.commons", "com.springsource.org.apache.commons.io", "1.4.0"), mavenBundle("com.rabbitmq", "amqp-client", "2.8.7"), mavenBundle("org.apache.xerces", "com.springsource.org.apache.xerces", "2.9.1").noStart(), mavenBundle("org.apache.commons", "com.springsource.org.apache.commons.logging", "1.1.1"), mavenBundle("org.apache.commons", "com.springsource.org.apache.commons.codec", "1.6.0"), mavenBundle("org.apache.httpcomponents", "com.springsource.org.apache.httpcomponents.httpcore", "4.2.1"), mavenBundle("org.apache.httpcomponents", "com.springsource.org.apache.httpcomponents.httpclient", "4.2.1"), mavenBundle("org.springframework", "org.springframework.beans", "3.0.7.RELEASE"), mavenBundle("org.springframework", "org.springframework.core", "3.0.7.RELEASE"), mavenBundle("org.springframework.ldap", "org.springframework.ldap", "1.3.1.RELEASE"), mavenBundle("org.springframework", "org.springframework.transaction", "3.0.7.RELEASE"), mavenBundle("it.txt.access.capability", "capability-commons", "2.0.1"), mavenBundle("it.txt.access.capability.schema", "capability-schema-commons", "1.0.0"), mavenBundle("it.txt.access.capability.schema", "capability-schema-creator", "1.0.1"), mavenBundle("it.txt.access.capability.schema", "capability-schema-verifier", "1.0.1"), mavenBundle("it.txt.access.capability.schema", "capability-schema-library", "1.0.1"), mavenBundle("it.txt.access.capability.libraries", "capability-libs-finder", "1.0.1"), mavenBundle("it.txt.ens", "ens-schema", "2.0.0"), mavenBundle("it.txt.ens", "ens-core", "1.0.0"), mavenBundle("it.txt.ens", "ens-core-util", "1.0.0"), mavenBundle("it.txt.ens", "ens-core-impl", "1.0.0"), mavenBundle("it.txt.ens.client", "ens-client-core", "1.0.0"), mavenBundle("it.txt.ens.client", "ens-client-core-impl", "1.0.0"), mavenBundle("it.txt.ens.client", "ens-client-util", "1.0.0"), mavenBundle("it.txt.ens.client.subscriber", "ens-subscriber", "1.0.0"), mavenBundle("it.txt.ens.client.subscriber", "ens-subscriber-impl", "1.0.0"), mavenBundle("it.txt.ens.client.subscriber", "ens-subscriber-test", "1.0.0"), frameworkProperty("platform").value("felix"), frameworkProperty("ens.authz.request.schemas.dir") .value("D:\\IoT@Work_DEV\\ENS\\ens-schemas\\src\\main\\resources\\xsd"), frameworkProperty("ens.authz.response.schemas.dir") .value("D:\\IoT@Work_DEV\\ENS\\ens-schemas\\src\\main\\resources\\xsd"), //TODO NOT TO BE USED - just for test systemProperty("fixedPath") .value("D:\\IoT@Work_DEV\\ENS\\ens-subscriber-test\\src\\test\\resources\\"), systemProperty("java.util.logging.config.file").value( "D:\\IoT@Work_DEV\\ENS\\ens-subscriber-impl\\src\\test\\resources\\logging-config.properties"), systemPackage("com.sun.jndi.ldap"), repositories("http://repository.springsource.com/maven/bundles/release", "http://repository.springsource.com/maven/bundles/external", "http://repo1.maven.org/maven2"), junitBundles()); } @Test public void testUseOfBasicENSSubscriber() throws Exception { for (Properties configuration : properties) declareEventPrinter(configuration); context.ungetService(configAdminSR); Thread.sleep(30000); ENSEventPrinter eventPrinter; for (ServiceReference<ENSEventPrinter> eventPrinterSR : serviceReferences) { eventPrinter = context.getService(eventPrinterSR); assertNotNull("The service is null", eventPrinter); eventPrinter.stop(); } } @Before public void setUp() { properties = new ArrayList<Properties>(); int i = 1; File file; FileInputStream inputStream = null; Properties propertyContainer; while ((file = new File(PROPERTIES_DIR_PATH + String.format(PROPERTY_NAME_FORMAT, i))).exists()) { try { inputStream = new FileInputStream(file); propertyContainer = new Properties(); propertyContainer.load(inputStream); properties.add(propertyContainer); LOGGER.log(Level.FINE, "Loaded properties file: " + file.getAbsolutePath()); } catch (IOException e) { LOGGER.log(Level.SEVERE, "An error occurred file reading the property file " + file.getAbsolutePath(), e); } finally { if (inputStream != null) try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } i++; } configAdminSR = context.getServiceReference(ConfigurationAdmin.class); configAdmin = context.getService(configAdminSR); serviceReferences = new ArrayList<ServiceReference<ENSEventPrinter>>(); } private void declareEventPrinter(Properties properties) throws Exception { LOGGER.fine("Declaring an event printer"); Configuration eventListenerConfig = configAdmin .createFactoryConfiguration(RegisteredServices.EVENT_PRINTER_LISTENER_SERVICE_PID, null); Dictionary<String, Object> configuration = MapConverter.convert(properties); configuration.put(FilterAttributes.EVENT_PRINTER_OWNER_ID, context.getBundle().getBundleId()); // configuration.put(FilterAttributes.IMPLEMENTATION_KEY, EventPrinterListenersMSF.IMPLEMENTATION_ID); eventListenerConfig.update(configuration); // Object patternOBJ = properties.get(ENSResourceFactory.PATTERN); // String pattern = null; // if (patternOBJ == null) { // Object resourceOBJ = properties.get(ENSResourceFactory.URI); // if (resourceOBJ == null) // throw new Exception("Missing property " + ENSResourceFactory.URI); // else { // ServiceReference<ENSResourceFactory> resourceFactorySR = context.getServiceReference( // ENSResourceFactory.class); // ENSResourceFactory resourceFactory = context.getService(resourceFactorySR); // try { // pattern = resourceFactory.create(new URI(resourceOBJ.toString())).getPattern(); // } finally { // context.ungetService(resourceFactorySR); // } // } // } else { // pattern = (String) patternOBJ; // } EqualsFilter ownership = new EqualsFilter(FilterAttributes.EVENT_PRINTER_OWNER_ID, (int) context.getBundle().getBundleId()); // EqualsFilter implementation = new EqualsFilter(FilterAttributes.IMPLEMENTATION_KEY, // EventPrinterListenersMSF.IMPLEMENTATION_ID); // EqualsFilter patternFilter = new EqualsFilter(ENSResource.PATTERN_PARAMETER_NAME, pattern); AndFilter filter = new AndFilter()/*.and(implementation)*/.and(ownership)/*.and(patternFilter)*/; // Filter f; try { f = FrameworkUtil.createFilter(filter.encode()); } catch (InvalidSyntaxException e) { throw new ConfigurationException(null, "An error occurred while tracking subscription services using filter " + filter.encode(), e); } ServiceTracker<ENSEventPrinter, ENSEventPrinter> tracker = new ServiceTracker<ENSEventPrinter, ENSEventPrinter>( context, f, null); tracker.open(); ENSEventPrinter eventPrinter = tracker.waitForService(TIMEOUT); tracker.close(); assertNotNull("No event printer has been found using the filter " + f.toString(), eventPrinter); // Collection<ServiceReference<ENSEventPrinter>> references; // long startTime = System.currentTimeMillis(); // do { // references = context.getServiceReferences(ENSEventPrinter.class, f.toString()); // } while (references.isEmpty() && (System.currentTimeMillis() - startTime) <= TIMEOUT); // // assertTrue("No event printer has been found using the filter " + f.toString(),references.iterator().hasNext()); // ServiceReference<ENSEventPrinter> eventPrinterSR = references.iterator().next(); // assertNotNull(eventPrinterSR); // serviceReferences.add(eventPrinterSR); // ENSEventPrinter eventPrinter = context.getService(eventPrinterSR); LOGGER.fine("Created ENS event printer with configuration: " + properties.toString()); eventPrinter.start(); } }