|
What this is
This file is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
* Microsystems, Inc. All Rights Reserved.
*/
/*
* FieldElem.java
*
* Created on June 26, 2000, 9:29 AM
*/
package org.netbeans.test.java.generating.SourceElem;
import org.netbeans.test.java.Common;
import org.openide.src.*;
import java.lang.reflect.Modifier;
import org.netbeans.junit.*;
/** Java Module General API Test: SourceElement
*
What it tests:
* Creating and handling with SourceElement.
* Test is focused on checking of correctness of generated code.
*
How it works:
* New class is created using DataObject.createFromTemplate().
* This SourceElement is then customized using setters. Some classes are inserted.
* These actions cause generating of .java code. This code is compared with supposed one.
*
Output:
* Generated Java code.
*
Possible reasons of failure:
*
SourceElement have/return bad properties
* See .diff file to get which ones
*
Bad indentation
* This is probably not a bug of Java Module. (Editor Bug)
* In .diff file could be some whitespaces.
*
Exception occured:
* See .log file for StackTrace
*
* @author Jan Becicka
*/
public class SourceElem extends org.netbeans.test.java.XRunner {
public static void main(java.lang.String[] args) {
junit.textui.TestRunner.run(suite());
}
public SourceElem() {
super("");
}
public SourceElem(java.lang.String testName) {
super(testName);
}
public static NbTest suite() {
return new NbTestSuite(SourceElem.class);
}
/** "body" of this TestCase
* @param o SourceElement - target for generating
* @param log log is used for logging StackTraces
* @throws Exception
* @return true if test passed
* false if failed
*/
public boolean go(Object o, java.io.PrintWriter log) throws Exception {
org.openide.src.SourceElement source = (org.openide.src.SourceElement) o;
boolean passed = true;
Import[] imports = new Import[] {
new Import(Identifier.create("java.awt.image.RGBImageFilter"),false),
new Import(Identifier.create("javax.swing.event.HyperlinkEvent"),false),
new Import(Identifier.create("javax.swing"),true)
};
source.addImports(imports);
if (source.getImports().length!=3){
passed = false;
log("getImports() failed");
}
source.addImport(new Import(Identifier.create("java.awt"),true));
if (!source.getImports()[1].getIdentifier().toString().equals("java.awt.image.RGBImageFilter")){
passed = false;
log("getImports() failed");
}
source.removeImport(source.getImports()[1]);
for (int i=0;i<100;i++){
source.setPackage(Identifier.create(Common.concat("somepackage",i)));
source.prepare().waitFinished();
if (source.getStatus()!=org.openide.src.SourceElement.STATUS_OK){
passed = false;
log("setPackage failed");
}
if (!source.getPackage().toString().equals(Common.concat("somepackage",i))){
passed = false;
log("setPackage failed");
}
}
if (!source.getPackage().toString().equals("somepackage99")){
passed = false;
log("getPackage() failed");
}
ClassElement ce = new ClassElement();
ce.setName(Identifier.create("newClass1"));
source.addClass(ce);
ce.setName(Identifier.create("newClass2"));
source.addClass(ce);
ClassElement[] ces = new ClassElement[] {
new ClassElement(),
new ClassElement()
};
ces[0].setName(Identifier.create("newClass3"));
ces[1].setName(Identifier.create("newClass4"));
source.addClasses(ces);
if (source.getClasses().length !=5) {
passed = false;
log("getClasses() failed");
}
source.removeClass(source.getClass(Identifier.create("newClass2")));
source.removeClasses(new ClassElement[] {source.getClass(Identifier.create("newClass4"))});
return passed;
}
/**
*/
protected void setUp() {
super.setUp();
name = "JavaTestSourceSourceElem";
packageName = "org.netbeans.test.java.testsources";
}
}
|