Refactor of OpenWire code generation together with move to apache package

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@378480 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Strachan 2006-02-17 10:52:25 +00:00
parent e5959da6d7
commit 83ec98be8a
3 changed files with 269 additions and 0 deletions

View File

@ -0,0 +1,67 @@
/**
*
* Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
*
* Licensed 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.activemq.openwire.tool;
/**
* A simple helper class to help auto-generate test data when code generating test cases
*
* @version $Revision: 1.1 $
*/
public class TestDataGenerator {
private int stringCounter;
private boolean boolCounter;
private byte byteCounter;
private char charCounter = 'a';
private short shortCounter;
private int intCounter;
private long longCounter;
public String createByte() {
return "(byte) " + (++byteCounter);
}
public String createChar() {
return "'" + (charCounter++) + "'";
}
public String createShort() {
return "(short) " + (++shortCounter);
}
public int createInt() {
return ++intCounter;
}
public long createLong() {
return ++longCounter;
}
public String createString(String property) {
return property + ":" + (++stringCounter);
}
public boolean createBool() {
boolCounter = !boolCounter;
return boolCounter;
}
public String createByteArray(String property) {
return "\"" + createString(property) + "\".getBytes()";
}
}

View File

@ -0,0 +1,202 @@
/**
*
* Copyright 2005-2006 The Apache Software Foundation
*
* Licensed 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.
*/
import org.apache.activemq.openwire.tool.OpenWireScript
import org.apache.activemq.openwire.tool.TestDataGenerator
/**
* Generates the Java test code for the Open Wire Format
*
* @version $Revision: $
*/
class GenerateJavaTests extends OpenWireScript {
Object run() {
def openwireVersion = System.getProperty("openwire.version");
def destDir = new File("src/test/java/org/apache/activemq/openwire/v${openwireVersion}")
println "Generating Java test code to directory ${destDir}"
def openwireClasses = classes.findAll {
it.getAnnotation("openwire:marshaller")!=null
}
def concreteClasses = new ArrayList()
def buffer = new StringBuffer()
int counter = 0
Map map = [:]
destDir.mkdirs()
for (jclass in openwireClasses) {
println "Processing ${jclass.simpleName}"
def abstractText = "abstract "
def isAbstract = isAbstract(jclass);
if( !isAbstract ) {
concreteClasses.add(jclass)
abstractText = ""
}
def properties = jclass.declaredProperties.findAll { isValidProperty(it) }
def file = new File(destDir, jclass.simpleName + "Test.java")
buffer << """
${jclass.simpleName}Test.class
"""
file.withWriter { out |
out << """/**
*
* Copyright 2005-2006 The Apache Software Foundation
*
* Licensed 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.activemq.openwire.v${openwireVersion};
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.apache.activemq.openwire.*;
import org.apache.activemq.command.*;
"""
for (pkg in jclass.importedPackages) {
for (clazz in pkg.classes) {
out << "import "+clazz.qualifiedName+";"
}
}
def baseClass = "DataFileGeneratorTestSupport"
if( !jclass.superclass.simpleName.equals("Object") ) {
baseClass = jclass.superclass.simpleName + "Test";
}
def marshallerAware = isMarshallAware(jclass);
out << """
/**
* Test case for the OpenWire marshalling for ${jclass.simpleName}
*
*
* NOTE!: This file is auto generated - do not modify!
* if you need to make a change, please see the modify the groovy scripts in the
* under src/gram/script and then use maven openwire:generate to regenerate
* this file.
*
* @version \$Revision: \$
*/
public ${abstractText}class ${jclass.simpleName}Test extends $baseClass {
"""
if (!isAbstract)
out << """
public static ${jclass.simpleName}Test SINGLETON = new ${jclass.simpleName}Test();
public Object createObject() throws Exception {
${jclass.simpleName} info = new ${jclass.simpleName}();
populateObject(info);
return info;
}
"""
out << """
protected void populateObject(Object object) throws Exception {
super.populateObject(object);
${jclass.simpleName} info = (${jclass.simpleName}) object;
"""
def generator = new TestDataGenerator();
for (property in properties) {
def annotation = property.getter.getAnnotation("openwire:property");
def size = annotation.getValue("size");
def testSize = stringValue(annotation, "testSize");
def type = property.type.simpleName
def cached = isCachedProperty(property);
def propertyName = property.simpleName;
out << " "
switch (type) {
case "boolean":
out << "info.${property.setter.simpleName}(${generator.createBool()});"; break;
case "byte":
out << "info.${property.setter.simpleName}(${generator.createByte()});"; break;
case "char":
out << "info.${property.setter.simpleName}(${generator.createChar()});"; break;
case "short":
out << "info.${property.setter.simpleName}(${generator.createShort()});"; break;
case "int":
out << "info.${property.setter.simpleName}(${generator.createInt()});"; break;
case "long":
out << "info.${property.setter.simpleName}(${generator.createLong()});"; break;
case "byte[]":
out << """info.${property.setter.simpleName}(${generator.createByteArray(propertyName)});"""; break;
case "String":
out << """info.${property.setter.simpleName}("${generator.createString(propertyName)}");"""; break;
case "ByteSequence":
out << """
{
byte data[] = ${generator.createByteArray(propertyName)};
info.${property.setter.simpleName}(new org.activeio.ByteSequence(data,0,data.length));
}
""";
break;
case "Throwable":
out << """info.${property.setter.simpleName}(createThrowable("${generator.createString(propertyName)}"));"""; break;
default:
if( property.type.arrayType ) {
def arrayType = property.type.arrayComponentType.simpleName;
if (size == null)
size = 2
if (arrayType == jclass.simpleName)
size = 0
out << """
{
${arrayType} value[] = new ${arrayType}[${size}];
for( int i=0; i < ${size}; i++ ) {
value[i] = create${arrayType}("${generator.createString(propertyName)}");
}
info.${property.setter.simpleName}(value);
}"""
}
else {
out << """info.${property.setter.simpleName}(create${type}("${generator.createString(propertyName)}"));"""
}
}
out.newLine()
}
out << """
}
}
"""
}
}
}
}