|
| 1 | +package com.taobao.arthas.core.util; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import java.util.regex.Pattern; |
| 8 | +import java.util.regex.PatternSyntaxException; |
| 9 | + |
| 10 | +/** |
| 11 | + * RegexCacheManager测试类 |
| 12 | + */ |
| 13 | +public class RegexCacheManagerTest { |
| 14 | + private RegexCacheManager cacheManager; |
| 15 | + |
| 16 | + @Before |
| 17 | + public void setUp() { |
| 18 | + // 获取单例实例 |
| 19 | + cacheManager = RegexCacheManager.getInstance(); |
| 20 | + // 清理缓存,确保测试环境干净 |
| 21 | + cacheManager.clearCache(); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * 测试基本缓存功能 |
| 26 | + */ |
| 27 | + @Test |
| 28 | + public void testBasicCacheFunctionality() { |
| 29 | + // 测试缓存未命中的情况 |
| 30 | + String regex1 = ".*Test.*"; |
| 31 | + Pattern pattern1 = cacheManager.getPattern(regex1); |
| 32 | + Assert.assertNotNull(pattern1); |
| 33 | + Assert.assertEquals(1, cacheManager.getCacheSize()); |
| 34 | + |
| 35 | + // 测试缓存命中的情况 |
| 36 | + Pattern pattern1Cached = cacheManager.getPattern(regex1); |
| 37 | + Assert.assertNotNull(pattern1Cached); |
| 38 | + Assert.assertSame(pattern1, pattern1Cached); // 应该是同一个对象 |
| 39 | + Assert.assertEquals(1, cacheManager.getCacheSize()); // 缓存大小应该保持不变 |
| 40 | + |
| 41 | + // 测试多个正则表达式 |
| 42 | + String regex2 = "^Test.*"; |
| 43 | + Pattern pattern2 = cacheManager.getPattern(regex2); |
| 44 | + Assert.assertNotNull(pattern2); |
| 45 | + Assert.assertEquals(2, cacheManager.getCacheSize()); |
| 46 | + |
| 47 | + // 测试空正则表达式 |
| 48 | + Pattern nullPattern = cacheManager.getPattern(null); |
| 49 | + Assert.assertNull(nullPattern); |
| 50 | + |
| 51 | + Pattern emptyPattern = cacheManager.getPattern(""); |
| 52 | + Assert.assertNotNull(emptyPattern); |
| 53 | + Assert.assertTrue(emptyPattern.matcher("").matches()); |
| 54 | + Assert.assertFalse(emptyPattern.matcher("non-empty").matches()); |
| 55 | + Assert.assertEquals(3, cacheManager.getCacheSize()); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * 测试LRU淘汰策略 |
| 60 | + */ |
| 61 | + @Test |
| 62 | + public void testLRUEvictionPolicy() { |
| 63 | + // 生成多个正则表达式,超过最大缓存大小 |
| 64 | + int maxCacheSize = 100; |
| 65 | + for (int i = 0; i < maxCacheSize + 5; i++) { |
| 66 | + String regex = "TestRegex" + i; |
| 67 | + Pattern pattern = cacheManager.getPattern(regex); |
| 68 | + Assert.assertNotNull(pattern); |
| 69 | + } |
| 70 | + |
| 71 | + // 缓存大小应该等于最大缓存大小 |
| 72 | + Assert.assertEquals(maxCacheSize, cacheManager.getCacheSize()); // 100 是实际的最大缓存大小 |
| 73 | + |
| 74 | + // 测试访问顺序,确保LRU策略生效 |
| 75 | + String firstRegex = "TestRegex0"; |
| 76 | + |
| 77 | + // 再次访问第一个正则表达式,使其成为最近使用的 |
| 78 | + Pattern firstPattern = cacheManager.getPattern(firstRegex); |
| 79 | + Assert.assertNotNull(firstPattern); |
| 80 | + |
| 81 | + // 再添加一个新的正则表达式,应该淘汰最久未使用的 |
| 82 | + String newRegex = "NewTestRegex"; |
| 83 | + Pattern newPattern = cacheManager.getPattern(newRegex); |
| 84 | + Assert.assertNotNull(newPattern); |
| 85 | + |
| 86 | + // 第一个正则表达式应该仍然在缓存中(因为刚被访问过) |
| 87 | + Pattern firstPatternAgain = cacheManager.getPattern(firstRegex); |
| 88 | + Assert.assertNotNull(firstPatternAgain); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * 测试缓存清理功能 |
| 93 | + */ |
| 94 | + @Test |
| 95 | + public void testCacheClear() { |
| 96 | + // 添加一些缓存项 |
| 97 | + cacheManager.getPattern(".*Test1"); |
| 98 | + cacheManager.getPattern(".*Test2"); |
| 99 | + Assert.assertTrue(cacheManager.getCacheSize() > 0); |
| 100 | + |
| 101 | + // 清理缓存 |
| 102 | + cacheManager.clearCache(); |
| 103 | + Assert.assertEquals(0, cacheManager.getCacheSize()); |
| 104 | + |
| 105 | + // 清理后应该可以重新添加缓存项 |
| 106 | + Pattern pattern = cacheManager.getPattern(".*Test3"); |
| 107 | + Assert.assertNotNull(pattern); |
| 108 | + Assert.assertEquals(1, cacheManager.getCacheSize()); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * 测试无效正则表达式处理 |
| 113 | + */ |
| 114 | + @Test |
| 115 | + public void testInvalidRegexHandling() { |
| 116 | + // 测试无效的正则表达式,应该抛出PatternSyntaxException |
| 117 | + String invalidRegex = "[a-z"; |
| 118 | + try { |
| 119 | + cacheManager.getPattern(invalidRegex); |
| 120 | + } catch (Exception e) { |
| 121 | + // 验证抛出的是PatternSyntaxException |
| 122 | + Assert.assertTrue("Expected PatternSyntaxException but got " + e.getClass().getName(), e instanceof PatternSyntaxException); |
| 123 | + } |
| 124 | + |
| 125 | + // 测试另一个无效的正则表达式,应该抛出PatternSyntaxException |
| 126 | + String anotherInvalidRegex = "(a-z"; |
| 127 | + try { |
| 128 | + cacheManager.getPattern(anotherInvalidRegex); |
| 129 | + } catch (Exception e) { |
| 130 | + // 验证抛出的是PatternSyntaxException |
| 131 | + Assert.assertTrue("Expected PatternSyntaxException but got " + e.getClass().getName(), e instanceof PatternSyntaxException); |
| 132 | + } |
| 133 | + |
| 134 | + // 确保缓存大小没有增加 |
| 135 | + Assert.assertEquals("无效正则表达式不应该被缓存", 0, cacheManager.getCacheSize()); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments