Skip to content
Snippets Groups Projects
Commit 8076021c authored by Torstein Strømme's avatar Torstein Strømme
Browse files

tests now compile

parent e59fe78b
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@ import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
......@@ -29,13 +30,33 @@ public class TestCellPositionToPixelConverter {
grid, new Rectangle2D.Double(30, 30, 340, 240), 30);
Rectangle2D expected = new Rectangle2D.Double(215, 130, 47.5, 40);
assertEquals(expected, converter.getBoundsForCell(new CellPosition(1, 2)));
assertEquals(expected, getBoundsForCell(converter, new CellPosition(1, 2)));
}
/////////////////////////////
// Helper methods
/////////////////////////////
static Rectangle2D getBoundsForCell(CellPositionToPixelConverter converter, CellPosition cp) {
try {
Method method = CellPositionToPixelConverter.class.getMethod("getBoundsForCell", CellPosition.class);
// Check that the method is public
assertFalse(Modifier.isPrivate(method.getModifiers()),
"The method getBoundsForCell(CellPosition) in the CellPositionToPixelConverter class should not be private");
Object result = method.invoke(converter, cp);
assertInstanceOf(Rectangle2D.class, result,
"The method getBoundsForCell(CellPosition) in the CellPositionToPixelConverter class should return a Rectangle2D");
return (Rectangle2D) result;
} catch (NoSuchMethodException e) {
fail("Could not find the method getBoundsForCell(CellPosition) in the CellPositionToPixelConverter class");
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
throw new IllegalStateException("Should not be possible to reach this point");
}
static CellPositionToPixelConverter getConverter(GridDimension grid, Rectangle2D rect, double margin) {
try {
Constructor<?> constructor = CellPositionToPixelConverter.class.getConstructor(
......
......@@ -14,6 +14,7 @@ package no.uib.inf101.gridview;
import no.uib.inf101.colorgrid.*;
import org.junit.jupiter.api.Test;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.lang.reflect.Constructor;
......@@ -28,10 +29,14 @@ public class TestGridView {
@Test
public void preferredSize() {
GridView view = newGridView(null);
assertNotNull(view, "Unable to create a new GridView object");
assertNotNull(view.getPreferredSize(), "PreferredSize should not be null");
assertEquals(400, view.getPreferredSize().width);
assertEquals(300, view.getPreferredSize().height);
if ((Object) view instanceof JPanel panel) {
assertNotNull(panel, "Unable to create a new GridView object");
assertNotNull(panel.getPreferredSize(), "PreferredSize should not be null");
assertEquals(400, panel.getPreferredSize().width);
assertEquals(300, panel.getPreferredSize().height);
} else {
fail("The GridView class should extend JPanel");
}
}
@Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment