Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D6DF71069A for ; Thu, 31 Oct 2013 11:00:47 +0000 (UTC) Received: (qmail 18904 invoked by uid 500); 31 Oct 2013 11:00:46 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 18847 invoked by uid 500); 31 Oct 2013 11:00:45 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 18840 invoked by uid 99); 31 Oct 2013 11:00:45 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 31 Oct 2013 11:00:45 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 31 Oct 2013 11:00:41 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id DFADD2388A56; Thu, 31 Oct 2013 11:00:19 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1537438 - in /commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy: BaseTestProxiedKeyedObjectPool.java TestProxiedKeyedObjectPoolWithCglibProxy.java TestProxiedKeyedObjectPoolWithJdkProxy.java Date: Thu, 31 Oct 2013 11:00:19 -0000 To: commits@commons.apache.org From: markt@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131031110019.DFADD2388A56@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: markt Date: Thu Oct 31 11:00:19 2013 New Revision: 1537438 URL: http://svn.apache.org/r1537438 Log: Add unit tests for KeyedObjectPool Added: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedKeyedObjectPool.java (with props) commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedKeyedObjectPoolWithCglibProxy.java (with props) commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedKeyedObjectPoolWithJdkProxy.java (with props) Added: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedKeyedObjectPool.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedKeyedObjectPool.java?rev=1537438&view=auto ============================================================================== --- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedKeyedObjectPool.java (added) +++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedKeyedObjectPool.java Thu Oct 31 11:00:19 2013 @@ -0,0 +1,163 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.pool2.proxy; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.apache.commons.pool2.BaseKeyedPooledObjectFactory; +import org.apache.commons.pool2.KeyedObjectPool; +import org.apache.commons.pool2.KeyedPooledObjectFactory; +import org.apache.commons.pool2.PooledObject; +import org.apache.commons.pool2.impl.DefaultPooledObject; +import org.apache.commons.pool2.impl.GenericKeyedObjectPool; +import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig; +import org.junit.Before; +import org.junit.Test; + + +public abstract class BaseTestProxiedKeyedObjectPool { + + private static final String KEY1 = "key1"; + private static final String DATA1 = "data1"; + + private KeyedObjectPool pool = null; + + @Before + public void setUp() { + GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig(); + config.setMaxTotal(3); + + KeyedPooledObjectFactory factory = + new TestKeyedObjectFactory(); + + KeyedObjectPool innerPool = + new GenericKeyedObjectPool( + factory, config); + + pool = new ProxiedKeyedObjectPool(innerPool, getproxySource()); + } + + + protected abstract ProxySource getproxySource(); + + @Test + public void testBorrowObject() throws Exception { + TestObject obj = pool.borrowObject(KEY1); + assertNotNull(obj); + + // Make sure proxied methods are working + obj.setData(DATA1); + assertEquals(DATA1, obj.getData()); + + pool.returnObject(KEY1, obj); + } + + + @Test(expected=IllegalStateException.class) + public void testAccessAfterReturn() throws Exception { + TestObject obj = pool.borrowObject(KEY1); + assertNotNull(obj); + + // Make sure proxied methods are working + obj.setData(DATA1); + assertEquals(DATA1, obj.getData()); + + pool.returnObject(KEY1, obj); + + assertNotNull(obj); + + obj.getData(); + } + + + @Test(expected=IllegalStateException.class) + public void testAccessAfterInvalidate() throws Exception { + TestObject obj = pool.borrowObject(KEY1); + assertNotNull(obj); + + // Make sure proxied methods are working + obj.setData(DATA1); + assertEquals(DATA1, obj.getData()); + + pool.invalidateObject(KEY1, obj); + + assertNotNull(obj); + + obj.getData(); + } + + + @Test + public void testPassThroughMethods01() throws Exception { + assertEquals(0, pool.getNumActive()); + assertEquals(0, pool.getNumIdle()); + + pool.addObject(KEY1); + + assertEquals(0, pool.getNumActive()); + assertEquals(1, pool.getNumIdle()); + + pool.clear(); + + assertEquals(0, pool.getNumActive()); + assertEquals(0, pool.getNumIdle()); + } + + + @Test(expected=IllegalStateException.class) + public void testPassThroughMethods02() throws Exception { + pool.close(); + pool.addObject(KEY1); + } + + private static class TestKeyedObjectFactory extends + BaseKeyedPooledObjectFactory { + + @Override + public TestObject create(String key) throws Exception { + return new TestObjectImpl(); + } + @Override + public PooledObject wrap(TestObject value) { + return new DefaultPooledObject(value); + } + } + + + protected static interface TestObject { + String getData(); + void setData(String data); + } + + + private static class TestObjectImpl implements TestObject { + + private String data; + + @Override + public String getData() { + return data; + } + + @Override + public void setData(String data) { + this.data = data; + } + } + +} Propchange: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/BaseTestProxiedKeyedObjectPool.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedKeyedObjectPoolWithCglibProxy.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedKeyedObjectPoolWithCglibProxy.java?rev=1537438&view=auto ============================================================================== --- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedKeyedObjectPoolWithCglibProxy.java (added) +++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedKeyedObjectPoolWithCglibProxy.java Thu Oct 31 11:00:19 2013 @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.pool2.proxy; + +public class TestProxiedKeyedObjectPoolWithCglibProxy extends + BaseTestProxiedKeyedObjectPool { + + @Override + protected ProxySource getproxySource() { + return new CglibProxySource(TestObject.class); + } +} Propchange: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedKeyedObjectPoolWithCglibProxy.java ------------------------------------------------------------------------------ svn:eol-style = native Added: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedKeyedObjectPoolWithJdkProxy.java URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedKeyedObjectPoolWithJdkProxy.java?rev=1537438&view=auto ============================================================================== --- commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedKeyedObjectPoolWithJdkProxy.java (added) +++ commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedKeyedObjectPoolWithJdkProxy.java Thu Oct 31 11:00:19 2013 @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.pool2.proxy; + +public class TestProxiedKeyedObjectPoolWithJdkProxy + extends BaseTestProxiedKeyedObjectPool { + + @Override + protected ProxySource getproxySource() { + return new JdkProxySource(this.getClass().getClassLoader(), + new Class[] { TestObject.class }); + } +} Propchange: commons/proper/pool/trunk/src/test/java/org/apache/commons/pool2/proxy/TestProxiedKeyedObjectPoolWithJdkProxy.java ------------------------------------------------------------------------------ svn:eol-style = native