Package org.jboss.serial.soaktest

Source Code of org.jboss.serial.soaktest.StringUtilTestCase$ThreadProducer

/*
* 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.serial.soaktest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import org.jboss.serial.DataSerializationTest;
import org.jboss.serial.data.NonSerializableTest;
import org.jboss.serial.io.JBossObjectInputStream;
import org.jboss.serial.io.JBossObjectOutputStream;

public class StringUtilTestCase extends DataSerializationTest
{
 
  static final boolean USE_JBSER=true;
 

  static Object staticOriginalData;
  static Exception currentException;
  public static synchronized void setException (Exception e)
  {
    if (e!=null)
    {
      e.printStackTrace(System.out);
    }
    currentException=e;
  }

  public static synchronized Exception getException ()
  {
    return currentException;
  }

  protected static final int TEST_THREADS = 10;
  protected static final int TEST_LOOP = 100;
 
 
  static class ThreadReader extends Thread
  {

    Object currentData;
    InputStream socketInput;
    BufferedInputStream bufferedInputStream;
    ObjectInputStream input;
   
   
    public ThreadReader(InputStream socketInput,Object currentData) throws Exception
    {
      this.socketInput = socketInput;
      bufferedInputStream = new BufferedInputStream(socketInput);
      if (USE_JBSER)
      {
        input = new JBossObjectInputStream(bufferedInputStream,(ClassLoader)null);
      }
      else
      {
        input = new ObjectInputStream(bufferedInputStream);
      }
      this.currentData=cloneUsingJavaSerializatoin(currentData);
    }

    public void run() {
      try
      {
        for (int i=0;i<TEST_LOOP;i++)
        {
          if (getException()!=null)
          {
            break;
          }
          Object newObject = input.readObject();
          if (currentData instanceof String[])
          {
            String[] origStr = (String[])currentData;
            String[] newStr = (String[])newObject;
            if (origStr.length!=newStr.length)
            {
              StringUtilTestCase.setException(new Exception("[" + i + "]=Data didn't match for " + currentData.getClass().getName()));
              break;
            }
            for (int strInt=0;strInt<origStr.length;strInt++)
            {
              if (!origStr[strInt].equals(newStr[strInt]))
              {
                StringUtilTestCase.setException(new Exception("[" + i + "]=Element " + strInt + " didn't match -> "
                     + origStr[strInt] + "!=" + newStr[strInt]));
                break;
              }
            }
          }
          else
          {
            if (!currentData.equals(newObject))
            {
              System.out.println("************************************** original  =" + currentData.toString() + "\n"+
                               "************************************** newObject =" + newObject.toString());
              StringUtilTestCase.setException(new Exception("Data didn't match for " + currentData.getClass().getName()));
            }
          }
        }
        input.close();
      }
      catch (Exception e)
      {
        setException(e);
      }
    }
   
  }
 
  static class ThreadProducer extends Thread
  {
    Object currentData;
   
    public ThreadProducer(OutputStream socketOut, Object data) throws Exception
    {
      this.currentData = cloneUsingJavaSerializatoin(data);
      this.socketOut=socketOut;

      buffOut = new BufferedOutputStream(socketOut,100*1024);

      if (USE_JBSER)
      {
        output = new JBossObjectOutputStream(buffOut);
      }
      else
      {
        output = new ObjectOutputStream(buffOut);
      }
      buffOut.flush();
    }

    OutputStream socketOut;
    BufferedOutputStream buffOut;
    BufferedInputStream buffInp;
    ObjectOutputStream output;
   
   
    public void run() {
      for (int i=0;i<TEST_LOOP;i++)
      {
        try
        {
          if (getException()!=null)
          {
            break;
          }
           
          output.writeObject(currentData);
          output.flush();
        }
        catch (Exception e)
        {
          setException(e);
          e.printStackTrace();
        }
      }
      try
      {
        output.close();
      }
      catch (Exception ignored)
      {
        ignored.printStackTrace();
      }
    }
   
  }
 
 
  public void executTest(Object obj) throws Throwable
  {
    if (obj instanceof NonSerializableTest)
    {
      return;
    }
    staticOriginalData=obj;
    try
    {
      ThreadGroup group = new ThreadGroup("test");
 
      System.out.println("Testing " + obj.getClass().getName());
 
      ThreadProducer producers[] = new ThreadProducer[TEST_THREADS];
      ThreadReader   readers[]   = new ThreadReader[TEST_THREADS];
     
      for (int i=0;i<TEST_THREADS;i++)
      {
        PipedInputStream pipedInput = new PipedInputStream();
        PipedOutputStream pipedOutput = new PipedOutputStream(pipedInput);
        producers[i] = new ThreadProducer(pipedOutput, staticOriginalData);
        readers[i] = new ThreadReader(pipedInput,staticOriginalData);
      }
     
      for (int i=0;i<TEST_THREADS;i++)
      {
        readers[i].start();
      }
     
      for (int i=0;i<TEST_THREADS;i++)
      {
        producers[i].start();
      }
     
      for (int i=0;i<TEST_THREADS;i++)
      {
        producers[i].join();
      }
     
      if (getException()!=null)
      {
        throw getException();
      }
     
      //Thread.sleep(1000);
     
     
    }
    catch (Throwable e)
    {
      e.printStackTrace(System.out);
      System.out.flush();
      throw e;
    }
    System.out.println("Test finished");
  }
 
}
TOP

Related Classes of org.jboss.serial.soaktest.StringUtilTestCase$ThreadProducer

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.