/** * Create a virtual instance of the given type that delegates to the given instance. * * @param <T> the type of the object being virtualized * @param type the class of the type being virtualized * @param delegate the instance calls are delegated to * @return a virtual instance */ default <T> T createVirtualSut(Class<? extends T> type, T delegate) { return createVirtual(type, delegate); }
/** * Provides a fully-constructed and injected instance of the service from the service * instance. * * @return an instance of the service. */ @Override public Object get() { return qualifiers == null || qualifiers.length == 0 ? serviceInstance.getService(type) : serviceInstance.getService(type, qualifiers); }
/** * Constructs a new runtime exception with the specified detail message. * * @param message the message details * @return a new testify exception */ public static TestifyException of(String message) { return new TestifyException(message); }
void createClient(TestContext testContext, TestConfigurer testConfigurer, ClientProvider clientProvider, Application application) { URI baseURI = testContext.getProperty(TestContextProperties.SERVER_BASE_URI); //configure and create the client Object clientConfig = clientProvider.configure(testContext, application, baseURI); clientConfig = testConfigurer.configure(testContext, clientConfig); ClientInstance<Object, Object> clientInstance = clientProvider.create(testContext, application, baseURI, clientConfig); testContext .addProperty(TestContextProperties.CLIENT_INSTANCE, clientInstance) .addProperty(clientInstance.getFqn(), clientInstance.getProperties()) .addProperty(TestContextProperties.CLIENT, clientInstance .getClient().getValue()); clientInstance.getClientSupplier() .map(Instance::getValue) .ifPresent(value -> { testContext.addProperty(TestContextProperties.CLIENT_SUPPLIER, value); }); }
void createServer(TestContext testContext, TestConfigurer testConfigurer, ServerProvider serverProvider, Application application) throws Exception { //configure and start the server Object serverConfig = serverProvider.configure(testContext); serverConfig = testConfigurer.configure(testContext, serverConfig); ServerInstance serverInstance = serverProvider.start(testContext, application, serverConfig); testContext .addProperty(TestContextProperties.SERVER_INSTANCE, serverInstance) .addProperty(serverInstance.getFqn(), serverInstance.getProperties()) .addProperty(TestContextProperties.SERVER_BASE_URI, serverInstance.getBaseURI()) .addProperty(TestContextProperties.SERVER, serverInstance .getServer().getValue()); }
@Override public ServiceLocator create(TestContext testContext) { TestConfigurer testConfigurer = testContext.getTestConfigurer(); ServiceLocatorFactory locatorFactory = ServiceLocatorFactory.getInstance(); ServiceLocator serviceLocator = locatorFactory.create(testContext.getName()); return testConfigurer.configure(testContext, serviceLocator); }
@Override public Object postProcessAfterInitialization(Object bean, String beanName) { Class<? extends Object> beanClass = bean.getClass(); //XXX: DO NOT remove this method and code as it is required to extract //collaborators before the sut class is proxied down stream. Once the //sut class is proxied we will not be able to access the sut class //fields with ease. testContext.getSutDescriptor().ifPresent(sutDescriptor -> { if (sutDescriptor.isSutClass(beanClass)) { sutDescriptor.setValue(testContext.getTestInstance(), bean); } }); return bean; }
@Override public InjectionManager create(TestContext testContext) { TestConfigurer testConfigurer = testContext.getTestConfigurer(); InjectionManager injectionManager = Injections.createInjectionManager(); return testConfigurer.configure(testContext, injectionManager); }
/** * Execute the given consumer function with the {@link #getServer()} value and * {@link #getBaseURI()} as its parameters. * * @param consumer the consumer function */ default void command(BiConsumer<T, URI> consumer) { consumer.accept(getServer().getValue(), getBaseURI()); }
/** * Execute the given consumer function with the {@link #getClient()} value and * {@link #getClientSupplier()} value as its parameters. If client supplier is not present * {@code null} will passed in as client supplier value. * * @param consumer the consumer function */ default void command(BiConsumer<C, P> consumer) { consumer.accept( getClient().getValue(), getClientSupplier().map(Instance::getValue).orElse(null) ); } }
/** * Atomically execute the given function with the {@link #getValue() } as its parameters. * * @param <R> function return type * @param function the function * @return the result of executing the function */ default <R> R operation(Function<T, R> function) { synchronized (this) { return function.apply(getValue()); } }
@Override public void destroy(ClientInstance<WebTarget, Client> clientInstance) { clientInstance.getClientSupplier() .map(Instance::getValue) .map(Client.class::cast) .ifPresent(Client::close); }
@Override public void stop(ServerInstance<HttpServer> serverInstance) { if (serverInstance != null) { serverInstance.command((httpServer, baseURI) -> { httpServer.shutdown(); }); } }
/** * Get {@link Sut} annotation. * * @return sut annotation */ default Sut getSut() { return getMember().getDeclaredAnnotation(Sut.class); }
/** * Execute the given function with the {@link #getServer()} value and {@link #getBaseURI()} * as its parameters. * * @param <R> function return type * @param function the function * @return the result of executing the function */ default <R> R query(BiFunction<T, URI, R> function) { return function.apply(getServer().getValue(), getBaseURI()); }
/** * Execute the given function with the {@link #getValue() } as its parameters. * * @param <R> function return type * @param function the function * @return the result of executing the function */ default <R> R query(Function<T, R> function) { return function.apply(getValue()); }
@Override public void destroy(ClientInstance<AbstractStub, ManagedChannel> clientInstance) { clientInstance.getClientSupplier() .map(Instance::getValue) .map(ManagedChannel.class::cast) .ifPresent(ManagedChannel::shutdownNow); }
/** * Constructs a new runtime exception with the specified detail cause. * * @param cause the cause of the exception * @return a new logged exception instance */ public static TestifyException of(Throwable cause) { return new TestifyException(cause); }
/** * Constructs a new runtime exception with the specified detail message and cause. * * @param message the message details * @param cause the cause of the exception * @return a new testify exception */ public static TestifyException of(String message, Throwable cause) { return new TestifyException(message, cause); }