01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11: Suite 350, San Mateo, CA 94403, USA.
12:
13: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14: WARRANTY; without even the implied warranty of MERCHANTABILITY or
15: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16: for more details.
17:
18: You should have received a copy of the GNU General Public License along
19: with this program; if not, write to the Free Software Foundation, Inc.,
20: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21: package com.db4o.ta.instrumentation.test;
22:
23: import java.io.*;
24: import java.net.*;
25:
26: import com.db4o.foundation.io.*;
27: import com.db4o.instrumentation.core.*;
28: import com.db4o.instrumentation.filter.*;
29: import com.db4o.instrumentation.main.*;
30: import com.db4o.ta.*;
31: import com.db4o.ta.instrumentation.*;
32: import com.db4o.test.util.*;
33:
34: import db4ounit.*;
35:
36: public class TAFileEnhancerTestCase implements TestCase {
37:
38: private final static Class INSTRUMENTED_CLAZZ = ToBeInstrumentedWithFieldAccess.class;
39:
40: private final static Class NOT_INSTRUMENTED_CLAZZ = NotToBeInstrumented.class;
41:
42: public void test() throws Exception {
43: final String srcDir = Path4.combine(Path4.getTempPath(),
44: "tafileinstr/source");
45: File4.mkdirs(srcDir);
46: final String targetDir = Path4.combine(Path4.getTempPath(),
47: "tafileinstr/target");
48: File4.mkdirs(targetDir);
49:
50: final Class[] clazzes = { INSTRUMENTED_CLAZZ,
51: NOT_INSTRUMENTED_CLAZZ };
52:
53: for (int clazzIdx = 0; clazzIdx < clazzes.length; clazzIdx++) {
54: copyClassFile(srcDir, clazzes[clazzIdx]);
55: }
56:
57: ClassFilter filter = new ByNameClassFilter(
58: new String[] { INSTRUMENTED_CLAZZ.getName() });
59: Db4oFileEnhancer enhancer = new Db4oFileEnhancer(
60: new InjectTransparentActivationEdit(filter));
61: enhancer.enhance(srcDir, targetDir, new String[] {}, "");
62:
63: ExcludingClassLoader excludingLoader = new ExcludingClassLoader(
64: getClass().getClassLoader(), clazzes);
65: URLClassLoader loader = new URLClassLoader(
66: new URL[] { new File(targetDir).toURL() },
67: excludingLoader);
68:
69: Class instrumented = loader.loadClass(INSTRUMENTED_CLAZZ
70: .getName());
71: Assert.isTrue(Activatable.class.isAssignableFrom(instrumented));
72: Class uninstrumented = loader.loadClass(NOT_INSTRUMENTED_CLAZZ
73: .getName());
74: Assert.isFalse(Activatable.class
75: .isAssignableFrom(uninstrumented));
76: }
77:
78: private void copyClassFile(String srcDir, Class clazz)
79: throws IOException {
80: File file = fileForClass(clazz);
81: String targetPath = Path4.combine(srcDir, clazz.getName()
82: .replace('.', '/')
83: + ".class");
84: File4.delete(targetPath);
85: File4.copy(file.getCanonicalPath(), targetPath);
86: }
87:
88: private File fileForClass(Class clazz) throws IOException {
89: String clazzName = clazz.getName();
90: int dotIdx = clazzName.lastIndexOf('.');
91: String simpleName = clazzName.substring(dotIdx + 1);
92: URL url = clazz.getResource(simpleName + ".class");
93: return new File(url.getFile());
94: }
95: }
|