~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to test/java/org/apache/xmlgraphics/image/loader/cache/ImageCacheTestCase.java

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-02-11 14:15:14 UTC
  • mfrom: (8.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110211141514-h67achft6x31gju1
Tags: 1.4.dfsg-3
Uploading to unstable, hoping we won't break too many things ;-)...

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 * limitations under the License.
16
16
 */
17
17
 
18
 
/* $Id: ImageCacheTestCase.java 606580 2007-12-23 17:45:02Z jeremias $ */
 
18
/* $Id: ImageCacheTestCase.java 816640 2009-09-18 14:14:55Z maxberger $ */
19
19
 
20
20
package org.apache.xmlgraphics.image.loader.cache;
21
21
 
35
35
 */
36
36
public class ImageCacheTestCase extends TestCase {
37
37
 
 
38
    private static final boolean DEBUG = false;
 
39
 
38
40
    private MockImageContext imageContext = MockImageContext.getInstance();
39
 
    
 
41
    private ImageSessionContext sessionContext = imageContext.newSessionContext();
 
42
    private ImageManager manager = imageContext.getImageManager();
 
43
    private ImageCacheStatistics statistics = (DEBUG
 
44
                ? new ImageCacheLoggingStatistics(true) : new ImageCacheStatistics(true));
 
45
 
 
46
    /** {@inheritDoc} */
 
47
    protected void setUp() throws Exception {
 
48
        super.setUp();
 
49
 
 
50
        manager.getCache().clearCache();
 
51
        statistics.reset();
 
52
        manager.getCache().setCacheListener(statistics);
 
53
    }
 
54
 
40
55
    /**
41
56
     * Tests the ImageInfo cache.
42
57
     * @throws Exception if an error occurs
43
58
     */
44
59
    public void testImageInfoCache() throws Exception {
45
 
 
46
 
        ImageSessionContext sessionContext = imageContext.newSessionContext();
47
 
        ImageManager manager = imageContext.getImageManager();
48
 
        
49
60
        String invalid1 = "invalid1.jpg";
50
61
        String invalid2 = "invalid2.jpg";
51
62
        String valid1 = "bgimg300dpi.bmp";
52
63
        String valid2 = "big-image.png";
53
 
        
54
 
        ImageCacheStatistics statistics = new ImageCacheLoggingStatistics(true);
55
 
        manager.getCache().setCacheListener(statistics);
56
 
        
 
64
 
 
65
 
57
66
        ImageInfo info1, info2;
58
67
        info1 = manager.getImageInfo(valid1, sessionContext);
59
68
        assertNotNull(info1);
60
69
        assertEquals(valid1, info1.getOriginalURI());
61
 
        
 
70
 
62
71
        try {
63
72
            manager.getImageInfo(invalid1, sessionContext);
64
73
            fail("Expected FileNotFoundException for invalid URI");
65
74
        } catch (FileNotFoundException e) {
66
75
            //expected
67
76
        }
68
 
        
 
77
 
69
78
        //2 requests:
70
79
        assertEquals(0, statistics.getImageInfoCacheHits());
71
80
        assertEquals(2, statistics.getImageInfoCacheMisses());
72
81
        assertEquals(0, statistics.getInvalidHits());
73
82
        statistics.reset();
74
 
        
 
83
 
75
84
        //Cache Hit
76
85
        info1 = manager.getImageInfo(valid1, sessionContext);
77
86
        assertNotNull(info1);
81
90
        info2 = manager.getImageInfo(valid2, sessionContext);
82
91
        assertNotNull(info2);
83
92
        assertEquals(valid2, info2.getOriginalURI());
84
 
        
 
93
 
85
94
        try {
86
95
            //Invalid Hit
87
96
            manager.getImageInfo(invalid1, sessionContext);
96
105
        } catch (FileNotFoundException e) {
97
106
            //expected
98
107
        }
99
 
        
 
108
 
100
109
        //4 requests:
101
110
        assertEquals(1, statistics.getImageInfoCacheHits());
102
111
        assertEquals(2, statistics.getImageInfoCacheMisses());
103
112
        assertEquals(1, statistics.getInvalidHits());
104
113
        statistics.reset();
105
 
        
 
114
    }
 
115
 
 
116
    public void testInvalidURIExpiration() throws Exception {
 
117
        MockTimeStampProvider provider = new MockTimeStampProvider();
 
118
        ImageCache cache = new ImageCache(provider, new DefaultExpirationPolicy(2));
 
119
        cache.setCacheListener(statistics);
 
120
 
 
121
        String invalid1 = "invalid1.jpg";
 
122
        String invalid2 = "invalid2.jpg";
 
123
        String valid1 = "valid1.jpg";
 
124
 
 
125
        provider.setTimeStamp(1000);
 
126
        cache.registerInvalidURI(invalid1);
 
127
        provider.setTimeStamp(1100);
 
128
        cache.registerInvalidURI(invalid2);
 
129
 
 
130
        assertEquals(0, statistics.getInvalidHits());
 
131
 
 
132
        //not expired, yet
 
133
        provider.setTimeStamp(1200);
 
134
        assertFalse(cache.isInvalidURI(valid1));
 
135
        assertTrue(cache.isInvalidURI(invalid1));
 
136
        assertTrue(cache.isInvalidURI(invalid2));
 
137
        assertEquals(2, statistics.getInvalidHits());
 
138
 
 
139
        //first expiration time reached
 
140
        provider.setTimeStamp(3050);
 
141
        assertFalse(cache.isInvalidURI(valid1));
 
142
        assertFalse(cache.isInvalidURI(invalid1));
 
143
        assertTrue(cache.isInvalidURI(invalid2));
 
144
        assertEquals(3, statistics.getInvalidHits());
 
145
 
 
146
        //second expiration time reached
 
147
        provider.setTimeStamp(3200);
 
148
        assertFalse(cache.isInvalidURI(valid1));
 
149
        assertFalse(cache.isInvalidURI(invalid1));
 
150
        assertFalse(cache.isInvalidURI(invalid2));
 
151
        assertEquals(3, statistics.getInvalidHits());
106
152
    }
107
153
 
108
154
    /**
110
156
     * @throws Exception if an error occurs
111
157
     */
112
158
    public void testImageCache1() throws Exception {
113
 
        ImageSessionContext sessionContext = imageContext.newSessionContext();
114
 
        ImageManager manager = imageContext.getImageManager();
115
 
        
116
159
        String valid1 = "bgimg72dpi.gif";
117
 
        
118
 
        ImageCacheStatistics statistics = new ImageCacheLoggingStatistics(true);
119
 
        manager.getCache().setCacheListener(statistics);
120
 
        
 
160
 
121
161
        ImageInfo info = manager.getImageInfo(valid1, sessionContext);
122
162
        assertNotNull(info);
123
 
        
 
163
 
124
164
        ImageBuffered img1 = (ImageBuffered)manager.getImage(
125
165
                info, ImageFlavor.BUFFERED_IMAGE, sessionContext);
126
166
        assertNotNull(img1);
127
167
        assertNotNull(img1.getBufferedImage());
128
 
        
 
168
 
129
169
        ImageBuffered img2 = (ImageBuffered)manager.getImage(
130
170
                info, ImageFlavor.BUFFERED_IMAGE, sessionContext);
131
171
        //ImageBuffered does not have to be the same instance but we want at least the
132
172
        //BufferedImage to be reused.
133
173
        assertTrue("BufferedImage must be reused",
134
174
                img1.getBufferedImage() == img2.getBufferedImage());
135
 
        
 
175
 
136
176
        assertEquals(1, statistics.getImageCacheHits());
137
177
        assertEquals(1, statistics.getImageCacheMisses());
138
178
    }
 
179
 
139
180
    
140
181
    /**
141
 
     * Tests the image cache reusing a cacheable Image created by one of the ImageConverters in
142
 
     * a converter pipeline.
143
 
     * @throws Exception if an error occurs
 
182
     * Test to check if doInvalidURIHouseKeeping() throws a
 
183
     * ConcurrentModificationException.
144
184
     */
145
 
    public void DISABLEDtestImageCache2() throws Exception {
146
 
        ImageSessionContext sessionContext = imageContext.newSessionContext();
147
 
        ImageManager manager = imageContext.getImageManager();
148
 
        
149
 
        String valid1 = "test/resources/images/img-w-size.svg";
150
 
        
151
 
        ImageCacheStatistics statistics = new ImageCacheLoggingStatistics(true);
152
 
        manager.getCache().setCacheListener(statistics);
153
 
        
154
 
        ImageInfo info = manager.getImageInfo(valid1, sessionContext);
155
 
        assertNotNull(info);
156
 
        
157
 
        ImageBuffered img1 = (ImageBuffered)manager.getImage(
158
 
                info, ImageFlavor.BUFFERED_IMAGE, sessionContext);
159
 
        assertNotNull(img1);
160
 
        assertNotNull(img1.getBufferedImage());
161
 
        
162
 
        ImageBuffered img2 = (ImageBuffered)manager.getImage(
163
 
                info, ImageFlavor.BUFFERED_IMAGE, sessionContext);
164
 
        //ImageBuffered does not have to be the same instance but we want at least the
165
 
        //BufferedImage to be reused.
166
 
        assertTrue("BufferedImage must be reused",
167
 
                img1.getBufferedImage() == img2.getBufferedImage());
168
 
 
169
 
        assertEquals(1, statistics.getImageCacheHits()); //1=BufferedImage
170
 
        assertEquals(3, statistics.getImageCacheMisses()); //3=BufferedImage,Graphics2DImage,DOM
 
185
    public void testImageCacheHouseKeeping() {
 
186
        ImageCache imageCache = new ImageCache(new TimeStampProvider(),
 
187
                new DefaultExpirationPolicy(1));
 
188
        imageCache.registerInvalidURI("invalid");
 
189
        imageCache.registerInvalidURI("invalid2");
 
190
        try {
 
191
            Thread.sleep(1200);
 
192
        } catch (InterruptedException e) {
 
193
            e.printStackTrace();
 
194
        }
 
195
        imageCache.doHouseKeeping();
171
196
    }
172
 
    
173
197
}