Mercurial > hg > digilib
changeset 1448:a2da0b5caedd new_scaling
adding the first tests for digilib's scaling modes.
uses JUnit and Jetty's ServletTester.
test are disabled by default, enable with "mvn test -DskipTests=false".
author | robcast |
---|---|
date | Wed, 11 Nov 2015 20:30:09 +0100 |
parents | 4043aa19dd30 |
children | 7f6b5e7e2afd |
files | webapp/pom.xml webapp/src/test/java/digilib/servlet/ScalerTest.java |
diffstat | 2 files changed, 210 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/webapp/pom.xml Wed Nov 11 11:42:29 2015 +0100 +++ b/webapp/pom.xml Wed Nov 11 20:30:09 2015 +0100 @@ -13,6 +13,10 @@ <description>The Digital Image Library - web application server and HTML and JS clients.</description> <url>http://digilib.sourceforge.net</url> <packaging>war</packaging> + + <properties> + <skipTests>true</skipTests> + </properties> <build> <pluginManagement> @@ -33,7 +37,15 @@ </includes> </configuration> </plugin> - </plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.19</version> + <configuration> + <skip>${skipTests}</skip> + </configuration> + </plugin> + </plugins> </pluginManagement> </build> <profiles> @@ -169,11 +181,30 @@ <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlets</artifactId> - <version>9.3.5.v20151012</version> + <version>9.2.13.v20150730</version> + <!-- <version>9.3.5.v20151012</version> --> </dependency> </dependencies> </profile> </profiles> <dependencies> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-servlet</artifactId> + <version>9.2.13.v20150730</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-http</artifactId> + <version>9.2.13.v20150730</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + <scope>test</scope> + </dependency> </dependencies> </project>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/webapp/src/test/java/digilib/servlet/ScalerTest.java Wed Nov 11 20:30:09 2015 +0100 @@ -0,0 +1,177 @@ +package digilib.servlet; + +/* + * #%L + * ScalerTest -- tests for the digilib Scaler servlet + * + * Digital Image Library servlet components + * + * %% + * Copyright (C) 2015 MPIWG Berlin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + * Author: Robert Casties (robcast@users.sourceforge.net) + */ + +import static org.junit.Assert.assertEquals; + +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.ByteBuffer; + +import javax.imageio.ImageIO; + +import org.eclipse.jetty.http.HttpTester; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletTester; +import org.junit.BeforeClass; +import org.junit.Test; + +import digilib.conf.DigilibServlet3Configuration; + +/** + * ScalerTest -- tests for the digilib Scaler servlet + * + * @author casties + * + */ +public class ScalerTest { + + private static ServletTester tester; + + public static String testFileName = "xterm_color_chart"; + + @BeforeClass + public static void startServer() throws Exception { + tester = new ServletTester(); + ServletContextHandler ctx = tester.getContext(); + // set up ServletContext + ctx.setContextPath("/"); + ctx.setResourceBase("src/main/webapp"); + ctx.setClassLoader(ServletTester.class.getClassLoader()); + // add digilib ContextListener + DigilibServlet3Configuration dlConfig = new DigilibServlet3Configuration(); + ctx.addEventListener(dlConfig); + tester.addServlet(Scaler.class, "/Scaler/*"); + // start the servlet + tester.start(); + } + + /** + * Requests the image file testFileName from the Scaler with the given parameters and returns the image. + * + * Checks the returned content-type (if contentType != null). + * + * @param params + * @param contentType + * @return + * @throws Exception + * @throws IOException + */ + private BufferedImage loadImage(String params, String contentType) throws Exception, IOException { + // prepare request + HttpTester.Request request = HttpTester.newRequest(); + request.setMethod("GET"); + request.setHeader("Host", "tester"); // should be "tester" + request.setURI("/Scaler?fn="+testFileName+"&"+params); + request.setContent(""); + ByteBuffer reqBuf = request.generate(); + // get response + ByteBuffer respBuf = tester.getResponses(reqBuf); + // parse response + HttpTester.Response response = HttpTester.parseResponse(respBuf); + // should be 200 - OK + assertEquals("status code", 200, response.getStatus()); + // check content-type + if (contentType != null) { + String ct = response.getStringField("content-type"); + assertEquals("content-type", contentType, ct); + } + // load response as image + ByteArrayInputStream bis = new ByteArrayInputStream(response.getContentBytes()); + BufferedImage img = ImageIO.read(bis); + return img; + } + + /** + * Test scaling with mo=fit + * @throws Exception + */ + @Test + public void testScaleFit() throws Exception { + BufferedImage img = loadImage("ww=0.0836&wh=0.0378&wx=0&wy=0.961&dw=173&dh=235&mo=fit,errcode", null); + assertEquals("height", 125, img.getHeight()); + assertEquals("width", 173, img.getWidth()); + } + + /** + * Test scaling with mo=squeeze + * @throws Exception + */ + @Test + public void testScaleSqueeze() throws Exception { + BufferedImage img = loadImage("ww=0.0836&wh=0.0378&wx=0&wy=0.961&dw=173&dh=235&mo=squeeze,errcode", null); + assertEquals("height", 235, img.getHeight()); + assertEquals("width", 173, img.getWidth()); + } + + /** + * Test scaling with mo=clip + * @throws Exception + */ + @Test + public void testScaleClip() throws Exception { + BufferedImage img = loadImage("ww=0.0836&wh=0.0378&wx=0&wy=0.961&dw=173&dh=235&mo=clip,errcode", null); + assertEquals("height", 60, img.getHeight()); + assertEquals("width", 173, img.getWidth()); + } + + /** + * Test scaling with mo=ascale + * @throws Exception + */ + @Test + public void testScaleAbsolute() throws Exception { + BufferedImage img = loadImage("mo=ascale&scale=0.1&mo=errcode", null); + assertEquals("height", 154, img.getHeight()); + assertEquals("width", 96, img.getWidth()); + } + + /** + * Test forced image type with mo=jpg + * @throws Exception + */ + @Test + public void testTypeJpg() throws Exception { + BufferedImage img = loadImage("ww=0.0836&wh=0.0378&wx=0&wy=0.961&dw=173&dh=235&mo=jpg,errcode", "image/jpeg"); + int px = img.getRGB(100, 100); + assertEquals("pixel color", -8421505, px); + } + + /** + * Test forced image type with mo=png + * @throws Exception + */ + @Test + public void testTypePng() throws Exception { + BufferedImage img = loadImage("ww=0.0836&wh=0.0378&wx=0&wy=0.961&dw=173&dh=235&mo=png,errcode", "image/png"); + int px = img.getRGB(100, 100); + assertEquals("pixel color", -8421505, px); + } + + +}