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 java.util.Collections;
import junit.textui.TestRunner;
import org.netbeans.jmi.javamodel.Expression;
import org.netbeans.jmi.javamodel.ForEachStatement;

import org.netbeans.jmi.javamodel.JavaClass;
import org.netbeans.jmi.javamodel.JavaModelPackage;
import org.netbeans.junit.NbTestCase;
import org.netbeans.junit.NbTestSuite;
import org.netbeans.jmi.javamodel.Method;
import org.netbeans.jmi.javamodel.MethodInvocation;
import org.netbeans.jmi.javamodel.MultipartId;
import org.netbeans.jmi.javamodel.Parameter;
import org.netbeans.jmi.javamodel.Statement;
import org.netbeans.jmi.javamodel.StatementBlock;
import org.netbeans.jmi.javamodel.TypeReference;
import org.openide.filesystems.AbstractFileSystem.List;
import org.openide.filesystems.FileStateInvalidException;

/**
 *
 * @author  Pavel Flaska
 */
public class ForEachTest1 extends NbTestCase {

    /** Creates a new instance of ForEachTest1 */
    public ForEachTest1() {
        super("ForEachTest1");
    }

    public static NbTestSuite suite() {
        NbTestSuite suite = new NbTestSuite(ForEachTest1.class);
        return suite;
    }
    
    Method method;
    JavaModelPackage pkg;
    
    protected void setUp() {
        JavaClass clazz = Utility.findClass("org.netbeans.test.codegen.ForEachTest1");
        pkg = (JavaModelPackage) clazz.refImmediatePackage();
        method = (Method) clazz.getContents().iterator().next();
    }

    public void testForCreation() throws java.io.IOException, FileStateInvalidException {
        boolean fail = true;
        Utility.beginTrans(true);
        try {
            // parameter
            TypeReference typeRef = pkg.getMultipartId().createMultipartId("String", null, null);
            Parameter parameter = pkg.getParameter().createParameter("s", null, false, typeRef, 0, false);
            // iterable
            Expression iterable = pkg.getMultipartId().createMultipartId("c", null, null);
            // method invocation
            Expression par = pkg.getMultipartId().createMultipartId("s", null, null);
            MultipartId parent = pkg.getMultipartId().createMultipartId("System.out", null, null);
            MethodInvocation invocation = pkg.getMethodInvocation().createMethodInvocation("println", Collections.singletonList(par), parent, false);
            Statement statement = pkg.getExpressionStatement().createExpressionStatement(invocation);
            // for body
            StatementBlock forBody = pkg.getStatementBlock().createStatementBlock();
            forBody.getStatements().add(statement);
            // for (String s : c) {
            //     System.out.println(s);
            // }
            ForEachStatement fes = pkg.getForEachStatement().createForEachStatement(forBody, parameter, iterable);
            // add to method
            method.getBody().getStatements().add(fes);
            fail = false;
        } finally {
            Utility.endTrans(fail);
        }
        assertFile("File is not correctly generated.",
            Utility.getFile(getDataDir(), "org/netbeans/test/codegen/ForEachTest1.java"),
            getGoldenFile("testForCreation_ForEachTest1.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.