001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.ta.instrumentation.test.integration;
022:
023: import java.lang.reflect.*;
024: import java.net.*;
025: import java.util.*;
026:
027: import com.db4o.config.*;
028: import com.db4o.db4ounit.common.ta.collections.*;
029: import com.db4o.instrumentation.core.*;
030: import com.db4o.instrumentation.filter.*;
031: import com.db4o.instrumentation.main.*;
032: import com.db4o.reflect.jdk.*;
033: import com.db4o.ta.*;
034: import com.db4o.ta.instrumentation.*;
035:
036: import db4ounit.*;
037: import db4ounit.extensions.*;
038:
039: public class TransparentActivationInstrumentationIntegrationTestCase
040: extends AbstractDb4oTestCase {
041:
042: private static final int PRIORITY = 42;
043: private ClassLoader _classLoader;
044:
045: protected void configure(Configuration config) {
046: ClassLoader baseLoader = TransparentActivationInstrumentationIntegrationTestCase.class
047: .getClassLoader();
048: URL[] urls = {};
049: ClassFilter filter = new ByNameClassFilter(new String[] {
050: Project.class.getName(), UnitOfWork.class.getName(),
051: PrioritizedProject.class.getName() });
052: BloatClassEdit edit = new InjectTransparentActivationEdit(
053: filter);
054: _classLoader = new BloatInstrumentingClassLoader(urls,
055: baseLoader, filter, edit);
056: config.add(new PagedListSupport());
057: config.add(new TransparentActivationSupport());
058: config.reflectWith(new JdkReflector(_classLoader));
059: }
060:
061: protected void store() throws Exception {
062: Class unitOfWorkClass = _classLoader.loadClass(UnitOfWork.class
063: .getName());
064: Constructor unitOfWorkConstructor = unitOfWorkClass
065: .getConstructor(new Class[] { String.class, Date.class,
066: Date.class });
067: unitOfWorkConstructor.setAccessible(true);
068: Object unitOfWork = unitOfWorkConstructor
069: .newInstance(new Object[] { "ta kick-off",
070: new Date(1000), new Date(2000) });
071:
072: Class projectClass = _classLoader
073: .loadClass(PrioritizedProject.class.getName());
074: Constructor projectConstructor = projectClass
075: .getConstructor(new Class[] { String.class,
076: Integer.TYPE });
077: projectConstructor.setAccessible(true);
078: Object project = projectConstructor.newInstance(new Object[] {
079: "db4o", new Integer(PRIORITY) });
080:
081: Method logWorkDoneMethod = projectClass.getMethod(
082: "logWorkDone", new Class[] { unitOfWorkClass });
083: logWorkDoneMethod.setAccessible(true);
084: logWorkDoneMethod.invoke(project, new Object[] { unitOfWork });
085: store(project);
086: }
087:
088: public void test() throws Exception {
089: final Object project = retrieveOnlyInstance(Project.class);
090:
091: Method getPriorityMethod = project.getClass()
092: .getDeclaredMethod("getPriority", new Class[] {});
093: getPriorityMethod.setAccessible(true);
094: Integer priority = (Integer) getPriorityMethod.invoke(project,
095: new Object[] {});
096: Assert.areEqual(PRIORITY, priority.intValue());
097:
098: Method totalTimeSpentMethod = project.getClass().getMethod(
099: "totalTimeSpent", new Class[] {});
100: totalTimeSpentMethod.setAccessible(true);
101: Long totalTimeSpent = (Long) totalTimeSpentMethod.invoke(
102: project, new Object[] {});
103: Assert.areEqual(1000, totalTimeSpent.intValue());
104: }
105: }
|