001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.springframework.richclient.image;
017    
018    import java.awt.Image;
019    import java.io.IOException;
020    
021    import junit.framework.TestCase;
022    
023    import org.springframework.context.ApplicationContext;
024    import org.springframework.context.support.ClassPathXmlApplicationContext;
025    import org.springframework.core.io.Resource;
026    
027    /**
028     * Tests the image loading and caching library.
029     * 
030     * @author Keith Donald
031     */
032    public class ImageSourceTests extends TestCase {
033        private ApplicationContext context;
034    
035        public void testValidImageAccess() throws IOException {
036            ImageSource source = (ImageSource)context.getBean("imageSource");
037            Resource resource = source.getImageResource("test.image.key");
038            assertNotNull(resource);
039            String urlExternalForm = resource.getURL().toExternalForm();
040            assertTrue(urlExternalForm.endsWith("org/springframework/richclient/image/test.gif"));
041            Image image = source.getImage("test.image.key");
042            assertNotNull(image);
043        }
044    
045        public void testBrokenImageAccess() throws IOException {
046            ImageSource source = (ImageSource)context.getBean("imageSourceBroken");
047            Resource resource = source.getImageResource("bogus.image.key");
048            assertNotNull(resource);
049            String urlExternalForm = resource.getURL().toExternalForm();
050            assertTrue(urlExternalForm.endsWith("org/springframework/richclient/image/broken.gif"));
051            Image image = source.getImage("bogus.image.key");
052            assertNotNull(image);
053        }
054    
055        public void testInvalidImageAccess() {
056            ImageSource source = (ImageSource)context.getBean("imageSourceBroken");
057            try {
058                source.getImageResource("invalid.image.key");
059            }
060            catch (NoSuchImageResourceException e) {
061                // expected
062            }
063            try {
064                source.getImage("invalid.image.key");
065            }
066            catch (NoSuchImageResourceException e) {
067                // expected
068            }
069        }
070    
071        protected void setUp() throws Exception {
072            context = new ClassPathXmlApplicationContext("org/springframework/richclient/image/application-context.xml");
073        }
074    }