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.gui.web.warpackager; 

import org.netbeans.jellytools.JellyTestCase;
import org.netbeans.jellytools.Bundle;


import org.netbeans.web.test.nodes.WebInfNode;
import org.netbeans.web.test.ExtraFilesDialogOperator;

import org.netbeans.jellytools.OutputWindowOperator;
import org.netbeans.jellytools.NbDialogOperator;
import org.netbeans.jellytools.RuntimeTabOperator;
import org.netbeans.jellytools.ExplorerOperator;


import org.netbeans.jellytools.properties.Property;
import org.netbeans.jellytools.properties.TextFieldProperty;
import org.netbeans.jellytools.properties.PropertySheetTabOperator;
import org.netbeans.jellytools.properties.PropertySheetOperator;

import org.netbeans.junit.NbTestSuite;

import org.netbeans.jemmy.operators.JTextFieldOperator;
import org.netbeans.jemmy.operators.JTreeOperator;

import java.util.HashSet;
import java.util.jar.JarFile;
import java.util.jar.JarEntry;
import java.util.Enumeration;

/* */
public class ExtraFilesProperty extends JellyTestCase {
    private static String workDir = null;     
    private static String webModule = null;
    private static String wmName = "wm2";
    private static String fSep = System.getProperty("file.separator");
    private static String iSep = "|";
    private static String efTitle = null;
    private static String classes = null;
    private static String from1 = null;
    private static String from2 = null;
    private static String from3 = null;
    private static String to1 = null;
    private static String to2 = null;
    private static String to3 = null;
    private static ExplorerOperator explorer = null;
    private static String renameTitle = null;
    private static String renameLabel = null;
    private static String tempDir = null;
    //constructor required by JUnit
    public ExtraFilesProperty(java.lang.String testName) {
        super(testName);
    }
    
    //method allowing to execute test directly
    //from IDE for debugging    
    public static void main(java.lang.String[] args) {
        junit.textui.TestRunner.run(suite());
    }
         
    //method required by JUnit
    public static junit.framework.Test suite() {
        junit.framework.Test suite =  new NbTestSuite(ExtraFilesProperty.class);
	workDir = System.getProperty("warpackager.workdir").replace('/', fSep.charAt(0));
	tempDir = System.getProperty("nbjunit.workdir");
     	explorer = ExplorerOperator.invoke();
	explorer.selectPageFilesystems();
	webModule = workDir + fSep + wmName;
	efTitle = "Extra Files";
	return suite;
    }
    /**
       "Extra files property"
     */

    public void testExtraFiles1() {
	WebInfNode node1 = null;

        try {
	    System.out.println("Check mounts");
            node1 = new WebInfNode(webModule + "|WEB-INF");
        } catch (Exception e) {
            fail("Please check that all mounts are done properly");
            e.printStackTrace();
        }
	try {
	    node1.select();
            node1.properties();
        }catch(Exception e) {
            fail("Exception while try to rename WAR via popup: " + e);
        }
	
	String tab = "Archive"; //NOI18N
	String propName = Bundle.getString("org.netbeans.modules.web.war.Bundle", "PROP_ExtraFiles");
	PropertySheetOperator pso = new PropertySheetOperator("Properties of WEB-INF"); //NOI18N
	PropertySheetTabOperator psto = new PropertySheetTabOperator(pso, tab);
	Property ef  = new Property(psto,propName);
	try {
	    ef.getValue();
	} catch(Exception e) {
	    fail("Failed. Details in 32046");
	}
	ef.openEditor();
	ExtraFilesDialogOperator dialog = new ExtraFilesDialogOperator(efTitle);
	JTreeOperator tree = dialog.sourceTree();
	dialog.selectItem(workDir + "|extra|some_extra_jsp");
	dialog.add();
	dialog.ok();
	System.out.println("##EF" + new Property(psto,propName).getValue());
	ef.openEditor();
	dialog = new ExtraFilesDialogOperator(efTitle);
	dialog.selectItem(workDir + "|extra|extra");
	dialog.add();
	dialog.ok();
	System.out.println("##EF" + new Property(psto,propName).getValue());
	//NB: Check property value.
	/*
	  PROP_ExtraFiles=Extra Files
	  MSG_ExtraFiles_Zero=No Extra Files
	  MSG_ExtraFiles_Some={0} Extra Files

	 */
    }


    /** Add extra files - JSP
     */

    public void testExtraFiles3() {
	String[]  expectedContent = {"META-INF/MANIFEST.MF","WEB-INF/classes/pkg/Servlet2.class", "WEB-INF/classes/Servlet1.class", "WEB-INF/web.xml", "jsp/sample_jsp.jsp", "extra/some_extra_jsp.jsp","html/wm2.gif", "html/wm2_html.html", "images/wm2.gif", "data/JApplet.form", "data/JApplet.class", "data/JApplet.html"}; 
	String name = "ef3.war";
	String webInf = webModule + "|WEB-INF";
	String selection = workDir + "|extra|some_extra_jsp";
	testExtraFile(webInf, expectedContent, selection, name);
    }


    /** Add extra files - html
     */
    public void testExtraFiles4() {
	String[]  expectedContent = {"META-INF/MANIFEST.MF","WEB-INF/classes/pkg/Servlet2.class", "WEB-INF/classes/Servlet1.class", "WEB-INF/web.xml", "jsp/sample_jsp.jsp", "extra/extra_html.html","html/wm2.gif", "html/wm2_html.html", "images/wm2.gif", "data/JApplet.form", "data/JApplet.class", "data/JApplet.html"}; 
	String name = "ef4.war";
	String webInf = webModule + "|WEB-INF";
	String selection = workDir + "|extra|extra_html";
	testExtraFile(webInf, expectedContent, selection, name);
    }

     /** Add extra files - jar
     */
    public void testExtraFiles5() {
	String[]  expectedContent = {"META-INF/MANIFEST.MF","WEB-INF/classes/pkg/Servlet2.class", "WEB-INF/classes/Servlet1.class", "WEB-INF/web.xml", "jsp/sample_jsp.jsp", "extra/extra_jar.jar","html/wm2.gif", "html/wm2_html.html", "images/wm2.gif", "data/JApplet.form", "data/JApplet.class", "data/JApplet.html"}; 
	String name = "ef5.war";
	String webInf = webModule + "|WEB-INF";
	String selection = workDir + "|extra|extra_jar";
	testExtraFile(webInf, expectedContent, selection, name);
    }

    /** Add extra files - class
     */
    public void testExtraFiles6() {
	String[]  expectedContent = {"META-INF/MANIFEST.MF","WEB-INF/classes/pkg/Servlet2.class", "WEB-INF/classes/Servlet1.class", "WEB-INF/web.xml", "jsp/sample_jsp.jsp", "extra/ExtraClass.class","html/wm2.gif", "html/wm2_html.html", "images/wm2.gif", "data/JApplet.form", "data/JApplet.class", "data/JApplet.html"}; 
	String name = "ef6.war";
	String webInf = webModule + "|WEB-INF";
	String selection = workDir + "|extra|ExtraClass";
	testExtraFile(webInf, expectedContent, selection, name);
    }

    /* 
        Private methods.
	These methods are used in ExtraFiles and Filter tests. So may be they must be placed somewhere in Common .
     */


     private void exportWARFile(String webInf, String fullPath) {
	String title = Bundle.getString("org.netbeans.modules.web.war.actions.Bundle","CTL_ExportWarDialogTitle");
	String title2 = "Information"; //NOI18N
	new WebInfNode(webInf).export();
	NbDialogOperator dialog = new NbDialogOperator(title);
	new JTextFieldOperator(dialog,0).setText(fullPath);
	dialog.ok();
	try {
	    dialog = new NbDialogOperator(title2);
	    dialog.ok();
	}catch(Exception e) {
	    e.printStackTrace();
	    fail("WAR file not created. Information dialog not appear");
	}
    }
    private HashSet getWARContent(String fullPath) {
	HashSet hash = new HashSet();
	try {
	    JarFile jar = new JarFile(fullPath);
	    for (Enumeration e = jar.entries() ; e.hasMoreElements() ;) {
		hash.add(((JarEntry)e.nextElement()).getName());
	    }
	}catch(Exception e) {
	    e.printStackTrace();
	    fail("Can't check content of war file: " + fullPath);
	}
        return hash;
    }



 private void testExtraFile(String webInf, String[] expectedContent, String selection, String name) {
	WebInfNode node1 = null;
        try {
	    System.out.println("Check mounts");
            node1 = new WebInfNode(webInf);
        } catch (Exception e) {
            fail("Please check that all mounts are done properly");
            e.printStackTrace();
        }
	try {
	    node1.select();
            node1.properties();
        }catch(Exception e) {
            fail("Exception while try to rename WAR via popup: " + e);
        }
	
	String tab = "Archive"; //NOI18N
	String propName = Bundle.getString("org.netbeans.modules.web.war.Bundle", "PROP_ExtraFiles");
	PropertySheetOperator pso = new PropertySheetOperator("Properties of WEB-INF"); //NOI18N
	PropertySheetTabOperator psto = new PropertySheetTabOperator(pso, tab);
	Property ef  = new Property(psto,propName);
	ef.openEditor();
	ExtraFilesDialogOperator dialog = new ExtraFilesDialogOperator(efTitle);
	if(dialog.btRemoveAll().isEnabled()) {
	    dialog.removeAll();
	}
	dialog.selectItem(selection);
	dialog.add();
	dialog.ok();


	exportWARFile(webModule + "|WEB-INF",tempDir + fSep + name);
	HashSet content = getWARContent(tempDir + fSep + name);
	
	if(content.remove("WEB-INF/sun-web.xml")) { //Remove server-specific data 
	    System.out.println("S1S AS specific sun-web.xml was added");
	}
	
	if(content.size() != expectedContent.length) {
	    fail(content.size() + " files exported instead of " + expectedContent.length);
	} 
	for(int i=0; i< expectedContent.length;i++) {
	    if(!content.contains(expectedContent[i])) {
		fail("Item " + expectedContent[i] + " not found in exported WAR ");
	    }
	}

	//PASS

    }






}








... 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.