|
| 1 | +package org.example; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.BeforeEach; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import static org.assertj.core.api.Assertions.*; |
| 6 | + |
| 7 | +class FileCacheTest { |
| 8 | + private FileCache cache; |
| 9 | + |
| 10 | + @BeforeEach |
| 11 | + void setUp() { |
| 12 | + cache = new FileCache(); |
| 13 | + } |
| 14 | + |
| 15 | + @Test |
| 16 | + void testPutAndGet() { |
| 17 | + byte[] fileContent = "Hello World".getBytes(); |
| 18 | + cache.put("test.html", fileContent); |
| 19 | + |
| 20 | + byte[] retrieved = cache.get("test.html"); |
| 21 | + assertThat(retrieved).isEqualTo(fileContent); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + void testContainsReturnsTrueAfterPut() { |
| 26 | + byte[] fileContent = "test content".getBytes(); |
| 27 | + cache.put("file.txt", fileContent); |
| 28 | + |
| 29 | + assertThat(cache.contains("file.txt")).isTrue(); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void testContainsReturnsFalseForNonExistentKey() { |
| 34 | + assertThat(cache.contains("nonexistent.html")).isFalse(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void testGetReturnsNullForNonExistentKey() { |
| 39 | + byte[] result = cache.get("missing.txt"); |
| 40 | + assertThat(result).isNull(); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testMultipleFiles() { |
| 45 | + byte[] content1 = "Content 1".getBytes(); |
| 46 | + byte[] content2 = "Content 2".getBytes(); |
| 47 | + byte[] content3 = "Content 3".getBytes(); |
| 48 | + |
| 49 | + cache.put("file1.html", content1); |
| 50 | + cache.put("file2.css", content2); |
| 51 | + cache.put("file3.js", content3); |
| 52 | + |
| 53 | + assertThat(cache.get("file1.html")).isEqualTo(content1); |
| 54 | + assertThat(cache.get("file2.css")).isEqualTo(content2); |
| 55 | + assertThat(cache.get("file3.js")).isEqualTo(content3); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testClear() { |
| 60 | + cache.put("file1.html", "content1".getBytes()); |
| 61 | + cache.put("file2.html", "content2".getBytes()); |
| 62 | + assertThat(cache.size()).isEqualTo(2); |
| 63 | + |
| 64 | + cache.clear(); |
| 65 | + |
| 66 | + assertThat(cache.size()).isEqualTo(0); |
| 67 | + assertThat(cache.contains("file1.html")).isFalse(); |
| 68 | + assertThat(cache.contains("file2.html")).isFalse(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void testSize() { |
| 73 | + assertThat(cache.size()).isEqualTo(0); |
| 74 | + |
| 75 | + cache.put("file1.html", "content".getBytes()); |
| 76 | + assertThat(cache.size()).isEqualTo(1); |
| 77 | + |
| 78 | + cache.put("file2.html", "content".getBytes()); |
| 79 | + assertThat(cache.size()).isEqualTo(2); |
| 80 | + |
| 81 | + cache.put("file3.html", "content".getBytes()); |
| 82 | + assertThat(cache.size()).isEqualTo(3); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void testOverwriteExistingKey() { |
| 87 | + byte[] oldContent = "old".getBytes(); |
| 88 | + byte[] newContent = "new".getBytes(); |
| 89 | + |
| 90 | + cache.put("file.html", oldContent); |
| 91 | + assertThat(cache.get("file.html")).isEqualTo(oldContent); |
| 92 | + |
| 93 | + cache.put("file.html", newContent); |
| 94 | + assertThat(cache.get("file.html")).isEqualTo(newContent); |
| 95 | + assertThat(cache.size()).isEqualTo(1); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void testLargeFileContent() { |
| 100 | + byte[] largeContent = new byte[10000]; |
| 101 | + for (int i = 0; i < largeContent.length; i++) { |
| 102 | + largeContent[i] = (byte) (i % 256); |
| 103 | + } |
| 104 | + |
| 105 | + cache.put("large.bin", largeContent); |
| 106 | + assertThat(cache.get("large.bin")).isEqualTo(largeContent); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void testEmptyByteArray() { |
| 111 | + byte[] emptyContent = new byte[0]; |
| 112 | + cache.put("empty.txt", emptyContent); |
| 113 | + |
| 114 | + assertThat(cache.contains("empty.txt")).isTrue(); |
| 115 | + assertThat(cache.get("empty.txt")).isEmpty(); |
| 116 | + } |
| 117 | +} |
0 commit comments