[P-unit-devel] SF.net SVN: p-unit: [232] trunk/punit
Status: Beta
Brought to you by:
zhanghuangzhu
|
From: <le...@us...> - 2007-08-19 22:46:40
|
Revision: 232
http://p-unit.svn.sourceforge.net/p-unit/?rev=232&view=rev
Author: leshik
Date: 2007-08-19 15:45:41 -0700 (Sun, 19 Aug 2007)
Log Message:
-----------
* Added junit-style shouldStop() method to detect timeout expiration from inside the test
* Allocate thread objects once for all test iterations.
* Replaced logger.* and assert.* domains with sensible ones.
* Slightly corrected messages in float and double asserts.
* Fixed testInstance allocation: allocate it once for class (as it is done in ng-unit).
* Improved memory deallocation algorithm in MemoryUtil.clear() to free objects with finalizers on RI, added wilderness utilities and maximum total memory calculation. Restored org.punit.exception.OutOfMemoryException. Added related test.
* Fixed Maximum/Minimum watcher value accumulation in case when _scale changes.
* Fixed formatting and tabs.
Modified Paths:
--------------
trunk/punit/src/org/punit/assertion/Assert.java
trunk/punit/src/org/punit/method/runner/AbstractMethodRunner.java
trunk/punit/src/org/punit/method/runner/ConcurrentMethodRunner.java
trunk/punit/src/org/punit/method/runner/MethodRunner.java
trunk/punit/src/org/punit/reporter/stream/StreamLogger.java
trunk/punit/src/org/punit/runner/AbstractRunner.java
trunk/punit/src/org/punit/util/MemoryUtil.java
trunk/punit/src/org/punit/util/ReflectionUtil.java
trunk/punit/src/org/punit/util/TraverserUtil.java
trunk/punit/src/org/punit/watcher/AbstractWatcher.java
trunk/punit/src/org/punit/watcher/CustomWatcher.java
trunk/punit/src/org/punit/watcher/MaximumWatcher.java
trunk/punit/src/org/punit/watcher/MinimumWatcher.java
trunk/punit/src/punit.properties
trunk/punit.extension/src/org/punit/listener/FieldSetter.java
trunk/punit.test/src/tests/api/org/punit/testclasses/SetResultTestClass.java
trunk/punit.test/src/tests/api/org/punit/util/IOUtilTest.java
trunk/punit.test/src/tests/api/org/punit/watcher/CustomWatcherTest.java
Added Paths:
-----------
trunk/punit/src/org/punit/exception/OutOfMemoryException.java
trunk/punit.test/src/tests/api/org/punit/util/MemoryUtilTest.java
Modified: trunk/punit/src/org/punit/assertion/Assert.java
===================================================================
--- trunk/punit/src/org/punit/assertion/Assert.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit/src/org/punit/assertion/Assert.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -14,7 +14,7 @@
public static void assertNotSame(Object expected, Object actual) {
if (expected == actual) {
- fail(Messages.getString("Assert.2") + expected); //$NON-NLS-1$
+ fail(Messages.getString("assert.notsame") + expected); //$NON-NLS-1$
}
}
@@ -37,15 +37,19 @@
}
public static void assertEquals(double expected, double actual, double delta) {
- assertTrue(Math.abs(expected - actual) <= delta);
+ if (Math.abs(expected - actual) > delta) {
+ fail(new Double(expected), new Double(actual));
+ }
}
-
+
public static void assertEquals(float expected, float actual) {
assertEquals(new Float(expected), new Float(actual));
}
public static void assertEquals(float expected, float actual, float delta) {
- assertTrue(Math.abs(expected - actual) <= delta);
+ if (Math.abs(expected - actual) > delta) {
+ fail(new Float(expected), new Float(actual));
+ }
}
public static void assertTrue(boolean flag) {
@@ -91,6 +95,6 @@
}
private static void fail(Object expected, Object actual) {
- fail(Messages.getString("Assert.0") + expected + Messages.getString("Assert.1") + actual); //$NON-NLS-1$ //$NON-NLS-2$
+ fail(Messages.getString("assert.expected") + expected + Messages.getString("assert.got") + actual); //$NON-NLS-2$
}
}
Added: trunk/punit/src/org/punit/exception/OutOfMemoryException.java
===================================================================
--- trunk/punit/src/org/punit/exception/OutOfMemoryException.java (rev 0)
+++ trunk/punit/src/org/punit/exception/OutOfMemoryException.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -0,0 +1,15 @@
+/* (C) Copyright 2007, by Andrew Zhang */
+
+package org.punit.exception;
+
+/**
+ * Indicates framework memory problem.
+ */
+public class OutOfMemoryException extends PUnitException {
+
+ private static final long serialVersionUID = -2694385721230707311L;
+
+ public OutOfMemoryException(OutOfMemoryError e) {
+ super("Framework didn't release allocated memory", e);
+ }
+}
Modified: trunk/punit/src/org/punit/method/runner/AbstractMethodRunner.java
===================================================================
--- trunk/punit/src/org/punit/method/runner/AbstractMethodRunner.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit/src/org/punit/method/runner/AbstractMethodRunner.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -35,7 +35,7 @@
private transient ToStopThread _toStopThread;
- private transient boolean _isLoop;
+ private transient boolean _shouldStop;
protected transient Object _testInstance;
@@ -87,9 +87,7 @@
startWatchers(testInstance, method, params);
setUpAfterWatchers(params);
startToStopThread();
- do {
- runImpl();
- } while (isLoop());
+ runImpl();
stopToStopThread();
runCheckMethod(testInstance, params);
} else {
@@ -175,23 +173,23 @@
stop();
}
}, toWork);
- _isLoop = true;
+ _shouldStop = false;
} else {
- _isLoop = false;
+ _shouldStop = true;
_toStopThread = null;
}
}
- protected synchronized boolean isLoop() {
- return _isLoop;
+ public boolean shouldStop() {
+ return _shouldStop;
}
protected synchronized void stop() {
- _isLoop = false;
+ _shouldStop = true;
}
/**
- * This method might be overriden by subclass. The runner may do some more
+ * This method might be overridden by subclass. The runner may do some more
* things during this step.
*
*/
@@ -354,7 +352,9 @@
protected void runMethod() throws Throwable {
if (_expectedException == null) {
- invokeMethod();
+ do {
+ invokeMethod();
+ } while (!shouldStop());
} else {
Assert.assertException(_expectedException, new CodeRunner() {
public void run() throws Throwable {
Modified: trunk/punit/src/org/punit/method/runner/ConcurrentMethodRunner.java
===================================================================
--- trunk/punit/src/org/punit/method/runner/ConcurrentMethodRunner.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit/src/org/punit/method/runner/ConcurrentMethodRunner.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -2,70 +2,71 @@
package org.punit.method.runner;
-import org.punit.exception.*;
+import org.punit.exception.ConcurrentException;
public class ConcurrentMethodRunner extends AbstractMethodRunner {
- private static final long serialVersionUID = 6423244395038097893L;
+ private static final long serialVersionUID = 6423244395038097893L;
- private ConcurrentException _concurrentException = new ConcurrentException();
+ private ConcurrentException _concurrentException = new ConcurrentException();
- private int _concurrentCount;
-
- private transient TestMethodThread[] _threads;
-
- public ConcurrentMethodRunner(int concurrentCount) {
- _concurrentCount = concurrentCount;
- }
+ private int _concurrentCount;
- protected void runImpl() throws Throwable {
- startThreads();
- joinThreads();
- if (_concurrentException.size() > 0) {
- throw _concurrentException;
- }
- }
-
- private void startThreads() {
- int count = _convention.getConcurrentCount(_testInstance, _method);
- int threadCount = count > 0? count : _concurrentCount;
- _threads = new TestMethodThread[threadCount];
- for (int i = 0; i < _threads.length; ++i) {
- _threads[i] = new TestMethodThread(this, "punit concurrent method runner"); //$NON-NLS-1$
- }
- for (int i = 0; i < _threads.length; ++i) {
- _threads[i].start();
- }
- }
+ private transient TestMethodThread[] _threads;
- private void joinThreads() {
- for (int i = 0; i < _threads.length; ++i) {
- try {
- _threads[i].join();
- } catch (InterruptedException e) {
- _concurrentException.add(e);
- }
- }
- }
-
- protected String getCheckMethodName() {
- return "check_" + _method.getName(); //$NON-NLS-1$
- }
+ public ConcurrentMethodRunner(int concurrentCount) {
+ _concurrentCount = concurrentCount;
+ }
- private class TestMethodThread extends Thread {
- ConcurrentMethodRunner _runner;
+ protected void runImpl() throws Throwable {
+ startThreads();
+ joinThreads();
+ if (_concurrentException.size() > 0) {
+ throw _concurrentException;
+ }
+ }
- public TestMethodThread(ConcurrentMethodRunner runner, String threadName) {
- super(threadName);
- _runner = runner;
- }
+ private void startThreads() {
+ int count = _convention.getConcurrentCount(_testInstance, _method);
+ int threadCount = count > 0 ? count : _concurrentCount;
+ _threads = new TestMethodThread[threadCount];
+ for (int i = 0; i < _threads.length; ++i) {
+ _threads[i] = new TestMethodThread(this,
+ "punit concurrent method runner"); //$NON-NLS-1$
+ }
+ for (int i = 0; i < _threads.length; ++i) {
+ _threads[i].start();
+ }
+ }
- public void run() {
- try {
- _runner.runMethod();
- } catch (Throwable t) {
- _concurrentException.add(t);
- }
- }
- }
+ private void joinThreads() {
+ for (int i = 0; i < _threads.length; ++i) {
+ try {
+ _threads[i].join();
+ } catch (InterruptedException e) {
+ _concurrentException.add(e);
+ }
+ }
+ }
+
+ protected String getCheckMethodName() {
+ return "check_" + _method.getName(); //$NON-NLS-1$
+ }
+
+ private class TestMethodThread extends Thread {
+ ConcurrentMethodRunner _runner;
+
+ public TestMethodThread(ConcurrentMethodRunner runner, String threadName) {
+ super(threadName);
+ _runner = runner;
+ }
+
+ public void run() {
+ try {
+ _runner.runMethod();
+ } catch (Throwable t) {
+ _concurrentException.add(t);
+ }
+ }
+ }
}
Modified: trunk/punit/src/org/punit/method/runner/MethodRunner.java
===================================================================
--- trunk/punit/src/org/punit/method/runner/MethodRunner.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit/src/org/punit/method/runner/MethodRunner.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -38,4 +38,6 @@
public Object clone();
public void setConvention(Convention convention);
+
+ public boolean shouldStop();
}
Modified: trunk/punit/src/org/punit/reporter/stream/StreamLogger.java
===================================================================
--- trunk/punit/src/org/punit/reporter/stream/StreamLogger.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit/src/org/punit/reporter/stream/StreamLogger.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -65,18 +65,18 @@
public void logResult() {
stopTimeWatcher();
StringBuffer sb = new StringBuffer();
- sb.append(Messages.getString("logger.02")); //$NON-NLS-1$
+ sb.append(Messages.getString("logger.total")); //$NON-NLS-1$
sb.append(_result.methodCount());
sb.append(", "); //$NON-NLS-1$
- sb.append(Messages.getString("logger.03")); //$NON-NLS-1$
+ sb.append(Messages.getString("logger.failures")); //$NON-NLS-1$
List failures = _result.failures();
int failuresCount = failures.size();
sb.append(failuresCount);
sb.append(" ("); //$NON-NLS-1$
if (failuresCount == 0) {
- sb.append(Messages.getString("logger.04")); //$NON-NLS-1$
+ sb.append(Messages.getString("logger.green")); //$NON-NLS-1$
} else {
- sb.append(Messages.getString("logger.05")); //$NON-NLS-1$
+ sb.append(Messages.getString("logger.red")); //$NON-NLS-1$
}
sb.append(") - "); //$NON-NLS-1$
sb.append(_timeWatcher.stringValue());
Modified: trunk/punit/src/org/punit/runner/AbstractRunner.java
===================================================================
--- trunk/punit/src/org/punit/runner/AbstractRunner.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit/src/org/punit/runner/AbstractRunner.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -251,7 +251,7 @@
}
private void runTestClassImpl(final Class clazz) {
- Object testInstance = ReflectionUtil.newInstance(clazz);
+ final Object testInstance = ReflectionUtil.newInstance(clazz);
Throwable storedException = null;
Collection testMethods = extractTestMethods(clazz);
@@ -261,7 +261,7 @@
TraverserUtil.traverse(testMethods.iterator(), new Traverser() {
public void traverse(Object obj) {
Method method = (Method) obj;
- runTestMethod(clazz, method);
+ runTestMethod(testInstance, method);
}
});
} catch (Throwable t) {
@@ -322,9 +322,8 @@
});
}
- private void runTestMethod(Class clazz, Method method) {
- Object testInstance = ReflectionUtil.newInstance(clazz);
- if (_convention.isParameterizedTest(clazz)) {
+ private void runTestMethod(Object testInstance, Method method) {
+ if (_convention.isParameterizedTest(testInstance.getClass())) {
Parameterized pInstance = (Parameterized) testInstance;
Parameter[] params = pInstance.parameters();
for (int i = 0; i < params.length; ++i) {
Modified: trunk/punit/src/org/punit/util/MemoryUtil.java
===================================================================
--- trunk/punit/src/org/punit/util/MemoryUtil.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit/src/org/punit/util/MemoryUtil.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -1,5 +1,7 @@
package org.punit.util;
+import org.punit.assertion.Assert;
+import org.punit.exception.OutOfMemoryException;
public class MemoryUtil {
@@ -8,18 +10,80 @@
private static long freeMemory() {
return _runtime.freeMemory();
}
-
+
public static long totalMemory() {
return _runtime.totalMemory();
}
-
+
public static long usedMemory() {
+ clear();
return totalMemory() - freeMemory();
}
public static void clear() {
- System.gc();
- System.runFinalization();
+ final int CLEAR_RETRIES = 3;
+
+ long free;
+
+ // continue while amount of free memory grows
+ do {
+ free = freeMemory();
+ for (int i = 0; i < CLEAR_RETRIES; i++) {
+ System.gc();
+ System.runFinalization();
+ // give a finalization thread a chance to do it's job
+ Thread.yield();
+ }
+ } while (free < freeMemory());
+
}
+ /**
+ * Allocates objects till OutOfMemoryError is not thrown.
+ *
+ * @return total memory when virtual pages can be no longer committed
+ */
+ public static long maxTotalMemory() {
+ int size = Integer.MAX_VALUE;
+
+ Object[] list = new Object[1];
+
+ // decrease the chunk size
+ while (size > 0) {
+ try {
+ while (true) {
+ Object[] newElement = new Object[size];
+ newElement[0] = list;
+ list = newElement;
+ }
+ } catch (OutOfMemoryError oome) {
+ }
+ size = size / 2;
+ }
+ return totalMemory();
+ }
+
+ private static final int WILDERNESS_SIZE = 4096;
+
+ private static byte[] _wilderness;
+
+ static {
+ allocateWilderness();
+ }
+
+ public static void allocateWilderness() {
+ Assert.assertNull(_wilderness);
+ try {
+ _wilderness = new byte[WILDERNESS_SIZE];
+ } catch (OutOfMemoryError oome) {
+ System.out.println("failed to allocate wilderness");
+ Thread.dumpStack();
+ throw new OutOfMemoryException(oome);
+ }
+ }
+
+ public static void releaseWilderness() {
+ Assert.assertNotNull(_wilderness);
+ _wilderness = null;
+ }
}
Modified: trunk/punit/src/org/punit/util/ReflectionUtil.java
===================================================================
--- trunk/punit/src/org/punit/util/ReflectionUtil.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit/src/org/punit/util/ReflectionUtil.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -62,6 +62,7 @@
field.setInt(testInstance, intVal);
} else if (fieldClass.equals(long.class)) {
long longVal = Long.parseLong(value);
+
field.setLong(testInstance, longVal);
} else {
field.set(testInstance, value);
@@ -117,6 +118,5 @@
private static boolean isBitSet(int value, int expectedBit) {
return (value & expectedBit) != 0;
-
}
}
Modified: trunk/punit/src/org/punit/util/TraverserUtil.java
===================================================================
--- trunk/punit/src/org/punit/util/TraverserUtil.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit/src/org/punit/util/TraverserUtil.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -7,5 +7,6 @@
while (iter.hasNext()) {
traverser.traverse(iter.next());
}
+
}
}
Modified: trunk/punit/src/org/punit/watcher/AbstractWatcher.java
===================================================================
--- trunk/punit/src/org/punit/watcher/AbstractWatcher.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit/src/org/punit/watcher/AbstractWatcher.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -22,7 +22,7 @@
public String stringValue() {
if (_scale == 1) {
- return _value + " " + unit(); //$NON-NLS-1$
+ return _value + unit(); //$NON-NLS-1$
} else {
return value() + unit();
}
Modified: trunk/punit/src/org/punit/watcher/CustomWatcher.java
===================================================================
--- trunk/punit/src/org/punit/watcher/CustomWatcher.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit/src/org/punit/watcher/CustomWatcher.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -48,14 +48,12 @@
public static synchronized void setResult(final String name,
final long value, final String unit, final long scale) {
Iterator iter = _methodRunner.watchers().iterator();
-// System.out.println(name + " " + value + " " + unit + " " + scale);
while (iter.hasNext()) {
try {
CustomWatcher watcher = (CustomWatcher) iter.next();
if (watcher.punitName().equals(name)) {
- watcher.setValue(value);
+ watcher.setValue(value, scale);
watcher._unit = unit;
- watcher._scale = scale;
}
} catch (ClassCastException cce) {
}
@@ -73,18 +71,23 @@
try {
CustomWatcher watcher = (CustomWatcher) iter.next();
if (watcher.punitName().equals(name)) {
- watcher.setValue(value);
+ watcher.setValue(value, watcher._scale);
}
} catch (ClassCastException cce) {
}
}
}
+
+ public static boolean shouldStop() {
+ return _methodRunner.shouldStop();
+ }
/**
* Updates the value for this runner.
*/
- void setValue(final long value) {
+ public void setValue(final long value, final long scale) {
_value = value;
+ _scale = scale;
}
public String punitName() {
Modified: trunk/punit/src/org/punit/watcher/MaximumWatcher.java
===================================================================
--- trunk/punit/src/org/punit/watcher/MaximumWatcher.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit/src/org/punit/watcher/MaximumWatcher.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -26,9 +26,12 @@
/**
* Updates the value for this runner.
*/
- void setValue(final long value) {
- if (value > _value) {
+ public void setValue(final long value, final long scale) {
+ long dv = value - _value;
+ long ds = scale - _scale;
+ if (dv * _scale > _value * ds) {
_value = value;
+ _scale = scale;
}
}
}
Modified: trunk/punit/src/org/punit/watcher/MinimumWatcher.java
===================================================================
--- trunk/punit/src/org/punit/watcher/MinimumWatcher.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit/src/org/punit/watcher/MinimumWatcher.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -26,9 +26,12 @@
/**
* Updates the value for this runner.
*/
- void setValue(final long value) {
- if (value < _value) {
+ public void setValue(final long value, final long scale) {
+ long dv = value - _value;
+ long ds = scale - _scale;
+ if (dv * _scale < _value * ds) {
_value = value;
+ _scale = scale;
}
}
}
Modified: trunk/punit/src/punit.properties
===================================================================
--- trunk/punit/src/punit.properties 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit/src/punit.properties 2007-08-19 22:45:41 UTC (rev 232)
@@ -1,23 +1,23 @@
-builder.01=\ should implement
-logger.01=Started running
-logger.02=total:
-logger.03=failures:
-logger.04=GREEN
-logger.05=RED
-logger.06=TestSuite:
-runner.01=solo
-runner.02=solo.param
-runner.03=concurrent
-runner.04=concurrent.param
-runner.arguments=\ arguments
-watcher.bytes=bytes
-watcher.ms=ms
-watcher.memory=Memory
-watcher.time=Time
-reporter.01=result
-reporter.02=overview
-PDFRender.01=PUnit result\n
-PDFRender.02=the open source performance benchmark framework\n
-Assert.0=Excepted
-Assert.1=, but
-Assert.2=expected not the same:
+builder.01=\ should implement
+logger.01=Started running
+logger.total=total:
+logger.failures=failures:
+logger.green=GREEN
+logger.red=RED
+logger.06=TestSuite:
+runner.01=solo
+runner.02=solo.param
+runner.03=concurrent
+runner.04=concurrent.param
+runner.arguments=\ arguments
+watcher.bytes=bytes
+watcher.ms=ms
+watcher.memory=Memory
+watcher.time=Time
+reporter.01=result
+reporter.02=overview
+PDFRender.01=PUnit result\n
+PDFRender.02=the open source performance benchmark framework\n
+assert.expected=Expected
+assert.got=, but got
+assert.notsame=Expected a different object:
Modified: trunk/punit.extension/src/org/punit/listener/FieldSetter.java
===================================================================
--- trunk/punit.extension/src/org/punit/listener/FieldSetter.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit.extension/src/org/punit/listener/FieldSetter.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -18,7 +18,7 @@
public class FieldSetter implements EventListener {
private static final long serialVersionUID = 5194809928409791777L;
-
+
private final Properties _properties;
public FieldSetter(Properties properties) {
@@ -92,11 +92,14 @@
*/
private void setClassFields(Object testInstance, Properties properties) {
for (Field field : testInstance.getClass().getFields()) {
- String fieldName = PACKAGE_SEPARATOR + field.getName();
+ String fieldName = PACKAGE_SEPARATOR
+ + field.getDeclaringClass().getName() + PACKAGE_SEPARATOR
+ + field.getName();
for (Object key : properties.keySet()) {
final String keyName = key.toString();
if (fieldName.indexOf(PACKAGE_SEPARATOR + keyName) >= 0) {
- ReflectionUtil.setField(testInstance, field, properties.getProperty(keyName));
+ ReflectionUtil.setField(testInstance, field, properties
+ .getProperty(keyName));
}
}
}
Modified: trunk/punit.test/src/tests/api/org/punit/testclasses/SetResultTestClass.java
===================================================================
--- trunk/punit.test/src/tests/api/org/punit/testclasses/SetResultTestClass.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit.test/src/tests/api/org/punit/testclasses/SetResultTestClass.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -15,7 +15,7 @@
public static final String SUPPORTED_THREADS = "Supported Number of Threads"; //$NON-NLS-1$
- public static final String THREAD_UNITS = "threads"; //$NON-NLS-1$
+ public static final String THREAD_UNITS = " threads"; //$NON-NLS-1$
private boolean _stop;
Modified: trunk/punit.test/src/tests/api/org/punit/util/IOUtilTest.java
===================================================================
--- trunk/punit.test/src/tests/api/org/punit/util/IOUtilTest.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit.test/src/tests/api/org/punit/util/IOUtilTest.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -14,45 +14,47 @@
public class IOUtilTest extends TestCase {
- public void testSerialize1() throws java.io.IOException {
- File file = File.createTempFile("test", "ser"); //$NON-NLS-1$//$NON-NLS-2$
- file.deleteOnExit();
- IOUtil.serialize(new SerializableClass1(), file.getCanonicalPath());
- }
-
- public void testSerialize2() throws java.io.IOException {
- final File file = File.createTempFile("test", "ser"); //$NON-NLS-1$//$NON-NLS-2$
- file.deleteOnExit();
- AssertUtil.assertException(IOException.class, new CodeRunner() {
- public void run() throws Throwable {
- IOUtil.serialize(new NonSerializableClass1(), file.getCanonicalPath());
- }
- });
- }
-
- public void testgetSerializableObject() throws java.io.IOException {
- File file = File.createTempFile("test", "ser"); //$NON-NLS-1$//$NON-NLS-2$
- file.deleteOnExit();
- String fileName = file.getCanonicalPath();
- SerializableClass1 obj1 = new SerializableClass1();
- IOUtil.serialize(obj1, fileName);
- SerializableClass1 obj2 = (SerializableClass1) IOUtil.getSerialiableObject(fileName);
- assertEquals(3, obj2.i);
- }
+ public void testSerialize1() throws java.io.IOException {
+ File file = File.createTempFile("test", "ser"); //$NON-NLS-2$
+ file.deleteOnExit();
+ IOUtil.serialize(new SerializableClass1(), file.getCanonicalPath());
+ }
- public void testDeleteFile() throws Exception {
- String fileName = "hello.txt"; //$NON-NLS-1$
- new FileOutputStream(fileName).close();
- IOUtil.deleteFile(fileName);
- assertFalse(new File(fileName).exists());
- }
-
- private static class SerializableClass1 implements Serializable {
- private static final long serialVersionUID = -5238909303048584560L;
- public int i = 3;
- }
-
- private static class NonSerializableClass1 {
- public int i = 3;
- }
+ public void testSerialize2() throws java.io.IOException {
+ final File file = File.createTempFile("test", "ser"); //$NON-NLS-2$
+ file.deleteOnExit();
+ AssertUtil.assertException(IOException.class, new CodeRunner() {
+ public void run() throws Throwable {
+ IOUtil.serialize(new NonSerializableClass1(), file
+ .getCanonicalPath());
+ }
+ });
+ }
+
+ public void testgetSerializableObject() throws java.io.IOException {
+ File file = File.createTempFile("test", "ser"); //$NON-NLS-2$
+ file.deleteOnExit();
+ String fileName = file.getCanonicalPath();
+ SerializableClass1 obj1 = new SerializableClass1();
+ IOUtil.serialize(obj1, fileName);
+ SerializableClass1 obj2 = (SerializableClass1) IOUtil
+ .getSerialiableObject(fileName);
+ assertEquals(3, obj2.i);
+ }
+
+ public void testDeleteFile() throws Exception {
+ String fileName = "hello.txt"; //$NON-NLS-1$
+ new FileOutputStream(fileName).close();
+ IOUtil.deleteFile(fileName);
+ assertFalse(new File(fileName).exists());
+ }
+
+ private static class SerializableClass1 implements Serializable {
+ private static final long serialVersionUID = -5238909303048584560L;
+ public int i = 3;
+ }
+
+ private static class NonSerializableClass1 {
+ public int i = 3;
+ }
}
Added: trunk/punit.test/src/tests/api/org/punit/util/MemoryUtilTest.java
===================================================================
--- trunk/punit.test/src/tests/api/org/punit/util/MemoryUtilTest.java (rev 0)
+++ trunk/punit.test/src/tests/api/org/punit/util/MemoryUtilTest.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -0,0 +1,27 @@
+package tests.api.org.punit.util;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.punit.util.MemoryUtil;
+
+public class MemoryUtilTest extends TestCase {
+
+ private static final float TOLERANCE = 0.1f;
+
+ public void testUsedMemory() {
+ long used = MemoryUtil.usedMemory();
+ Assert.assertTrue(used <= MemoryUtil.totalMemory());
+ Assert.assertTrue(used <= MemoryUtil.maxTotalMemory());
+
+ int size = Math.round((1 - TOLERANCE)
+ * (MemoryUtil.totalMemory() - used));
+ Object a = new byte[size];
+ Assert.assertEquals(1, ((float) MemoryUtil.usedMemory() - used) / size, TOLERANCE);
+ }
+
+ public void testTotalMemory() {
+ Assert.assertTrue(MemoryUtil.totalMemory() <= MemoryUtil
+ .maxTotalMemory());
+ }
+}
Property changes on: trunk/punit.test/src/tests/api/org/punit/util/MemoryUtilTest.java
___________________________________________________________________
Name: svn:executable
+ *
Modified: trunk/punit.test/src/tests/api/org/punit/watcher/CustomWatcherTest.java
===================================================================
--- trunk/punit.test/src/tests/api/org/punit/watcher/CustomWatcherTest.java 2007-08-12 14:03:50 UTC (rev 231)
+++ trunk/punit.test/src/tests/api/org/punit/watcher/CustomWatcherTest.java 2007-08-19 22:45:41 UTC (rev 232)
@@ -18,7 +18,7 @@
private final String HEAP_UTILIZATION = "Heap Utilization"; //$NON-NLS-1$
- private final double HEAP_WASTE_LIMIT = 1;
+ private final float HEAP_WASTE_TOLERANCE = 10;
private int CAPACITY = 128;
@@ -27,8 +27,8 @@
protected void setUp() throws Exception {
MethodRunner methodRunner = new SoloMethodRunner();
- _minWatcher = new MinimumWatcher(methodRunner, HEAP_WASTE, "%"); //$NON-NLS-1$
- _maxWatcher = new MaximumWatcher(methodRunner, HEAP_UTILIZATION, "%"); //$NON-NLS-1$
+ _minWatcher = new MinimumWatcher(methodRunner, HEAP_UTILIZATION, "%"); //$NON-NLS-1$
+ _maxWatcher = new MaximumWatcher(methodRunner, HEAP_WASTE, "%"); //$NON-NLS-1$
}
public void test() throws Exception {
@@ -38,26 +38,15 @@
_minWatcher.stop();
_maxWatcher.stop();
- assertEquals(0.0, _minWatcher.value(), HEAP_WASTE_LIMIT);
- assertEquals(100.0, _maxWatcher.value(), HEAP_WASTE_LIMIT);
+ assertEquals(100, _minWatcher.value(), HEAP_WASTE_TOLERANCE);
+ assertEquals(0, _maxWatcher.value(), HEAP_WASTE_TOLERANCE);
}
void allocateAll() {
- int size = 1;
+ int size = Integer.MAX_VALUE;
List<byte[]> list = new ArrayList<byte[]>(CAPACITY);
- MemoryUtil.clear();
long allocated = MemoryUtil.usedMemory();
- // increase the chunk size
- try {
- while (true) {
- list.add(new byte[size]);
- allocated += size;
- size += size;
- }
- } catch (OutOfMemoryError oome) {
- }
-
// decrease the chunk size
while (size > 0) {
try {
@@ -67,7 +56,7 @@
}
} catch (OutOfMemoryError oome) {
try {
- setWatcherValue(allocated);
+ setWatcherValue(allocated + size);
} catch (OutOfMemoryError oomex) {
return;
}
@@ -78,15 +67,17 @@
private void setWatcherValue(long allocated) {
long total = MemoryUtil.totalMemory();
- CustomWatcher.setResult(HEAP_WASTE, (total - allocated) * 100, "%", //$NON-NLS-1$
+ CustomWatcher.setResult(HEAP_WASTE, (total - allocated) * 100, "%",
total);
- CustomWatcher.setResult(HEAP_UTILIZATION, allocated * 100, "%", total); //$NON-NLS-1$
+ CustomWatcher.setResult(HEAP_UTILIZATION, allocated * 100, "%", total);
}
public void testSerializable() throws Exception {
- MinimumWatcher minWatcher = (MinimumWatcher) TestUtil.getSerialiableObject(_minWatcher);
+ MinimumWatcher minWatcher = (MinimumWatcher) TestUtil
+ .getSerialiableObject(_minWatcher);
assertNotNull(minWatcher);
- MaximumWatcher maxWatcher = (MaximumWatcher) TestUtil.getSerialiableObject(_maxWatcher);
+ MaximumWatcher maxWatcher = (MaximumWatcher) TestUtil
+ .getSerialiableObject(_maxWatcher);
assertNotNull(maxWatcher);
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|