commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sandy...@apache.org
Subject svn commit: r389394 - in /jakarta/commons/proper/pool/trunk/src: java/org/apache/commons/pool/composite/ test/org/apache/commons/pool/composite/
Date Tue, 28 Mar 2006 05:04:44 GMT
Author: sandymac
Date: Mon Mar 27 21:04:36 2006
New Revision: 389394

URL: http://svn.apache.org/viewcvs?rev=389394&view=rev
Log:
Unit tests for the composite Tracker implementations.

Added:
    jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestDebugTracker.java
  (with props)
    jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestNullTracker.java
  (with props)
    jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestReferenceTracker.java
  (with props)
    jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestSimpleTracker.java
  (with props)
    jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestTracker.java
  (with props)
Modified:
    jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/ReferenceTracker.java
    jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/Tracker.java
    jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestAll.java
    jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestLender.java

Modified: jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/ReferenceTracker.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/ReferenceTracker.java?rev=389394&r1=389393&r2=389394&view=diff
==============================================================================
--- jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/ReferenceTracker.java
(original)
+++ jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/ReferenceTracker.java
Mon Mar 27 21:04:36 2006
@@ -53,6 +53,9 @@
     private transient int lost = 0;
 
     public void borrowed(final Object obj) {
+        if (obj == null) {
+            throw new IllegalArgumentException("Cannot track null borrowed from pool.");
+        }
         workQueue();
         final IdentityReference ref;
         synchronized (rq) {
@@ -79,6 +82,9 @@
      * @throws IllegalStateException when an object that wasn't brorrowed from this pool
is returned.
      */
     public void returned(final Object obj) throws IllegalStateException {
+        if (obj == null) {
+            throw new IllegalArgumentException("Cannot track null returned to the pool.");
+        }
         workQueue();
         final IdentityKey key = new IdentityKey(obj);
         final IdentityReference ref = (IdentityReference)map.remove(key);

Modified: jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/Tracker.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/Tracker.java?rev=389394&r1=389393&r2=389394&view=diff
==============================================================================
--- jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/Tracker.java
(original)
+++ jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/composite/Tracker.java
Mon Mar 27 21:04:36 2006
@@ -40,20 +40,20 @@
      * {@link PoolableObjectFactory#activateObject(Object) activated} and
      * {@link PoolableObjectFactory#validateObject(Object) validated} and is about to be
returned to the client.
      *
-     * <p>Objects created via {@link ObjectPool#addObject()} are also "borrowed" so
that when
-     * {@link ObjectPool#returnObject(Object)} {@link #returned(Object) returns} that object
it isn't unexpected.
-     *
      * @param obj was borrowed from the pool.
+     * @throws IllegalArgumentException may be thrown if <code>null</code> is
not allowed.
      */
-    public void borrowed(Object obj);
+    public void borrowed(Object obj) throws IllegalArgumentException;
 
     /**
      * An object is being {@link ObjectPool#returnObject(Object) returned} to the pool.
      * {@link ObjectPool#invalidateObject(Object) Invalid} objects are also "returned" via
this method.
      *
      * @param obj being returned to the pool.
+     * @throws IllegalArgumentException may be thrown if <code>null</code> is
not allowed.
+     * @throws IllegalStateException may be thrown if an object is returned that wasn't borrowed.
      */
-    public void returned(Object obj);
+    public void returned(Object obj) throws IllegalArgumentException, IllegalStateException;
 
     /**
      * The number of "borrowed" or active objects from the pool.

Modified: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestAll.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestAll.java?rev=389394&r1=389393&r2=389394&view=diff
==============================================================================
--- jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestAll.java
(original)
+++ jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestAll.java
Mon Mar 27 21:04:36 2006
@@ -38,6 +38,7 @@
 
     public static Test suite() {
         final TestSuite suite = new TestSuite();
+
         // Lenders unit tests
         suite.addTest(TestFifoLender.suite());
         suite.addTest(TestIdleEvictorLender.suite());
@@ -45,8 +46,14 @@
         suite.addTest(TestLifoLender.suite());
         suite.addTest(TestNullLender.suite());
         suite.addTest(TestSoftLender.suite());
+
         // Managers unit tests
+
         // Tackers unit tests
+        suite.addTest(TestNullTracker.suite());
+        suite.addTest(TestReferenceTracker.suite());
+        suite.addTest(TestDebugTracker.suite());
+        suite.addTest(TestSimpleTracker.suite());
 
         // Remaining unit tests
         suite.addTest(TestCompositeObjectPool.suite());

Added: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestDebugTracker.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestDebugTracker.java?rev=389394&view=auto
==============================================================================
--- jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestDebugTracker.java
(added)
+++ jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestDebugTracker.java
Mon Mar 27 21:04:36 2006
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.pool.composite;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * Unit tests for {@link DebugTracker}.
+ * Note: This test will generate some output to stderr.
+ *
+ * @author Sandy McArthur
+ * @version $Revision$ $Date$
+ */
+public class TestDebugTracker extends TestReferenceTracker {
+    /**
+     * Constructs a test case with the given name.
+     */
+    public TestDebugTracker(final String name) {
+        super(name);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestDebugTracker.class);
+    }
+
+    protected Tracker createTracker() {
+        return new DebugTracker();
+    }
+}

Propchange: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestDebugTracker.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestDebugTracker.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestLender.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestLender.java?rev=389394&r1=389393&r2=389394&view=diff
==============================================================================
--- jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestLender.java
(original)
+++ jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestLender.java
Mon Mar 27 21:04:36 2006
@@ -17,8 +17,6 @@
 package org.apache.commons.pool.composite;
 
 import junit.framework.TestCase;
-import junit.framework.Test;
-import junit.framework.TestSuite;
 import org.apache.commons.pool.BasePoolableObjectFactory;
 import org.apache.commons.pool.PoolableObjectFactory;
 

Added: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestNullTracker.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestNullTracker.java?rev=389394&view=auto
==============================================================================
--- jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestNullTracker.java
(added)
+++ jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestNullTracker.java
Mon Mar 27 21:04:36 2006
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.pool.composite;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * Tests for {@link NullTracker}.
+ *
+ * @author Sandy McArthur
+ * @version $Revision$ $Date$
+ */
+public class TestNullTracker extends TestTracker {
+    /**
+     * Constructs a test case with the given name.
+     */
+    public TestNullTracker(final String name) {
+        super(name);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestNullTracker.class);
+    }
+
+    protected Tracker createTracker() {
+        return new NullTracker();
+    }
+}

Propchange: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestNullTracker.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestNullTracker.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestReferenceTracker.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestReferenceTracker.java?rev=389394&view=auto
==============================================================================
--- jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestReferenceTracker.java
(added)
+++ jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestReferenceTracker.java
Mon Mar 27 21:04:36 2006
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.pool.composite;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import java.util.List;
+import java.util.LinkedList;
+
+/**
+ * Unit tests for {@link ReferenceTracker}s.
+ *
+ * @author Sandy McArthur
+ * @version $Revision$ $Date$
+ */
+public class TestReferenceTracker extends TestTracker {
+    /**
+     * Constructs a test case with the given name.
+     */
+    public TestReferenceTracker(final String name) {
+        super(name);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestReferenceTracker.class);
+    }
+
+    protected Tracker createTracker() {
+        return new ReferenceTracker();
+    }
+
+    public void testBorrowed() {
+        super.testBorrowed();
+
+        final Tracker tracker = createTracker();
+        try {
+            tracker.borrowed(null);
+            fail("Tracking of null is now allowed.");
+        } catch (IllegalArgumentException iae) {
+            // expected
+        }
+    }
+
+    public void testLostBorrowed() {
+        final Tracker tracker = createTracker();
+
+        Object obj = new Object();
+        tracker.borrowed(obj);
+        assertEquals(1, tracker.getBorrowed());
+        obj = null;
+
+        final List garbage = new LinkedList();
+        final Runtime runtime = Runtime.getRuntime();
+        while (tracker.getBorrowed() > 0) {
+            try {
+                garbage.add(new byte[Math.min(1024 * 1024, (int)runtime.freeMemory()/2)]);
+            } catch (OutOfMemoryError oome) {
+                System.gc();
+            }
+            System.gc();
+        }
+        garbage.clear();
+        System.gc();
+
+        assertEquals(0, tracker.getBorrowed());
+    }
+
+    public void testReturned() {
+        super.testReturned();
+
+        final Tracker tracker = createTracker();
+        try {
+            tracker.returned(null);
+            fail("Tracking of null is now allowed.");
+        } catch (IllegalArgumentException iae) {
+            // expected
+        }
+
+        try {
+            tracker.returned(new Object());
+            fail("Cannot return an object that wasn't borrowed.");
+        } catch (IllegalStateException ise) {
+            // expected
+        }
+
+
+    }
+}

Propchange: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestReferenceTracker.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestReferenceTracker.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestSimpleTracker.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestSimpleTracker.java?rev=389394&view=auto
==============================================================================
--- jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestSimpleTracker.java
(added)
+++ jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestSimpleTracker.java
Mon Mar 27 21:04:36 2006
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.pool.composite;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * Unit tests for {@link SimpleTracker}.
+ *
+ * @author Sandy McArthur
+ * @version $Revision$ $Date$
+ */
+public class TestSimpleTracker extends TestTracker {
+    /**
+     * Constructs a test case with the given name.
+     */
+    public TestSimpleTracker(final String name) {
+        super(name);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestSimpleTracker.class);
+    }
+
+    protected Tracker createTracker() {
+        return new SimpleTracker();
+    }
+
+    public void testReturned() {
+        super.testReturned();
+
+        final Tracker tracker = createTracker();
+        try {
+            tracker.returned(new Object()); // should drive counter negative
+            fail("Cannot return more objects than were borrowed.");
+        } catch (IllegalStateException ise) {
+            // expected
+        }
+    }
+}

Propchange: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestSimpleTracker.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestSimpleTracker.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestTracker.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestTracker.java?rev=389394&view=auto
==============================================================================
--- jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestTracker.java
(added)
+++ jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestTracker.java
Mon Mar 27 21:04:36 2006
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.pool.composite;
+
+import junit.framework.TestCase;
+
+/**
+ * Common unit tests for all {@link Tracker}s.
+ *
+ * @author Sandy McArthur
+ * @version $Revision$ $Date$
+ */
+public abstract class TestTracker extends TestCase {
+    /**
+     * Constructs a test case with the given name.
+     */
+    public TestTracker(final String name) {
+        super(name);
+    }
+
+    protected abstract Tracker createTracker();
+
+    public void testBorrowed() {
+        final Tracker tracker = createTracker();
+
+        tracker.borrowed(new Object());
+    }
+
+    public void testReturned() {
+        final Tracker tracker = createTracker();
+
+        final Object o = new Object();
+        tracker.borrowed(o);
+        tracker.returned(o);
+    }
+
+    public void testGetBorrowed() {
+        final Tracker tracker = createTracker();
+        if (tracker.getBorrowed() >= 0) {
+            final Object obj = new Object();
+            assertEquals(0, tracker.getBorrowed());
+            tracker.borrowed(obj);
+            assertEquals(1, tracker.getBorrowed());
+            tracker.returned(obj);
+            assertEquals(0, tracker.getBorrowed());
+        }
+    }
+
+    public void testToString() {
+        createTracker().toString();
+    }
+}

Propchange: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestTracker.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/composite/TestTracker.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message