Source Code Cross Referenced for TestQueueCache.java in  » Cache » OSCache » com » opensymphony » oscache » base » algorithm » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Cache » OSCache » com.opensymphony.oscache.base.algorithm 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2003 by OpenSymphony
003:         * All rights reserved.
004:         */
005:        package com.opensymphony.oscache.base.algorithm;
006:
007:        import com.opensymphony.oscache.base.Config;
008:        import com.opensymphony.oscache.base.persistence.PersistenceListener;
009:        import com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener;
010:        import com.opensymphony.oscache.plugins.diskpersistence.TestDiskPersistenceListener;
011:
012:        import java.util.Iterator;
013:        import java.util.Properties;
014:
015:        /**
016:         * Test class for the QueueCache class, which is the base class for FIFO
017:         * and LIFO algorithm classes. All the public methods of QueueCache are tested
018:         * here.
019:         *
020:         * $Id: TestQueueCache.java 383 2006-09-10 22:00:01Z larst $
021:         * @version        $Revision: 383 $
022:         * @author <a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
023:         */
024:        public abstract class TestQueueCache extends TestAbstractCache {
025:            /**
026:             * Entry content
027:             */
028:            protected final String CONTENT = "Test Queue Cache content";
029:
030:            /**
031:             * Entry key
032:             */
033:            protected final String KEY = "Test Queue Cache key";
034:
035:            /**
036:             * Constructor
037:             * <p>
038:             * @param str The test name (required by JUnit)
039:             */
040:            public TestQueueCache(String str) {
041:                super (str);
042:            }
043:
044:            /**
045:             * Test the specific algorithms
046:             */
047:            public abstract void testRemoveItem();
048:
049:            /**
050:             * Test the clear
051:             */
052:            public void testClear() {
053:                getCache().clear();
054:                assertEquals(0, getCache().size());
055:            }
056:
057:            /**
058:             * Test the ContainsKey method
059:             */
060:            public void testContainsKey() {
061:                getCache().put(KEY, CONTENT);
062:                assertTrue(getCache().containsKey(KEY));
063:                getCache().clear();
064:            }
065:
066:            /**
067:             * Test the get method
068:             */
069:            public void testGet() {
070:                // Add an entry and verify that it is there
071:                getCache().put(KEY, CONTENT);
072:                assertTrue(getCache().get(KEY).equals(CONTENT));
073:
074:                // Call with invalid parameters
075:                try {
076:                    getCache().get(null);
077:                    fail("Get called with null parameters!");
078:                } catch (Exception e) { /* This is what we expect */
079:                }
080:
081:                getCache().clear();
082:            }
083:
084:            /**
085:             * Test the getter and setter for the max entries
086:             */
087:            public void testGetSetMaxEntries() {
088:                // Check that the cache is full, then chop it by one and assert that
089:                // an element has been removed
090:                for (int count = 0; count < MAX_ENTRIES; count++) {
091:                    getCache().put(KEY + count, CONTENT + count);
092:                }
093:
094:                assertEquals(MAX_ENTRIES, getCache().size());
095:                getCache().setMaxEntries(MAX_ENTRIES - 1);
096:                assertEquals(MAX_ENTRIES - 1, getCache().getMaxEntries());
097:                assertEquals(MAX_ENTRIES - 1, getCache().size());
098:
099:                // Specify an invalid capacity
100:                try {
101:                    getCache().setMaxEntries(INVALID_MAX_ENTRIES);
102:                    fail("Cache capacity set with an invalid argument");
103:                } catch (Exception e) {
104:                    // This is what we expect
105:                }
106:
107:                getCache().clear();
108:            }
109:
110:            /**
111:             * Test the iterator
112:             */
113:            public void testIterator() {
114:                // Verify that the iterator returns MAX_ENTRIES and no more elements
115:                int nbEntries = getCache().size();
116:                Iterator iterator = getCache().entrySet().iterator();
117:                assertNotNull(iterator);
118:
119:                for (int count = 0; count < nbEntries; count++) {
120:                    assertNotNull(iterator.next());
121:                }
122:
123:                assertTrue(!iterator.hasNext());
124:            }
125:
126:            /**
127:             * Test the put method
128:             */
129:            public void testPut() {
130:                // Put elements in cache
131:                for (int count = 0; count < MAX_ENTRIES; count++) {
132:                    getCache().put(KEY + count, CONTENT + count);
133:                }
134:
135:                // Call with invalid parameters
136:                try {
137:                    getCache().put(null, null);
138:                    fail("Put called with null parameters!");
139:                } catch (Exception e) { /* This is what we expect */
140:                }
141:
142:                getCache().clear();
143:            }
144:
145:            /**
146:             * Test the put method with overflow parameter set
147:             */
148:            public void testPutOverflow() {
149:                // Create a listener
150:                PersistenceListener listener = new DiskPersistenceListener();
151:
152:                Properties p = new Properties();
153:                p.setProperty("cache.path",
154:                        TestDiskPersistenceListener.CACHEDIR);
155:                p.setProperty("cache.memory", "true");
156:                p.setProperty("cache.persistence.overflow.only", "true");
157:                p
158:                        .setProperty("cache.persistence.class",
159:                                "com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener");
160:                listener.configure(new Config(p));
161:                getCache().setPersistenceListener(listener);
162:                getCache().clear();
163:                getCache().setMaxEntries(MAX_ENTRIES);
164:                getCache().setOverflowPersistence(true);
165:
166:                if (getCache() instanceof  UnlimitedCache) {
167:                    return; // nothing to test since memory will never overflow.
168:                }
169:
170:                // Put elements in cache
171:                for (int count = 0; count <= MAX_ENTRIES; count++) {
172:                    getCache().put(KEY + count, CONTENT + count);
173:                }
174:
175:                try {
176:                    int numPersisted = 0;
177:
178:                    // Check that number of elements persisted == 1 if it is an overflow cache or all
179:                    // if it is not overflow and writes every time.
180:                    for (int count = 0; count <= MAX_ENTRIES; count++) {
181:                        if (getCache().getPersistenceListener().isStored(
182:                                KEY + count)) {
183:                            numPersisted++;
184:                        }
185:                    }
186:
187:                    if (getCache().isOverflowPersistence()) {
188:                        assertTrue(
189:                                "Only 1 element should have been persisted ",
190:                                numPersisted == 1);
191:                    } else {
192:                        assertTrue("All elements should have been persisted ",
193:                                numPersisted == (MAX_ENTRIES + 1));
194:                    }
195:                } catch (Exception e) {
196:                    fail();
197:                }
198:
199:                getCache().clear();
200:            }
201:
202:            /**
203:             * Test if bug CACHE-255 disappeared.
204:             */
205:            public void testBugCache255() {
206:                if (!getCache().isMemoryCaching()) {
207:                    return; // nothing to test since memory won't be used.
208:                }
209:                if (getCache() instanceof  UnlimitedCache) {
210:                    return; // nothing to test since memory will never overflow.
211:                }
212:
213:                // fill up the cache
214:                for (int count = 0; count < MAX_ENTRIES; count++) {
215:                    getCache().put(KEY + count, CONTENT + count);
216:                }
217:
218:                // get the old value
219:                Object oldValue = getCache().put(KEY + MAX_ENTRIES,
220:                        CONTENT + MAX_ENTRIES);
221:
222:                assertEquals("Evicted object content should be the same",
223:                        CONTENT + "0", oldValue);
224:
225:                getCache().clear();
226:            }
227:
228:            /**
229:             * Test the remove from cache
230:             */
231:            public void testRemove() {
232:                getCache().put(KEY, CONTENT);
233:
234:                // Remove the object and assert the return
235:                assertNotNull(getCache().remove(KEY));
236:                getCache().clear();
237:            }
238:        }
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.