001: /**
002: *
003: */package clime.messadmin.providers.sizeof;
004:
005: import java.util.ArrayList;
006: import java.util.Date;
007: import java.util.Enumeration;
008: import java.util.List;
009:
010: import javax.servlet.ServletContext;
011: import javax.servlet.http.HttpSession;
012: import javax.servlet.http.HttpSessionContext;
013:
014: import clime.messadmin.model.Application;
015: import clime.messadmin.model.ApplicationInfo;
016: import clime.messadmin.model.Request;
017: import clime.messadmin.model.RequestInfo;
018: import clime.messadmin.model.Server;
019: import clime.messadmin.model.ServerInfo;
020: import clime.messadmin.model.Session;
021: import clime.messadmin.model.SessionInfo;
022: import clime.messadmin.model.stats.HitsCounter;
023: import clime.messadmin.model.stats.MinMaxTracker;
024: import clime.messadmin.model.stats.StatisticsAgregator;
025:
026: import junit.framework.TestCase;
027:
028: /**
029: * @author Cédrik LIME
030: */
031: public class ObjectProfilerTest extends TestCase {
032: private static HttpSession emptySession = new HttpSession() {
033: public boolean isNew() {
034: return false;
035: }
036:
037: public void invalidate() {
038: }
039:
040: public void removeValue(String name) {
041: }
042:
043: public void removeAttribute(String name) {
044: }
045:
046: public void putValue(String name, Object value) {
047: }
048:
049: public void setAttribute(String name, Object value) {
050: }
051:
052: public String[] getValueNames() {
053: return null;
054: }
055:
056: public Enumeration getAttributeNames() {
057: return null;
058: }
059:
060: public Object getValue(String name) {
061: return null;
062: }
063:
064: public Object getAttribute(String name) {
065: return null;
066: }
067:
068: public HttpSessionContext getSessionContext() {
069: return null;
070: }
071:
072: public int getMaxInactiveInterval() {
073: return 0;
074: }
075:
076: public void setMaxInactiveInterval(int interval) {
077: }
078:
079: public ServletContext getServletContext() {
080: return null;
081: }
082:
083: public long getLastAccessedTime() {
084: return 0;
085: }
086:
087: public String getId() {
088: return "0123456789ABCDEF";
089: }
090:
091: public long getCreationTime() {
092: return 0;
093: }
094: };
095: protected String[] sunProblematicClassesNames;
096: protected Class[] sunProblematicClasses;
097:
098: /**
099: * Constructor for ObjectProfilerTest.
100: * @param name
101: */
102: public ObjectProfilerTest(String name) {
103: super (name);
104: sunProblematicClassesNames = new String[] {
105: "java.lang.Throwable", // 1.3+ 20
106: "sun.reflect.ConstantPool", // 1.5+ 8
107: "sun.reflect.UnsafeStaticFieldAccessorImpl", // 1.4+
108: "sun.reflect.UnsafeStaticObjectFieldAccessorImpl", // 1.4+
109: "sun.reflect.UnsafeStaticLongFieldAccessorImpl" // 1.4+
110: };
111: List classes = new ArrayList(sunProblematicClassesNames.length);
112: for (int i = 0; i < sunProblematicClassesNames.length; ++i) {
113: String className = sunProblematicClassesNames[i];
114: try {
115: classes.add(Class.forName(className));
116: } catch (ClassNotFoundException cnfe) {
117: //System.out.println(cnfe);
118: }
119: }
120: sunProblematicClasses = (Class[]) classes.toArray(new Class[0]);
121: }
122:
123: public static void main(String[] args) {
124: junit.textui.TestRunner.run(ObjectProfilerTest.class);
125: }
126:
127: /**
128: * {@inheritDoc}
129: */
130: protected void setUp() throws Exception {
131: super .setUp();
132: }
133:
134: /**
135: * {@inheritDoc}
136: */
137: protected void tearDown() throws Exception {
138: super .tearDown();
139: }
140:
141: /*
142: * Test method for 'clime.messadmin.utils.ObjectProfiler.sizeof(Object)'
143: */
144: public void testSizeof() {
145: assertEquals(8, ObjectProfiler.sizeof(new Object()));
146: assertEquals(20, ObjectProfiler.sizeof(new Date()));
147: assertEquals(40, ObjectProfiler.sizeof(new String()));
148: assertEquals(16, ObjectProfiler.sizeof(new Object[0]));
149: assertEquals(16, ObjectProfiler.sizeof(new String[0]));
150: assertEquals(56, ObjectProfiler.sizeof(new Object[10]));
151: assertEquals(56, ObjectProfiler.sizeof(new String[10]));
152:
153: System.out.println("sizeOf(HitsCounter) == "
154: + ObjectProfiler.sizeof(new HitsCounter()));
155: System.out.println("sizeOf(MinMaxTracker) == "
156: + ObjectProfiler.sizeof(new MinMaxTracker()));
157: System.out.println("sizeOf(StatisticsAgregator) == "
158: + ObjectProfiler.sizeof(new StatisticsAgregator()));
159: System.out.println("sizeOf(Request) == "
160: + ObjectProfiler.sizeof(new Request(null)));
161: System.out.println("sizeOf(empty RequestInfo) == "
162: + ObjectProfiler.sizeof(new RequestInfo(null)));
163: System.out
164: .println("sizeOf(Session) == "
165: + (ObjectProfiler.sizeof(new Session(
166: emptySession)) - ObjectProfiler
167: .sizeof(emptySession)));
168: System.out.println("sizeOf(empty SessionInfo) == "
169: + ObjectProfiler.sizeof(new SessionInfo()));
170: System.out.println("sizeOf(Application) == "
171: + ObjectProfiler.sizeof(new Application(null)));
172: System.out.println("sizeOf(empty ApplicationInfo) == "
173: + ObjectProfiler.sizeof(new ApplicationInfo(null)));
174: System.out.println("sizeOf(Server) == "
175: + ObjectProfiler.sizeof(Server.getInstance()));
176: System.out.println("sizeOf(empty ServerInfo) == "
177: + ObjectProfiler.sizeof(new ServerInfo()));
178: }
179:
180: public void testSunBuggyJVM() {
181: for (int i = 0; i < sunProblematicClasses.length; ++i) {
182: Class clazz = sunProblematicClasses[i];
183: Object instance = null;
184: try {
185: instance = clazz.newInstance();
186: } catch (InstantiationException e) {
187: //System.err.println(e);
188: } catch (IllegalAccessException e) {
189: //System.err.println(e);
190: }
191: if (instance != null) {
192: ObjectProfiler.sizeof(instance); //should not throw exception
193: }
194: }
195: }
196: }
|