📄 introspectionhelpertest.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. * */package org.apache.tools.ant;import junit.framework.TestCase;import junit.framework.AssertionFailedError;import java.io.File;import java.lang.reflect.Method;import java.lang.reflect.InvocationTargetException;import java.util.Enumeration;import java.util.HashMap;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;import org.apache.tools.ant.taskdefs.condition.Os;/** * JUnit 3 testcases for org.apache.tools.ant.IntrospectionHelper. * */public class IntrospectionHelperTest extends TestCase { private Project p; private IntrospectionHelper ih; private static final String projectBasedir = File.separator; public IntrospectionHelperTest(String name) { super(name); } public void setUp() { p = new Project(); p.setBasedir(projectBasedir); ih = IntrospectionHelper.getHelper(getClass()); } public void testIsDynamic() { assertTrue("Not dynamic", false == ih.isDynamic()); } public void testIsContainer() { assertTrue("Not a container", false == ih.isContainer()); } public void testAddText() throws BuildException { ih.addText(p, this, "test"); try { ih.addText(p, this, "test2"); fail("test2 shouldn\'t be equal to test"); } catch (BuildException be) { assertTrue(be.getException() instanceof AssertionFailedError); } ih = IntrospectionHelper.getHelper(String.class); try { ih.addText(p, "", "test"); fail("String doesn\'t support addText"); } catch (BuildException be) { } } public void testGetAddTextMethod() { Method m = ih.getAddTextMethod(); assertMethod(m, "addText", String.class, "test", "bing!"); ih = IntrospectionHelper.getHelper(String.class); try { m = ih.getAddTextMethod(); } catch (BuildException e) {} } public void testSupportsCharacters() { assertTrue("IntrospectionHelperTest supports addText", ih.supportsCharacters()); ih = IntrospectionHelper.getHelper(String.class); assertTrue("String doesn\'t support addText", !ih.supportsCharacters()); } public void addText(String text) { assertEquals("test", text); } public void testElementCreators() throws BuildException { try { ih.getElementType("one"); fail("don't have element type one"); } catch (BuildException be) { } try { ih.getElementType("two"); fail("createTwo takes arguments"); } catch (BuildException be) { } try { ih.getElementType("three"); fail("createThree returns void"); } catch (BuildException be) { } try { ih.getElementType("four"); fail("createFour returns array"); } catch (BuildException be) { } try { ih.getElementType("five"); fail("createFive returns primitive type"); } catch (BuildException be) { } assertEquals(String.class, ih.getElementType("six")); assertEquals("test", ih.createElement(p, this, "six")); try { ih.getElementType("seven"); fail("addSeven takes two arguments"); } catch (BuildException be) { } try { ih.getElementType("eight"); fail("addEight takes no arguments"); } catch (BuildException be) { } try { ih.getElementType("nine"); fail("nine return non void"); } catch (BuildException be) { } try { ih.getElementType("ten"); fail("addTen takes array argument"); } catch (BuildException be) { } try { ih.getElementType("eleven"); fail("addEleven takes primitive argument"); } catch (BuildException be) { } try { ih.getElementType("twelve"); fail("no primitive constructor for java.lang.Class"); } catch (BuildException be) { } assertEquals(StringBuffer.class, ih.getElementType("thirteen")); assertEquals("test", ih.createElement(p, this, "thirteen").toString()); try { ih.createElement(p, this, "fourteen"); fail("fourteen throws NullPointerException"); } catch (BuildException be) { assertTrue(be.getException() instanceof NullPointerException); } try { ih.createElement(p, this, "fourteen"); fail("fifteen throws NullPointerException"); } catch (BuildException be) { assertTrue(be.getException() instanceof NullPointerException); } } private Map getExpectedNestedElements() { Map elemMap = new Hashtable(); elemMap.put("six", String.class); elemMap.put("thirteen", StringBuffer.class); elemMap.put("fourteen", StringBuffer.class); elemMap.put("fifteen", StringBuffer.class); return elemMap; } public void testGetNestedElements() { Map elemMap = getExpectedNestedElements(); Enumeration e = ih.getNestedElements(); while (e.hasMoreElements()) { String name = (String) e.nextElement(); Class expect = (Class) elemMap.get(name); assertNotNull("Support for "+name+" in IntrospectioNHelperTest?", expect); assertEquals("Return type of "+name, expect, ih.getElementType(name)); elemMap.remove(name); } assertTrue("Found all", elemMap.isEmpty()); } public void testGetNestedElementMap() { Map elemMap = getExpectedNestedElements(); Map actualMap = ih.getNestedElementMap(); for (Iterator i = actualMap.entrySet().iterator(); i.hasNext();) { Map.Entry entry = (Map.Entry) i.next(); String elemName = (String) entry.getKey(); Class elemClass = (Class) elemMap.get(elemName); assertNotNull("Support for " + elemName + " in IntrospectionHelperTest?", elemClass); assertEquals("Type of " + elemName, elemClass, entry.getValue()); elemMap.remove(elemName); } assertTrue("Found all", elemMap.isEmpty()); // Check it's a read-only map. try { actualMap.clear(); } catch (UnsupportedOperationException e) {} } public void testGetElementMethod() { assertElemMethod("six", "createSix", String.class, null); assertElemMethod("thirteen", "addThirteen", null, StringBuffer.class); assertElemMethod("fourteen", "addFourteen", null, StringBuffer.class); assertElemMethod("fifteen", "createFifteen", StringBuffer.class, null); } private void assertElemMethod(String elemName, String methodName, Class returnType, Class methodArg) { Method m = ih.getElementMethod(elemName); assertEquals("Method name", methodName, m.getName()); Class expectedReturnType = (returnType == null)? Void.TYPE: returnType; assertEquals("Return type", expectedReturnType, m.getReturnType()); Class[] args = m.getParameterTypes(); if (methodArg != null) { assertEquals("Arg Count", 1, args.length); assertEquals("Arg Type", methodArg, args[0]); } else { assertEquals("Arg Count", 0, args.length); } } public Object createTwo(String s) { return null; } public void createThree() {} public Object[] createFour() { return null; } public int createFive() { return 0; } public String createSix() { return "test"; } public StringBuffer createFifteen() { throw new NullPointerException(); } public void addSeven(String s, String s2) {} public void addEight() {} public String addNine(String s) { return null; } public void addTen(String[] s) {} public void addEleven(int i) {} public void addTwelve(Class c) {} public void addThirteen(StringBuffer sb) { sb.append("test"); } public void addFourteen(StringBuffer s) { throw new NullPointerException(); } public void testAttributeSetters() throws BuildException { try { ih.setAttribute(p, this, "one", "test"); fail("setOne doesn't exist"); } catch (BuildException be) { } try { ih.setAttribute(p, this, "two", "test"); fail("setTwo returns non void"); } catch (BuildException be) { } try { ih.setAttribute(p, this, "three", "test"); fail("setThree takes no args"); } catch (BuildException be) { } try { ih.setAttribute(p, this, "four", "test"); fail("setFour takes two args"); } catch (BuildException be) { } try { ih.setAttribute(p, this, "five", "test"); fail("setFive takes array arg"); } catch (BuildException be) { } try { ih.setAttribute(p, this, "six", "test"); fail("Project doesn't have a String constructor"); } catch (BuildException be) { } ih.setAttribute(p, this, "seven", "2"); try { ih.setAttribute(p, this, "seven", "3"); fail("2 shouldn't be equals to three"); } catch (BuildException be) { assertTrue(be.getException() instanceof AssertionFailedError); } ih.setAttribute(p, this, "eight", "2"); try { ih.setAttribute(p, this, "eight", "3"); fail("2 shouldn't be equals to three - as int"); } catch (BuildException be) { assertTrue(be.getException() instanceof AssertionFailedError); } ih.setAttribute(p, this, "nine", "2"); try { ih.setAttribute(p, this, "nine", "3"); fail("2 shouldn't be equals to three - as Integer"); } catch (BuildException be) { assertTrue(be.getException() instanceof AssertionFailedError); } ih.setAttribute(p, this, "ten", "2"); try { ih.setAttribute(p, this, "ten", "3");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -