Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
romankh3 committed Apr 7, 2019
1 parent 13e481a commit 4e8fe12
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ repositories {

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.26.0'
}

jar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ public static BufferedImage readImageFromFile(File path) throws IOException {
*/
public static void saveImage(File path, BufferedImage image) throws IOException {
File dir = path.getParentFile();
// make dir if it's not using from Gradle.
boolean dirExists = dir == null || dir.isDirectory() || dir.mkdirs();
if (!dirExists) {
throw new RuntimeException("Unable to create directory " + dir);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ua.comparison.image;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static ua.comparison.image.ImageComparisonTools.createGUI;
import static ua.comparison.image.ImageComparisonTools.readImageFromResources;

Expand Down Expand Up @@ -40,4 +43,26 @@ public void testSaveImage() throws IOException, URISyntaxException {
ImageComparisonTools.saveImage(new File(path), image);
Assert.assertTrue(new File(path).exists());
}

@Test(expected = RuntimeException.class)
public void testNullParent() throws IOException {
//given
File path = mock(File.class);
File parent = mock(File.class);
when(path.isDirectory()).thenReturn(false);
when(path.mkdirs()).thenReturn(false);
when(path.getParentFile()).thenReturn(parent);

//when-then
ImageComparisonTools.saveImage(path, null);
}

@Test
public void testCreation() {
//when
ImageComparisonTools imageComparisonTools = new ImageComparisonTools();

//then
assertNotNull(imageComparisonTools);
}
}
53 changes: 42 additions & 11 deletions src/test/java/ua/comparison/image/model/RectangleUnitTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ua.comparison.image.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

Expand Down Expand Up @@ -53,7 +55,7 @@ public void testIsOverlap() {
Rectangle rectangleTwo = new Rectangle(4, 4, 10, 10);

//when-then
Assert.assertTrue(rectangleOne.isOverlapping(rectangleTwo));
assertTrue(rectangleOne.isOverlapping(rectangleTwo));
}

/**
Expand All @@ -75,7 +77,7 @@ public void testNonOverlappingUpDown() {
Rectangle rectangleTwo = new Rectangle(4, 6, 6, 8);

//when-then
Assert.assertFalse(rectangleOne.isOverlapping(rectangleTwo));
assertFalse(rectangleOne.isOverlapping(rectangleTwo));
}

/**
Expand All @@ -95,7 +97,7 @@ public void testNonOverlappingLeftRight() {
Rectangle rectangleTwo = new Rectangle(6, 2, 10, 4);

//when-then
Assert.assertFalse(rectangleOne.isOverlapping(rectangleTwo));
assertFalse(rectangleOne.isOverlapping(rectangleTwo));
}

@Test
Expand Down Expand Up @@ -123,7 +125,7 @@ public void testEqual() {
Rectangle rectangleTwo = new Rectangle(1, 1, 2, 2);

//when-then
Assert.assertEquals(rectangleOne, rectangleTwo);
assertEquals(rectangleOne, rectangleTwo);
}

@Test
Expand All @@ -132,7 +134,7 @@ public void testEqualTheSame() {
Rectangle rectangleOne = new Rectangle(1, 1, 2, 2);

//when-then
Assert.assertEquals(rectangleOne, rectangleOne);
assertEquals(rectangleOne, rectangleOne);
}

@Test
Expand All @@ -141,7 +143,7 @@ public void testEqualNull() {
Rectangle rectangle = new Rectangle(1, 1, 2, 2);

//when-then
Assert.assertNotEquals(rectangle, null);
assertNotEquals(rectangle, null);
}

@Test
Expand All @@ -154,11 +156,40 @@ public void testNonEqual() {
Rectangle rectangleMaxY = new Rectangle(1, 1, 2, 5);

//when-then
Assert.assertNotEquals(rectangle, rectangleMinX);
Assert.assertNotEquals(rectangle, rectangleMinY);
Assert.assertNotEquals(rectangle, rectangleMaxX);
Assert.assertNotEquals(rectangle, rectangleMaxY);
assertNotEquals(rectangle, rectangleMinX);
assertNotEquals(rectangle, rectangleMinY);
assertNotEquals(rectangle, rectangleMaxX);
assertNotEquals(rectangle, rectangleMaxY);

}


@Test
public void testTheSameHashCode() {
//given
Rectangle rectangleOne = new Rectangle(1, 1, 2, 2);
Rectangle rectangleTwo = new Rectangle(1, 1, 2, 2);

//when
int hashCodeOne = rectangleOne.hashCode();
int hashCodeTwo = rectangleTwo.hashCode();

//then
assertEquals(hashCodeOne, hashCodeTwo);
}

@Test
public void testNonTheSameHashCode() {
//given
Rectangle rectangleOne = new Rectangle(1, 1, 2, 2);
Rectangle rectangleTwo = new Rectangle(1, 3, 2, 2);

//when
int hashCodeOne = rectangleOne.hashCode();
int hashCodeTwo = rectangleTwo.hashCode();

//then
assertNotEquals(hashCodeOne, hashCodeTwo);
}

}

0 comments on commit 4e8fe12

Please sign in to comment.