|
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.FieldElem;
import org.netbeans.test.java.Common;
import org.openide.src.Type;
import java.lang.reflect.Modifier;
import org.netbeans.junit.*;
/** Java Module General API Test: FieldElement
*
What it tests:
* Creating and handling with FieldElement.
* Test is focused on checking of correctness of generated code.
*
How it works:
* FieldElements are created and customized using setters and then added using ClassElement.addField() into ClassElement.
* These actions cause generating of .java code. This code is compared with supposed one.
*
Output:
* Generated Java code.
*
Possible reasons of failure:
* Fields are not inserted properly:
* If there are some fields in .diff file.
*
Fields have bad properties (e.g. modifiers, return type)
* 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 FieldElem extends org.netbeans.test.java.XRunner {
public static void main(java.lang.String[] args) {
junit.textui.TestRunner.run(suite());
}
public FieldElem() {
super("");
}
public FieldElem(java.lang.String testName) {
super(testName);
}
public static NbTest suite() {
return new NbTestSuite(FieldElem.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.ClassElement clazz = ((org.openide.src.SourceElement) o).getClasses()[0];
boolean passed = true;
//let's add some fields newField1 .. newField4
int i=1;
clazz.addField(Common.createField(Common.getFieldName(i++),Modifier.PUBLIC | Modifier.STATIC , Type.BOOLEAN));
clazz.addField(Common.createField(Common.getFieldName(i++),Modifier.PRIVATE | Modifier.STATIC , Type.INT));
clazz.addField(Common.createField(Common.getFieldName(i++),Modifier.PROTECTED,Type.BOOLEAN));
clazz.addField(Common.createField(Common.getFieldName(i++),Modifier.SYNCHRONIZED | Modifier.PUBLIC,Type.FLOAT));
// now 4 methods should be in hierarchy lets' look at them
if (clazz.getFields().length != 4) {
passed = false;
log("Fields were not properly added");
}
org.openide.src.FieldElement fe=clazz.getField(org.openide.src.Identifier.create(Common.getFieldName(1)));
if (fe==null) {
passed = false;
log("Field not found");
} else {
if (!fe.getName().equals(org.openide.src.Identifier.create(Common.getFieldName(1)))){
passed = false;
log("getName failed");
}
if (!(fe.getModifiers()==(Modifier.PUBLIC| Modifier.STATIC))){
passed = false;
log("getModifiers failed");
}
if (!fe.getType().equals(Type.BOOLEAN)){
passed = false;
log("getReturn failed");
}
/*
if (!(fe.getDeclaringClass().getName().getName().equals(name))){
passed = false;
log("getDeclaringClass failed");
}
*/
}
return passed;
}
/**
*/
protected void setUp() {
super.setUp();
name = "JavaTestSourceFieldElem";
packageName = "org.netbeans.test.java.testsources";
}
}
|