Method from org.apache.activemq.systest.task.SystemTestGenerator Detail: |
protected void addAllInterfaces(List list,
JClass type) {
JClass[] interfaces = type.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
JClass interfaceType = interfaces[i];
list.add(interfaceType);
}
JClass superclass = type.getSuperclass();
if (superclass != null) {
addAllInterfaces(list, superclass);
}
}
|
protected String asPackageName(String[] paths) {
StringBuffer buffer = new StringBuffer(paths[0]);
for (int i = 1; i < paths.length; i++) {
buffer.append(".");
buffer.append(paths[i]);
}
return buffer.toString();
}
|
protected String destinationExpression(int destinationType) {
switch (destinationType) {
case DestinationFactory.QUEUE:
return "DestinationFactory.QUEUE";
default:
return "DestinationFactory.TOPIC";
}
}
|
public void generate() throws IOException {
List list = new ArrayList();
for (int i = 0; i < classes.length; i++) {
JClass type = classes[i];
if (implementsInterface(type, Scenario.class) && !type.isAbstract() && !type.isInterface()) {
generateTestsFor(type);
list.add(type);
}
}
// now lets generate a list of all the available
if (scenariosFile != null) {
generatePropertiesFile(list);
}
}
|
protected void generateFile(PrintWriter writer,
JClass type,
File clientFile,
File brokerFile,
String packageName,
int destinationType) throws IOException {
writer.println(getLicenseHeader());
writer.println("package " + packageName + ";");
writer.println();
writer.println("import org.apache.activemq.systest.DestinationFactory;");
writer.println("import org.apache.activemq.systest.ScenarioTestSuite;");
writer.println("import " + type.getQualifiedName() + ";");
writer.println("import org.springframework.context.ApplicationContext;");
writer.println("import org.springframework.context.support.FileSystemXmlApplicationContext;");
writer.println();
writer.println("import junit.framework.TestSuite;");
writer.println();
writer.println("/**");
writer.println(" * System test case for Scenario: " + type.getSimpleName());
writer.println(" *");
writer.println(" *");
writer.println(" * NOTE!: This file is auto generated - do not modify!");
writer.println(" * if you need to make a change, please see SystemTestGenerator code");
writer.println(" * in the activemq-systest module in ActiveMQ 4.x");
writer.println(" *");
writer.println(" * @version $Revision:$");
writer.println(" */");
writer.println("public class " + type.getSimpleName() + "Test extends ScenarioTestSuite {");
writer.println();
writer.println(" public static TestSuite suite() throws Exception {");
writer.println(" ApplicationContext clientContext = new FileSystemXmlApplicationContext(\"" + relativePath(clientFile) + "\");");
writer.println(" ApplicationContext brokerContext = new FileSystemXmlApplicationContext(\"" + relativePath(brokerFile) + "\");");
writer.println(" Class[] scenarios = { " + type.getSimpleName() + ".class };");
writer.println(" return createSuite(clientContext, brokerContext, scenarios, " + destinationExpression(destinationType) + ");");
writer.println(" }");
writer.println("}");
}
|
protected void generatePropertiesFile(List list) throws IOException {
PrintWriter writer = new PrintWriter(new FileWriter(scenariosFile));
try {
for (Iterator iter = list.iterator(); iter.hasNext();) {
JClass type = (JClass) iter.next();
writer.print(type.getQualifiedName());
writer.print(" = ");
writeInterfaces(writer, type);
writer.println();
}
}
finally {
writer.close();
}
}
|
protected void generateTestsFor(JClass type) throws IOException {
String[] files = clientsScanner.getIncludedFiles();
for (int i = 0; i < files.length; i++) {
String name = files[i];
File file = new File(clientsScanner.getBasedir(), name);
generateTestsFor(type, name, file);
}
}
|
protected void generateTestsFor(JClass type,
String clientName,
File clientFile) throws IOException {
String[] files = brokersScanner.getIncludedFiles();
for (int i = 0; i < files.length; i++) {
String name = files[i];
File basedir = brokersScanner.getBasedir();
File file = new File(basedir, name);
if (!implementsInterface(type, TopicOnlyScenario.class)) {
generateTestsFor(type, clientName, clientFile, name, file, DestinationFactory.QUEUE);
}
if (!implementsInterface(type, QueueOnlyScenario.class)) {
generateTestsFor(type, clientName, clientFile, name, file, DestinationFactory.TOPIC);
}
}
}
|
protected void generateTestsFor(JClass type,
String clientName,
File clientFile,
String brokerName,
File brokerFile,
int destinationType) throws IOException {
String clientPrefix = trimPostFix(clientName);
String brokerPrefix = trimPostFix(brokerName);
String destinationName = ScenarioTestSuite.destinationDescription(destinationType);
String[] paths = { "org", "activemq", "systest", brokerPrefix, destinationName, clientPrefix };
String packageName = asPackageName(paths);
File dir = makeDirectories(paths);
File file = new File(dir, type.getSimpleName() + "Test.java");
PrintWriter writer = new PrintWriter(new FileWriter(file));
try {
System.out.println("Generating: " + file);
generateFile(writer, type, clientFile, brokerFile, packageName, destinationType);
}
finally {
writer.close();
}
}
|
public String getLicenseHeader() throws IOException {
if( licenseHeader == null ) {
// read the LICENSE_HEADER.txt into the licenseHeader variable.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = SystemTestGenerator.class.getResourceAsStream("LICENSE_HEADER.txt");
try {
int c;
while( (c=is.read()) >=0 ) {
baos.write(c);
}
} finally {
is.close();
}
baos.close();
licenseHeader = new String(baos.toByteArray(),"UTF-8");
}
return licenseHeader;
}
|
protected boolean implementsInterface(JClass type,
Class interfaceClass) {
JClass[] interfaces = type.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
JClass anInterface = interfaces[i];
if (anInterface.getQualifiedName().equals(interfaceClass.getName())) {
return true;
}
}
JClass superclass = type.getSuperclass();
if (superclass == null || superclass == type) {
return false;
}
else {
return implementsInterface(superclass, interfaceClass);
}
}
|
protected File makeDirectories(String[] paths) {
File dir = destDir;
for (int i = 0; i < paths.length; i++) {
dir = new File(dir, paths[i]);
}
dir.mkdirs();
return dir;
}
|
protected String relativePath(File file) {
String name = file.toString();
String prefix = baseDir.toString();
if (name.startsWith(prefix)) {
return name.substring(prefix.length() + 1);
}
return name;
}
|
public void setLicenseHeader(String licenseHeader) {
this.licenseHeader = licenseHeader;
}
|
protected String trimPostFix(String uri) {
int idx = uri.lastIndexOf('.');
if (idx > 0) {
return uri.substring(0, idx);
}
return uri;
}
|
protected void writeInterfaces(PrintWriter writer,
JClass type) {
List interfaces = new ArrayList();
addAllInterfaces(interfaces, type);
boolean first = true;
for (Iterator iter = interfaces.iterator(); iter.hasNext();) {
JClass interfaceType = (JClass) iter.next();
if (first) {
first = false;
}
else {
writer.print(", ");
}
writer.print(interfaceType.getQualifiedName());
}
}
|