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.

SimpleClassCacheTest.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * Abraham Nevado (lucierna) initial implementation
  11. ********************************************************************************/
  12. package org.aspectj.weaver.tools.cache;
  13. import junit.framework.TestCase;
  14. import java.io.File;
  15. public class SimpleClassCacheTest extends TestCase {
  16. byte[] FAKE_BYTES_V1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  17. byte[] FAKE_BYTES_V2 = { 1, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  18. byte[] FAKE_WOVEN_BYTES_V1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  19. byte[] FAKE_WOVEN_BYTES_V2 = { 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  20. private SimpleCache createCache() {
  21. return new SimpleCache(System.getProperty("java.io.tmpdir"), true);
  22. }
  23. public void testCache() {
  24. String classA = "com.generated.A";
  25. SimpleCache cache = createCache();
  26. cache.put(classA, FAKE_BYTES_V1, FAKE_WOVEN_BYTES_V1);
  27. // Returned woven bytes are the original ones
  28. byte[] result = cache.getAndInitialize(classA, FAKE_BYTES_V1, null, null).orElse(null);
  29. assertNotNull(result);
  30. for (int i = 0; i < result.length; i++)
  31. assertEquals(
  32. "Cached version byte[" + i + "] should be equal to the original woven class",
  33. result[i], FAKE_WOVEN_BYTES_V1[i]
  34. );
  35. // Class is properly backed up
  36. File f = new File(System.getProperty("java.io.tmpdir") + File.separator + "com.generated.A-1164760902");
  37. assertTrue(
  38. "Class should be backed up with CRC 1164760902",
  39. f.exists()
  40. );
  41. }
  42. public void testDifferentVersionCache() {
  43. String classA = "com.generated.A";
  44. SimpleCache cache = createCache();
  45. cache.put(classA, FAKE_BYTES_V1, FAKE_WOVEN_BYTES_V1);
  46. cache.put(classA, FAKE_BYTES_V2, FAKE_WOVEN_BYTES_V2);
  47. // Returned woven bytes are the original ones for v1
  48. byte[] result = cache.getAndInitialize(classA, FAKE_BYTES_V1, null, null).orElse(null);
  49. assertNotNull(result);
  50. for (int i = 0; i < result.length; i++)
  51. assertEquals(
  52. "Cached version v1 byte[" + i + "] should be equal to the original woven class",
  53. result[i], FAKE_WOVEN_BYTES_V1[i]
  54. );
  55. // Returned woven bytes are the original ones for v2
  56. result = cache.getAndInitialize(classA, FAKE_BYTES_V2, null, null).orElse(null);
  57. assertNotNull(result);
  58. for (int i = 0; i < result.length; i++)
  59. assertEquals(
  60. "Cached version v2 byte[" + i + "] should be equal to the original woven class",
  61. result[i], FAKE_WOVEN_BYTES_V2[i]
  62. );
  63. }
  64. public void testCacheMiss() {
  65. String classA = "com.generated.A";
  66. SimpleCache cache = createCache();
  67. // Woven bytes not found in cache
  68. assertNull(cache.getAndInitialize(classA, FAKE_BYTES_V1, null, null));
  69. }
  70. public void testCacheHitUnwoven() {
  71. String classA = "com.generated.A";
  72. SimpleCache cache = createCache();
  73. cache.put(classA, FAKE_BYTES_V1, SimpleCache.SAME_BYTES);
  74. // Returned woven bytes are null, indicating an unwoven class
  75. byte[] result = cache.getAndInitialize(classA, FAKE_BYTES_V1, null, null).orElse(null);
  76. assertNull(result);
  77. }
  78. }