Package org.apache.kato.tests.junit

Source Code of org.apache.kato.tests.junit.JavaFieldTest

/*******************************************************************************
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.apache.kato.tests.junit;

import java.util.Iterator;

import javax.tools.diagnostics.image.CorruptDataException;
import javax.tools.diagnostics.image.MemoryAccessException;
import javax.tools.diagnostics.runtime.java.JavaClass;
import javax.tools.diagnostics.runtime.java.JavaClassLoader;
import javax.tools.diagnostics.runtime.java.JavaField;
import javax.tools.diagnostics.runtime.java.JavaObject;


public class JavaFieldTest extends AbstractImageTestcase
{
  private JavaObject _testInstance;
  private JavaField _field;
  private JavaField _anotherField;

  public static JavaField defaultJavaField(JavaClass clazz)
  {
    return (JavaField) clazz.getDeclaredFields().get(0);
  }
 
  protected void setUp() throws Exception
  {
    try {
      _testInstance = defaultJavaObject(false);
      Iterator declaredFields=_testInstance.getJavaClass().getDeclaredFields().iterator();
      _field = (JavaField)declaredFields.next(); //guaranteed to exist - see JavaObjectTest#defaultJavaObject()
      _anotherField=(JavaField)declaredFields.next(); //guaranteed to exist - see JavaObjectTest#defaultJavaObject()
      super.setUp();
    } catch (TestNotImplementedException e) {
      throw e;
    } catch (Throwable t) {
      //we weren't expecting any exceptions during startup so that is a test failure
      t.printStackTrace();
      fail();
    }
  }

  /**
   * Test method for 'javax.tools.diagnostics.runtime.java.JavaField.get(JavaObject)'
   *
   * This test will fail if a runtime exception is thrown (i.e. if _field does not represent a proper field of _testInstance)
   */
  public void testGet()
  {
    try {
      Object field = _field.get(_testInstance);
    } catch (CorruptDataException e) {
    } catch (MemoryAccessException e) {
      assertNotNull(e.getPointer());
    }
  }

  /**
   * Test method for 'javax.tools.diagnostics.runtime.java.JavaField.getBoolean(JavaObject)'
   *
   * Ensures that we succeed or get expected exceptions
   */
  public void testGetBoolean()
  {
    try {
      _field.getBoolean(_testInstance);
    } catch (IllegalArgumentException e) {
    } catch (CorruptDataException e) {
    } catch (MemoryAccessException e) {
      assertNotNull(e.getPointer());
    }
  }

  /**
   * Test method for 'javax.tools.diagnostics.runtime.java.JavaField.getByte(JavaObject)'
   *
   * Ensures that we succeed or get expected exceptions
   */
  public void testGetByte()
  {
    try {
      _field.getByte(_testInstance);
    } catch (IllegalArgumentException e) {
    } catch (CorruptDataException e) {
    } catch (MemoryAccessException e) {
      assertNotNull(e.getPointer());
    }
  }

  /**
   * Test method for 'javax.tools.diagnostics.runtime.java.JavaField.getChar(JavaObject)'
   *
   * Ensures that we succeed or get expected exceptions
   */
  public void testGetChar()
  {
    try {
      _field.getChar(_testInstance);
    } catch (IllegalArgumentException e) {
    } catch (CorruptDataException e) {
    } catch (MemoryAccessException e) {
      assertNotNull(e.getPointer());
    }
  }

  /**
   * Test method for 'javax.tools.diagnostics.runtime.java.JavaField.getDouble(JavaObject)'
   *
   * Ensures that we succeed or get expected exceptions
   */
  public void testGetDouble()
  {
    try {
      _field.getDouble(_testInstance);
    } catch (IllegalArgumentException e) {
    } catch (CorruptDataException e) {
    } catch (MemoryAccessException e) {
      assertNotNull(e.getPointer());
    }
  }

  /**
   * Test method for 'javax.tools.diagnostics.runtime.java.JavaField.getFloat(JavaObject)'
   *
   * Ensures that we succeed or get expected exceptions
   */
  public void testGetFloat()
  {
    try {
      _field.getFloat(_testInstance);
    } catch (IllegalArgumentException e) {
    } catch (CorruptDataException e) {
    } catch (MemoryAccessException e) {
      assertNotNull(e.getPointer());
    }
  }

  /**
   * Test method for 'javax.tools.diagnostics.runtime.java.JavaField.getInt(JavaObject)'
   *
   * Ensures that we succeed or get expected exceptions
   */
  public void testGetInt()
  {
    try {
      _field.getInt(_testInstance);
    } catch (IllegalArgumentException e) {
    } catch (CorruptDataException e) {
    } catch (MemoryAccessException e) {
      assertNotNull(e.getPointer());
    }
  }

  /**
   * Test method for 'javax.tools.diagnostics.runtime.java.JavaField.getLong(JavaObject)'
   *
   * Ensures that we succeed or get expected exceptions
   */
  public void testGetLong()
  {
    try {
      _field.getLong(_testInstance);
    } catch (IllegalArgumentException e) {
    } catch (CorruptDataException e) {
    } catch (MemoryAccessException e) {
      assertNotNull(e.getPointer());
    }
  }

  /**
   * Test method for 'javax.tools.diagnostics.runtime.java.JavaField.getShort(JavaObject)'
   *
   * Ensures that we succeed or get expected exceptions
   */
  public void testGetShort()
  {
    try {
      _field.getShort(_testInstance);
    } catch (IllegalArgumentException e) {
    } catch (CorruptDataException e) {
    } catch (MemoryAccessException e) {
      assertNotNull(e.getPointer());
    }
  }

  /**
   * Test method for 'javax.tools.diagnostics.runtime.java.JavaField.getString(JavaObject)'
   *
   * Ensures that we succeed or get expected exceptions
   */
  public void testGetString()
  {
    try {
      String string = _field.getString(_testInstance);
      assertNotNull(string);
    } catch (IllegalArgumentException e) {
    } catch (CorruptDataException e) {
    } catch (MemoryAccessException e) {
      assertNotNull(e.getPointer());
    }
  }

  /**
   * Verify that the equals call doesn't throw
   */
  public void testEquals()
  {
    try{
      assertFalse(_field.equals(_anotherField));
      assertTrue(_field.equals(_field));
    } catch (Throwable t) {
      assertTrue(false);
    }
  }
 
  /**
   * Verify that hashCode() doesn't throw and returns non-zero (technically zero is ok but it will be
   * flagged here to ensure that we aren't doing anything bad to create the hashcode)
   */
  public void testHashCode()
  {
    try {
      assertTrue(0 != _testInstance.hashCode());
    } catch (Throwable t) {
      assertTrue(false);
    }
  }
 
  /**
   * This test is to cover an issue where a static field is not parsed correctly.  It is very specialized
   * because of this interest in very special information.
   * It looks up the PI constant on java.lang.Math and compares it to the running VM's notion of that constant
   */
  public void testJavaStaticFieldDouble()
  {
    JavaClass math = null;
    Iterator loaders = defaultJavaRuntime().getJavaClassLoaders().iterator();
   
    while (loaders.hasNext()) {
      JavaClassLoader loader = (JavaClassLoader) loaders.next();
      try {
        for (Iterator iter = loader.getDefinedClasses().iterator(); iter.hasNext();) {
          JavaClass clazz = (JavaClass) iter.next();
          if (clazz.getName().equals("java/lang/Math")) {
            math = clazz;
            break;
          }
        }
      } catch (CorruptDataException e) {
        // Ignore.
      }
    }
   
    if (null != math) {
      Iterator fields = math.getDeclaredFields().iterator();
      while (fields.hasNext()) {
        JavaField field = (JavaField) fields.next();
        try {
          if (field.getName().equals("PI")) {
            double pi = field.getDouble(null);
            assertTrue(Math.PI == pi);
          }
        } catch (CorruptDataException e) {
          //ignore
        } catch (MemoryAccessException e) {
          assertNotNull(e.getPointer());
          //fail
          assertTrue(false);
        }
      }
    } else {
      throw new TestNotImplementedException();
    }
  }
}
TOP

Related Classes of org.apache.kato.tests.junit.JavaFieldTest

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.