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-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.test.java.compilation;

import java.io.File;
import junit.textui.TestRunner;
import org.netbeans.jellytools.*;
import org.netbeans.jellytools.nodes.FolderNode;
import org.netbeans.jellytools.nodes.JavaNode;
import org.netbeans.jellytools.properties.Property;
import org.netbeans.jellytools.properties.PropertySheetOperator;
import org.netbeans.jemmy.EventTool;
import org.netbeans.jemmy.operators.JTreeOperator;
import org.netbeans.jemmy.operators.Operator;
import org.netbeans.jemmy.operators.Operator.DefaultStringComparator;
import org.netbeans.jemmy.operators.Operator.StringComparator;
import org.netbeans.jemmy.util.PNGEncoder;
import org.netbeans.junit.NbTestSuite;
import org.openide.actions.SaveAllAction;
import org.openide.util.Utilities;


public class CompilerSettings extends JellyTestCase {
    
    
    private final String sampleDir = org.netbeans.test.java.Utilities.getSampleDir().getDisplayName();
    private final String BUILDING = Bundle.getString("org.netbeans.core.Bundle", "UI/Services/Building");
    private final String EDITING = Bundle.getString("org.netbeans.core.Bundle", "UI/Services/Editing");
    private final String JAVA_SOURCES = Bundle.getString("org.netbeans.modules.java.settings.Bundle", "CTL_Java_option");
    private final String COMPILER_TYPES = Bundle.getString("org.netbeans.core.Bundle", "Templates/Services/CompilerType");
    private final String EXTERNAL = Bundle.getString("org.netbeans.modules.java.Bundle", "Templates/Services/CompilerType/JavaExternalCompiler.settings");
    private final String TEST_COMPILER = EXTERNAL + "_1";
    private final String DEFAULT_COMPILER = Bundle.getString("org.netbeans.modules.java.settings.Bundle", "PROP_COMPILER");
    private final String CONFIRM = Bundle.getString("org.openide.explorer.Bundle", "MSG_ConfirmDeleteObjectTitle");
    
    private final String PROP_DEBUG = Bundle.getString("org.netbeans.modules.java.settings.Bundle", "DEBUG_PROP");
    private final String PROP_ENCODING = Bundle.getString("org.netbeans.modules.java.settings.Bundle", "ENCODING_PROP");
    private final String PROP_OPTIMIZE = Bundle.getString("org.netbeans.modules.java.settings.Bundle", "OPTIMIZE_PROP");
    
    private final String PROP_BOOTCLASSPATH = Bundle.getString("org.netbeans.modules.java.Bundle", "PROP_BOOTCLASSPATH");
    private final String PROP_BOOTCLASSPATH_REPLACE = Bundle.getString("org.netbeans.modules.java.Bundle", "PROP_BOOTCLASSPATH_REPLACE");
    private final String PROP_CLASSPATH = Bundle.getString("org.netbeans.modules.java.Bundle", "PROP_CLASSPATH");
    private final String PROP_DEBUGINFO_REPLACE = Bundle.getString("org.netbeans.modules.java.Bundle", "PROP_DEBUGINFO_REPLACE");
    private final String PROP_DEPRECATION_REPLACE = Bundle.getString("org.netbeans.modules.java.Bundle", "PROP_DEPRECATION_REPLACE");
    private final String PROP_OPTIMIZE_REPLACE = Bundle.getString("org.netbeans.modules.java.Bundle", "PROP_OPTIMIZE_REPLACE");
    private final String PROP_D_REPLACE = Bundle.getString("org.netbeans.modules.java.Bundle", "PROP_D_REPLACE");
    private final String PROP_TARGET = Bundle.getString("org.netbeans.modules.java.Bundle", "TARGET_PROP");
    private final String PROP_DEPRECATION = Bundle.getString("org.netbeans.modules.java.Bundle", "DEPRECATION_PROP");
    private final String PROP_SOURCE_14ENABLE = Bundle.getString("org.netbeans.modules.java.Bundle", "PROP_SOURCE_14ENABLE");
    private final String PROP_EXTERNAL_COMPILER = Bundle.getString("org.netbeans.modules.java.Bundle", "PROP_EXTERNAL_COMPILER");
    
    private final String EXPERT = Bundle.getString("org.openide.nodes.Bundle", "Expert");
    private final String PROPERTIES = Bundle.getString("org.openide.nodes.Bundle", "Properties");
    private final String ECHO = Utilities.isWindows()?"cmd /C echo":"echo";
    
    /** Need to be defined because of JUnit */
    public CompilerSettings(String name) {
        super(name);
    }
    
    public static NbTestSuite suite() {
        NbTestSuite suite = new NbTestSuite();
        suite.addTest(new CompilerSettings("testOptions"));
        return suite;
    }
    
    /** Use for execution inside IDE */
    public static void main(java.lang.String[] args) {
        TestRunner.run(suite());
    }
    
    public void setUp() {
        System.out.println("########  "+getName()+"  #######");
        createTestCompiler();
    }
    
    
    private void createTestCompiler() {
        OptionsOperator optionsOperator = OptionsOperator.invoke();
        JTreeOperator optTree= optionsOperator.treeTable().tree();
        
        JavaNode node = new JavaNode(optTree, BUILDING + "|" + COMPILER_TYPES + "|" + EXTERNAL);
        node.copy();
        
        FolderNode folder = new FolderNode(optTree, BUILDING + "|" + COMPILER_TYPES);
        folder.pasteCopy();
        
        new EventTool().waitNoEvent(5000);

        optionsOperator.selectOption(EDITING + "|" + JAVA_SOURCES);
        PropertySheetOperator pso = optionsOperator.getPropertySheet(EDITING + "|" + JAVA_SOURCES);
        new Property(pso, DEFAULT_COMPILER).setValue(TEST_COMPILER);
        optionsOperator.close();
        
    }
    
    private void deleteTestCompiler() {
        OptionsOperator optionsOperator = OptionsOperator.invoke();
        optionsOperator.selectOption(BUILDING + "|" + COMPILER_TYPES + "|" + TEST_COMPILER);
        
        JTreeOperator optTree= optionsOperator.treeTable().tree();

        JavaNode node = new JavaNode(optTree, BUILDING + "|" + COMPILER_TYPES + "|" + TEST_COMPILER);
        new EventTool().waitNoEvent(1000);
        node.delete();
        
        new NbDialogOperator(CONFIRM).yes();

        optionsOperator.selectOption(EDITING + "|" + JAVA_SOURCES);
        PropertySheetOperator pso = optionsOperator.getPropertySheet(EDITING + "|" + JAVA_SOURCES);

        new Property(pso, DEFAULT_COMPILER).setValue(EXTERNAL);
        optionsOperator.close();
        
    }
        
    
    public void tearDown() {
        ((SaveAllAction) SaveAllAction.findObject(SaveAllAction.class, true)).performAction();
        deleteTestCompiler();
    }
    
    public void testOptions() { 
        OptionsOperator optionsOperator = OptionsOperator.invoke();

        PropertySheetOperator pso = optionsOperator.getPropertySheet(BUILDING + "|" + COMPILER_TYPES + "|" + TEST_COMPILER);
        pso.setComparator(new DefaultStringComparator(true, true));

        new Property(pso, PROP_DEBUG).setValue("True");
        new Property(pso, PROP_ENCODING).setValue("iso-8859-2");
        new Property(pso, PROP_OPTIMIZE).setValue("True");
        new Property(pso, PROP_DEPRECATION).setValue("True");
        new Property(pso, PROP_SOURCE_14ENABLE).setValue("True");
        new Property(pso, PROP_TARGET).setValue(sampleDir);
        String command = new Property(pso, PROP_EXTERNAL_COMPILER).getValue();
        new Property(pso, PROP_EXTERNAL_COMPILER).setValue(ECHO + " " + modifyCMD(command));
        new EventTool().waitNoEvent(500);
        try {
            PNGEncoder.captureScreen(getWorkDir().getAbsolutePath()+File.separator+"options1.png");
        } catch (Exception e1) {}

        

        new Property(pso, PROP_BOOTCLASSPATH).setValue("testbootclasspath");
        new Property(pso, PROP_BOOTCLASSPATH_REPLACE).setValue("-bcpreplace");
        new Property(pso, PROP_CLASSPATH).setValue("testclasspath");
        new Property(pso, PROP_DEBUGINFO_REPLACE).setValue("-debugreplace");
        new Property(pso, PROP_DEPRECATION_REPLACE).setValue("-depr");
        new Property(pso, PROP_OPTIMIZE_REPLACE).setValue("-opt");
        new Property(pso, PROP_D_REPLACE).setValue("-target");

        new EventTool().waitNoEvent(500);
        try {
            PNGEncoder.captureScreen(getWorkDir().getAbsolutePath()+File.separator+"options2.png");
        } catch (Exception e1) {}

        optionsOperator.close();
        
        String output = compile();
        assertEquals("-debugreplace -depr -opt -target " + sampleDir + " -encoding iso-8859-2 -source 1.4 -bcpreplace testbootclasspath -classpath testclasspath", cut(output, "Finished ColorPicker.").trim());
    }
    
    private String modifyCMD(String command) {
        String result = cut(command, "{jdk.home}{/}bin{/}javac");
        result = cut(result, "{filesystems}{:}");
        result = cut(result, "{files}");
        return result;
    }
    
    private String cut(String string, final String what) {
        return string.substring(0, string.indexOf(what)) + string.substring(string.indexOf(what) + what.length(), string.length());
    }
        
    
    private String compile() {
        ExplorerOperator.invoke().selectPageFilesystems();
        JavaNode colorPickerNode = new JavaNode(sampleDir + "|examples|colorpicker|ColorPicker");
        colorPickerNode.build();
        
        MainWindowOperator.getDefault().waitStatusText("Finished ColorPicker");
        OutputWindowOperator.invoke().selectCompilerPage();
        return OutputWindowOperator.invoke().getText();
    }
}
... 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.