FieldConstraintArrayUtil { public static void moveUp(int index, FieldConstraint[] fieldConstraints) { if (index > 0) { final FieldConstraint first = fieldConstraints[index - 1]; final FieldConstraint second = fieldConstraints[index]; if (blockSwap(first, second)) { return; } fieldConstraints[index - 1] = second; fieldConstraints[index] = first; } } private  FieldConstraintArrayUtil(); static void moveDown(int index, FieldConstraint[] fieldConstraints); static void moveUp(int index, FieldConstraint[] fieldConstraints);  }
@Test public void up() { final FieldConstraint[] fieldConstraints = new FieldConstraint[2]; fieldConstraints[0] = mock(FieldConstraint.class); final FieldConstraint bottom = mock(FieldConstraint.class); fieldConstraints[1] = bottom; FieldConstraintArrayUtil.moveUp(1, fieldConstraints); assertEquals(bottom, fieldConstraints[0]); }
@Test public void upWhenAlreadyTop() { final FieldConstraint[] fieldConstraints = new FieldConstraint[2]; final FieldConstraint top = mock(FieldConstraint.class); fieldConstraints[0] = top; fieldConstraints[1] = mock(FieldConstraint.class); FieldConstraintArrayUtil.moveUp(0, fieldConstraints); assertEquals(top, fieldConstraints[0]); }
@Test public void shouldNotMoveHigherThanUsedVariables() { final FieldConstraint[] fieldConstraints = new FieldConstraint[2]; final SingleFieldConstraint top = new SingleFieldConstraint(); top.setFieldBinding("a"); fieldConstraints[0] = top; final SingleFieldConstraint bottom = new SingleFieldConstraint(); bottom.setConstraintValueType(BaseSingleFieldConstraint.TYPE_VARIABLE); bottom.setValue("a"); fieldConstraints[1] = bottom; FieldConstraintArrayUtil.moveUp(1, fieldConstraints); assertEquals(top, fieldConstraints[0]); assertEquals(bottom, fieldConstraints[1]); }
@Test public void shouldNotMoveUpperThanUsedVariables_MoveToMiddle() { final FieldConstraint[] fieldConstraints = new FieldConstraint[3]; final SingleFieldConstraint top = new SingleFieldConstraint(); top.setConstraintValueType(BaseSingleFieldConstraint.TYPE_VARIABLE); top.setValue("b"); fieldConstraints[0] = top; final SingleFieldConstraint middle = new SingleFieldConstraint(); middle.setConstraintValueType(BaseSingleFieldConstraint.TYPE_VARIABLE); middle.setValue("a"); fieldConstraints[1] = middle; final SingleFieldConstraint bottom = new SingleFieldConstraint(); bottom.setFieldBinding("a"); fieldConstraints[2] = bottom; FieldConstraintArrayUtil.moveUp(2, fieldConstraints); FieldConstraintArrayUtil.moveUp(1, fieldConstraints); assertEquals(bottom, fieldConstraints[0]); assertEquals(top, fieldConstraints[1]); assertEquals(middle, fieldConstraints[2]); }