NodeCheck { public static boolean isValidNode(final Node n) { if (n == null) { return false; } int degree = n.getNumberConnectedLines(); if (degree == DEGREE_2) { Iterator<? extends Line> iter = n.getConnectedLines(); Line line1 = iter.next(); Line line2 = iter.next(); return isPair(line1, line2); } if (degree == DEGREE_4) { Iterator<? extends Line> connectedLines = n.getConnectedLines(); List<Line> lines = new ArrayList<Line>(); while (connectedLines.hasNext()) { lines.add(connectedLines.next()); } Line line = lines.get(0); boolean found = false; int pos = 1; while (!found && pos < lines.size()) { if (isPair(line, lines.get(pos))) { found = true; lines.remove(pos); lines.remove(0); } pos++; } if (!found) { return true; } if (isPair(lines.get(0), lines.get(1))) { return false; } return true; } return true; } private  NodeCheck(); static boolean isValidNode(final Node n); static boolean isPair(final Line line1, final Line line2);  }
@Test public void testIsNodeValid() { Mockery context = new Mockery(); final Node valid1 = context.mock(Node.class, "valid1"); final Node valid2 = context.mock(Node.class, "valid2"); final Node valid3 = context.mock(Node.class, "valid3"); final Node valid4 = context.mock(Node.class, "valid4"); final Node valid5 = context.mock(Node.class, "valid5"); final Node invalid2 = context.mock(Node.class, "invalid2"); final Node invalid4 = context.mock(Node.class, "invalid4"); final Node n1 = context.mock(Node.class, "n1"); final Node n2 = context.mock(Node.class, "n2"); final Node n3 = context.mock(Node.class, "n3"); final Line l1 = context.mock(Line.class, "l1"); final Line l2 = context.mock(Line.class, "l2"); final Line l3 = context.mock(Line.class, "l3"); final Line l4 = context.mock(Line.class, "l4"); final Line l5 = context.mock(Line.class, "l5"); context.checking(new Expectations() { { allowing(valid2).getID(); will(returnValue(2l)); } }); context.checking(new Expectations() { { allowing(valid1).getNumberConnectedLines(); will(returnValue(1)); } }); final HashSet<Line> set2 = new HashSet<Line>(); set2.add(l1); set2.add(l2); context.checking(new Expectations() { { allowing(valid2).getNumberConnectedLines(); will(returnValue(2)); } { allowing(valid2).getConnectedLines(); will(returnValue(set2.iterator())); } }); context.checking(new Expectations() { { allowing(n1).getID(); will(returnValue(1l)); } { allowing(n2).getID(); will(returnValue(2l)); } { allowing(n3).getID(); will(returnValue(3l)); } }); context.checking(new Expectations() { { allowing(l1).getStartNode(); will(returnValue(n1)); } { allowing(l1).getEndNode(); will(returnValue(valid2)); } }); context.checking(new Expectations() { { allowing(l2).getStartNode(); will(returnValue(valid2)); } { allowing(l2).getEndNode(); will(returnValue(n1)); } }); context.checking(new Expectations() { { allowing(valid3).getNumberConnectedLines(); will(returnValue(THREE_CONNECTED_LINES)); } }); context.checking(new Expectations() { { allowing(valid5).getNumberConnectedLines(); will(returnValue(FIVE_CONNECTED_LINES)); } }); final HashSet<Line> set4 = new HashSet<Line>(); set4.addAll(set2); set4.add(l3); set4.add(l4); context.checking(new Expectations() { { allowing(valid4).getNumberConnectedLines(); will(returnValue(FOUR_CONNECTED_LINES)); } { allowing(valid4).getConnectedLines(); will(returnValue(set4.iterator())); } }); context.checking(new Expectations() { { allowing(l3).getStartNode(); will(returnValue(n2)); } { allowing(l3).getEndNode(); will(returnValue(valid2)); } }); context.checking(new Expectations() { { allowing(l4).getStartNode(); will(returnValue(valid2)); } { allowing(l4).getEndNode(); will(returnValue(n3)); } }); context.checking(new Expectations() { { allowing(l5).getStartNode(); will(returnValue(valid2)); } { allowing(l5).getEndNode(); will(returnValue(n2)); } }); final HashSet<Line> set4invalid = new HashSet<Line>(); set4invalid.addAll(set2); set4invalid.add(l3); set4invalid.add(l5); context.checking(new Expectations() { { allowing(invalid4).getNumberConnectedLines(); will(returnValue(FOUR_CONNECTED_LINES)); } { allowing(invalid4).getConnectedLines(); will(returnValue(set4invalid.iterator())); } }); final HashSet<Line> set2invalid = new HashSet<Line>(); set2invalid.add(l3); set2invalid.add(l4); context.checking(new Expectations() { { allowing(invalid2).getNumberConnectedLines(); will(returnValue(2)); } { allowing(invalid2).getConnectedLines(); will(returnValue(set2invalid.iterator())); } }); assertTrue(NodeCheck.isValidNode(valid1)); assertTrue(NodeCheck.isValidNode(valid2)); assertTrue(NodeCheck.isValidNode(valid3)); assertTrue(NodeCheck.isValidNode(valid4)); assertTrue(NodeCheck.isValidNode(valid5)); assertFalse(NodeCheck.isValidNode(invalid2)); assertFalse(NodeCheck.isValidNode(invalid4)); }