public static OMEROMetadataStoreClient mockStore(ServiceFactory sf, String password) throws Exception { System.setProperty("omero.testing", "true"); OmeroContext inner = sf.getContext(); OmeroContext outer = new OmeroContext( new String[] { "classpath:ome/services/messaging.xml", // To share events "classpath:ome/formats/fixture.xml", "classpath:ome/services/blitz-servantDefinitions.xml", "classpath:ome/services/throttling/throttling.xml", "classpath:ome/config.xml" }, false); outer.setParent(inner); outer.refresh(); EventContext ec = sf.getAdminService().getEventContext(); String username = ec.getCurrentUserName(); long groupid = ec.getCurrentGroupId(); MockFixture fixture = new MockFixture(new MockObjectTestCase() {}, outer); omero.client client = fixture.newClient(); // Fixing group permissions from 4.2.0 client .createSession(username, password) .setSecurityContext(new omero.model.ExperimenterGroupI(groupid, false)); OMEROMetadataStoreClient store = new OMEROMetadataStoreClient(); store.initialize(client); return store; }
/** * Determines the necessary arguments for the given method and calls with random values to test * the Ice mapping code. */ void callWithFuzz(Object service, Method method) throws Exception { Type[] parameterTypes = method.getGenericParameterTypes(); Object[] parameters = new Object[parameterTypes.length]; for (int i = 0; i < parameters.length; i++) { Type t = parameterTypes[i]; parameters[i] = makeFuzz(method, t); } Mock mock = fixture.blitzMock(service.getClass()); ArgumentsMatchBuilder builder = mock.expects(once()).method(method.getName()); Class<?> returnClass = method.getReturnType(); if (void.class.isAssignableFrom(returnClass)) { // nothing } else if (long.class.isAssignableFrom(returnClass)) { builder.will(returnValue(1L)); } else if (int.class.isAssignableFrom(returnClass)) { builder.will(returnValue(1)); } else if (double.class.isAssignableFrom(returnClass)) { builder.will(returnValue(0.0)); } else if (float.class.isAssignableFrom(returnClass)) { builder.will(returnValue(0.0f)); } else if (boolean.class.isAssignableFrom(returnClass)) { builder.will(returnValue(false)); } else { builder.will(returnValue(null)); } String msg = "Error running " + method + " with parameters " + Arrays.deepToString(parameters); try { method.invoke(service, parameters); } catch (InvocationTargetException ite) { Exception t = (Exception) ite.getCause(); if (t instanceof omero.ApiUsageException) { // Ok. This means our fuzz was bad, but we can try to improve it omero.ApiUsageException aue = (omero.ApiUsageException) t; if (aue.message.contains("does not specify a valid class")) { for (int i = 0; i < parameters.length; i++) { if (parameters[i] instanceof String) { parameters[i] = "Image"; } } // TODO } } else { throw new RuntimeException(msg, t); } } catch (IllegalArgumentException iae) { throw new RuntimeException(msg, iae); } }
@Test(groups = "integration") public void testByReflection() throws Exception { fixture = new MockFixture(this); ServiceFactoryPrx sf = fixture.createServiceFactory(); List<Method> factoryMethods = factoryMethods(); for (Method factoryMethod : factoryMethods) { Object service = factoryMethod.invoke(sf); // Filtering the blitz-only services for now if (service instanceof IScriptPrx || service instanceof GatewayPrx) { continue; } List<Method> serviceMethods = serviceMethods(service.getClass()); for (Method method : serviceMethods) { callWithFuzz(service, method); } } System.out.println("boo"); }
@AfterMethod(groups = "integration") public void shutdownFixture() { fixture.tearDown(); }