PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/j2ee.sun.ddui/libvalidationtest/org/netbeans/modules/j2ee/sun/validation/util/UtilsTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 371 lines | 220 code | 59 blank | 92 comment | 27 complexity | 9de1661d1b759f7333d1a784bc8acfea MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.j2ee.sun.validation.util;
  45. import junit.framework.*;
  46. import java.lang.reflect.Constructor;
  47. import java.lang.reflect.Method;
  48. /**
  49. *
  50. * @author Rajeshwar Patil
  51. * @version %I%, %G%
  52. */
  53. public class UtilsTest extends TestCase{
  54. /* A class implementation comment can go here. */
  55. private Utils utils = null;
  56. public UtilsTest(String name){
  57. super(name);
  58. utils = new Utils();
  59. }
  60. public static void main(String args[]){
  61. junit.textui.TestRunner.run(suite());
  62. }
  63. public void testGetIndexedName(){
  64. String element = "element"; //NOI18N
  65. int index = 5;
  66. assertTrue("element(5)".equals( //NOI18N
  67. utils.getIndexedName(element,index)));
  68. assertTrue(null == utils.getIndexedName(null,index));
  69. }
  70. //test for :
  71. // public static Object getElement(String elementName, Object object)
  72. // public static Object[] getElements(String elementName,
  73. // Object object)
  74. // public static Object getElement(String elementName,
  75. // int index, Object object)
  76. // public static Object getElement(String elementName,
  77. // int index, Object object)
  78. // public static Object getElement(String elementName, Object object,
  79. // String prefix)
  80. public void testGetElement(){
  81. Object object = new CustomObject();
  82. assertTrue("foo".equals( //NOI18N
  83. (String)utils.getElement("foo", object))); //NOI18N
  84. assertTrue(null == utils.getElement("", object)); //NOI18N
  85. assertTrue(null == utils.getElement(null, object));
  86. assertTrue(null == utils.getElement("foo", null)); //NOI18N
  87. assertTrue(null == utils.getElement(null, null)); //NOI18N
  88. Integer integer = (Integer) utils.getElement("integer", object);//NOI18N
  89. assertTrue(5 == integer.intValue());
  90. String[] colours =
  91. (String[]) utils.getElements("primaryColours", object); //NOI18N
  92. assertTrue("green".equals(colours[1])); //NOI18N
  93. String colour =
  94. (String) utils.getElement("primaryColours", 2, object); //NOI18N
  95. assertTrue("blue".equals(colour)); //NOI18N
  96. assertTrue(null == utils.getElement("", 2, object)); //NOI18N
  97. assertTrue(null == utils.getElement(null, 2, object));
  98. assertTrue(null == utils.getElement("primaryColours", 2, null));//NOI18N
  99. assertTrue(null == utils.getElement("primaryColours", -1, //NOI18N
  100. object));
  101. assertTrue(null == utils.getElement(null, -3, null));
  102. assertTrue(null == utils.getElement("", object, "")); //NOI18N
  103. assertTrue(null == utils.getElement("primaryColours", //NOI18N
  104. null, "size")); //NOI18N
  105. integer = (Integer) utils.getElement(
  106. "primaryColours", object, ""); //NOI18N
  107. assertTrue(3 == integer.intValue());
  108. integer = (Integer) utils.getElement(
  109. "primaryColours", object, null); //NOI18N
  110. assertTrue(3 == integer.intValue());
  111. integer = (Integer) utils.getElement(
  112. "primaryColours", object, "size"); //NOI18N
  113. assertTrue(4 == integer.intValue());
  114. }
  115. public void testMethodNameFromDtdName(){
  116. String name = "ejb-name"; //NOI18N
  117. String prefix = "set"; //NOI18N
  118. assertTrue("setEjbName".equals( //NOI18N
  119. utils.methodNameFromDtdName(name, prefix)));
  120. }
  121. public void testMethodNameFromBeanName(){
  122. String beanName = "name"; //NOI18N
  123. String prefix = "get"; //NOI18N
  124. assertTrue("getName".equals( //NOI18N
  125. utils.methodNameFromBeanName(beanName, prefix)));
  126. prefix = null;
  127. assertTrue("name".equals( //NOI18N
  128. utils.methodNameFromBeanName(beanName, prefix)));
  129. prefix = ""; //NOI18N
  130. assertTrue("name".equals( //NOI18N
  131. utils.methodNameFromBeanName(beanName, prefix)));
  132. beanName = ""; //NOI18N
  133. prefix = "set"; //NOI18N
  134. assertTrue("set".equals( //NOI18N
  135. utils.methodNameFromBeanName(beanName, prefix)));
  136. beanName = null;
  137. prefix = "set"; //NOI18N
  138. assertTrue(null == utils.methodNameFromBeanName(beanName, prefix));
  139. }
  140. public void testEleminateHypen(){
  141. String str = "hello"; //NOI18N
  142. assertTrue(null == utils.eleminateHypen(null));
  143. assertTrue("".equals(utils.eleminateHypen(""))); //NOI18N
  144. assertTrue("hello".equals(utils.eleminateHypen(str))); //NOI18N
  145. str = "-hello"; //NOI18N
  146. assertTrue("hello".equals(utils.eleminateHypen(str))); //NOI18N
  147. str = "hello-"; //NOI18N
  148. assertTrue("hello".equals(utils.eleminateHypen(str))); //NOI18N
  149. str = "hello-world"; //NOI18N
  150. assertTrue("helloWorld".equals(utils.eleminateHypen(str))); //NOI18N
  151. str = "hello-whole-world"; //NOI18N
  152. assertTrue("helloWholeWorld".equals(utils.eleminateHypen(str)));//NOI18N
  153. str = "-hellO-WhOle-wOrld-"; //NOI18N
  154. assertTrue("hellOWhOleWOrld".equals(utils.eleminateHypen(str)));//NOI18N
  155. }
  156. //test for :
  157. // public static Method getMethod(String type, String methodName)
  158. // public static Method getMethod(Class classObject, String methodName)
  159. // public static Object invoke(Object object, Method method)
  160. public void testGetMethod_One(){
  161. String str = "hello"; //NOI18N
  162. Method method = utils.getMethod("java.lang.String", "length"); //NOI18N
  163. int length = ((Integer)utils.invoke(str, method)).intValue();
  164. assertTrue(5 == length);
  165. method = utils.getMethod(String.class, "length"); //NOI18N
  166. length = ((Integer)utils.invoke(str, method)).intValue();
  167. assertTrue(5 == length);
  168. }
  169. //test for :
  170. // public static Method getMethod(String type, String methodName,
  171. // Class[] argumentClass)
  172. // public static Method getMethod(Class classObject, String methodName,
  173. // Class[] argumentClass){
  174. // public static Object invoke(Object object, Method method,
  175. // Object[] arguments)
  176. public void testGetMethod(){
  177. String str = "hello"; //NOI18N
  178. Class[] argumentClass = new Class[] {char.class, char.class};
  179. Method method =
  180. utils.getMethod("java.lang.String", "replace", //NOI18N
  181. argumentClass);
  182. Character oldChar = new Character('h');
  183. Character newChar = new Character('H');
  184. Object[] parameters = new Object[] { oldChar, newChar };
  185. assertTrue("Hello".equals(utils.invoke(str, method, //NOI18N
  186. parameters)));
  187. method = utils.getMethod(String.class, "replace", //NOI18N
  188. argumentClass);
  189. assertTrue("Hello".equals(utils.invoke(str, method, //NOI18N
  190. parameters)));
  191. }
  192. //test for :
  193. // public static Class getClass(String type)
  194. // public static Class getClass(Object object)
  195. public void testGetClass(){
  196. assertTrue(String.class == utils.getClass("java.lang.String")); //NOI18N
  197. Integer integer = new Integer(1234);
  198. assertTrue(Integer.class == utils.getClass(integer));
  199. }
  200. //test for :
  201. // public static Object createObject(Constructor constructor,
  202. // Object[] arguments)
  203. // public static Constructor getConstructor(String type,
  204. // Class[] argumentClass)
  205. // public static Constructor getConstructor(Class classObject,
  206. // Class[] argumentClass)
  207. public void testCreatObject_Two(){
  208. Class[] argumentTypes = new Class[] {String.class};
  209. Constructor constructor =
  210. utils.getConstructor("java.lang.Integer", argumentTypes); //NOI18N
  211. Object[] argumentValues = new Object[] {"1234"}; //NOI18N
  212. Object object = utils.createObject(constructor, argumentValues);
  213. Class objectClass = object.getClass();
  214. String objectName = objectClass.getName();
  215. assertTrue("java.lang.Integer".equals(objectName)); //NOI18N
  216. object = (Integer)object;
  217. assertTrue("1234".equals(object.toString())); //NOI18N
  218. Class[] argTypes = new Class[] {int.class};
  219. Constructor cons = utils.getConstructor(object.getClass(), argTypes);
  220. Integer parameter = new Integer(4567);
  221. Object[] arguments = new Object[] {parameter};
  222. Object obj = utils.createObject(cons, arguments);
  223. assertTrue("java.lang.Integer".equals(objectName)); //NOI18N
  224. obj = (Integer)obj;
  225. assertTrue("4567".equals(obj.toString())); //NOI18N
  226. }
  227. //test for : public static Object createObject(Class classObject)
  228. public void testCreateObject_One(){
  229. Object object = utils.createObject("java.lang.String"); //NOI18N
  230. String str = "hello"; //NOI18N
  231. Class objectClass = str.getClass();
  232. String name = objectClass.getName();
  233. assertTrue("java.lang.String".equals(name)); //NOI18N
  234. }
  235. //test for : public static Object createObject(String type)
  236. public void testCreateObject(){
  237. Object object = utils.createObject("java.lang.String"); //NOI18N
  238. Class objectClass = object.getClass();
  239. String name = objectClass.getName();
  240. assertTrue("java.lang.String".equals(name)); //NOI18N
  241. }
  242. public void testGetUpperCaseFirstLetter() {
  243. assertTrue("Hello".equals( //NOI18N
  244. utils.upperCaseFirstLetter("hello"))); //NOI18N
  245. assertTrue("Hello".equals( //NOI18N
  246. utils.upperCaseFirstLetter("Hello"))); //NOI18N
  247. assertTrue("H".equals( //NOI18N
  248. utils.upperCaseFirstLetter("h"))); //NOI18N
  249. assertTrue(null == utils.upperCaseFirstLetter(null)); //NOI18N
  250. assertTrue("".equals( //NOI18N
  251. utils.upperCaseFirstLetter(""))); //NOI18N
  252. }
  253. public void testGetParentName() {
  254. assertTrue("/root/parent".equals( //NOI18N
  255. utils.getParentName("/root/parent/child", '/'))); //NOI18N
  256. assertTrue(null == utils.getParentName("child", '/')); //NOI18N
  257. assertTrue(null == utils.getParentName("/root", '/')); //NOI18N
  258. assertTrue(null == utils.getParentName(null, '/'));
  259. }
  260. public void testGetName() {
  261. assertTrue("child".equals(
  262. utils.getName("/root/parent/child", '/'))); //NOI18N
  263. assertTrue("child".equals(utils.getName("child", '/'))); //NOI18N
  264. assertTrue("root".equals(utils.getName("/root", '/'))); //NOI18N
  265. assertTrue(null == utils.getName(null, '/'));
  266. }
  267. /**
  268. * Define suite of all the Tests to run.
  269. */
  270. public static Test suite(){
  271. TestSuite suite = new TestSuite(UtilsTest.class);
  272. return suite;
  273. }
  274. /**
  275. * Initialize; allocate any resources needed to perform Tests.
  276. */
  277. protected void setUp() {
  278. }
  279. /**
  280. * Free all the resources initilized/allocated to perform Tests.
  281. */
  282. protected void tearDown() {
  283. }
  284. private void nyi() {
  285. ///fail("Not yet implemented"); //NOI18N
  286. }
  287. class CustomObject extends Object
  288. {
  289. String name = "foo"; //NOI18N
  290. Integer integer = new Integer(5);
  291. String[] primaryColours =
  292. new String[] {"red", "green", "blue", "yellow"}; //NOI18N
  293. public String getFoo(){
  294. return name;
  295. }
  296. public int getInteger(){
  297. return integer.intValue();
  298. }
  299. public String[] getPrimaryColours(){
  300. return primaryColours;
  301. }
  302. public String getPrimaryColours(int index){
  303. return primaryColours[index];
  304. }
  305. public Integer sizePrimaryColours(){
  306. return new Integer(4);
  307. }
  308. public Integer primaryColours(){
  309. return new Integer(3);
  310. }
  311. }
  312. }