final Factory.LocalMachines localMachines = threadLocal(); localMachines.addMachine(customizerMachine);
Factory.LocalMachines.threadLocal() .set("OutPrintStreamComponent", new LocalStreamComponent(out, outPrintStream)) .set("ErrPrintStreamComponent", new LocalStreamComponent(err, errPrintStream))
private static Factory.Builder newFactoryBuilder(String serverId) { Factory.Builder builder = newFactoryBuilder(); if (serverId != null) { builder.addLocalMachines(Factory.LocalMachines.contextLocal(serverId)); // make server id and base url accessible in the factory components Optional<WebServer> serverById = WebServers.getServerById(serverId); builder .addMachine(new SingletonFactoryMachine<>( 0, NamedComponent.of(String.class, "restx.server.id", serverId))) .addMachine(new SingletonFactoryMachine<>( 0, NamedComponent.of(String.class, "restx.server.baseUrl", serverById.isPresent() ? serverById.get().baseUrl() : ""))) .addMachine(new SingletonFactoryMachine<>( 0, NamedComponent.of(String.class, "restx.server.port", serverById.isPresent() ? String.valueOf(serverById.get().getPort()) : ""))) .addMachine(new SingletonFactoryMachine<>( 0, NamedComponent.of(String.class, "restx.server.type", serverById.isPresent() ? serverById.get().getServerType() : ""))) ; } return builder; }
Factory.LocalMachines.threadLocal() .set("OutPrintStreamComponent", new LocalStreamComponent(out, outPrintStream)) .set("ErrPrintStreamComponent", new LocalStreamComponent(err, errPrintStream))
@Override public void route(final RestxRequest restxRequest, final RestxResponse restxResponse) throws IOException { Factory.LocalMachines.threadLocal().set("RecordedSpecsRepository", repository);
@Override public void route(final RestxRequest restxRequest, final RestxResponse restxResponse) throws IOException { Factory.LocalMachines.threadLocal().set("RecordedSpecsRepository", repository);
@Override public AutoCloseable recordIn(Map<String, Given> givens) { final Tape tape = new Tape(givens); final Factory.Query<Mapper> mapperQuery = Factory.Query.byName(Name.of(Mapper.class, "Mapper")); Factory.LocalMachines.threadLocal().addMachine( new SingleNameFactoryMachine<>(0, new StdMachineEngine<ComponentCustomizerEngine>( Name.of(ComponentCustomizerEngine.class, "JongoCollectionSequenceSupplier"), BoundlessComponentBox.FACTORY) { @Override protected ComponentCustomizerEngine doNewComponent(SatisfiedBOM satisfiedBOM) { final Mapper mapper = satisfiedBOM.getOne(mapperQuery).get().getComponent(); final ObjectIdUpdater objectIdUpdater = mapper.getObjectIdUpdater(); return new SingleComponentClassCustomizerEngine<JongoCollection>(0, JongoCollection.class) { @Override public NamedComponent<JongoCollection> customize(NamedComponent<JongoCollection> namedComponent) { final JongoCollection collection = namedComponent.getComponent(); return new NamedComponent<>(namedComponent.getName(), new SequencingJongoCollection(tape, collection, mapper, objectIdUpdater)); } }; } @Override public BillOfMaterials getBillOfMaterial() { return BillOfMaterials.of(mapperQuery); } })); return tape; }
private void runSpec(RestxSpec restxSpec, ImmutableMap<String, String> params) { List<GivenCleaner> givenCleaners = newArrayList(); try { for (Given given : restxSpec.getGiven()) { Set<GivenRunner<Given>> runnersFor = findRunnersFor(given); if (runnersFor.isEmpty()) { throw new IllegalStateException( "no runner found for given " + given + ". double check your classpath and factory settings."); } for (GivenRunner<Given> runner : runnersFor) { givenCleaners.add(runner.run(given, params)); } } for (When when : restxSpec.getWhens()) { Optional<WhenChecker<When>> checkerFor = findCheckerFor(when); if (!checkerFor.isPresent()) { throw new IllegalStateException("no checker found for when " + when + "." + " double check your classpath and factory settings."); } checkerFor.get().check(when, params); } } finally { for (GivenCleaner givenCleaner : givenCleaners) { givenCleaner.cleanUp(); } overrideComponents().clear(); } }
@Override public AutoCloseable recordIn(final Map<String, Given> givens) { final Tape tape = new Tape(givens); Factory.LocalMachines.threadLocal().addMachine( new SingleNameFactoryMachine<>(0, new NoDepsMachineEngine<ComponentCustomizerEngine>( Name.of(ComponentCustomizerEngine.class, "UUIDGeneratorSequenceSupplier"), BoundlessComponentBox.FACTORY) { @Override protected ComponentCustomizerEngine doNewComponent(SatisfiedBOM satisfiedBOM) { return new SingleComponentClassCustomizerEngine<UUIDGenerator>(0, UUIDGenerator.class) { @Override public NamedComponent<UUIDGenerator> customize(final NamedComponent<UUIDGenerator> namedComponent) { return new NamedComponent<>(namedComponent.getName(), new UUIDGenerator() { @Override public String doGenerate() { String uuid = namedComponent.getComponent().doGenerate(); tape.recordGeneratedId(uuid); return uuid; } }); } }; } })); return tape; }
private void runSpec(RestxSpec restxSpec, ImmutableMap<String, String> params) { List<GivenCleaner> givenCleaners = newArrayList(); try { for (Given given : restxSpec.getGiven()) { Set<GivenRunner<Given>> runnersFor = findRunnersFor(given); if (runnersFor.isEmpty()) { throw new IllegalStateException( "no runner found for given " + given + ". double check your classpath and factory settings."); } for (GivenRunner<Given> runner : runnersFor) { givenCleaners.add(runner.run(given, params)); } } for (When when : restxSpec.getWhens()) { Optional<WhenChecker<When>> checkerFor = findCheckerFor(when); if (!checkerFor.isPresent()) { throw new IllegalStateException("no checker found for when " + when + "." + " double check your classpath and factory settings."); } checkerFor.get().check(when, params); } } finally { for (GivenCleaner givenCleaner : givenCleaners) { givenCleaner.cleanUp(); } overrideComponents().clear(); } }
@Override public AutoCloseable recordIn(Map<String, Given> givens) { final Tape tape = new Tape(givens); final Factory.Query<Mapper> mapperQuery = Factory.Query.byName(Name.of(Mapper.class, "Mapper")); Factory.LocalMachines.threadLocal().addMachine( new SingleNameFactoryMachine<>(0, new StdMachineEngine<ComponentCustomizerEngine>( Name.of(ComponentCustomizerEngine.class, "JongoCollectionSequenceSupplier"), BoundlessComponentBox.FACTORY) { @Override protected ComponentCustomizerEngine doNewComponent(SatisfiedBOM satisfiedBOM) { final Mapper mapper = satisfiedBOM.getOne(mapperQuery).get().getComponent(); final ObjectIdUpdater objectIdUpdater = mapper.getObjectIdUpdater(); return new SingleComponentClassCustomizerEngine<JongoCollection>(0, JongoCollection.class) { @Override public NamedComponent<JongoCollection> customize(NamedComponent<JongoCollection> namedComponent) { final JongoCollection collection = namedComponent.getComponent(); return new NamedComponent<>(namedComponent.getName(), new SequencingJongoCollection(tape, collection, mapper, objectIdUpdater)); } }; } @Override public BillOfMaterials getBillOfMaterial() { return BillOfMaterials.of(mapperQuery); } })); return tape; }
@Override public AutoCloseable recordIn(final Map<String, Given> givens) { final Tape tape = new Tape(givens); Factory.LocalMachines.threadLocal().addMachine( new SingleNameFactoryMachine<>(0, new NoDepsMachineEngine<ComponentCustomizerEngine>( Name.of(ComponentCustomizerEngine.class, "UUIDGeneratorSequenceSupplier"), BoundlessComponentBox.FACTORY) { @Override protected ComponentCustomizerEngine doNewComponent(SatisfiedBOM satisfiedBOM) { return new SingleComponentClassCustomizerEngine<UUIDGenerator>(0, UUIDGenerator.class) { @Override public NamedComponent<UUIDGenerator> customize(final NamedComponent<UUIDGenerator> namedComponent) { return new NamedComponent<>(namedComponent.getName(), new UUIDGenerator() { @Override public String doGenerate() { String uuid = namedComponent.getComponent().doGenerate(); tape.recordGeneratedId(uuid); return uuid; } }); } }; } })); return tape; }
private static Factory.Builder newFactoryBuilder(String serverId) { Factory.Builder builder = newFactoryBuilder(); if (serverId != null) { builder.addLocalMachines(Factory.LocalMachines.contextLocal(serverId)); // make server id and base url accessible in the factory components Optional<WebServer> serverById = WebServers.getServerById(serverId); builder .addMachine(new SingletonFactoryMachine<>( 0, NamedComponent.of(String.class, "restx.server.id", serverId))) .addMachine(new SingletonFactoryMachine<>( 0, NamedComponent.of(String.class, "restx.server.baseUrl", serverById.isPresent() ? serverById.get().baseUrl() : ""))) .addMachine(new SingletonFactoryMachine<>( 0, NamedComponent.of(String.class, "restx.server.port", serverById.isPresent() ? String.valueOf(serverById.get().getPort()) : ""))) .addMachine(new SingletonFactoryMachine<>( 0, NamedComponent.of(String.class, "restx.server.type", serverById.isPresent() ? serverById.get().getServerType() : ""))) ; } return builder; }
public RunningServer(WebServer server, RestxSpecRunner runner, RestxSpecRepository repository, UUIDGenerator uuidGenerator, RestxErrors errors, RunningServerSettings settings) { this.server = server; this.runner = runner; this.repository = repository; this.uuidGenerator = uuidGenerator; this.errors = errors; storeLocation = Paths.get(settings.targetTestsRoot()); objectMapper = new ObjectMapper(); objectMapper.registerModule(new JodaModule()); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); lastResults = loadLastResults(); System.setOut(out); System.setErr(err); Factory.LocalMachines.contextLocal(server.getServerId()).addMachine( new SingletonFactoryMachine<>(0, NamedComponent.of(RunningServer.class, "RunningServer", this))); }
public RunningServer(WebServer server, RestxSpecRunner runner, RestxSpecRepository repository, UUIDGenerator uuidGenerator, RestxErrors errors, RunningServerSettings settings) { this.server = server; this.runner = runner; this.repository = repository; this.uuidGenerator = uuidGenerator; this.errors = errors; storeLocation = Paths.get(settings.targetTestsRoot()); objectMapper = new ObjectMapper(); objectMapper.registerModule(new JodaModule()); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); lastResults = loadLastResults(); System.setOut(out); System.setErr(err); Factory.LocalMachines.contextLocal(server.getServerId()).addMachine( new SingletonFactoryMachine<>(0, NamedComponent.of(RunningServer.class, "RunningServer", this))); }
/** * An alias for threadLocal() which makes the intention of using it to override components clearer. * * It's usually used like this: * <code> * overrideComponents().set("componentName", "componentValue"); * </code> * * Note that it doesn't do anything special to actually override components: they will be used only in Factory * relying on threadLocal() LocalMachines is built after the call. * * @return LocalMachines associated with current thread. */ public static LocalMachines overrideComponents() { return threadLocal(); }
public HttpRequest http(String method, String url) { HttpRequest httpRequest = new HttpRequest(baseUrl + root(url), method) .header("RestxThreadLocal", Factory.LocalMachines.threadLocal().getId()) .header("RestxBlade", Blade.current()) ; if (!cookies.isEmpty()) { StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : cookies.entrySet()) { sb.append(entry.getKey()).append("=\"").append(entry.getValue().replace("\"", "\\\"")).append("\"; "); } sb.setLength(sb.length() - 2); httpRequest.header("Cookie", sb.toString()); } return httpRequest; }
private static Factory.Builder newFactoryBuilder(String serverId, Optional<String> bladeId, Optional<String> threadLocalId, String mode) { Factory.Builder builder = newFactoryBuilder(serverId); if (threadLocalId.isPresent()) { builder.addLocalMachines(Factory.LocalMachines.threadLocalFrom(threadLocalId.get())); } if (bladeId.isPresent()) { builder.addLocalMachines(Factory.LocalMachines.contextLocal(Blade.contextId(serverId, bladeId.get()))); } builder.addMachine(new SingletonFactoryMachine<>( -100000, NamedComponent.of(String.class, "restx.mode", mode))); return builder; }
private static Factory.Builder newFactoryBuilder(String serverId, Optional<String> bladeId, Optional<String> threadLocalId, String mode) { Factory.Builder builder = newFactoryBuilder(serverId); if (threadLocalId.isPresent()) { builder.addLocalMachines(Factory.LocalMachines.threadLocalFrom(threadLocalId.get())); } if (bladeId.isPresent()) { builder.addLocalMachines(Factory.LocalMachines.contextLocal(Blade.contextId(serverId, bladeId.get()))); } builder.addMachine(new SingletonFactoryMachine<>( -100000, NamedComponent.of(String.class, "restx.mode", mode))); return builder; }
public HttpRequest http(String method, String url) { HttpRequest httpRequest = new HttpRequest(baseUrl + root(url), method) .header("RestxThreadLocal", Factory.LocalMachines.threadLocal().getId()) .header("RestxBlade", Blade.current()) ; if (!cookies.isEmpty()) { StringBuilder sb = new StringBuilder(); for (Map.Entry<String, String> entry : cookies.entrySet()) { sb.append(entry.getKey()).append("=\"").append(entry.getValue().replace("\"", "\\\"")).append("\"; "); } sb.setLength(sb.length() - 2); httpRequest.header("Cookie", sb.toString()); } return httpRequest; }