1   /*
2    * Copyright 2002-2004 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.richclient.image;
17  
18  import java.awt.Image;
19  import java.io.IOException;
20  
21  import junit.framework.TestCase;
22  
23  import org.springframework.context.ApplicationContext;
24  import org.springframework.context.support.ClassPathXmlApplicationContext;
25  import org.springframework.core.io.Resource;
26  
27  /**
28   * Tests the image loading and caching library.
29   * 
30   * @author Keith Donald
31   */
32  public class ImageSourceTests extends TestCase {
33      private ApplicationContext context;
34  
35      public void testValidImageAccess() throws IOException {
36          ImageSource source = (ImageSource)context.getBean("imageSource");
37          Resource resource = source.getImageResource("test.image.key");
38          assertNotNull(resource);
39          String urlExternalForm = resource.getURL().toExternalForm();
40          assertTrue(urlExternalForm.endsWith("org/springframework/richclient/image/test.gif"));
41          Image image = source.getImage("test.image.key");
42          assertNotNull(image);
43      }
44  
45      public void testBrokenImageAccess() throws IOException {
46          ImageSource source = (ImageSource)context.getBean("imageSourceBroken");
47          Resource resource = source.getImageResource("bogus.image.key");
48          assertNotNull(resource);
49          String urlExternalForm = resource.getURL().toExternalForm();
50          assertTrue(urlExternalForm.endsWith("org/springframework/richclient/image/broken.gif"));
51          Image image = source.getImage("bogus.image.key");
52          assertNotNull(image);
53      }
54  
55      public void testInvalidImageAccess() {
56          ImageSource source = (ImageSource)context.getBean("imageSourceBroken");
57          try {
58              source.getImageResource("invalid.image.key");
59          }
60          catch (NoSuchImageResourceException e) {
61              // expected
62          }
63          try {
64              source.getImage("invalid.image.key");
65          }
66          catch (NoSuchImageResourceException e) {
67              // expected
68          }
69      }
70  
71      protected void setUp() throws Exception {
72          context = new ClassPathXmlApplicationContext("org/springframework/richclient/image/application-context.xml");
73      }
74  }