alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

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-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.jmi.javamodel.codegen;

import junit.textui.TestRunner;
import org.netbeans.jmi.javamodel.*;
import org.netbeans.junit.NbTestCase;
import org.netbeans.junit.NbTestSuite;
import java.io.IOException;
import java.lang.reflect.Modifier;

/**
 *
 * @author  Pavel Flaska
 */
public class AnonClassTest extends NbTestCase {
    
    /** Need to be defined because of JUnit */
    public AnonClassTest() {
        super("AnonClassTest");
    }
    
    public static NbTestSuite suite() {
        NbTestSuite suite = new NbTestSuite(AnonClassTest.class);
        return suite;
    }
    
    Method method;
    JavaModelPackage pkg;
    
    protected void setUp() {
        JavaClass clazz = Utility.findClass("org.netbeans.test.codegen.AnonClassTestClass");
        pkg = (JavaModelPackage) clazz.refImmediatePackage();
        method = (Method) clazz.getContents().iterator().next();
    }

    public void testRead() {
        Utility.beginTrans(false);
        try {
            LocalVarDeclaration varDecl = (LocalVarDeclaration) method.getBody().getStatements().get(0);
            LocalVariable var = (LocalVariable) varDecl.getVariables().get(0);
            NewClassExpression nce = (NewClassExpression) var.getInitialValue();
            ClassDefinition cd = nce.getClassDefinition();
            assertEquals("Number of features does not match: ", 3, cd.getFeatures().size());
        } finally {
            Utility.endTrans();
        }
    }

    public void testChange() throws IOException {
        boolean fail = true;
        Utility.beginTrans(true);
        try {
            LocalVarDeclaration varDecl = (LocalVarDeclaration) method.getBody().getStatements().get(0);
            LocalVariable var = (LocalVariable) varDecl.getVariables().get(0);
            NewClassExpression nce = (NewClassExpression) var.getInitialValue();
            ClassDefinition cd = nce.getClassDefinition();
            Method compare = (Method) cd.getContents().get(0);
            ((Parameter) compare.getParameters().get(0)).setName("param1");
            ((Parameter) compare.getParameters().get(1)).setName("param2");
            fail = false;
        }
        finally {
            Utility.endTrans(fail);
        }
        assertFile("File is not correctly generated.",
            Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnonClassTestClass.java"),
            getGoldenFile("AnonClass1.pass"),
            getWorkDir()
        );
    }

    public void testCreate() throws IOException {
        boolean fail = true;
        Utility.beginTrans(true);
        try {
            LocalVarDeclaration varDecl = pkg.getLocalVarDeclaration().createLocalVarDeclaration(true, null, null);
            varDecl.setType(pkg.getType().resolve("java.lang.Object"));
            ClassDefinition cd = pkg.getClassDefinition().createClassDefinition();
            NewClassExpression nce = pkg.getNewClassExpression().createNewClassExpression("java.lang.Object", null, null, null, cd);
            LocalVariable var = pkg.getLocalVariable().createLocalVariable("test", null, false, null, 0, nce, null);
            varDecl.getVariables().add(var);
            cd.getContents().add(pkg.getMethod().createMethod("toString", null, Modifier.PUBLIC,  null, null, null, "return null;", null, null, null, pkg.getMultipartId().createMultipartId("String", null, null), 0));
            method.getBody().getStatements().add(0, varDecl);
            fail = false;
        } finally {
            Utility.endTrans(fail);
        }
        assertFile("File is not correctly generated.",
            Utility.getFile(getDataDir(), "org/netbeans/test/codegen/AnonClassTestClass.java"),
            getGoldenFile("AnonClass2.pass"),
            getWorkDir()
        );
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        TestRunner.run(suite());
    }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.