Ola galera.
Tenho algumas duvidas quanto a aplicação de testes unitários com JUnit.
uma delas seria como eu testaria o metodo a seguir.:
O objetivo deste método é redimencionar uma imagem passado como InputStream e devolver uma nova como InputStream também.
public static InputStream redimencionarJPG(InputStream inputStream,
int largura, int altura) throws Exception {
byte[] array = new byte[inputStream.available()];
inputStream.read(array);
Image image = new ImageIcon( array ).getImage();
int quality = 100;
// Calculos necessários para manter as propoçoes da imagem, conhecido
// como "aspect ratio"
double thumbRatio = (double) largura / (double) altura;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double) imageWidth / (double) imageHeight;
if (thumbRatio < imageRatio) {
altura = (int) (largura / imageRatio);
} else {
largura = (int) (altura * imageRatio);
}
// Fim do cálculo
BufferedImage thumbImage = new BufferedImage(largura, altura,
BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, largura, altura, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder
.getDefaultJPEGEncodeParam(thumbImage);
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float) quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
return new ByteArrayInputStream( out.toByteArray() );
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException " + e.getMessage());
throw e;
} catch (ImageFormatException e) {
System.out.println("ImageFormatException " + e.getMessage());
throw e;
} catch (IOException e) {
System.out.println("IOException " + e.getMessage());
throw e;
}
}
Obrigado pela atenção.
Abraços.

