File: ClassCreatorTest.java

package info (click to toggle)
libcommons-jexl3-java 3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 1,976 kB
  • sloc: java: 22,447; xml: 2,499; makefile: 4; sh: 1
file content (237 lines) | stat: -rw-r--r-- 8,144 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * 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.jexl3;

import java.io.File;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * Basic check on automated class creation
 */
@SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
@org.junit.Ignore
public class ClassCreatorTest extends JexlTestCase {
    static final Log logger = LogFactory.getLog(JexlTestCase.class);
    static final int LOOPS = 8;
    private File base = null;
    private JexlEngine jexl = null;

    public ClassCreatorTest() {
        super("ClassCreatorTest");
    }

    @Before
    @Override
    public void setUp() throws Exception {
        base = new File(System.getProperty("java.io.tmpdir"), "jexl" + System.currentTimeMillis());
        jexl = JEXL;

    }

    @After
    @Override
    public void tearDown() throws Exception {
        deleteDirectory(base);
    }

    private void deleteDirectory(File dir) {
        if (dir.isDirectory()) {
            for (File file : dir.listFiles()) {
                if (file.isFile()) {
                    file.delete();
                }
            }
        }
        dir.delete();
    }

    // A space hog class
    static final int MEGA = 1024 * 1024;

    public class BigObject {
        @SuppressWarnings("unused")
        private final byte[] space = new byte[MEGA];
        private final int id;

        public BigObject(int id) {
            this.id = id;
        }

        public int getId() {
            return id;
        }
    }

    // A weak reference on class
    static final class ClassReference extends WeakReference<Class<?>> {
        ClassReference(Class<?> clazz, ReferenceQueue<Object> queue) {
            super(clazz, queue);
        }
    }

    // A soft reference on instance
    static final class InstanceReference extends SoftReference<Object> {
        InstanceReference(Object obj, ReferenceQueue<Object> queue) {
            super(obj, queue);
        }
    }

    @Test
    public void testOne() throws Exception {
        // abort test if class creator can not run
        if (!ClassCreator.canRun) {
            return;
        }
        ClassCreator cctor = new ClassCreator(jexl, base);
        cctor.setSeed(1);
        Class<?> foo1 = cctor.createClass();
        Assert.assertEquals("foo1", foo1.getSimpleName());
        cctor.clear();
    }

    @Test
    public void testMany() throws Exception {
        // abort test if class creator can not run
        if (!ClassCreator.canRun) {
            return;
        }
        int pass = 0;
        int gced = -1;
        ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
        List<Reference<?>> stuff = new ArrayList<Reference<?>>();
        // keeping a reference on methods prevent classes from being GCed
//        List<Object> mm = new ArrayList<Object>();
        JexlExpression expr = jexl.createExpression("foo.value");
        JexlExpression newx = jexl.createExpression("foo = new(clazz)");
        JexlEvalContext context = new JexlEvalContext();
        context.setStrict(false);
        context.setSilent(true);

        ClassCreator cctor = new ClassCreator(jexl, base);
        for (int i = 0; i < LOOPS && gced < 0; ++i) {
            cctor.setSeed(i);
            Class<?> clazz;
            if (pass == 0) {
                clazz = cctor.createClass();
            } else {
                clazz = cctor.getClassInstance();
                if (clazz == null) {
                    Assert.assertEquals(i, gced);
                    break;
                }
            }
            // this code verifies the assumption that holding a strong reference to a method prevents
            // its owning class from being GCed
//          Method m = clazz.getDeclaredMethod("getValue", new Class<?>[0]);
//          mm.add(m);
            // we should not be able to create foox since it is unknown to the JEXL classloader
            context.set("clazz", cctor.getClassName());
            context.set("foo", null);
            Object z = newx.evaluate(context);
            Assert.assertNull(z);
            // check with the class itself
            context.set("clazz", clazz);
            z = newx.evaluate(context);
            Assert.assertNotNull(clazz + ": class " + i + " could not be instantiated on pass " + pass, z);
            Assert.assertEquals(new Integer(i), expr.evaluate(context));
            // with the proper class loader, attempt to create an instance from the class name
            jexl.setClassLoader(cctor.getClassLoader());
            z = newx.evaluate(context);
            Assert.assertTrue(z.getClass().equals(clazz));
            Assert.assertEquals(new Integer(i), expr.evaluate(context));
            cctor.clear();
            jexl.setClassLoader(null);

            // on pass 0, attempt to force GC to run and collect generated classes
            if (pass == 0) {
                // add a weak reference on the class
                stuff.add(new ClassReference(clazz, queue));
                // add a soft reference on an instance
                stuff.add(new InstanceReference(clazz.newInstance(), queue));

                // attempt to force GC:
                // while we still have a MB free, create & store big objects
                for (int b = 0; b < 1024 && Runtime.getRuntime().freeMemory() > MEGA; ++b) {
                    BigObject big = new BigObject(b);
                    stuff.add(new InstanceReference(big, queue));
                }
                // hint it...
                System.gc();
                // let's see if some weak refs got collected
                boolean qr = false;
                while (queue.poll() != null) {
                    Reference<?> ref = queue.remove(1);
                    if (ref instanceof ClassReference) {
                        gced = i;
                        qr = true;
                    }
                }
                if (qr) {
                    //logger.warn("may have GCed class around " + i);
                    pass = 1;
                    i = 0;
                }
            }
        }

        if (gced < 0) {
            logger.warn("unable to force GC");
            //Assert.assertTrue(gced > 0);
        }
    }

    public static class TwoCtors {
        int value;

        public TwoCtors(int v) {
            this.value = v;
        }

        public TwoCtors(Number x) {
            this.value = -x.intValue();
        }

        public int getValue() {
            return value;
        }
    }

    @Test
    public void testBasicCtor() throws Exception {
        JexlScript s = jexl.createScript("(c, v)->{ var ct2 = new(c, v); ct2.value; }");
        Object r = s.execute(null, TwoCtors.class, 10);
        Assert.assertEquals(10, r);
        r = s.execute(null, TwoCtors.class, 5 + 5);
        Assert.assertEquals(10, r);
        r = s.execute(null, TwoCtors.class, 10d);
        Assert.assertEquals(-10, r);
        r = s.execute(null, TwoCtors.class, 100f);
        Assert.assertEquals(-100, r);
    }
}