|
Glassfish example source code file (MapInjectionResolverTest.java)
The Glassfish MapInjectionResolverTest.java source code/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 2008-2010 Oracle and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can * obtain a copy of the License at * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html * or packager/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at packager/legal/LICENSE.txt. * * GPL Classpath Exception: * Oracle designates this particular file as subject to the "Classpath" * exception as provided by Oracle in the GPL Version 2 section of the License * file that accompanied this code. * * Modifications: * If applicable, add the following below the License Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyright [year] [name of copyright owner]" * * Contributor(s): * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */ package org.glassfish.common.util.admin; import static org.junit.Assert.*; import org.junit.Test; import org.junit.Before; import java.util.Properties; import java.util.List; import java.util.ArrayList; import java.lang.reflect.AnnotatedElement; import org.glassfish.api.Param; import org.glassfish.api.admin.ParameterMap; /** * junit test to test MapInjectionResolver class */ public class MapInjectionResolverTest { @Test public void getParameterValueTest() { ParameterMap params = new ParameterMap(); params.set("foo", "bar"); params.set("hellO", "world"); params.set("one", "two"); params.set("thrEE", "Four"); params.set("FivE", "six"); params.set("sIx", "seVen"); params.set("eiGHT", "niNe"); String value = MapInjectionResolver.getParameterValue(params, "foo", false); assertEquals("value is bar", "bar", value); value = MapInjectionResolver.getParameterValue(params, "hello", true); assertEquals("value is world", "world", value); value = MapInjectionResolver.getParameterValue(params, "onE", true); assertEquals("value is two", "two", value); value = MapInjectionResolver.getParameterValue(params, "three", true); assertEquals("value is four", "Four", value); value = MapInjectionResolver.getParameterValue(params, "five", false); assertEquals("value is null", null, value); value = MapInjectionResolver.getParameterValue(params, "six", true); assertEquals("value is SeVen", "seVen", value); value = MapInjectionResolver.getParameterValue(params, "eight", true); assertEquals("value is niNe", "niNe", value); value = MapInjectionResolver.getParameterValue(params, "none", true); assertEquals("value is null", null, value); } @Test public void convertStringToPropertiesTest() { String propsStr = "prop1=valA:prop2=valB:prop3=valC"; Properties propsExpected = new Properties(); propsExpected.put("prop1", "valA"); propsExpected.put("prop2", "valB"); propsExpected.put("prop3", "valC"); Properties propsActual = MapInjectionResolver.convertStringToProperties(propsStr, ':'); assertEquals(propsExpected, propsActual); } @Test public void parsePropertiesEscapeCharTest() { String propsStr = "connectionAttributes=\\;create\\\\\\=true"; Properties propsExpected = new Properties(); propsExpected.put("connectionAttributes", ";create\\=true"); Properties propsActual = null; propsActual = MapInjectionResolver.convertStringToProperties(propsStr, ':'); assertEquals(propsExpected, propsActual); } @Test public void parsePropertiesEscapeCharTest2() { String propsStr = "connectionAttributes=;create\\=true"; Properties propsExpected = new Properties(); propsExpected.put("connectionAttributes", ";create=true"); Properties propsActual = null; propsActual = MapInjectionResolver.convertStringToProperties(propsStr, ':'); assertEquals(propsExpected, propsActual); } @Test public void parsePropertiesQuoteTest() { String propsStr = "java.naming.provider.url=\"ldap://ldapserver.sun.com:389\":" + "java.naming.security.authentication=simple:" + "java.naming.security.credentials=changeit:" + "java.naming.security.principal=\"uid=admin,ou=People," + "o=foo,o=something\""; Properties propsExpected = new Properties(); propsExpected.put("java.naming.provider.url", "ldap://ldapserver.sun.com:389"); propsExpected.put("java.naming.security.authentication", "simple"); propsExpected.put("java.naming.security.credentials", "changeit"); propsExpected.put("java.naming.security.principal", "uid=admin,ou=People,o=foo,o=something"); Properties propsActual = MapInjectionResolver.convertStringToProperties(propsStr, ':'); assertEquals(propsExpected, propsActual); } @Test public void convertStringToObjectTest() throws Exception { DummyCommand dc = new DummyCommand(); Class<?> cl = dc.getClass(); AnnotatedElement target = (AnnotatedElement)cl.getDeclaredField("foo"); String paramValueStr = "prop1=valA:prop2=valB:prop3=valC"; Object paramValActual = MapInjectionResolver.convertStringToObject( target, String.class, paramValueStr); Object paramValExpected = "prop1=valA:prop2=valB:prop3=valC"; assertEquals("String type", paramValExpected, paramValActual); target = (AnnotatedElement)cl.getDeclaredField("prop"); paramValActual = MapInjectionResolver.convertStringToObject( target, Properties.class, paramValueStr); paramValExpected = new Properties(); ((Properties)paramValExpected).put("prop1", "valA"); ((Properties)paramValExpected).put("prop2", "valB"); ((Properties)paramValExpected).put("prop3", "valC"); assertEquals("Properties type", paramValExpected, paramValActual); target = (AnnotatedElement)cl.getDeclaredField("portnum"); paramValueStr = "8080"; paramValActual = MapInjectionResolver.convertStringToObject( target, Integer.class, paramValueStr); paramValExpected = new Integer(8080); assertEquals("Integer type", paramValExpected, paramValActual); paramValueStr = "server1:server2:server3"; target = (AnnotatedElement)cl.getDeclaredField("lstr"); paramValActual = MapInjectionResolver.convertStringToObject( target, List.class, paramValueStr); List<String> paramValueList = new java.util.ArrayList(); paramValueList.add("server1"); paramValueList.add("server2"); paramValueList.add("server3"); assertEquals("List type", paramValueList, paramValActual); paramValueStr = "server1,server2,server3"; target = (AnnotatedElement)cl.getDeclaredField("astr"); paramValActual = MapInjectionResolver.convertStringToObject( target, (new String[]{}).getClass(), paramValueStr); String[] strArray = new String[3]; strArray[0] = "server1"; strArray[1] = "server2"; strArray[2] = "server3"; assertEquals("String Array type", strArray, (String[])paramValActual); } @Test public void convertListToObjectTest() throws Exception { DummyCommand dc = new DummyCommand(); Class<?> cl = dc.getClass(); AnnotatedElement target = (AnnotatedElement)cl.getDeclaredField("propm"); List<String> paramValueList = new ArrayList Other Glassfish examples (source code examples)Here is a short list of links related to this Glassfish MapInjectionResolverTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.