Source Code Cross Referenced for TestFileCache.java in  » Portal » jetspeed-2.1.3 » org » apache » jetspeed » cache » file » 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
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » jetspeed 2.1.3 » org.apache.jetspeed.cache.file 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.jetspeed.cache.file;
019:
020:        import java.io.BufferedInputStream;
021:        import java.io.File;
022:        import java.io.FileInputStream;
023:        import java.util.Date;
024:        import java.util.Iterator;
025:
026:        import junit.framework.Test;
027:        import junit.framework.TestCase;
028:        import junit.framework.TestSuite;
029:
030:        import org.apache.commons.io.StreamUtils;
031:        import org.apache.commons.lang.exception.ExceptionUtils;
032:
033:        /**
034:         * Unit test for FileCache 
035:         * 
036:         * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
037:         * @version $Id: TestFileCache.java 516448 2007-03-09 16:25:47Z ate $
038:         */
039:
040:        public class TestFileCache extends TestCase implements 
041:                FileCacheEventListener {
042:            protected static final String TEST_DIRECTORY = "./testdata";
043:            protected static final int CACHE_SIZE = 20;
044:            protected static final int SCAN_RATE = 10;
045:            String refreshedEntry = null;
046:
047:            /**
048:             * Creates the test suite.
049:             *
050:             * @return a test suite (<code>TestSuite</code>) that includes all methods
051:             *         starting with "test"
052:             */
053:            public static Test suite() {
054:                // All methods starting with "test" will be executed in the test suite.
055:                return new TestSuite(TestFileCache.class);
056:            }
057:
058:            private FileCache cache = null;
059:
060:            protected void setUp() throws Exception {
061:                super .setUp();
062:
063:                cache = new FileCache(SCAN_RATE, CACHE_SIZE);
064:            }
065:
066:            /**
067:             * @see junit.framework.TestCase#tearDown()
068:             */
069:            public void tearDown() throws Exception {
070:                super .tearDown();
071:                removeTestFiles();
072:            }
073:
074:            /**
075:             * Tests loading the cache
076:             * @throws Exception
077:             */
078:
079:            public void testLoadCache() throws Exception {
080:                String templateFile = TEST_DIRECTORY + "/default.psml";
081:                try {
082:                    File file = new File(templateFile);
083:                    assertTrue(file.exists());
084:
085:                    createTestFiles(templateFile);
086:
087:                    // create the Cache  wake up after 10 seconds, cache size 20
088:                    // FileCache cache = new FileCache(10, 20);
089:
090:                    // load the Cache
091:                    File directory = new File(TEST_DIRECTORY);
092:                    File[] files = directory.listFiles();
093:                    for (int ix = 0; ix < files.length; ix++) {
094:                        if (files[ix].isDirectory()
095:                                || files[ix].getName().equals(".cvsignore")) {
096:                            continue;
097:                        }
098:                        String testData = readFile(files[ix]);
099:                        cache.put(files[ix], testData);
100:                    }
101:
102:                    assertTrue(cache.getSize() == 31);
103:
104:                    dumpCache(cache.getIterator());
105:
106:                    cache.addListener(this );
107:
108:                    // start the cache's scanner
109:                    cache.startFileScanner();
110:
111:                    Thread.sleep(2000);
112:
113:                    assertTrue(cache.getSize() == 20);
114:
115:                    dumpCache(cache.getIterator());
116:
117:                    // Reload files array to get the files back in the correct order
118:                    // because the cache CAN have reordered them while evicting.
119:                    // This can happen if test files where left over from a previous 
120:                    // test which then will have an older timestamp.
121:                    // In that case it is NOT garanteed that files[18] below will still
122:                    // be in the cache!
123:                    // Note: this is only an issue for the test itself and not for the
124:                    // cache as such. 
125:
126:                    Iterator it = cache.getIterator();
127:                    for (int ix = 0; it.hasNext(); ix++) {
128:                        FileCacheEntry entry = (FileCacheEntry) it.next();
129:                        files[ix] = entry.getFile();
130:                    }
131:
132:                    String stuff = (String) cache.getDocument(files[18]
133:                            .getCanonicalPath());
134:                    assertNotNull(stuff);
135:
136:                    files[18].setLastModified(new Date().getTime());
137:
138:                    Thread.sleep(9000);
139:
140:                    assertNotNull(refreshedEntry);
141:                    System.out.println("refreshed entry = " + refreshedEntry);
142:
143:                    cache.stopFileScanner();
144:
145:                    // evict all from cache
146:                    cache.evictAll();
147:                    assertTrue(cache.getSize() == 0);
148:
149:                    removeTestFiles();
150:                } catch (Exception e) {
151:                    fail(ExceptionUtils.getStackTrace(e));
152:                }
153:
154:                System.out.println("Completed loadCache Test OK ");
155:
156:            }
157:
158:            private void createTestFiles(String templateFile)
159:                    throws java.io.IOException {
160:                for (int ix = 1; ix < 31; ix++) {
161:                    String testFile = TEST_DIRECTORY + "/testFile-" + ix
162:                            + ".psml";
163:                    FileCopy.copy(templateFile, testFile);
164:                }
165:            }
166:
167:            private void removeTestFiles() {
168:                for (int ix = 1; ix < 31; ix++) {
169:                    String testFile = TEST_DIRECTORY + "/testFile-" + ix
170:                            + ".psml";
171:                    File file = new File(testFile);
172:                    if (file.exists())
173:                        file.delete();
174:                }
175:            }
176:
177:            private String readFile(File file) throws java.io.IOException,
178:                    java.io.FileNotFoundException {
179:                BufferedInputStream input;
180:
181:                input = new BufferedInputStream(new FileInputStream(file));
182:                String result = StreamUtils.streamAsString(input);
183:                input.close();
184:                return result;
185:            }
186:
187:            /**
188:             * Refresh event, called when the entry is being refreshed from file system.
189:             *
190:             * @param entry the entry being refreshed.
191:             */
192:            public void refresh(FileCacheEntry entry) {
193:                System.out.println("entry is refreshing: "
194:                        + entry.getFile().getName());
195:                this .refreshedEntry = entry.getFile().getName();
196:            }
197:
198:            /**
199:             * Evict event, called when the entry is being evicted out of the cache
200:             *
201:             * @param entry the entry being refreshed.
202:             */
203:            public void evict(FileCacheEntry entry) {
204:                System.out.println("entry is evicting: "
205:                        + entry.getFile().getName());
206:            }
207:
208:            private void dumpCache(Iterator it) {
209:                for (; it.hasNext();) {
210:                    FileCacheEntry entry = (FileCacheEntry) it.next();
211:                    System.out.println(entry.getFile().getName());
212:                }
213:            }
214:
215:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.