### Question:
AdaptationTree { public void addLeaf (T root, T leaf) { addLeaf (root).addLeaf (leaf); } AdaptationTree(T head);  AdaptationTree(AdaptationExecutionOperatorT operator); void setId(String id); String getId(); void addLeaf(T root, T leaf); AdaptationTree<T> addLeaf(T leaf); AdaptationTree<T> setAsParent(T parentRoot); T getHead(); AdaptationTree<T> getParent(); Collection<AdaptationTree<T>> getSubTrees(); AdaptationExecutionOperatorT getOperator(); @Override String toString(); boolean visit(IAdaptationVisitor<T> visitor);  }### Answer:
@Test public void testSequence () throws InterruptedException { AdaptationTree root = new AdaptationTree<> (AdaptationExecutionOperatorT.SEQUENCE); root.addLeaf (new ExecutableTest (1)); root.addLeaf (new ExecutableTest (2)); root.addLeaf (new ExecutableTest (3)); CountDownLatch countdownLatch = new CountDownLatch (1); DefaultAdaptationExecutorVisitor<IEvaluable> visitor = new DefaultAdaptationExecutorVisitor<IEvaluable> (root, new ThreadGroup ("execution"), "", countdownLatch, new TestCaseReportingPort ()) { @Override protected boolean evaluate (IEvaluable adaptation) throws RainbowException{ return (Boolean )adaptation.evaluate (null); } @Override protected DefaultAdaptationExecutorVisitor<IEvaluable> spawnNewExecutorForTree (AdaptationTree<IEvaluable> adt, ThreadGroup g, CountDownLatch doneSignal) { return null; } }; visitor.start (); countdownLatch.await (5, TimeUnit.SECONDS); assertTrue (numbers.size () == 3); assertTrue (numbers.get (0) == 1); assertTrue (numbers.get (1) == 2); assertTrue (numbers.get (2) == 3); }
@Test public void testSequenceFail () throws InterruptedException { AdaptationTree root = new AdaptationTree<> (AdaptationExecutionOperatorT.SEQUENCE_STOP_FAILURE); root.addLeaf (new ExecutableTest (1)); root.addLeaf (new ExecutableTest (2, false)); root.addLeaf (new ExecutableTest (3)); CountDownLatch countdownLatch = new CountDownLatch (1); DefaultAdaptationExecutorVisitor<IEvaluable> visitor = new TestAdaptationVisitor (root, new ThreadGroup ("execution"), countdownLatch); visitor.start (); countdownLatch.await (5, TimeUnit.SECONDS); assertTrue (numbers.size () == 2); assertTrue (numbers.get (0) == 1); assertTrue (numbers.get (1) == 2); }
@Test public void testSequenceSuccess () throws InterruptedException { AdaptationTree root = new AdaptationTree<> (AdaptationExecutionOperatorT.SEQUENCE_STOP_SUCCESS); root.addLeaf (new ExecutableTest (1)); root.addLeaf (new ExecutableTest (2, false)); root.addLeaf (new ExecutableTest (3)); CountDownLatch countdownLatch = new CountDownLatch (1); DefaultAdaptationExecutorVisitor<IEvaluable> visitor = new TestAdaptationVisitor (root, new ThreadGroup ("test"), countdownLatch); visitor.start (); countdownLatch.await (5, TimeUnit.SECONDS); assertTrue (numbers.size () == 1); assertTrue (numbers.get (0) == 1); }
@Test public void testParallel () throws InterruptedException { AdaptationTree root = new AdaptationTree<> (AdaptationExecutionOperatorT.PARALLEL); int number = 20; for (int i = 0; i < number; i++) { root.addLeaf (new ExecutableTest (i)); } CountDownLatch countdownLatch = new CountDownLatch (1); DefaultAdaptationExecutorVisitor<IEvaluable> visitor = new TestAdaptationVisitor (root, new ThreadGroup ("execution"), countdownLatch); visitor.start (); countdownLatch.await (5, TimeUnit.SECONDS); assertTrue (numbers.size () == number); }