package pl.vgtworld.imagedraw.filters.matrix;

import static org.assertj.core.api.Assertions.assertThat;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.junit.Test;

public class ImageEdgeResizeHelperTest {
	
	private static final int IMAGE_WIDTH_HEIGHT = 100;
	
	private static final int ADDITIONAL_EDGE_SIZE = 5;
	
	private static final String IMAGE_PATH = "/image-100-100.jpg";
	
	private ImageEdgeResizeHelper helper = new ImageEdgeResizeHelper();
	
	@Test
	public void shouldCopyTopEdgeOfImage() throws IOException {
		BufferedImage sourceImage = ImageIO.read(getClass().getResourceAsStream(IMAGE_PATH));
		
		BufferedImage result = helper.addEdgesToImage(sourceImage, 5);
		
		assertThat(result.getWidth()).isEqualTo(IMAGE_WIDTH_HEIGHT + 2 * ADDITIONAL_EDGE_SIZE);
		assertThat(result.getHeight()).isEqualTo(IMAGE_WIDTH_HEIGHT + 2 * ADDITIONAL_EDGE_SIZE);
		for (int i = 0; i < IMAGE_WIDTH_HEIGHT + 2 * ADDITIONAL_EDGE_SIZE; ++i) {
			int firstPixel = result.getRGB(i, 0);
			for (int j = 1; j <= ADDITIONAL_EDGE_SIZE; ++j) {
				assertThat(result.getRGB(i, j)).isEqualTo(firstPixel);
			}
		}
	}
	
	@Test
	public void shouldCopyBottomEdgeOfImage() throws IOException {
		BufferedImage sourceImage = ImageIO.read(getClass().getResourceAsStream(IMAGE_PATH));
		
		BufferedImage result = helper.addEdgesToImage(sourceImage, ADDITIONAL_EDGE_SIZE);
		
		assertThat(result.getWidth()).isEqualTo(IMAGE_WIDTH_HEIGHT + 2 * ADDITIONAL_EDGE_SIZE);
		assertThat(result.getHeight()).isEqualTo(IMAGE_WIDTH_HEIGHT + 2 * ADDITIONAL_EDGE_SIZE);
		for (int i = 0; i < IMAGE_WIDTH_HEIGHT + 2 * ADDITIONAL_EDGE_SIZE; ++i) {
			int firstPixel = result.getRGB(i, IMAGE_WIDTH_HEIGHT + ADDITIONAL_EDGE_SIZE - 1);
			for (int j = IMAGE_WIDTH_HEIGHT + ADDITIONAL_EDGE_SIZE; j < IMAGE_WIDTH_HEIGHT + 2 * ADDITIONAL_EDGE_SIZE; ++j) {
				assertThat(result.getRGB(i, j)).isEqualTo(firstPixel);
			}
		}
	}
	
	@Test
	public void shouldCopyLeftEdgeOfImage() throws IOException {
		BufferedImage sourceImage = ImageIO.read(getClass().getResourceAsStream(IMAGE_PATH));
		
		BufferedImage result = helper.addEdgesToImage(sourceImage, ADDITIONAL_EDGE_SIZE);
		
		assertThat(result.getWidth()).isEqualTo(IMAGE_WIDTH_HEIGHT + 2 * ADDITIONAL_EDGE_SIZE);
		assertThat(result.getHeight()).isEqualTo(IMAGE_WIDTH_HEIGHT + 2 * ADDITIONAL_EDGE_SIZE);
		for (int j = 0; j < IMAGE_WIDTH_HEIGHT + 2 * ADDITIONAL_EDGE_SIZE; ++j) {
			int firstPixel = result.getRGB(0, j);
			for (int i = 1; i <= ADDITIONAL_EDGE_SIZE; ++i) {
				assertThat(result.getRGB(i, j)).isEqualTo(firstPixel);
			}
		}
	}
	
	@Test
	public void shouldCopyRightEdgeOfImage() throws IOException {
		BufferedImage sourceImage = ImageIO.read(getClass().getResourceAsStream(IMAGE_PATH));
		
		BufferedImage result = helper.addEdgesToImage(sourceImage, ADDITIONAL_EDGE_SIZE);
		
		assertThat(result.getWidth()).isEqualTo(IMAGE_WIDTH_HEIGHT + 2 * ADDITIONAL_EDGE_SIZE);
		assertThat(result.getHeight()).isEqualTo(IMAGE_WIDTH_HEIGHT + 2 * ADDITIONAL_EDGE_SIZE);
		for (int j = 0; j < IMAGE_WIDTH_HEIGHT + 2 * ADDITIONAL_EDGE_SIZE; ++j) {
			int firstPixel = result.getRGB(IMAGE_WIDTH_HEIGHT + ADDITIONAL_EDGE_SIZE - 1, j);
			for (int i = IMAGE_WIDTH_HEIGHT + ADDITIONAL_EDGE_SIZE; i < IMAGE_WIDTH_HEIGHT + 2 * ADDITIONAL_EDGE_SIZE; ++i) {
				assertThat(result.getRGB(i, j)).isEqualTo(firstPixel);
			}
		}
	}
	
}

@@ -292,32 +292,73 @@
  int IMAGE_WIDTH
+ = 300;%0A%09%0A%09private static final int IMAGE
 _HEIGHT = 100;%0A%09
@@ -462,17 +462,17 @@
 %22/image-
-1
+3
 00-100.j
@@ -821,39 +821,32 @@
 alTo(IMAGE_WIDTH
-_HEIGHT
  + 2 * ADDITIONA
@@ -900,38 +900,32 @@
 isEqualTo(IMAGE_
-WIDTH_
 HEIGHT + 2 * ADD
@@ -969,39 +969,32 @@
  i %3C IMAGE_WIDTH
-_HEIGHT
  + 2 * ADDITIONA
@@ -1474,39 +1474,32 @@
 alTo(IMAGE_WIDTH
-_HEIGHT
  + 2 * ADDITIONA
@@ -1553,38 +1553,32 @@
 isEqualTo(IMAGE_
-WIDTH_
 HEIGHT + 2 * ADD
@@ -1622,39 +1622,32 @@
  i %3C IMAGE_WIDTH
-_HEIGHT
  + 2 * ADDITIONA
@@ -1705,30 +1705,24 @@
 GB(i, IMAGE_
-WIDTH_
 HEIGHT + ADD
@@ -1759,38 +1759,32 @@
  (int j = IMAGE_
-WIDTH_
 HEIGHT + ADDITIO
@@ -1800,38 +1800,32 @@
 SIZE; j %3C IMAGE_
-WIDTH_
 HEIGHT + 2 * ADD
@@ -2215,39 +2215,32 @@
 alTo(IMAGE_WIDTH
-_HEIGHT
  + 2 * ADDITIONA
@@ -2294,38 +2294,32 @@
 isEqualTo(IMAGE_
-WIDTH_
 HEIGHT + 2 * ADD
@@ -2358,38 +2358,32 @@
  = 0; j %3C IMAGE_
-WIDTH_
 HEIGHT + 2 * ADD
@@ -2868,39 +2868,32 @@
 alTo(IMAGE_WIDTH
-_HEIGHT
  + 2 * ADDITIONA
@@ -2951,30 +2951,24 @@
 ualTo(IMAGE_
-WIDTH_
 HEIGHT + 2 *
@@ -3011,38 +3011,32 @@
  = 0; j %3C IMAGE_
-WIDTH_
 HEIGHT + 2 * ADD
@@ -3098,39 +3098,32 @@
 tRGB(IMAGE_WIDTH
-_HEIGHT
  + ADDITIONAL_ED
@@ -3166,23 +3166,16 @@
 GE_WIDTH
-_HEIGHT
  + ADDIT
@@ -3206,23 +3206,16 @@
 GE_WIDTH
-_HEIGHT
  + 2 * A

Change unit tests to work with rectangular instead of square image.