Package org.jboss.jopr.jsfunit.as5

Source Code of org.jboss.jopr.jsfunit.as5.JBossASNodeTest

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package org.jboss.jopr.jsfunit.as5;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import java.io.IOException;
import java.util.Properties;
import javax.management.JMException;
import org.apache.commons.lang.math.NumberUtils;
import org.jboss.jopr.jsfunit.AppConstants;
import org.jboss.jopr.jsfunit.DebugUtils;
import org.jboss.jopr.jsfunit.EmbjoprTestCase;
import org.jboss.jopr.jsfunit.JMXUtils;
import org.jboss.jopr.jsfunit.exceptions.EmbJoprTestException;
import org.jboss.jopr.jsfunit.util.EmbJoprTestToolkit.ContentInfoTable;
import org.jboss.jopr.jsfunit.util.EmbJoprTestToolkit.ContentTable;

/**
*
* @author ondra
*/
public class JBossASNodeTest extends EmbjoprTestCase {

  public void testASNodeMetricsTab() throws EmbJoprTestException, IOException, JMException {

    String jbossConfig = ejtt.getJBossConfig();

    // Go to the AS node.
    String nodeText = String.format(AS_NODE_NAME_FORMAT, jbossConfig);
    ejtt.getNavTree().getNodeByLabel(nodeText, true).click();


    // JBoss App Server:${config} node
    String pageText = client.getPageAsText();

    // Whole page contains
    String pageHeaderText = String.format(AS_NODE_NAME_FORMAT, jbossConfig);
    //assertTrue("Page doesn't contain the header: "+headerText, pageText.contains(headerText));
    assertTrue("Content doesn't contain the header: "+pageHeaderText,
            client.getElement("content").getTextContent().contains(pageHeaderText));

    ContentTable table;
    ContentInfoTable infoTable;
    String headerText;
    Properties properties;

    JMXUtils jmxUtils = JMXUtils.getInstanceForLocalJBoss();


    /*
     * General Properties
        Name:JBoss AS 5 (default)
        Version:5.1.0.CR1
        Description:JBoss Application Server
     */
    headerText = "General Properties";
    assertTrue("Page doesn't contain the header: "+headerText, pageText.contains(headerText));
    table = ejtt.getTabMenu().getTabContentBox().getTableUnderHeader(headerText);
    infoTable = ejtt.getContentInfoTable(table.getElement());
    properties = infoTable.getProperties();

    assertEquals("General Properties / Name", pageHeaderText, properties.getProperty("Name") );

    String versionPrefix = "5.1";
    String jmxMBeanName = "jboss.system:type=Server";
    versionPrefix = (String) jmxUtils.getMBeanAttribute(jmxMBeanName, "VersionNumber");

    assertTrue("General Properties / Version startsWith('"+versionPrefix+"')",
            properties.getProperty("Version","~not found~").startsWith(versionPrefix) );
    assertEquals("General Properties / Description", AS_NODE_DESCRIPTION, properties.get("Description") );


    /*
     * Resource Traits
        Server Name: default
        Server Home Dir: /home/brq/ozizka/.../jboss-as-5.x/build/output/jboss-5.1.0.CR1/server/default
        Version Name: The Oracle
        Build Date: April 22 2009
        Start Date: Wed Apr 22 18:26:43 CEST 2009
     */
    headerText = AppConstants.LABEL_TRAITS;
    assertTrue("Page doesn't contain the header: "+headerText, pageText.contains(headerText));
    table = ejtt.getTabMenu().getTabContentBox().getTableUnderHeader(headerText);
    infoTable = ejtt.getContentInfoTable(table.getElement());
    properties = infoTable.getProperties();

    assertEquals(AppConstants.LABEL_TRAITS+" / Server Name", ejtt.getJBossConfig(),
            properties.getProperty("Server Name"));

    assertEquals(AppConstants.LABEL_TRAITS+" / Server Home Dir",
            //String.format("%s/server/%s", ejtt.getJBossHomeDir(), ejtt.getJBossConfig()),
            ejtt.getJBossHomeDir(),
            properties.getProperty("Server Home Dir"));


    /*
     * Metrics Summary
        Name  Value  Description
        Active Thread Count  7.0  The current number of active threads for this app server instance
        JVM Free Memory  183.6MB  The amount of free memory for the JVM this app server instance is running on
        JVM Total Memory  218.7MB  The amount of total memory for the JVM this app server instance is running on

        TODO: Split - grab some resources and do a page refresh for every part?
     */
    headerText = AppConstants.LABEL_NUMERIC_METRICS;
    assertTrue("Page doesn't contain the header: "+headerText, pageText.contains(headerText));
    table = ejtt.getTabMenu().getTabContentBox().getTableUnderHeader(headerText);

    // Get values as properties.
    table.analyzeColumns();
    Properties metricsProps = table.getProperties("Name", "Value");
    log.info( headerText +" 1st: "+ metricsProps.toString() );

    // Parse the values.
    int threads1 = (int)NumberUtils.toDouble(metricsProps.getProperty("Active Thread Count").replace(',', '.'));
    double memFree1 = NumberUtils.toDouble(metricsProps.getProperty("JVM Free Memory").replace(',', '.').replace("MB", ""));
    double memTotal1 = NumberUtils.toDouble(metricsProps.getProperty("JVM Total Memory").replace(',', '.').replace("MB", ""));



    // Do some requests to add threads.
    // Bad way - thread pool can cause thread number not to change.
    /*
    // Create HTMLUnit WebClient.
    WebClient wc = new WebClient(BrowserVersion.FIREFOX_3);
    wc.setCssEnabled(false);
    wc.setJavaScriptEnabled(false);
    // Get some page.
    wc.getPage("http://localhost:8080/");
     */

    // Launch as much threads as the current number to get higher number.
    log.info("Starting "+threads1+" threads.");
    ThreadGroup threadGroup = new ThreadGroup("Dummy threads");
    for( int i = 0; i < threads1; i++ ) {
      log.debug("Starting thread 'Dummy thread "+i+"'");
      Thread thread = new Thread(threadGroup, new MyRunnable(), "Dummy thread "+i);
      thread.start();
    }
   


    // Allocate some memory to decrease free mem.
    log.info("Allocating 500 MB of ram.");
    byte tmp[] = new byte[500 * 1024 * 1024]; // Few MB.
    tmp[0] = 1;
    tmp[tmp.length-1] = 1;



    // Refresh the page
    ((HtmlPage)client.getContentPage()).refresh();

    // Stop the threads.
    threadGroup.interrupt();


    // Read new values.
    headerText = "Metrics Summary";
    assertTrue("Page doesn't contain the header: "+headerText, pageText.contains(headerText));
    table = ejtt.getTabMenu().getTabContentBox().getTableUnderHeader(headerText);

    // Get values as properties.
    table.analyzeColumns();
    metricsProps = table.getProperties("Name", "Value");
    log.info( headerText +" 2nd: "+ metricsProps.toString() );

    // Parse the values.
    int threads2 = (int)NumberUtils.toDouble( metricsProps.getProperty("Active Thread Count").replace(',', '.') );
    double memFree2 = NumberUtils.toDouble(metricsProps.getProperty("JVM Free Memory").replace(',', '.').replace("MB", "") );
    double memTotal2 = NumberUtils.toDouble(metricsProps.getProperty("JVM Total Memory").replace(',', '.').replace("MB", "") );

    log.info(               "Value      |  old  |  new ");
    log.info( String.format("Threads:     %d,  %d", threads1, threads2) );
    log.info( String.format("Mem free:    %f,  %f", memFree1, memFree2) );
    log.info( String.format("Mem total:   %f,  %f", memTotal1, memTotal2) );

    assertTrue("New thread count should be > old. Is: "+threads1 +" -> "+ threads2,  threads1 < threads2);
    assertTrue("New free mem should be != old. Is: "+memFree1 +" -> "+ memFree2,      memFree1 != memFree2);
    //assertTrue("New total mem should be ?? old. Is: "+memTotal1 +","+ memTotal2,    memTotal1 == memTotal2);

  }



  /** Runnable for creating threads to increase JVM thread count. */
  class MyRunnable implements Runnable {
    public void run() {
      ejtt.sleep(20000); // Ends on InterruptedException.
    }
  }
 


}// class



TOP

Related Classes of org.jboss.jopr.jsfunit.as5.JBossASNodeTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.