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 org.netbeans.junit.NbTestCase;
import org.netbeans.junit.NbTestSuite;
import org.netbeans.jmi.javamodel.JavaModelPackage;
import org.netbeans.jmi.javamodel.Import;
import org.netbeans.jmi.javamodel.Resource;
import org.netbeans.jmi.javamodel.JavaClass;
import java.io.IOException;
import java.util.Iterator;
import junit.textui.TestRunner;
import org.openide.filesystems.FileStateInvalidException;

/**
 * Tests the static imports as they are defined in JSR201 and implemented
 * in JDK 1.5.
 *
 * @author  Pavel Flaska
 */
public class StaticImportTest extends NbTestCase {
    
    /** Creates a new instance of StaticImportTest */
    public StaticImportTest() {
        super("StaticImportTest");
    }

    public static NbTestSuite suite() {
        NbTestSuite suite = new NbTestSuite(StaticImportTest.class);
        return suite;
    }
    
    JavaModelPackage pkg;
    JavaClass clazz;
    Import arraysImport;
    
    protected void setUp() {
        clazz = Utility.findClass("org.netbeans.test.codegen.StaticImport");
        pkg = (JavaModelPackage) clazz.refImmediatePackage();
        for (Iterator it = clazz.getResource().getImports().iterator(); it.hasNext(); ) {
            arraysImport = (Import) it.next();
        }
    }
    
    public void testCreation() throws IOException, FileStateInvalidException {
        boolean fail = true;
        Utility.beginTrans(true);
        try {
            Resource res = clazz.getResource();
            Import i = pkg.getImport().createImport("java.util.Collections", null, true, false);
            res.getImports().add(i);
            fail = false;
        } finally {
            Utility.endTrans(fail);
        }
        assertFile("File is not correctly generated.",
            Utility.getFile(getDataDir(), "org/netbeans/test/codegen/StaticImport.java"),
            getGoldenFile("testCreation_StaticImport.pass"),
            getWorkDir()
        );
    }
    
    public void testCreation2() throws IOException, FileStateInvalidException {
        boolean fail = true;
        Utility.beginTrans(true);
        try {
            Resource res = clazz.getResource();
            arraysImport = pkg.getImport().createImport("java.util.Arrays", null, false, false);
            res.getImports().add(arraysImport);
            fail = false;
        } finally {
            Utility.endTrans(fail);
        }
        assertFile("File is not correctly generated.",
            Utility.getFile(getDataDir(), "org/netbeans/test/codegen/StaticImport.java"),
            getGoldenFile("testCreation2_StaticImport.pass"),
            getWorkDir()
        );
    }
    
    public void testSetStatic() throws IOException, FileStateInvalidException {
        boolean fail = true;
        Utility.beginTrans(true);
        try {
            arraysImport.setStatic(true);
            fail = false;
        } finally {
            Utility.endTrans(fail);
        }
        assertFile("File is not correctly generated.",
            Utility.getFile(getDataDir(), "org/netbeans/test/codegen/StaticImport.java"),
            getGoldenFile("testSetStatic_StaticImport.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.