/** * Uses 'loader' to load class 'clzz', and calls the static method 'method'. If the return value * does not equal 'value' (or if an exception is thrown), then a test failure is indicated. * * <p>If 'value' is null, then no equality check is performed -- the assertion fails only if an * exception is thrown. */ protected void assertStaticCallEquals( ClassLoader loader, Class clzz, String method, Object value) { java.lang.Class<?> cls = null; try { cls = java.lang.Class.forName(clzz.getName(), true, loader); } catch (ClassNotFoundException e) { } assertNotNull(cls); java.lang.reflect.Method m = null; try { m = cls.getMethod(method); } catch (NoSuchMethodException e) { } assertNotNull(m); try { Object res = m.invoke(null); assertNotNull(res); if (value != null) { assertEquals(res, value); } } catch (InvocationTargetException | IllegalAccessException e) { fail("Unexpected exception thrown: " + e.getCause(), e.getCause()); } }
@SuppressWarnings("unchecked") public <E extends Event> E getNextEvent(Class<E> eventClass) { checkInputEmpty(); log.info("Asserting output event equals {}", eventClass); Event next = eventQueue.removeFirst(); assertTrue( eventClass.isInstance(next), "Event " + next.getClass() + " doesn't match " + eventClass); return (E) next; }
protected static ReportItem assertReportContains( Collection<ReportItem> report, Class<? extends ReportItem> clazz, ItemStatus status, String text) { ReportItem i = loopItems(report.iterator(), clazz, status, text); assertNotNull(i, "Didn't find report of class " + clazz.getSimpleName()); return i; }
/* * Validate that readObject returns the correct value when a Struct is * next on the stream */ @Test() public void test11() throws Exception { Object[] attributes = new Object[] {"Bruce", "Wayne", 1939, "Batman"}; map.put(sqlType, Class.forName("util.SuperHero")); Struct struct = new StubStruct(sqlType, attributes); Object[] values = {struct}; SQLInputImpl sqli = new SQLInputImpl(values, map); Object o = sqli.readObject(); assertTrue(hero.equals(o)); }
/** Returns a set of tests to run for varied log configurations. */ protected Object[] testsFor(Class<? extends LogTest> testClass) throws Throwable { List<Object> tests = new ArrayList<>(); for (int i = 1; i < 10; i++) { LogTest test = testClass.newInstance(); test.entriesPerSegment = i; test.entryPadding = i / 3; tests.add(test); } return tests.toArray(new Object[tests.size()]); }
/** * Returns a class which has a static method with the same name as 'method', whose body creates an * new instance of 'specimen', casts it to 'iface' (including the type parameters) and invokes * 'method' upon it via an invokeinterface instruction with 'args' as function call parameters. */ private Class invokeInterfaceHarness( Class specimen, Extends iface, AbstractMethod method, String... args) { Interface istub = new Interface( iface.getType().getName(), iface.getType().getAccessFlags(), iface.getType().getParameters(), null, Arrays.asList((Method) method)); Class cstub = new Class(specimen.getName()); String params = toJoinedString(args, ", "); ConcreteMethod sm = new ConcreteMethod( "int", SourceModel.stdMethodName, String.format( "return ((%s)(new %s())).%s(%s);", iface.toString(), specimen.getName(), method.getName(), params), new AccessFlag("public"), new AccessFlag("static")); sm.suppressWarnings(); Class ii = new Class("II_" + specimen.getName() + "_" + iface.getType().getName(), sm); ii.addCompilationDependency(istub); ii.addCompilationDependency(cstub); ii.addCompilationDependency(method); return ii; }
/** * Returns a class which has a static method with the same name as 'method', whose body creates an * new instance of 'specimen' and invokes 'method' upon it via an invokevirtual instruction with * 'args' as function call parameters. * * <p>'returns' is a dummy return value that need only match 'methods' return type (it is only * used in the dummy class when compiling IV). */ private Class invokeVirtualHarness( Class specimen, ConcreteMethod method, String returns, String... args) { Method cm = new ConcreteMethod( method.getReturnType(), method.getName(), "return " + returns + ";", method.getElements()); Class stub = new Class(specimen.getName(), cm); String params = toJoinedString(args, ", "); ConcreteMethod sm = new ConcreteMethod( method.getReturnType(), method.getName(), String.format( "return (new %s()).%s(%s);", specimen.getName(), method.getName(), params), new AccessFlag("public"), new AccessFlag("static")); Class iv = new Class("IV_" + specimen.getName(), sm); iv.addCompilationDependency(stub); iv.addCompilationDependency(cm); return iv; }
/** * Creates a class which calls target::method(args) via invokevirtual, compiles and loads both the * new class and 'target', and then invokes the method. If an exception of type 'exceptionType' is * not thrown, then a test failure is indicated. */ public void assertThrows( java.lang.Class<?> exceptionType, Class target, ConcreteMethod method, String returns, String... args) { Compiler compiler = compilerLocal.get(); compiler.setFlags(compilerFlags()); Class iv = invokeVirtualHarness(target, method, returns, args); ClassLoader loader = compiler.compile(iv, target); java.lang.Class<?> cls = null; try { cls = java.lang.Class.forName(iv.getName(), true, loader); } catch (ClassNotFoundException e) { } assertNotNull(cls); java.lang.reflect.Method m = null; try { m = cls.getMethod(method.getName()); } catch (NoSuchMethodException e) { } assertNotNull(m); try { m.invoke(null); fail("Exception should have been thrown"); } catch (InvocationTargetException | IllegalAccessException e) { if (verboseLocal.get() == Boolean.TRUE) { System.out.println(e.getCause()); } assertTrue(exceptionType.isAssignableFrom(e.getCause().getClass())); } compiler.cleanup(); }
/** * Verify that a {@link Throwable} is a {@link TTransportException} wrapping the expected cause * * @param throwable The {@link Throwable} to check * @param expectedCause The expected cause of the {@link TTransportException} */ private void checkTransportException( Throwable throwable, Class<? extends Throwable> expectedCause) { assertNotNull(throwable); Throwable cause = throwable.getCause(); if (!(throwable instanceof TTransportException)) { fail("Exception of type " + throwable.getClass() + " when expecting a TTransportException"); } else if (!(expectedCause.isAssignableFrom(throwable.getCause().getClass()))) { fail( "TTransportException caused by " + cause.getClass() + " when expecting a TTransportException caused by " + expectedCause); } }
// Test to ensure that the inference verifier accepts older classfiles // with classes that implement interfaces with defaults. @Test(groups = "vm") public void testInferenceVerifier() { // interface I { int m() default { return 99; } } byte I_bytes[] = { (byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe, 0x00, 0x00, 0x00, 0x34, 0x00, 0x08, 0x07, 0x00, 0x06, 0x07, 0x00, 0x07, 0x01, 0x00, 0x03, 0x66, 0x6f, 0x6f, 0x01, 0x00, 0x03, 0x28, 0x29, 0x49, 0x01, 0x00, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x01, 0x00, 0x01, 0x49, 0x01, 0x00, 0x10, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x06, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x01, 0x00, 0x03, 0x00, 0x04, 0x00, 0x01, 0x00, 0x05, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x10, 0x63, (byte) 0xac, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // public class C implements I {} /* -target 1.5 */ byte C_bytes[] = { (byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe, 0x00, 0x00, 0x00, 0x31, 0x00, 0x0c, 0x0a, 0x00, 0x03, 0x00, 0x08, 0x07, 0x00, 0x09, 0x07, 0x00, 0x0a, 0x07, 0x00, 0x0b, 0x01, 0x00, 0x06, 0x3c, 0x69, 0x6e, 0x69, 0x74, 0x3e, 0x01, 0x00, 0x03, 0x28, 0x29, 0x56, 0x01, 0x00, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x0c, 0x00, 0x05, 0x00, 0x06, 0x01, 0x00, 0x01, 0x43, 0x01, 0x00, 0x10, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x01, 0x00, 0x01, 0x49, 0x00, 0x21, 0x00, 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x05, 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00, 0x00, 0x11, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x2a, (byte) 0xb7, 0x00, 0x01, (byte) 0xb1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; ClassLoader cl = new ClassLoader() { protected Class<?> findClass(String name) { if (name.equals("I")) { return defineClass("I", I_bytes, 0, I_bytes.length); } else if (name.equals("C")) { return defineClass("C", C_bytes, 0, C_bytes.length); } else { return null; } } }; try { Class.forName("C", true, cl); } catch (Exception e) { // unmodified verifier will throw VerifyError fail("No exception should be thrown"); } }