View Javadoc

1   /**
2    * Copyright (c) 2002-2015, JWebUnit team.
3    *
4    * This file is part of JWebUnit.
5    *
6    * JWebUnit is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * JWebUnit is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package net.sourceforge.jwebunit.tests;
20  
21  import static net.sourceforge.jwebunit.junit.JWebUnit.*;
22  import static org.junit.Assert.assertNotNull;
23  
24  import java.awt.Image;
25  import java.awt.image.BufferedImage;
26  import java.io.File;
27  
28  import javax.imageio.ImageIO;
29  
30  import org.junit.Test;
31  
32  /**
33   *
34   * @author gjoseph
35   */
36  public class ImageTest extends JWebUnitAPITestCase {
37  
38      public void setUp() throws Exception {
39          super.setUp();
40          setBaseUrl(HOST_PATH + "/ImageTest");
41          beginAt("/PageWithImages.html");
42      }
43  
44      @Test 
45      public void testSimpleImagePresenceAssertion() throws Throwable {
46          assertImagePresent("images/Image1.gif", "image 1");
47          assertImagePresent("images/Image2.png", "image 2");
48          assertImagePresent("images/photos/Image3.jpg", "image 3");
49          assertImagePresent("somedir/Image4.gif", null);
50          assertImagePresent("images/InvalidImage.gif", "invalid image");
51  
52          assertFail("assertImagePresent", new Object[]{"images/Image4.jpg", "image 4"});
53          assertFail("assertImagePresent", new Object[]{"images/wrongUrl.jpg", "image 3"});
54          assertFail("assertImagePresent", new Object[]{"images/Image2.png", "wrong alt"});
55      }
56  
57      @Test 
58      public void testGifCanBeLoaded() throws Throwable {
59          assertPass("assertImageValid", new Object[]{"images/Image1.gif", "image 1"});
60      }
61  
62      @Test 
63      public void testPngCanBeLoaded() throws Throwable {
64          assertPass("assertImageValid", new Object[]{"images/Image2.png", "image 2"});
65      }
66  
67      @Test 
68      public void testJpgCanBeLoaded() throws Throwable {
69          assertPass("assertImageValid", new Object[]{"images/photos/Image3.jpg", "image 3"});
70      }
71  
72      @Test 
73      public void testFailsOnInvalidImages() throws Throwable {
74          assertFail("assertImageValid", new Object[]{"images/InvalidImage.gif", "invalid image"});
75      }
76  
77      @Test 
78      public void testSavesImage() throws Throwable {
79          File testOut = File.createTempFile("jwebunit-test-", ".png");
80          testOut.deleteOnExit();
81          assertImageValidAndStore("images/Image2.png", "image 2", testOut);
82          BufferedImage testImg = ImageIO.read(testOut);
83          // let's just assume it's ok if the image was loaded from the filesystem
84          assertNotNull(testImg);
85      }
86  
87      @Test 
88      public void testImagesAreExposed() throws Throwable {
89          Image image = getImage("images/Image1.gif", "image 1");
90          // let's just assume it's ok if the image is there
91          assertNotNull(image);
92      }
93  
94      @Test 
95      public void testRelativePathsAreCorrectlyResolved() {
96          beginAt("/somedir/AnotherPageWithImages.html");
97          assertImageValid("Image4.gif", "image 4 - same dir");
98          assertImageValid("images/Image5.png", "image 5 - subdir");
99          assertImageValid("../images/photos/Image3.jpg", "image 3 again - topdir");
100     }
101     
102     @Test
103     public void testAbsolutePath() {
104         assertImagePresent("/jwebunit/ImageTest/images/Image1.gif", "absolute image 1");
105         assertImageValid("/jwebunit/ImageTest/images/Image1.gif", "absolute image 1");        
106     }
107 
108 }