RotateMatrix { public void rotate(int[][] matrix) { validateInput(matrix); int length = matrix.length; for (int i = 0; i < length / 2; ++i) { int first = i; int last = length - 1 - i; for (int j = first; j < last; ++j) { int offset = j - first; int top = matrix[first][j]; moveLeftToTop(matrix, first, last, j, offset); moveBottomToLeft(matrix, first, last, offset); moveRithToBottom(matrix, last, j, offset); moveTopToRight(matrix, last, j, top); } } }  void rotate(int[][] matrix);  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullMatrix() { rotateMatrix.rotate(null); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptMatrixWithDifferentHeightAndWidth() { int[][] matrix = new int[6][3]; rotateMatrix.rotate(matrix); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptAnEmtpyMatrix() { int[][] matrix = new int[0][0]; rotateMatrix.rotate(matrix); }
@Test public void shouldRotateMatrix() { int[][] matrix = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } }; rotateMatrix.rotate(matrix); int[][] expectedResult = { { 6, 3, 0 }, { 7, 4, 1 }, { 8, 5, 2 } }; assertArrayEquals(expectedResult, matrix); }