01: package org.drools.rule;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.IOException;
05: import java.io.InputStream;
06: import java.security.CodeSource;
07:
08: import junit.framework.TestCase;
09:
10: import org.drools.WorkingMemory;
11: import org.drools.spi.EvalExpression;
12: import org.drools.spi.Tuple;
13:
14: public class PackageCompilationDataTest extends TestCase {
15: public static class TestEvalExpression implements EvalExpression {
16: public boolean evaluate(Tuple t, Declaration[] d,
17: WorkingMemory w) {
18: return false;
19: }
20: }
21:
22: public void testCodeSourceUrl() throws IOException {
23: final String className = TestEvalExpression.class.getName();
24: final PackageCompilationData pcData = new PackageCompilationData(
25: getClass().getClassLoader());
26: final EvalCondition invoker = new EvalCondition(null);
27: pcData.putInvoker(className, invoker);
28: final InputStream is = getClass().getClassLoader()
29: .getResourceAsStream(
30: className.replace('.', '/') + ".class");
31: try {
32: pcData.write(className.replace('.', '/') + ".class",
33: read(is));
34: } finally {
35: is.close();
36: }
37: final CodeSource codeSource = invoker.getEvalExpression()
38: .getClass().getProtectionDomain().getCodeSource();
39: assertNotNull(codeSource.getLocation());
40: }
41:
42: private static byte[] read(final InputStream is) throws IOException {
43: final ByteArrayOutputStream os = new ByteArrayOutputStream();
44: final byte[] b = new byte[1024];
45: int len;
46: while ((len = is.read(b)) > 0) {
47: os.write(b, 0, len);
48: }
49: return os.toByteArray();
50: }
51: }
|