001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.simple;
023:
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Properties;
029: import javax.management.MBeanServer;
030: import javax.management.MBeanServerFactory;
031: import javax.management.ObjectName;
032: import javax.naming.InitialContext;
033:
034: import junit.framework.Test;
035: import org.jboss.ejb.EntityContainer;
036: import org.jboss.ejb.plugins.cmp.ejbql.Catalog;
037: import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
038: import org.jboss.ejb.plugins.cmp.jdbc.ReadAheadCache;
039: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge;
040: import org.jboss.mx.server.ServerConstants;
041: import org.jboss.mx.server.registry.MBeanEntry;
042: import org.jboss.mx.server.registry.MBeanRegistry;
043: import org.jboss.mx.util.MBeanProxyExt;
044: import org.jboss.test.JBossTestCase;
045: import org.jboss.test.util.ejb.EJBTestCase;
046:
047: public class PageSizeUnitTestCase extends EJBTestCase {
048: private JDBCStoreManager jdbcStoreManager;
049: private List pkList;
050:
051: public static Test suite() throws Exception {
052: return JBossTestCase.getDeploySetup(PageSizeUnitTestCase.class,
053: "cmp2-simple.jar");
054: }
055:
056: public PageSizeUnitTestCase(String name) {
057: super (name);
058: }
059:
060: private SimpleHome getSimpleHome() {
061: try {
062: InitialContext jndiContext = new InitialContext();
063: return (SimpleHome) jndiContext
064: .lookup("cmp2/simple/Simple");
065: } catch (Exception e) {
066: fail("Exception in getSimpleHome: " + e.getMessage());
067: }
068: return null;
069: }
070:
071: public void testOnLoad() throws Exception {
072: SimpleHome simpleHome = getSimpleHome();
073: Iterator simpleIter = simpleHome.findAll().iterator();
074: Simple simple = (Simple) simpleIter.next();
075: Object pk = simple.getPrimaryKey();
076: ReadAheadCache cache = jdbcStoreManager.getReadAheadCache();
077: ReadAheadCache.EntityReadAheadInfo info = cache
078: .getEntityReadAheadInfo(pk);
079: assertEquals(pkList.subList(0, 4), info.getLoadKeys());
080:
081: for (int i = 0; i < 4; i++) {
082: Object o = pkList.get(i);
083: assertNull(cache.getPreloadDataMap(o, false));
084: }
085:
086: simple.getStringValue(); // test0
087:
088: assertNull(cache.getPreloadDataMap("test0", false));
089: for (int i = 1; i < 4; i++) {
090: Object o = pkList.get(i);
091: assertNotNull(cache.getPreloadDataMap(o, false));
092: }
093: assertNull(cache.getPreloadDataMap("test4", false));
094:
095: simple = (Simple) simpleIter.next(); // test1
096: simple.getStringValue();
097: assertNull(cache.getPreloadDataMap("test1", false));
098: simple = (Simple) simpleIter.next(); // test2
099: simple.getStringValue();
100: simple = (Simple) simpleIter.next(); // test3
101: simple.getStringValue();
102: for (int i = 0; i < 4; i++) {
103: Object o = pkList.get(i);
104: assertNull(cache.getPreloadDataMap(o, false));
105: }
106:
107: simple = (Simple) simpleIter.next(); // test4
108: simple.getStringValue();
109: for (int i = 5; i < 8; i++) {
110: Object o = pkList.get(i);
111: assertNotNull(cache.getPreloadDataMap(o, false));
112: }
113: }
114:
115: public void setUpEJB(Properties props) throws Exception {
116: super .setUpEJB(props);
117: SimpleHome simpleHome = getSimpleHome();
118: pkList = new ArrayList();
119:
120: for (int i = 0; i < 10; i++) {
121: Simple simple = simpleHome.create("test" + i);
122: pkList.add(simple.getPrimaryKey());
123: simple.setIntegerPrimitive(i);
124: }
125:
126: MBeanServer server = (MBeanServer) MBeanServerFactory
127: .findMBeanServer(null).get(0);
128: ObjectName name = new ObjectName(
129: "jboss.j2ee:jndiName=cmp2/simple/Simple,service=EJB");
130: MBeanRegistry registry = (MBeanRegistry) MBeanProxyExt.create(
131: MBeanRegistry.class, ServerConstants.MBEAN_REGISTRY,
132: server);
133: MBeanEntry entry = registry.get(name);
134: EntityContainer container = (EntityContainer) entry
135: .getResourceInstance();
136: Catalog catalog = (Catalog) container.getEjbModule()
137: .getModuleData("CATALOG");
138: JDBCEntityBridge bridge = (JDBCEntityBridge) catalog
139: .getEntityByEJBName("SimpleEJB");
140: jdbcStoreManager = (JDBCStoreManager) bridge.getManager();
141: }
142:
143: public void tearDownEJB(Properties props) throws Exception {
144: SimpleHome simpleHome = getSimpleHome();
145: Collection c = simpleHome.findAll();
146: for (Iterator iterator = c.iterator(); iterator.hasNext();) {
147: Simple simple = (Simple) iterator.next();
148: simple.remove();
149: }
150: super.tearDownEJB(props);
151: }
152: }
|