You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SignatureTest.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*******************************************************************************
  2. * Copyright (c) 2004 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.aspectj.runtime.reflect;
  12. import java.lang.ref.Reference;
  13. import java.lang.reflect.Field;
  14. import junit.framework.TestCase;
  15. /**
  16. */
  17. public class SignatureTest extends TestCase {
  18. public void testGetDeclaringTypeName() {
  19. FieldSignatureImpl fsi = new FieldSignatureImpl(0,"x",SignatureTest.class,String.class);
  20. assertEquals(SignatureTest.class.getName(),fsi.getDeclaringTypeName());
  21. assertSame(fsi.getDeclaringTypeName(),fsi.getDeclaringTypeName()); // should be cached.
  22. }
  23. public void testToShortMiddleLongString () {
  24. MethodSignatureImpl msi = new MethodSignatureImpl(0,"test",SignatureTest.class,new Class[] { String.class, Integer.TYPE }, new String[] { "s", "i" }, new Class[] {}, Runnable.class);
  25. String shortString = msi.toShortString();
  26. assertSame(shortString,msi.toShortString()); // should be cached.
  27. String middleString = msi.toString();
  28. assertSame(middleString,msi.toString()); // should be cached.
  29. String longString = msi.toLongString();
  30. assertSame(longString,msi.toLongString()); // should be cached.
  31. assertTrue("String representations should be different",!(shortString.equals(middleString) || middleString.equals(longString) || longString.equals(shortString)));
  32. }
  33. public void testClearCache() throws Exception {
  34. MethodSignatureImpl msi = new MethodSignatureImpl(0,"test",SignatureTest.class,new Class[] { String.class, Integer.TYPE }, new String[] { "s", "i" }, new Class[] {}, Runnable.class);
  35. String shortString = msi.toShortString();
  36. assertSame(shortString,msi.toShortString());
  37. Field field = SignatureImpl.class.getDeclaredField("stringCache");
  38. field.setAccessible(true);
  39. Object res = field.get(msi);
  40. field = res.getClass().getDeclaredField("toStringCacheRef");
  41. field.setAccessible(true);
  42. Reference ref = (Reference)field.get(res);
  43. ref.clear();
  44. assertEquals(shortString,msi.toShortString());
  45. String longString = msi.toLongString();
  46. assertSame(longString,msi.toLongString()); // should be cached.
  47. }
  48. }