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.test.nativequery.analysis;
022:
023: import EDU.purdue.cs.bloat.cfg.*;
024: import EDU.purdue.cs.bloat.editor.*;
025: import EDU.purdue.cs.bloat.file.*;
026:
027: import com.db4o.instrumentation.core.*;
028: import com.db4o.instrumentation.util.*;
029: import com.db4o.nativequery.analysis.*;
030: import com.db4o.nativequery.expr.*;
031:
032: import db4ounit.*;
033: import db4ounit.extensions.util.*;
034:
035: public abstract class NQOptimizationByteCodeTestCaseBase implements
036: TestLifeCycle {
037:
038: public final static int NO_MODIFIERS = 0;
039: public final static String CLASSNAME = "invalid.CandidateClass";
040: private static final String METHODNAME = "sampleMethod";
041:
042: protected final LocalVariable THIS_VAR = new LocalVariable(0);
043: protected final LocalVariable DATA_VAR = new LocalVariable(1);
044: private final LabelGenerator _labelGenerator = new LabelGenerator();
045: protected final Label START_LABEL = _labelGenerator
046: .createLabel(true);
047:
048: public void setUp() throws Exception {
049:
050: }
051:
052: public void tearDown() throws Exception {
053: IOUtil.deleteDir("invalid");
054: }
055:
056: public void testOptimization() throws Exception {
057: BloatLoaderContext bloatUtil = new BloatLoaderContext(
058: new ClassFileLoader());
059: ClassEditor clazz = bloatUtil.classEditor(NO_MODIFIERS,
060: CLASSNAME, Type.OBJECT, new Type[] {});
061: MethodEditor method = new MethodEditor(clazz, NO_MODIFIERS,
062: Type.BOOLEAN, METHODNAME, new Type[] { Type
063: .getType(Data.class) }, new Type[] {});
064: method.addLabel(START_LABEL);
065: generateMethodBody(method);
066: method.commit();
067: clazz.commit();
068:
069: FlowGraph flowGraph = new FlowGraph(method);
070: BloatExprBuilderVisitor visitor = new BloatExprBuilderVisitor(
071: bloatUtil);
072: flowGraph.visit(visitor);
073: Expression expression = visitor.expression();
074: assertOptimization(expression);
075: }
076:
077: protected MemberRef createMethodReference(Type parent, String name,
078: Type[] args, Type ret) {
079: NameAndType nameAndType = new NameAndType(name, Type.getType(
080: args, ret));
081: return new MemberRef(parent, nameAndType);
082: }
083:
084: protected MemberRef createFieldReference(Type parent, String name,
085: Type type) {
086: NameAndType nameAndType = new NameAndType(name, type);
087: return new MemberRef(parent, nameAndType);
088: }
089:
090: protected Label createLabel() {
091: return createLabel(true);
092: }
093:
094: protected Label createLabel(boolean startsBlock) {
095: return _labelGenerator.createLabel(startsBlock);
096: }
097:
098: protected abstract void generateMethodBody(MethodEditor method);
099:
100: protected abstract void assertOptimization(Expression expression)
101: throws Exception;
102: }
|