Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ElementValueGenTest.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*******************************************************************************
  2. * Copyright (c) 2004 IBM All rights reserved. This program and the accompanying
  3. * materials are made available under the terms of the Eclipse Public License
  4. * v1.0 which accompanies this distribution and is available at
  5. * http://www.eclipse.org/legal/epl-v10.html
  6. *
  7. * Contributors: Andy Clement - initial implementation
  8. ******************************************************************************/
  9. package org.aspectj.apache.bcel.classfile.tests;
  10. import java.io.ByteArrayInputStream;
  11. import java.io.ByteArrayOutputStream;
  12. import java.io.DataInputStream;
  13. import java.io.DataOutputStream;
  14. import java.io.IOException;
  15. import org.aspectj.apache.bcel.Constants;
  16. import org.aspectj.apache.bcel.generic.ClassGen;
  17. import org.aspectj.apache.bcel.classfile.ConstantPool;
  18. import org.aspectj.apache.bcel.classfile.annotation.ClassElementValue;
  19. import org.aspectj.apache.bcel.classfile.annotation.ElementValue;
  20. import org.aspectj.apache.bcel.classfile.annotation.EnumElementValue;
  21. import org.aspectj.apache.bcel.classfile.annotation.SimpleElementValue;
  22. import org.aspectj.apache.bcel.generic.ObjectType;
  23. public class ElementValueGenTest extends BcelTestCase {
  24. protected void setUp() throws Exception {
  25. super.setUp();
  26. }
  27. private ClassGen createClassGen(String classname) {
  28. return new ClassGen(classname, "java.lang.Object",
  29. "<generated>", Constants.ACC_PUBLIC | Constants.ACC_SUPER, null);
  30. }
  31. ////
  32. // Create primitive element values
  33. public void testCreateIntegerElementValue() {
  34. ClassGen cg = createClassGen("HelloWorld");
  35. ConstantPool cp = cg.getConstantPool();
  36. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_INT,cp,555);
  37. // Creation of an element like that should leave a new entry in the cpool
  38. assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+cp.lookupInteger(555),
  39. evg.getIndex()==cp.lookupInteger(555));
  40. checkSerialize(evg,cp);
  41. }
  42. public void testCreateFloatElementValue() {
  43. ClassGen cg = createClassGen("HelloWorld");
  44. ConstantPool cp = cg.getConstantPool();
  45. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_FLOAT,cp,111.222f);
  46. // Creation of an element like that should leave a new entry in the cpool
  47. assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+cp.lookupFloat(111.222f),
  48. evg.getIndex()==cp.lookupFloat(111.222f));
  49. checkSerialize(evg,cp);
  50. }
  51. public void testCreateDoubleElementValue() {
  52. ClassGen cg = createClassGen("HelloWorld");
  53. ConstantPool cp = cg.getConstantPool();
  54. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_DOUBLE,cp,333.44);
  55. // Creation of an element like that should leave a new entry in the cpool
  56. int idx = cp.lookupDouble(333.44);
  57. assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+idx,
  58. evg.getIndex()==idx);
  59. checkSerialize(evg,cp);
  60. }
  61. public void testCreateLongElementValue() {
  62. ClassGen cg = createClassGen("HelloWorld");
  63. ConstantPool cp = cg.getConstantPool();
  64. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_LONG,cp,3334455L);
  65. // Creation of an element like that should leave a new entry in the cpool
  66. int idx = cp.lookupLong(3334455L);
  67. assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+idx,
  68. evg.getIndex()==idx);
  69. checkSerialize(evg,cp);
  70. }
  71. public void testCreateCharElementValue() {
  72. ClassGen cg = createClassGen("HelloWorld");
  73. ConstantPool cp = cg.getConstantPool();
  74. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_CHAR,cp,(char)'t');
  75. // Creation of an element like that should leave a new entry in the cpool
  76. int idx = cp.lookupInteger((char)'t');
  77. assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+idx,
  78. evg.getIndex()==idx);
  79. checkSerialize(evg,cp);
  80. }
  81. public void testCreateByteElementValue() {
  82. ClassGen cg = createClassGen("HelloWorld");
  83. ConstantPool cp = cg.getConstantPool();
  84. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_CHAR,cp,(byte)'z');
  85. // Creation of an element like that should leave a new entry in the cpool
  86. int idx = cp.lookupInteger((byte)'z');
  87. assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+idx,
  88. evg.getIndex()==idx);
  89. checkSerialize(evg,cp);
  90. }
  91. public void testCreateBooleanElementValue() {
  92. ClassGen cg = createClassGen("HelloWorld");
  93. ConstantPool cp = cg.getConstantPool();
  94. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_BOOLEAN,cp,true);
  95. // Creation of an element like that should leave a new entry in the cpool
  96. int idx = cp.lookupInteger(1); // 1 == true
  97. assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+idx,
  98. evg.getIndex()==idx);
  99. checkSerialize(evg,cp);
  100. }
  101. public void testCreateShortElementValue() {
  102. ClassGen cg = createClassGen("HelloWorld");
  103. ConstantPool cp = cg.getConstantPool();
  104. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_SHORT,cp,(short)42);
  105. // Creation of an element like that should leave a new entry in the cpool
  106. int idx = cp.lookupInteger(42);
  107. assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+idx,
  108. evg.getIndex()==idx);
  109. checkSerialize(evg,cp);
  110. }
  111. ////
  112. // Create string element values
  113. public void testCreateStringElementValue() {
  114. // Create HelloWorld
  115. ClassGen cg = createClassGen("HelloWorld");
  116. ConstantPool cp = cg.getConstantPool();
  117. SimpleElementValue evg = new SimpleElementValue(ElementValue.STRING,cp,"hello");
  118. // Creation of an element like that should leave a new entry in the cpool
  119. assertTrue("Should have the same index in the constantpool but "+evg.getIndex()+"!="+cp.lookupUtf8("hello"),
  120. evg.getIndex()==cp.lookupUtf8("hello"));
  121. checkSerialize(evg,cp);
  122. }
  123. ////
  124. // Create enum element value
  125. public void testCreateEnumElementValue() {
  126. ClassGen cg = createClassGen("HelloWorld");
  127. ConstantPool cp = cg.getConstantPool();
  128. ObjectType enumType = new ObjectType("SimpleEnum"); // Supports rainbow :)
  129. EnumElementValue evg = new EnumElementValue(enumType,"Red",cp);
  130. // Creation of an element like that should leave a new entry in the cpool
  131. assertTrue("The new ElementValue value index should match the contents of the constantpool but "+
  132. evg.getValueIndex()+"!="+cp.lookupUtf8("Red"),
  133. evg.getValueIndex()==cp.lookupUtf8("Red"));
  134. //BCELBUG: Should the class signature or class name be in the constant pool? (see note in ConstantPool)
  135. // assertTrue("The new ElementValue type index should match the contents of the constantpool but "+
  136. // evg.getTypeIndex()+"!="+cp.lookupClass(enumType.getSignature()),
  137. // evg.getTypeIndex()==cp.lookupClass(enumType.getSignature()));
  138. checkSerialize(evg,cp);
  139. }
  140. ////
  141. // Create class element value
  142. public void testCreateClassElementValue() {
  143. ClassGen cg = createClassGen("HelloWorld");
  144. ConstantPool cp = cg.getConstantPool();
  145. ObjectType classType = new ObjectType("java.lang.Integer");
  146. ClassElementValue evg = new ClassElementValue(classType,cp);
  147. assertTrue("Unexpected value for contained class: '"+evg.getClassString()+"'",
  148. evg.getClassString().indexOf("Integer")!=-1);
  149. checkSerialize(evg,cp);
  150. }
  151. ////
  152. // Helper methods
  153. private void checkSerialize(ElementValue evgBefore,ConstantPool cpg) {
  154. try {
  155. String beforeValue = evgBefore.stringifyValue();
  156. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  157. DataOutputStream dos = new DataOutputStream(baos);
  158. evgBefore.dump(dos);
  159. dos.flush();
  160. dos.close();
  161. byte[] bs = baos.toByteArray();
  162. ByteArrayInputStream bais = new ByteArrayInputStream(bs);
  163. DataInputStream dis = new DataInputStream(bais);
  164. ElementValue evgAfter = ElementValue.readElementValue(dis,cpg);
  165. dis.close();
  166. String afterValue = evgAfter.stringifyValue();
  167. if (!beforeValue.equals(afterValue)) {
  168. fail("Deserialization failed: before='"+beforeValue+"' after='"+afterValue+"'");
  169. }
  170. } catch (IOException ioe) {
  171. fail("Unexpected exception whilst checking serialization: "+ioe);
  172. }
  173. }
  174. protected void tearDown() throws Exception {
  175. super.tearDown();
  176. }
  177. }