### Question:
NcTools { static Rectangle getGridRectangle(NetcdfFile netcdfFile, Variable variable, GridDef expectedGridDef) throws IOException { final int rank = variable.getRank(); if (rank < 2) { throw new IOException(String.format("Variable '%s' in file '%s': expected rank 2 or higher, but found %d.", variable.getShortName(), netcdfFile.getLocation(), rank)); } final int w = variable.getDimension(rank - 1).getLength(); final int h = variable.getDimension(rank - 2).getLength(); if (w != expectedGridDef.getWidth() || h != expectedGridDef.getHeight()) { throw new IOException(String.format("Variable '%s' in file '%s': unexpected grid size.", variable.getShortName(), netcdfFile.getLocation())); } return new Rectangle(0, 0, w, h); }  static Variable getVariable(NetcdfFile netcdfFile, String variableName); static boolean hasVariable(NetcdfFile netcdfFile, String variableName); static Grid readGrid(NetcdfFile netcdfFile, Variable variable, GridDef expectedGridDef, int z); static Grid readGrid(NetcdfFile netcdfFile, String variableName, GridDef expectedGridDef, int z); static Grid readGrid(NetcdfFile netcdfFile, String variableName, GridDef expectedGridDef);  }### Answer:
@Test public void testGetGridRectangle_variableRankIncorrect() { final Variable mockVariable = mock(Variable.class); when(mockVariable.getRank()).thenReturn(1); try { NcTools.getGridRectangle(netcdfFile, mockVariable, GridDef.createGlobal(2.9)); fail("IOException expected"); } catch (IOException expected) { } verify(mockVariable, times(1)).getRank(); verify(netcdfFile, times(1)).getLocation(); verifyNoMoreInteractions(netcdfFile); }
@Test public void testGetGridRectangle_incorrectDimensions() { final Variable mockVariable = mock(Variable.class); when(mockVariable.getRank()).thenReturn(2); final Dimension y_dim = mock(Dimension.class); when(y_dim.getLength()).thenReturn(11); final Dimension x_dim = mock(Dimension.class); when(x_dim.getLength()).thenReturn(19); when(mockVariable.getDimension(0)).thenReturn(y_dim); when(mockVariable.getDimension(1)).thenReturn(x_dim); final GridDef gridDef = GridDef.createGlobal(20, 10); try { NcTools.getGridRectangle(netcdfFile, mockVariable, gridDef); fail("IOException expected"); } catch (IOException expected) { } verify(mockVariable, times(1)).getRank(); verify(mockVariable, times(1)).getDimension(0); verify(mockVariable, times(1)).getDimension(1); verify(netcdfFile, times(1)).getLocation(); verifyNoMoreInteractions(netcdfFile); }
@Test public void testGetGridRectangle() throws IOException { final Variable variable = mock(Variable.class); when(variable.getRank()).thenReturn(2); final Dimension y_dim = mock(Dimension.class); when(y_dim.getLength()).thenReturn(10); final Dimension x_dim = mock(Dimension.class); when(x_dim.getLength()).thenReturn(20); when(variable.getDimension(0)).thenReturn(y_dim); when(variable.getDimension(1)).thenReturn(x_dim); final GridDef gridDef = GridDef.createGlobal(20, 10); final Rectangle gridRectangle = NcTools.getGridRectangle(netcdfFile, variable, gridDef); assertNotNull(gridRectangle); assertEquals(0, gridRectangle.getX(), 1e-8); assertEquals(0, gridRectangle.getY(), 1e-8); assertEquals(20, gridRectangle.getWidth(), 1e-8); assertEquals(10, gridRectangle.getHeight(), 1e-8); verify(variable, times(1)).getRank(); verify(variable, times(1)).getDimension(0); verify(variable, times(1)).getDimension(1); verifyNoMoreInteractions(netcdfFile); }