Package org.jboss.profiler.aop.logger

Source Code of org.jboss.profiler.aop.logger.FileProfileLoggerTest

/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.profiler.aop.logger;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.jboss.profiler.aop.logger.FileProfileLogger;
import org.jboss.profiler.fileProcessor.SpyFileReader;
import org.jboss.profiler.fileProcessor.SpyReader;
import org.jboss.profiler.fileProcessor.SpyRegister;
import org.jboss.profiler.fileProcessor.SpyRegisterBeginThread;
import org.jboss.profiler.fileProcessor.SpyRegisterEndThread;
import org.jboss.profiler.fileProcessor.SpyRegisterEnterMethod;
import org.jboss.profiler.fileProcessor.SpyRegisterExitMethod;
import org.jboss.profiler.fileProcessor.SpyRegisterFreeObject;
import org.jboss.profiler.fileProcessor.SpyRegisterLoadClass;
import org.jboss.profiler.fileProcessor.SpyRegisterLoadMethod;
import org.jboss.profiler.fileProcessor.SpyRegisterLoadObject;

import junit.framework.TestCase;

/**
* This test will test {@link FileProfileLogger} and regular package input files processors as well.
* @author Clebert Suconic
*/
public class FileProfileLoggerTest extends TestCase {


    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
    }

    /*
     * @see TestCase#tearDown()
     */
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testEnterMethodFormats() throws Exception {
        ByteArrayOutputStream arrayOutput = new ByteArrayOutputStream(1024);
        FileProfileLogger logger = new FileProfileLogger(arrayOutput);

        for (int i=0;i<100;i++) {
            logger.enterMethod(1,1,1,1);
        }

        ByteArrayInputStream input = new ByteArrayInputStream(arrayOutput.toByteArray());
        SpyFileReader reader = new SpyFileReader(input,false);

        for (int i=0;i<100;i++) {
          boolean result = reader.readSpyRegister();
          assertTrue(result);
          if (!result) return;

          assertTrue("Expected to be an enterMethod",reader.getCurrentRegister() instanceof SpyRegisterEnterMethod);

          SpyRegisterEnterMethod enterMethod = (SpyRegisterEnterMethod)reader.getCurrentRegister();

          assertEquals("classId",enterMethod.getClassId(),1);
          assertEquals("methodId",enterMethod.getMethodId(),1);
          assertEquals("time",enterMethod.getTime_t(),1);
        }
    }

    public void testExitMethodFormats() throws Exception {
        ByteArrayOutputStream arrayOutput = new ByteArrayOutputStream(1024);
        FileProfileLogger logger = new FileProfileLogger(arrayOutput);

        for (int i=0;i<100;i++) {
            logger.exitMethod(1,1,1);
        }

        ByteArrayInputStream input = new ByteArrayInputStream(arrayOutput.toByteArray());
        SpyFileReader reader = new SpyFileReader(input,false);

        for (int i=0;i<100;i++) {
          boolean result = reader.readSpyRegister();
          assertTrue(result);
          if (!result) return;

          assertTrue("Expected to be an exitMethod",reader.getCurrentRegister() instanceof SpyRegisterExitMethod);

          SpyRegisterExitMethod exitMethod = (SpyRegisterExitMethod)reader.getCurrentRegister();

          assertEquals("methodId",exitMethod.getMethodId(),1);
          assertEquals("time",exitMethod.getTime_t(),1);
        }
    }

    public void testLoadClassFormats() throws Exception {
        ByteArrayOutputStream arrayOutput = new ByteArrayOutputStream(1024);
        FileProfileLogger logger = new FileProfileLogger(arrayOutput);

        for (int i=0;i<100;i++) {
            logger.loadClass(1,1,this.getClass().getName(),10);
            for (int j=0;j<10;j++) logger.loadMethod(1,1,1,"method" +j,"()");
        }

        ByteArrayInputStream input = new ByteArrayInputStream(arrayOutput.toByteArray());
        SpyFileReader reader = new SpyFileReader(input,false);

        for (int i=0;i<100;i++) {
          boolean result = reader.readSpyRegister();
          assertTrue(result);
          if (!result) return;

          assertTrue("Expected to be an loadClass",reader.getCurrentRegister() instanceof SpyRegisterLoadClass);

          SpyRegisterLoadClass object = (SpyRegisterLoadClass)reader.getCurrentRegister();

          assertEquals("classId",object.getClassId(),1);
          assertEquals("time",object.getTime_t(),1);
          assertEquals("className",object.getClassName(),this.getClass().getName());

          for (int j=0;j<10;j++) {
              result = reader.readSpyRegister();
              assertTrue(result);
              assertTrue("instanceof SpyRegisterLoadMethod", reader.getCurrentRegister() instanceof SpyRegisterLoadMethod);

              SpyRegisterLoadMethod loadMethodObject = (SpyRegisterLoadMethod) reader.getCurrentRegister();
              assertEquals("method" + j, loadMethodObject.getMethodName());
              assertEquals("()",loadMethodObject.getSignature());
          }
        }
    }

    public void testNewObject() throws Exception {
        ByteArrayOutputStream arrayOutput = new ByteArrayOutputStream(1024);
        FileProfileLogger logger = new FileProfileLogger(arrayOutput);

        for (int i=0;i<100;i++) {
            logger.newObject(1,1,1,1,1);
            logger.releaseObject(1,1);
        }

        ByteArrayInputStream input = new ByteArrayInputStream(arrayOutput.toByteArray());
        SpyFileReader reader = new SpyFileReader(input,false);

        for (int i=0;i<100;i++) {
          boolean result = reader.readSpyRegister();
          assertTrue(result);

          assertTrue("Expected to be an objectAlloc",reader.getCurrentRegister() instanceof SpyRegisterLoadObject);

          SpyRegisterLoadObject tmp = (SpyRegisterLoadObject)reader.getCurrentRegister();

          assertEquals("threadId",1,tmp.getThreadId());
          assertEquals("classId",1,tmp.getClassId());
          assertEquals("objectId",1,tmp.getObjectId());

          result = reader.readSpyRegister();
          assertTrue(result);

          assertTrue("objectRelease",reader.getCurrentRegister() instanceof SpyRegisterFreeObject);
        }
    }

    public void testThread() throws Exception {
        ByteArrayOutputStream arrayOutput = new ByteArrayOutputStream(1024);
        FileProfileLogger logger = new FileProfileLogger(arrayOutput);

        for (int i=0;i<100;i++) {
            logger.startThread(1,1,"test"+i);
            logger.finishThread(1,1);
        }

        ByteArrayInputStream input = new ByteArrayInputStream(arrayOutput.toByteArray());
        SpyFileReader reader = new SpyFileReader(input,false);

        for (int i=0;i<100;i++) {
          boolean result = reader.readSpyRegister();
          assertTrue(result);

          assertTrue("Expected to be an threadStart",reader.getCurrentRegister() instanceof SpyRegisterBeginThread);

          SpyRegisterBeginThread tmp = (SpyRegisterBeginThread)reader.getCurrentRegister();

          assertEquals("threadId",1,tmp.getThreadId());
          assertEquals("envId",1,tmp.getEnvId());
          assertEquals("name","test"+i,tmp.getThreadName());

          result = reader.readSpyRegister();
          assertTrue(result);

          assertTrue("Expected to be an threadStart",reader.getCurrentRegister() instanceof SpyRegisterEndThread);
          SpyRegisterEndThread tmp2 = (SpyRegisterEndThread)reader.getCurrentRegister();

          assertEquals("threadId",1,tmp2.getThreadId());
        }
    }


    public void testHeader() throws Exception {
        ByteArrayOutputStream arrayOutput = new ByteArrayOutputStream(1024);
        FileProfileLogger logger = new FileProfileLogger(arrayOutput);

        logger.headerSignature(100);
        for (int i=0;i<100;i++) {
            logger.loadClass(1,1,"test",10);
        }

        ByteArrayInputStream input = new ByteArrayInputStream(arrayOutput.toByteArray());
        SpyReader reader = new SpyReader(input, new InputStream[] {});

        for (int i=0;i<100;i++) {
          SpyRegister register = reader.getBestRegister();
          assertNotNull(register);

          assertTrue("Expected to be an loadClass",register instanceof SpyRegisterLoadClass);

          reader.readNextRegister();
        }

    }

}
TOP

Related Classes of org.jboss.profiler.aop.logger.FileProfileLoggerTest

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.