Ver código fonte

junit: removed type deducation

Ranides Atterwim 10 anos atrás
pai
commit
2be2f89de4

+ 5 - 11
assira.junit/src/main/java/net/ranides/assira/junit/TestContractRunner.java

@@ -107,7 +107,7 @@ public final class TestContractRunner {
         return this;
     }
 	
-	public TesterWrapper appendWrapper(Object tester) {
+	TesterWrapper appendWrapper(Object tester) {
         TesterWrapper wrapper = new TesterWrapper(tester);
         testers.add(wrapper);
         for(Method m : tester.getClass().getMethods()) {
@@ -142,7 +142,7 @@ public final class TestContractRunner {
 		return this;
 	}
 	
-	@SuppressWarnings("unchecked")
+    @SuppressWarnings("unchecked")
 	public <T,R> TestContractRunner function(Function<T, R> function) {
 		this.function = (Function)function;
 		return this;
@@ -161,15 +161,9 @@ public final class TestContractRunner {
 		if(null == supplier && null == function) {
 			throw new AssertionError("You have to set Supplier or Function.");
 		}
-		Class<?> type;
-		if(null != supplier) {
-			type = supplier.get().getClass();
-		} else {
-			type = TestUtils.typeof(function);
-			if(debug) {
-				output.printf("Function is used for type deduction. Result: %s%n", type);
-			}
-		}
+		assert null != supplier: "Unknown supplier/function type.";
+        Class<?> type = supplier.get().getClass();
+
         ICounter counter = new ICounter();
         List<String> failed = new ArrayList<>();
         methods.stream().filter(m -> m.accept(type)).forEach(m -> {

+ 0 - 77
assira.junit/src/main/java/net/ranides/assira/junit/TestUtils.java

@@ -7,9 +7,7 @@
 
 package net.ranides.assira.junit;
 
-import java.lang.reflect.Array;
 import java.lang.reflect.Field;
-import java.lang.reflect.Member;
 import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
@@ -19,7 +17,6 @@ import java.util.function.Function;
 import java.util.function.Supplier;
 import java.util.regex.Pattern;
 import javax.annotation.Resource;
-import sun.reflect.ConstantPool;
 
 /**
  *
@@ -55,58 +52,6 @@ final class TestUtils {
         return cparam;
     }
 	
-	public static Class<?> typeof(Function<?,?> function) {
-		Class<?> clazz = function.getClass();
-		if(!clazz.isSynthetic()) {
-			return typeof_generic(clazz);
-		} else {
-			return typeof_lambda(clazz);
-		}
-	}
-	
-	private static Class<?> typeof_generic(Class<?> clazz) {
-		ParameterizedType it = getGenericInterface(clazz, Function.class);
-		java.lang.reflect.Type pt = it.getActualTypeArguments()[1];
-        if(pt instanceof Class<?>) {
-            return (Class<?>)pt;
-        }
-        if(pt instanceof ParameterizedType) {
-            return (Class<?>)(((ParameterizedType)pt).getRawType());
-        }
-        throw new AssertionError("Deduction of return type failed. Unsupported function: " + clazz);
-	}
-	
-	private static ParameterizedType getGenericInterface(Class<?> type, Class<?> intface) {
-		for(java.lang.reflect.Type t : type.getGenericInterfaces()) {
-			ParameterizedType pt = (ParameterizedType)t;
-			if( intface.equals(pt.getRawType()) ) {
-				return pt;
-			}
-		}
-		throw new AssertionError(type.getName() + " does not implement " + intface.getName());
-	}
-	
-	private static Class<?> typeof_lambda(Class<?> clazz) {
-		// dość generyczne filtrowanie, które chyba eliminuje różne cuda 
-		// związane z wewnętrzną implementacją lambdy. uwaga! to działa TYLKO
-		// dla lambdy.
-
-		ConstantPool cp = Li.get(clazz);
-		
-		for(int i=0, n=cp.getSize(); i<n; i++) {
-			try {
-				Member member = cp.getMethodAt(i);
-				if(member instanceof Method) {
-					Method method = (Method)member;
-					return method.getReturnType();
-				}
-			} catch(IllegalArgumentException $0) {
-				continue;
-			}
-		}
-		throw new AssertionError("Deduction of return type failed. Unsupported lambda: " + clazz);
-	}
-	
 	private static Class<?> typeof_supplier(Method method) throws AssertionError {
         ParameterizedType gparam = (ParameterizedType)method.getGenericParameterTypes()[0];
         Type tparam = gparam.getActualTypeArguments()[0];
@@ -285,26 +230,4 @@ final class TestUtils {
 
     }
 	
-	private static final class Li { // NOPMD - lazy init idiom
-		
-		public static final Method GET_CONSTANT_POOL;
-		
-		static {
-			try {
-				GET_CONSTANT_POOL = Class.class.getDeclaredMethod("getConstantPool");
-				GET_CONSTANT_POOL.setAccessible(true);
-			} catch(ReflectiveOperationException cause) {
-				throw rethrow(cause);
-			}
-		}
-		
-		public static ConstantPool get(Class<?> type) {
-			try {
-				return (ConstantPool) GET_CONSTANT_POOL.invoke(type);
-			} catch (ReflectiveOperationException cause) {
-				throw rethrow(cause);
-			}
-		}
-		
-	}
 }

+ 9 - 8
assira.junit/src/test/java/net/ranides/assira/junit/TestContractRunnerTest.java

@@ -155,14 +155,15 @@ public class TestContractRunnerTest {
     @Test
     public void testFunction() {
 		
-		TesterObjects.OUT.clear();
-		new TestContractRunner(System.err)
-			.ignore("ForFunction.list")
-            .append(new Testers.ForFunction())
-            .function((args) -> new ArrayList<>())
-			.run();
-		assertEquals(EXPECTED_FUNCTION_I, TesterObjects.OUT);
-		
+        assertThrows(AssertionError.class, ()->{
+            TesterObjects.OUT.clear();
+            new TestContractRunner(System.err)
+                .ignore("ForFunction.list")
+                .append(new Testers.ForFunction())
+                .function((args) -> new ArrayList<>())
+                .run();
+        });
+            
         TesterObjects.OUT.clear();
         new TestContractRunner(System.err)
             .append(new Testers.ForFunction())

+ 2 - 7
assira.junit/src/test/java/net/ranides/assira/junit/mockup/Testers.java

@@ -47,13 +47,8 @@ public class Testers {
 
         @TestContract
         public void supplier(Supplier<List<String>> s) {
-			try {
-				s.get();
-				TesterObjects.OUT.add("ForFunction.supplier");
-			} catch(AssertionError ae) {
-				TesterObjects.OUT.add("ForFunction.supplier=null");
-			}
-            
+            s.get();
+            TesterObjects.OUT.add("ForFunction.supplier");
         }
         
         @TestContract