Connector { public static Processor connect(EventTracker tracker, Processor p1, int i, Processor p2, int j) { if (s_checkForTypes) { checkForException(p1, i, p2, j); } if (p1 == p2) { throw new SelfLoopException(p1, i, p2, j); } try { Pullable p1_out = p1.getPullableOutput(i); p2.setPullableInput(j, p1_out); } catch (ArrayIndexOutOfBoundsException e) { throw new Connector.IndexOutOfBoundsException(p1, i, p2, j); } catch (UnsupportedOperationException e) { } try { Pushable p2_in = p2.getPushableInput(j); p1.setPushableOutput(i, p2_in); } catch (ArrayIndexOutOfBoundsException e) { throw new Connector.IndexOutOfBoundsException(p1, i, p2, j); } catch (UnsupportedOperationException e) { } if (tracker != null) { tracker.setConnection(p1.getId(), i, p2.getId(), j); } return p2; } private  Connector(); static Processor connect(EventTracker tracker, Processor p1, int i, Processor p2, int j); static Processor connect(Processor p1, int i, Processor p2, int j); static Processor connect(Processor ... procs); static Processor connect(EventTracker tracker, Processor ... procs); @SuppressWarnings("squid:S1166") static boolean isCompatible(Processor p1, int i, Processor p2, int j); static Collection<Connection> getConnections(Processor p); static final boolean s_checkForTypes; static final boolean s_checkForBounds; static final int INPUT; static final int OUTPUT; static final int LEFT; static final int TOP; static final int RIGHT; static final int BOTTOM; }
@Test public void testOutOfBounds1() { Apples a1 = new Apples(); Apples a2 = new Apples(); boolean got_exception = false; try { Connector.connect(a1, 10, a2, 0); } catch (ConnectorException ce) { Connector.IndexOutOfBoundsException ioobe = (Connector.IndexOutOfBoundsException) ce; assertNotNull(ioobe.getMessage()); got_exception = true; } assertTrue(got_exception); }
@Test public void testOutOfBounds2() { Apples a1 = new Apples(); Apples a2 = new Apples(); boolean got_exception = false; try { Connector.connect(a1, 0, a2, 10); } catch (ConnectorException ce) { Connector.IndexOutOfBoundsException ioobe = (Connector.IndexOutOfBoundsException) ce; assertNotNull(ioobe.getMessage()); got_exception = true; } assertTrue(got_exception); }
@Test public void testConnectWithTracker() { Apples a1 = new Apples(); Apples a2 = new Apples(); ProvenanceTest.DummyTracker tracker = new ProvenanceTest.DummyTracker(); Connector.connect(tracker, a1, a2); assertTrue(tracker.containsConnection(a1.getId(), 0, a2.getId(), 0)); assertFalse(tracker.containsConnection(a2.getId(), 0, a1.getId(), 0)); }
@Test public void testConnectNothing() { Apples a = new Apples(); Connector.connect(a); }
@Test public void testSelfLoop() { Apples a = new Apples(); boolean got_exception = false; try { Connector.connect(a, a); } catch (Connector.SelfLoopException sle) { assertNotNull(sle.getMessage()); got_exception = true; } catch (ConnectorException e) { } assertTrue(got_exception); }
@Test public void testTwoUnary() { Passthrough p1 = new Passthrough(1); QueueSink qs1 = new QueueSink(1); Connector.connect(p1, qs1); Pushable push1 = p1.getPushableInput(0); for (int k = 0; k < 2; k++) { Queue<Object> queue = qs1.getQueue(0); for (int i = 0; i < 5; i++) { push1.push(i); Utilities.queueContains(i, queue); } p1.reset(); qs1.reset(); } }
@Test public void testTwoBinary() { Passthrough p1 = new Passthrough(2); QueueSink qs1 = new QueueSink(2); Connector.connect(p1, 0, qs1, 1); Connector.connect(p1, 1, qs1, 0); Pushable push1 = p1.getPushableInput(0); Pushable push2 = p1.getPushableInput(1); for (int k = 0; k < 2; k++) { Queue<Object> queue1 = qs1.getQueue(0); Queue<Object> queue2 = qs1.getQueue(1); for (int i = 0; i < 5; i++) { push1.push(i); push2.push(2 * i + 1); Utilities.queueContains(i, queue2); Utilities.queueContains(2 * i + 1, queue1); } p1.reset(); qs1.reset(); } }
@Test public void testThreeUnary1() { Passthrough p1 = new Passthrough(1); QueueSink qs1 = new QueueSink(1); Connector.connect(p1, qs1); Pushable push1 = p1.getPushableInput(0); for (int k = 0; k < 2; k++) { Queue<Object> queue = qs1.getQueue(0); for (int i = 0; i < 5; i++) { push1.push(i); Utilities.queueContains(i, queue); } p1.reset(); qs1.reset(); } }
@Test public void testThreeUnary2() { Passthrough p1 = new Passthrough(1); Incrementer p2 = new Incrementer(10); QueueSink qs1 = new QueueSink(1); Connector.connect(p1, p2, qs1); Pushable push1 = p1.getPushableInput(0); for (int k = 0; k < 2; k++) { Queue<Object> queue = qs1.getQueue(0); for (int i = 0; i < 5; i++) { push1.push(i); Utilities.queueContains(i + 10, queue); } p1.reset(); qs1.reset(); } }
@Test public void testConnectIncompatible() { Processor apples = new Apples(); Processor oranges = new Oranges(); try { Connector.connect(apples, oranges); } catch (ConnectorException e) { IncompatibleTypesException ite = (IncompatibleTypesException) e; assertNotNull(ite.getMessage()); } }