package ru.r2cloud.jradio.sink;

import static org.junit.Assert.assertEquals;

import java.awt.image.BufferedImage;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import ru.r2cloud.jradio.WavFileSourceTest;
import ru.r2cloud.jradio.source.WavFileSource;

public class WaterfallTest {

	@Rule
	public TemporaryFolder tempFolder = new TemporaryFolder();

	@Test
	public void testSuccess() throws Exception {
		Waterfall waterfall = new Waterfall(100, 1024);
		WavFileSource source = new WavFileSource(WaterfallTest.class.getClassLoader().getResourceAsStream("aausat-4.wav"));
		BufferedImage image = waterfall.process(source);
		File actual = new File(tempFolder.getRoot(), UUID.randomUUID().toString());
		ImageIO.write(image, "png", new FileOutputStream(actual));
		source.close();
		try (InputStream is1 = WavFileSourceTest.class.getClassLoader().getResourceAsStream("expectedWaterfall.png"); InputStream is2 = new FileInputStream(actual)) {
			while (true) {
				try {
					int expected = is1.read();
					if (expected == -1) {
						break;
					}
					assertEquals(expected, is2.read());
				} catch (EOFException e) {
					break;
				}
			}
		}
	}

}

@@ -128,157 +128,19 @@
 .io.
-EOFException;%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0Aimport java.io.InputStream;%0Aimport java.util.UUID
+InputStream
 ;%0A%0Ai
@@ -724,513 +724,349 @@
 ;%0A%09%09
-File actual = new File(tempFolder.getRoot(), UUID.randomUUID().toString());%0A%09%09ImageIO.write(image, %22png%22, new FileOutputStream(actual));%0A%09%09source.close();%0A%09%09try (InputStream is1 = WavFileSourceTest.class.getClassLoader().getResourceAsStream(%22expectedWaterfall.png%22); InputStream is2 = new FileInputStream(actual)) %7B%0A%09%09%09while (true) %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09int expected = is1.read();%0A%09%09%09%09%09if (expected == -1) %7B%0A%09%09%09%09%09%09break;%0A%09%09%09%09%09%7D%0A%09%09%09%09%09assertEquals(expected, is2.read());%0A%09%09%09%09%7D catch (EOFException e) %7B%0A%09%09%09%09%09break
+source.close();%0A%09%09try (InputStream is1 = WavFileSourceTest.class.getClassLoader().getResourceAsStream(%22expectedWaterfall.png%22)) %7B%0A%09%09%09BufferedImage expected = ImageIO.read(is1);%0A%09%09%09for (int i = 0; i %3C expected.getWidth(); i++) %7B%0A%09%09%09%09for (int j = 0; j %3C expected.getHeight(); j++) %7B%0A%09%09%09%09%09assertEquals(expected.getRGB(i, j), image.getRGB(i, j))
 ;%0A%09%09

make test stable