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.core.impl.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import it.txt.ens.core.ENSResource; import it.txt.ens.core.factory.ENSResourceFactory; import it.txt.ens.core.factory.URIBuildingException; import it.txt.ens.core.impl.BasicENSResource; import it.txt.ens.core.impl.BasicENSResource.BasicENSResourceFactory; import java.net.URI; import java.net.URISyntaxException; import org.apache.http.client.utils.URIBuilder; import org.junit.Test; /** * @author Salvatore Piccione (TXT e-solutions SpA - salvatore.piccione AT txtgroup.com) * @contributor Domenico Rotondi (TXT e-solutions SpA - domenico.rotondi AT txtgroup.com) * */ public class BasicENSResourceTest { @Test public void testCreation() { System.out.println( "Creation of a " + BasicENSResource.class.getSimpleName() + " instance with full constructor"); String host = "www.myhost.com"; String path = "myENSService"; String namespace = "this is my namespace"; String pattern = "this.is.my.ens.pattern.*"; ENSResource resource; try { resource = testCreation(host, path, namespace, pattern); testURI(resource); } catch (IllegalArgumentException e) { fail(e.getMessage()); e.printStackTrace(); } System.out.println("TEST SUCCEEDED"); System.out.println("-----------------------------------------------------------------------"); System.out.println( "Creation of a " + BasicENSResource.class.getSimpleName() + " instance with 3-params constructor"); host = "www.myhost.com"; namespace = "this is my second namespace"; pattern = "this.is.my.second.ens.pattern.*"; try { resource = testCreation(host, null, namespace, pattern); testURI(resource); } catch (IllegalArgumentException e) { fail(e.getMessage()); e.printStackTrace(); } System.out.println("TEST SUCCEEDED"); } private void testURI(ENSResource resource) { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.addParameter(ENSResource.NAMESPACE_PARAMETER_NAME, resource.getNamespace()); uriBuilder.addParameter(ENSResource.PATTERN_PARAMETER_NAME, resource.getPattern()); uriBuilder.setHost(resource.getHost()); uriBuilder.setPath(resource.getPath()); uriBuilder.setScheme(ENSResource.URI_SCHEME); try { assertEquals("Unexpected resourceURI", uriBuilder.build(), resource.getURI()); } catch (URISyntaxException e) { fail(e.getMessage()); e.printStackTrace(); } } private ENSResource testCreation(String host, String path, String namespace, String pattern) { ENSResource resource = null; BasicENSResourceFactory factory = new BasicENSResourceFactory(); try { if (path == null) resource = factory.create(host, namespace, pattern); else resource = factory.create(host, path, namespace, pattern); assertEquals("Unexpected host", host, resource.getHost()); if (path == null) assertEquals("Unexpected path", ENSResource.DEFAULT_PATH, resource.getPath()); else if (path.startsWith("/")) assertEquals("Unexpected path", path, resource.getPath()); else assertEquals("Unexpected path", "/" + path, resource.getPath()); assertEquals("Unexpected namespace", namespace, resource.getNamespace()); assertEquals("Unexpected pattern", pattern, resource.getPattern()); } catch (IllegalArgumentException e) { fail(e.getMessage()); e.printStackTrace(); } catch (URIBuildingException e) { fail(e.getMessage()); e.printStackTrace(); } return resource; } @Test public void testParsing() throws Exception { System.out.println( "Creation of a " + BasicENSResource.class.getSimpleName() + " instance with full constructor"); String host = "www.iot-at-work.eu"; String path = "/ENS"; String namespace = "TXT Plant - Energy Monitoring"; String pattern = "TXTPlant.Cell1.Robots.#"; // URIBuilder builder = new URIBuilder().setScheme(ENSResource.URI_SCHEME) // .setHost(host).setPath(path) // .setParameter(ENSResource.NAMESPACE_PARAMETER_NAME, namespace) // .setParameter(ENSResource.PATTERN_PARAMETER_NAME, pattern); // URI uri = builder.build(); URI uri = new URI("amqp://www.iot-at-work.eu/ENS?Namespace=" + "TXT%20Plant%20-%20Energy%20Monitoring&Pattern=TXTPlant.Cell1.Robots.%23"); System.out.println("Resource URI: " + uri); ENSResource resource = new BasicENSResourceFactory().create(uri); assertEquals(ENSResourceFactory.HOST, host, resource.getHost()); assertEquals(ENSResourceFactory.PATH, path, resource.getPath()); assertEquals(ENSResourceFactory.NAMESPACE, namespace, resource.getNamespace()); assertEquals(ENSResourceFactory.PATTERN, pattern, resource.getPattern()); } //TODO TEST EXCEPTION THROWING }