KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Bug974609_2


1 /* Open Source Java Caching Service
2 * Copyright (C) 2002 Frank Karlstrøm
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * The author can be contacted by email: fjankk@users.sourceforge.net
18 */

19 import java.util.Map JavaDoc;
20 import javax.util.jcache.Cache;
21 import javax.util.jcache.CacheAccessFactory;
22 import javax.util.jcache.CacheAttributes;
23 import junit.framework.TestCase;
24
25 public class Bug974609_2 extends TestCase {
26     /**
27      * Tests the diskSize. Sets the maxObjects to 1, inserts 3 elements,
28      * where all elements are one Mb in size. The first goes to memory,
29      * the second should be spooled to disk, while the third should not fit
30      * anywhere, and an {@link javax.util.jcache.CacheFullException} should be thrown.
31      * The diskCaching system has the ability to read the size of the objects,
32      * so there is no need to specify the exact size of each object.
33      * Since the diskCache has some sizeoverhead, each object is slightly smaller than 1Mb
34      * to avoid that the cache stops before at least one object is added..
35      * @throws Exception if any exceptions occur.
36      */

37     final public void testSetDiskSize() throws Exception JavaDoc {
38         CacheAttributes ca = CacheAttributes.getDefaultCacheAttributes();
39         ca.setDiskPath(System.getProperty("java.io.tmpdir"));
40         ca.setDiskCacheSize(1);
41         ca.setMaxObjects(1);
42         ca.setLocal();
43         CacheAccessFactory factory = CacheAccessFactory.getInstance();
44         Cache cache = factory.getCache();
45         cache.close();
46         cache.init(ca);
47         Map JavaDoc map = factory.getMapAccess();
48         map.put("one", new byte[1024*1024]);
49         map.put("two", new byte[1024*900]);
50         try {
51             map.put("three", new byte[1024*900]);
52             fail("Should throw CacheException, but did not.");
53         }catch(IllegalArgumentException JavaDoc e) {
54             //this is good, its what supposed to happen.
55
}
56     }
57 }
58
Popular Tags