001/** 002 * Copyright (c) 2007-2008, Regents of the University of Colorado 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions are met: 007 * 008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 011 * 012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 022 * POSSIBILITY OF SUCH DAMAGE. 023 */ 024package org.cleartk.test.util; 025 026import java.lang.reflect.Constructor; 027import java.lang.reflect.Method; 028import java.util.HashMap; 029import java.util.Iterator; 030import java.util.Map; 031 032import org.apache.uima.cas.Feature; 033import org.apache.uima.cas.FeatureStructure; 034import org.apache.uima.cas.Type; 035import org.apache.uima.jcas.JCas; 036import org.apache.uima.jcas.cas.FSArray; 037import org.apache.uima.jcas.cas.StringArray; 038import org.apache.uima.jcas.cas.TOP; 039import org.apache.uima.jcas.tcas.Annotation; 040import org.junit.Assert; 041 042/** 043 * <br> 044 * Copyright (c) 2007-2011, Regents of the University of Colorado <br> 045 * All rights reserved. 046 * 047 * 048 * @author Steven Bethard 049 */ 050 051public class TypeTestUtil { 052 053 public static void testType(JCas jcas, TOP top) throws Exception { 054 Class<?> cls = top.getClass(); 055 if (top instanceof Annotation) { 056 testAnnotationType(jcas, (Annotation) top); 057 } 058 Type type = jcas.getTypeSystem().getType(cls.getName()); 059 for (Object obj : type.getFeatures()) { 060 Feature feature = (Feature) obj; 061 if (feature.getDomain().equals(type)) { 062 invokeMethods(cls, type, top, jcas, feature.getShortName()); 063 } 064 } 065 } 066 067 private static void testAnnotationType(JCas jcas, Annotation annotation) throws Exception { 068 Class<?> annotationClass = annotation.getClass(); 069 070 Constructor<?> constructor = annotationClass.getConstructor(JCas.class); 071 annotation = (Annotation) constructor.newInstance(jcas); 072 Assert.assertEquals(0, annotation.getBegin()); 073 Assert.assertEquals(0, annotation.getEnd()); 074 075 annotation.setBegin(15); 076 Assert.assertEquals(15, annotation.getBegin()); 077 078 annotation.setEnd(12); 079 Assert.assertEquals(12, annotation.getEnd()); 080 081 constructor = annotationClass.getConstructor(JCas.class, int.class, int.class); 082 annotation = (Annotation) constructor.newInstance(jcas, 4, 6); 083 Assert.assertEquals(4, annotation.getBegin()); 084 Assert.assertEquals(6, annotation.getEnd()); 085 } 086 087 private static Class<?> findFSType(Class<?> annotationClass, String setterName) { 088 for (Method method : annotationClass.getMethods()) { 089 if (method.getName().equals(setterName)) { 090 Class<?>[] types = method.getParameterTypes(); 091 if (types.length == 2) { 092 return method.getParameterTypes()[1]; 093 } 094 } 095 } 096 throw new RuntimeException("could not find FS type for setter:" + setterName); 097 } 098 099 private static void invokeMethods(Class<?> cls, Type type, TOP top, JCas jcas, String featureName) 100 throws Exception { 101 Map<Class<?>, Object> defaultValues = new HashMap<Class<?>, Object>(); 102 defaultValues.put(int.class, 0); 103 defaultValues.put(boolean.class, false); 104 defaultValues.put(double.class, 0.0); 105 defaultValues.put(JCas.class, jcas); 106 107 String suffix = featureName.substring(0, 1).toUpperCase() + featureName.substring(1); 108 for (Method method : cls.getMethods()) { 109 String name = method.getName(); 110 Class<?>[] types = method.getParameterTypes(); 111 if (name.endsWith(suffix) && types.length == 1) { 112 if (types[0].equals(FSArray.class)) { 113 FSArray value = new FSArray(jcas, 1); 114 Class<?> fsType = findFSType(cls, name); 115 Object fs = fsType.getConstructor(JCas.class).newInstance(jcas); 116 value.set(0, (FeatureStructure) fs); 117 method.invoke(top, new Object[] { value }); 118 method = top.jcasType.getClass().getMethod("set" + suffix, int.class, int.class); 119 method.invoke(top.jcasType, new Object[] { top.getAddress(), value.getAddress() }); 120 } 121 if (types[0].equals(StringArray.class)) { 122 StringArray value = new StringArray(jcas, 1); 123 value.set(0, "foo"); 124 method.invoke(top, new Object[] { value }); 125 method = top.jcasType.getClass().getMethod(name, int.class, int.class); 126 method.invoke(top.jcasType, new Object[] { top.getAddress(), value.getAddress() }); 127 } 128 } 129 } 130 for (Method method : cls.getMethods()) { 131 String name = method.getName(); 132 Class<?>[] types = method.getParameterTypes(); 133 if (name.equals("get" + suffix) || name.equals("set" + suffix)) { 134 if (types.length != 1 135 || (!types[0].equals(FSArray.class) && !types[0].equals(StringArray.class))) { 136 Class<?>[] jcasTypes = new Class<?>[types.length + 1]; 137 Object[] jcasTypeValues = new Object[types.length + 1]; 138 Object[] values = new Object[types.length]; 139 for (int i = 0; i < values.length; i++) { 140 values[i] = defaultValues.get(types[i]); 141 if (TOP.class.isAssignableFrom(types[i])) { 142 jcasTypeValues[i + 1] = values[i] == null ? 0 : ((TOP) values[i]).getAddress(); 143 jcasTypes[i + 1] = int.class; 144 } else { 145 jcasTypeValues[i + 1] = values[i]; 146 jcasTypes[i + 1] = types[i]; 147 } 148 149 } 150 method.invoke(top, values); 151 jcasTypes[0] = int.class; 152 jcasTypeValues[0] = top.getAddress(); 153 method = top.jcasType.getClass().getMethod(name, jcasTypes); 154 method.invoke(top.jcasType, jcasTypeValues); 155 } 156 } 157 } 158 } 159 160 public static void testTypeSystem(JCas jCas) throws Exception { 161 Iterator<Type> types = jCas.getTypeSystem().getTypeIterator(); 162 163 while (types.hasNext()) { 164 try { 165 Type type = types.next(); 166 @SuppressWarnings("unchecked") 167 Class<? extends TOP> cls = (Class<? extends TOP>) Class.forName(type.getName()); 168 TOP top = cls.getConstructor(JCas.class).newInstance(jCas); 169 testType(jCas, top); 170 } catch (Exception e) { 171 continue; 172 } 173 } 174 175 } 176}