Source Code Cross Referenced for TestDiskPersistenceListener.java in  » Cache » OSCache » com » opensymphony » oscache » plugins » diskpersistence » 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.plugins.diskpersistence 
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.plugins.diskpersistence;
006:
007:        import com.opensymphony.oscache.base.CacheEntry;
008:        import com.opensymphony.oscache.base.Config;
009:        import com.opensymphony.oscache.base.persistence.CachePersistenceException;
010:
011:        import junit.framework.Test;
012:        import junit.framework.TestCase;
013:        import junit.framework.TestSuite;
014:
015:        import java.io.File;
016:        import java.io.FilenameFilter;
017:
018:        import java.util.HashSet;
019:        import java.util.Properties;
020:        import java.util.Set;
021:
022:        /**
023:         * Test all the public methods of the disk persistance listener and assert the
024:         * return values
025:         *
026:         * $Id: TestDiskPersistenceListener.java 422 2007-03-17 23:47:29Z larst $
027:         * @version        $Revision: 422 $
028:         * @author <a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
029:         */
030:        public final class TestDiskPersistenceListener extends TestCase {
031:            /**
032:             * Cache dir to persist to
033:             */
034:            public static final String CACHEDIR = "/tmp/diskcache";
035:
036:            /**
037:             * The persistance listener used for the tests
038:             */
039:            private DiskPersistenceListener listener = null;
040:
041:            /**
042:             * Object content
043:             */
044:            private final String CONTENT = "Disk persistance content";
045:
046:            /**
047:             * Cache group
048:             */
049:            private final String GROUP = "test group";
050:
051:            /**
052:             * Object key
053:             */
054:            private final String KEY = "Test disk persistance listener key";
055:            private CacheFileFilter cacheFileFilter = new CacheFileFilter();
056:
057:            public TestDiskPersistenceListener(String str) {
058:                super (str);
059:            }
060:
061:            /**
062:             * This methods returns the name of this test class to JUnit
063:             * <p>
064:             * @return The test for this class
065:             */
066:            public static Test suite() {
067:                return new TestSuite(TestDiskPersistenceListener.class);
068:            }
069:
070:            /**
071:             * This method is invoked before each testXXXX methods of the
072:             * class. It set ups the variables required for each tests.
073:             */
074:            public void setUp() {
075:                // At first invocation, create a listener
076:                listener = new DiskPersistenceListener();
077:
078:                Properties p = new Properties();
079:                p.setProperty("cache.path", CACHEDIR);
080:                p.setProperty("cache.memory", "false");
081:                p
082:                        .setProperty("cache.persistence.class",
083:                                "com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener");
084:                listener.configure(new Config(p));
085:            }
086:
087:            /**
088:             * Test the cache directory removal
089:             */
090:            public void testClear() {
091:                // Create an new element since we removed it at the last test
092:                testStoreRetrieve();
093:
094:                // Remove the directory, and assert that we have no more entry
095:                try {
096:                    listener.clear();
097:                    assertTrue(!listener.isStored(KEY));
098:                } catch (CachePersistenceException cpe) {
099:                    cpe.printStackTrace();
100:                    fail("Exception thrown in test clear!");
101:                }
102:            }
103:
104:            /**
105:             * Test that the previouly created file exists
106:             */
107:            public void testIsStored() {
108:                try {
109:                    listener.store(KEY, CONTENT);
110:
111:                    // Retrieve the previously created file
112:                    assertTrue(listener.isStored(KEY));
113:
114:                    // Check that the fake key returns false
115:                    assertTrue(!listener.isStored(KEY + "fake"));
116:                } catch (Exception e) {
117:                    e.printStackTrace();
118:                    fail("testIsStored raised an exception");
119:                }
120:            }
121:
122:            /**
123:             * Test the cache removal
124:             */
125:            public void testRemove() {
126:                // Create an entry if it doesn't exists
127:                try {
128:                    if (!listener.isStored(KEY)) {
129:                        listener.store(KEY, CONTENT);
130:                    }
131:
132:                    // Remove the previously created file
133:                    listener.remove(KEY);
134:                } catch (CachePersistenceException cpe) {
135:                    cpe.printStackTrace();
136:                    fail("Exception thrown in test remove!");
137:                }
138:            }
139:
140:            /**
141:             * Force CachePersistenceException to get a 100% in the unit test
142:             */
143:            public void testCachePersistenceException() {
144:                try {
145:                    for (int i = 0; i < 2; i++) {
146:                        if (i == 1)
147:                            throw new CachePersistenceException("test");
148:                    }
149:                    fail("CachePersistenceException not thrown!");
150:                } catch (CachePersistenceException cpe) {
151:                    // ignore
152:                }
153:                try {
154:                    for (int i = 0; i < 2; i++) {
155:                        if (i == 1)
156:                            throw new CachePersistenceException();
157:                    }
158:                    fail("CachePersistenceException not thrown!");
159:                } catch (CachePersistenceException cpe) {
160:                    // ignore
161:                }
162:            }
163:
164:            /**
165:             * Test the disk store and retrieve
166:             */
167:            public void testStoreRetrieve() {
168:                // Create a cache entry and store it
169:                CacheEntry entry = new CacheEntry(KEY);
170:                entry.setContent(CONTENT);
171:
172:                try {
173:                    listener.store(KEY, entry);
174:
175:                    // Retrieve our entry and validate the values
176:                    CacheEntry newEntry = (CacheEntry) listener.retrieve(KEY);
177:                    assertTrue(entry.getContent().equals(newEntry.getContent()));
178:                    assertEquals(entry.getCreated(), newEntry.getCreated());
179:                    assertTrue(entry.getKey().equals(newEntry.getKey()));
180:
181:                    // Try to retrieve a non-existent object
182:                    assertNull(listener.retrieve("doesn't exist"));
183:                } catch (Exception ex) {
184:                    ex.printStackTrace();
185:                    fail("Exception raised!");
186:                }
187:            }
188:
189:            /**
190:             * Test the storing and retrieving of groups
191:             */
192:            public void testStoreRetrieveGroups() {
193:                // Store a group
194:                Set groupSet = new HashSet();
195:                groupSet.add("1");
196:                groupSet.add("2");
197:
198:                try {
199:                    listener.storeGroup(GROUP, groupSet);
200:
201:                    // Retrieve it and validate its contents
202:                    groupSet = listener.retrieveGroup(GROUP);
203:                    assertNotNull(groupSet);
204:
205:                    assertTrue(groupSet.contains("1"));
206:                    assertTrue(groupSet.contains("2"));
207:                    assertFalse(groupSet.contains("3"));
208:
209:                    // Try to retrieve a non-existent group
210:                    assertNull(listener.retrieveGroup("abc"));
211:                } catch (Exception ex) {
212:                    ex.printStackTrace();
213:                    fail("Exception raised!");
214:                }
215:            }
216:
217:            protected void tearDown() throws Exception {
218:                listener.clear();
219:                assertTrue("Cache not cleared", new File(CACHEDIR)
220:                        .list(cacheFileFilter).length == 0);
221:            }
222:
223:            private static class CacheFileFilter implements  FilenameFilter {
224:                public boolean accept(File dir, String name) {
225:                    return !"__groups__".equals(name);
226:                }
227:            }
228:        }
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.