Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipse-dev] Using refactoring code outside of eclipse


Hi,

I am trying to get this code working:


import junit.framework.TestCase;
import org.eclipse.jdt.testplugin.JavaProjectHelper;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.ICompilationUnit;

public class SpikeTestCase extends TestCase {
    private IJavaProject javaTestProject;
    private IPackageFragmentRoot root;
    private static final String CONTAINER = "";
    private IPackageFragment packageP;

    protected void setUp() throws Exception {
                super.setUp();
                JavaProjectHelper.setAutoBuilding(false);
                if (JavaPlugin.getActivePage() != null)
                        JavaPlugin.getActivePage().close();
                javaTestProject= JavaProjectHelper.createJavaProject("TestProject"+System.currentTimeMillis(), "bin");
                JavaProjectHelper.addRTJar(javaTestProject);
                root= JavaProjectHelper.addSourceContainer(javaTestProject, CONTAINER);
                packageP= root.createPackageFragment("p", true, null);
        }

    public void testSimpleRefactoring() throws Exception {
        ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    }

    protected ICompilationUnit createCUfromTestFile(IPackageFragment pack, String contents) throws Exception {
                return createCU(pack, "mike" + ".java", contents);
        }

    public static ICompilationUnit createCU(IPackageFragment pack, String name, String contents) throws Exception {
                if (pack.getCompilationUnit(name).exists())
                        return pack.getCompilationUnit(name);
                ICompilationUnit cu= pack.createCompilationUnit(name, contents, true, null);
                cu.save(null, true);
                return cu;
        }

    protected IPackageFragment getPackageP() {
            return packageP;
        }

}

I get a NullPointerException on the second line of the setUp method, the line that reads "JavaProjectHelper.setAutoBuilding(false);"

Does anybody have any idea what I need to do to get this to work?

Thanks,
Mike

Back to the top