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.

DefaultCacheKeyResolverTest.java 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*******************************************************************************
  2. * Copyright (c) 2012 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * John Kew (vmware) initial implementation
  11. *******************************************************************************/
  12. package org.aspectj.weaver.tools.cache;
  13. import java.net.URL;
  14. import java.net.URLClassLoader;
  15. import java.util.Arrays;
  16. import java.util.Collections;
  17. import junit.framework.TestCase;
  18. /**
  19. */
  20. public class DefaultCacheKeyResolverTest extends TestCase {
  21. byte[] FAKE_BYTES = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  22. String FAKE_CLASS = "com.example.foo.Bar";
  23. DefaultCacheKeyResolver resolver = new DefaultCacheKeyResolver();
  24. class BasicTestCL extends ClassLoader {
  25. public BasicTestCL () {
  26. super();
  27. }
  28. }
  29. class URLTestCL extends URLClassLoader {
  30. public URLTestCL(URL... urls) {
  31. super(urls);
  32. }
  33. }
  34. public void testNonURLClassLoaderScope() throws Exception {
  35. String scope = resolver.createClassLoaderScope(new BasicTestCL(), Collections.<String>emptyList());
  36. assertTrue(scope.startsWith(BasicTestCL.class.getSimpleName()));
  37. }
  38. public void testCreateURLClassLoaderScope() throws Exception {
  39. URL testURLA = new URL("http://example.com");
  40. URL testURLB = new URL("file:///tmp");
  41. URL testURLC = new URL("ftp://ftp.example.com");
  42. URLTestCL A = new URLTestCL(testURLA);
  43. URLTestCL AB = new URLTestCL(testURLA, testURLB);
  44. URLTestCL BC = new URLTestCL(testURLB, testURLC);
  45. URLTestCL BC2 = new URLTestCL(testURLC, testURLB);
  46. String[] a = {"one", "two", "three", "four"};
  47. String[] a2 = {"one", "two", "three"};
  48. String scopeAa = resolver.createClassLoaderScope(A, Arrays.asList(a));
  49. String scopeABa = resolver.createClassLoaderScope(AB, Arrays.asList(a));
  50. String scopeBCa = resolver.createClassLoaderScope(BC, Arrays.asList(a));
  51. String scopeBC2a = resolver.createClassLoaderScope(BC2, Arrays.asList(a));
  52. String scopeAa2 = resolver.createClassLoaderScope(A, Arrays.asList(a2));
  53. String scopeABa2 = resolver.createClassLoaderScope(AB, Arrays.asList(a2));
  54. String scopeBCa2 = resolver.createClassLoaderScope(BC, Arrays.asList(a2));
  55. String scopeBC2a2 = resolver.createClassLoaderScope(BC2, Arrays.asList(a2));
  56. assertFalse(scopeAa.equals(scopeABa));
  57. assertFalse(scopeAa.equals(scopeBCa));
  58. assertFalse(scopeABa.equals(scopeBCa));
  59. assertTrue(scopeBC2a.equals(scopeBCa));
  60. assertFalse(scopeAa.equals(scopeAa2));
  61. assertFalse(scopeABa.equals(scopeABa2));
  62. assertFalse(scopeBCa.equals(scopeBCa2));
  63. assertFalse(scopeBC2a.equals(scopeBC2a2));
  64. }
  65. public void testCreateGeneratedCacheKey() throws Exception {
  66. CachedClassReference ref = resolver.generatedKey(FAKE_CLASS);
  67. assertTrue(ref.getKey().startsWith(FAKE_CLASS));
  68. assertTrue(ref.getKey().matches(resolver.getGeneratedRegex()));
  69. assertEquals(FAKE_CLASS, resolver.keyToClass(ref.getKey()));
  70. }
  71. public void testCreateCacheKey() throws Exception {
  72. // crc hashing
  73. CachedClassReference ref = resolver.weavedKey(FAKE_CLASS, FAKE_BYTES);
  74. assertTrue("key " + ref.getKey() + " does not match " + resolver.getWeavedRegex(), ref.getKey().matches(resolver.getWeavedRegex()));
  75. String className = resolver.keyToClass(ref.getKey());
  76. assertEquals("class " + FAKE_CLASS + " != " + className, FAKE_CLASS, className);
  77. }
  78. }