Source Code Cross Referenced for AbstractCacheTest.java in  » Cache » ehcache » net » sf » ehcache » 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 » Cache » ehcache » net.sf.ehcache 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *  Copyright 2003-2007 Luck Consulting Pty Ltd
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         *  you may not use this file except in compliance with the License.
006:         *  You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         *  distributed under the License is distributed on an "AS IS" BASIS,
012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         *  See the License for the specific language governing permissions and
014:         *  limitations under the License.
015:         */package net.sf.ehcache;
016:
017:        import junit.framework.TestCase;
018:
019:        import javax.management.MBeanServer;
020:        import javax.management.MBeanServerFactory;
021:        import java.io.File;
022:        import java.io.IOException;
023:        import java.util.List;
024:        import java.lang.reflect.Method;
025:
026:        import org.apache.commons.logging.Log;
027:        import org.apache.commons.logging.LogFactory;
028:
029:        /**
030:         * Common fields and methods required by most test cases
031:         *
032:         * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
033:         * @version $Id: AbstractCacheTest.java 572 2007-12-31 03:13:33Z gregluck $
034:         */
035:        public abstract class AbstractCacheTest extends TestCase {
036:
037:            /**
038:             * Where the config is
039:             */
040:            public static final String SRC_CONFIG_DIR = "src/main/config/";
041:
042:            /**
043:             * Where the test config is
044:             */
045:            public static final String TEST_CONFIG_DIR = "src/test/resources/";
046:            /**
047:             * Where the test classes are compiled.
048:             */
049:            public static final String TEST_CLASSES_DIR = "target/test-classes/";
050:
051:            private static final Log LOG = LogFactory
052:                    .getLog(AbstractCacheTest.class.getName());
053:
054:            /**
055:             * name for sample cache 1
056:             */
057:            protected final String sampleCache1 = "sampleCache1";
058:            /**
059:             * name for sample cache 2
060:             */
061:            protected final String sampleCache2 = "sampleCache2";
062:            /**
063:             * the CacheManager instance
064:             */
065:            protected CacheManager manager;
066:
067:            /**
068:             * setup test
069:             */
070:            protected void setUp() throws Exception {
071:                manager = CacheManager.create();
072:            }
073:
074:            /**
075:             * teardown
076:             */
077:            protected void tearDown() throws Exception {
078:                if (manager != null) {
079:                    manager.shutdown();
080:                }
081:            }
082:
083:            /**
084:             * Force the VM to grow to its full size. This stops SoftReferences from being reclaimed in favour of
085:             * Heap growth. Only an issue when a VM is cold.
086:             */
087:            static public void forceVMGrowth() {
088:                allocateFiftyMegabytes();
089:                System.gc();
090:                try {
091:                    Thread.sleep(200);
092:                } catch (InterruptedException e) {
093:                    //
094:                }
095:                System.gc();
096:            }
097:
098:            private static void allocateFiftyMegabytes() {
099:                byte[] forceVMGrowth = new byte[50000000];
100:            }
101:
102:            /**
103:             * @param name
104:             * @throws IOException
105:             */
106:            protected void deleteFile(String name) throws IOException {
107:                String diskPath = System.getProperty("java.io.tmpdir");
108:                final File diskDir = new File(diskPath);
109:                File dataFile = new File(diskDir, name + ".data");
110:                if (dataFile.exists()) {
111:                    dataFile.delete();
112:                }
113:                File indexFile = new File(diskDir, name + ".index");
114:                if (indexFile.exists()) {
115:                    indexFile.delete();
116:                }
117:            }
118:
119:            /**
120:             * Measure memory used by the VM.
121:             *
122:             * @return
123:             * @throws InterruptedException
124:             */
125:            protected long measureMemoryUse() throws InterruptedException {
126:                System.gc();
127:                Thread.sleep(2000);
128:                System.gc();
129:                return Runtime.getRuntime().totalMemory()
130:                        - Runtime.getRuntime().freeMemory();
131:            }
132:
133:            /**
134:             * Runs a set of threads, for a fixed amount of time.
135:             */
136:            protected void runThreads(final List executables) throws Exception {
137:
138:                final long endTime = System.currentTimeMillis() + 10000;
139:                final Throwable[] errors = new Throwable[1];
140:
141:                // Spin up the threads
142:                final Thread[] threads = new Thread[executables.size()];
143:                for (int i = 0; i < threads.length; i++) {
144:                    final Executable executable = (Executable) executables
145:                            .get(i);
146:                    threads[i] = new Thread() {
147:                        public void run() {
148:                            try {
149:                                // Run the thread until the given end time
150:                                while (System.currentTimeMillis() < endTime) {
151:                                    executable.execute();
152:                                }
153:                            } catch (Throwable t) {
154:                                // Hang on to any errors
155:                                errors[0] = t;
156:                                LOG.info(t);
157:                            }
158:                        }
159:                    };
160:                    threads[i].start();
161:                }
162:
163:                // Wait for the threads to finish
164:                for (int i = 0; i < threads.length; i++) {
165:                    threads[i].join();
166:                }
167:
168:                // Throw any error that happened
169:                if (errors[0] != null) {
170:                    throw new Exception("Test thread failed.", errors[0]);
171:                }
172:            }
173:
174:            /**
175:             * Obtains an MBeanServer, which varies with Java version
176:             * @return
177:             */
178:            public MBeanServer createMBeanServer() {
179:                try {
180:                    Class managementFactoryClass = Class
181:                            .forName("java.lang.management.ManagementFactory");
182:                    Method method = managementFactoryClass.getMethod(
183:                            "getPlatformMBeanServer", null);
184:                    return (MBeanServer) method.invoke(null, null);
185:                } catch (Exception e) {
186:                    LOG
187:                            .info(
188:                                    "JDK1.5 ManagementFactory not found. Falling back to JMX1.2.1",
189:                                    e);
190:                    return MBeanServerFactory.createMBeanServer("SimpleAgent");
191:                }
192:            }
193:
194:            /**
195:             * A runnable, that can throw an exception.
196:             */
197:            protected interface Executable {
198:                /**
199:                 * Executes this object.
200:                 *
201:                 * @throws Exception
202:                 */
203:                void execute() throws Exception;
204:            }
205:
206:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.