source: josm/trunk/test/unit/org/openstreetmap/josm/tools/MemoryManagerTest.java

Last change on this file was 18893, checked in by taylor.smock, 6 months ago

Fix #16567: Upgrade to JUnit 5

JOSMTestRules and JOSMTestFixture can reset the default JOSM profile, which can
be unexpected for new contributors. This updates all tests to use JUnit 5 and
the new JUnit 5 annotations.

This also renames MapCSSStyleSourceFilterTest to MapCSSStyleSourceFilterPerformanceTest
to match the naming convention for performance tests and fixes some lint issues.

This was tested by running all tests individually and together.

File size: 5.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertSame;
7import static org.junit.jupiter.api.Assertions.assertThrows;
8import static org.junit.jupiter.api.Assertions.assertTrue;
9import static org.junit.jupiter.api.Assertions.fail;
10
11import java.util.List;
12
13import org.junit.jupiter.api.Test;
14import org.openstreetmap.josm.testutils.annotations.MemoryManagerLeaks;
15import org.openstreetmap.josm.tools.MemoryManager.MemoryHandle;
16import org.openstreetmap.josm.tools.MemoryManager.NotEnoughMemoryException;
17
18/**
19 * Tests the {@link MemoryManager} class.
20 * @author Michael Zangl
21 */
22@MemoryManagerLeaks
23public class MemoryManagerTest {
24 /**
25 * Test {@link MemoryManager#allocateMemory(String, long, java.util.function.Supplier)}
26 * @throws NotEnoughMemoryException if there is not enough memory
27 */
28 @Test
29 void testUseMemory() throws NotEnoughMemoryException {
30 MemoryManager manager = MemoryManager.getInstance();
31 long available = manager.getAvailableMemory();
32 assertTrue(available < Runtime.getRuntime().maxMemory());
33 assertEquals(available, manager.getMaxMemory());
34
35 Object o1 = new Object();
36 MemoryHandle<Object> testMemory = manager.allocateMemory("test", 10, () -> o1);
37 assertEquals(available - 10, manager.getAvailableMemory());
38 assertSame(o1, testMemory.get());
39 assertEquals(10, testMemory.getSize());
40 assertTrue(testMemory.toString().startsWith("MemoryHandle"));
41
42 manager.allocateMemory("test2", 10, Object::new);
43 assertEquals(available - 20, manager.getAvailableMemory());
44
45 testMemory.free();
46 assertEquals(available - 10, manager.getAvailableMemory());
47 }
48
49 /**
50 * Test that {@link MemoryHandle#get()} checks for use after free.
51 * @throws NotEnoughMemoryException if there is not enough memory
52 */
53 @Test
54 void testUseAfterFree() throws NotEnoughMemoryException {
55 MemoryManager manager = MemoryManager.getInstance();
56 MemoryHandle<Object> testMemory = manager.allocateMemory("test", 10, Object::new);
57 testMemory.free();
58 assertThrows(IllegalStateException.class, testMemory::get);
59 }
60
61 /**
62 * Test that {@link MemoryHandle#get()} checks for free after free.
63 * @throws NotEnoughMemoryException if there is not enough memory
64 */
65 @Test
66 void testFreeAfterFree() throws NotEnoughMemoryException {
67 MemoryManager manager = MemoryManager.getInstance();
68 MemoryHandle<Object> testMemory = manager.allocateMemory("test", 10, Object::new);
69 testMemory.free();
70 assertThrows(IllegalStateException.class, testMemory::free);
71 }
72
73 /**
74 * Test that too big allocations fail
75 */
76 @Test
77 void testAllocationFails() {
78 MemoryManager manager = MemoryManager.getInstance();
79 long available = manager.getAvailableMemory();
80
81 assertThrows(NotEnoughMemoryException.class, () -> manager.allocateMemory("test", available + 1, () -> {
82 fail("Should not reach");
83 return null;
84 }));
85 }
86
87 /**
88 * Test that allocations with null object fail
89 */
90 @Test
91 void testSupplierFails() {
92 MemoryManager manager = MemoryManager.getInstance();
93
94 assertThrows(IllegalArgumentException.class, () -> manager.allocateMemory("test", 1, () -> null));
95 }
96
97 /**
98 * Test {@link MemoryManager#isAvailable(long)}
99 */
100 @Test
101 void testIsAvailable() {
102 MemoryManager manager = MemoryManager.getInstance();
103 assertTrue(manager.isAvailable(10));
104 assertTrue(manager.isAvailable(100));
105 assertTrue(manager.isAvailable(10));
106 }
107
108 /**
109 * Test {@link MemoryManager#isAvailable(long)} for negative number
110 */
111 @Test
112 void testIsAvailableFails() {
113 MemoryManager manager = MemoryManager.getInstance();
114
115 assertThrows(IllegalArgumentException.class, () -> manager.isAvailable(-10));
116 }
117
118 /**
119 * Test {@link MemoryManager#resetState()}
120 * @throws NotEnoughMemoryException if there is not enough memory
121 */
122 @Test
123 void testResetState() throws NotEnoughMemoryException {
124 MemoryManager manager = MemoryManager.getInstance();
125 assertTrue(manager.resetState().isEmpty());
126
127 manager.allocateMemory("test", 10, Object::new);
128 manager.allocateMemory("test2", 10, Object::new);
129 assertEquals(2, manager.resetState().size());
130
131 assertTrue(manager.resetState().isEmpty());
132 }
133
134 /**
135 * Test {@link MemoryManager#resetState()}
136 * @throws NotEnoughMemoryException if there is not enough memory
137 */
138 @Test
139 void testResetStateUseAfterFree() throws NotEnoughMemoryException {
140 MemoryManager manager = MemoryManager.getInstance();
141 MemoryHandle<Object> testMemory = manager.allocateMemory("test", 10, Object::new);
142
143 assertFalse(manager.resetState().isEmpty());
144 assertThrows(IllegalStateException.class, testMemory::get);
145 }
146
147 /**
148 * Reset the state of the memory manager
149 * @param allowMemoryManagerLeaks If this is set, no exception is thrown if there were leaking entries.
150 */
151 public static void resetState(boolean allowMemoryManagerLeaks) {
152 List<MemoryHandle<?>> hadLeaks = MemoryManager.getInstance().resetState();
153 if (!allowMemoryManagerLeaks) {
154 assertTrue(hadLeaks.isEmpty(), "Memory manager had leaking memory: " + hadLeaks);
155 }
156 }
157}
Note: See TracBrowser for help on using the repository browser.