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