From 5e16f7f37c984d7ee1d1f0484cf0a8154bbb849d Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 20 Jun 2025 17:18:45 +0300 Subject: Improve code quality: Replace instanceof with polymorphism and extract constants MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Major improvements: 1. Replace instanceof checks with polymorphic methods in VSAbstractEvent hierarchy - Added isInternalEvent(), isMessageReceiveEvent(), etc. methods - Added getEventPriority() for clean event ordering - Added shouldIncreaseTimestamps() to control timestamp behavior - Refactored VSTask to use these polymorphic methods 2. Extract magic numbers and strings to constants - Created VSConstants class for centralized configuration values - Added event priority constants (PRIORITY_HIGHEST, PRIORITY_HIGH, etc.) - Extracted string constants like CLASS_PREFIX - Moved magic numbers to named constants (PERCENTAGE_RANGE, etc.) 3. Update tests to work with new polymorphic approach - Fixed mocking in VSTaskTest to return correct values - All 132 tests passing These changes improve maintainability, reduce coupling, and make the codebase more self-documenting. The polymorphic approach eliminates type checking and makes it easier to add new event types. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/test/java/core/VSTaskTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/test/java/core') diff --git a/src/test/java/core/VSTaskTest.java b/src/test/java/core/VSTaskTest.java index 3fbec3c..66b7dac 100644 --- a/src/test/java/core/VSTaskTest.java +++ b/src/test/java/core/VSTaskTest.java @@ -153,6 +153,10 @@ class VSTaskTest { VSAbstractEvent normalEvent = mock(VSAbstractEvent.class); VSMessageReceiveEvent internalEvent = mock(VSMessageReceiveEvent.class); + // Setup mocks to return correct values + when(normalEvent.isInternalEvent()).thenReturn(false); + when(internalEvent.isInternalEvent()).thenReturn(true); + // When/Then task = new VSTask(1000L, mockProcess, normalEvent, VSTask.LOCAL); assertFalse(task.hasInternalEvent()); @@ -166,6 +170,7 @@ class VSTaskTest { void testHasMessageReceiveEvent() { // Given VSMessageReceiveEvent messageEvent = mock(VSMessageReceiveEvent.class); + when(messageEvent.isMessageReceiveEvent()).thenReturn(true); // When task = new VSTask(1000L, mockProcess, messageEvent, VSTask.LOCAL); @@ -179,6 +184,7 @@ class VSTaskTest { void testHasProcessRecoverEvent() { // Given VSProcessRecoverEvent recoverEvent = mock(VSProcessRecoverEvent.class); + when(recoverEvent.isProcessRecoverEvent()).thenReturn(true); // When task = new VSTask(1000L, mockProcess, recoverEvent, VSTask.LOCAL); @@ -266,6 +272,7 @@ class VSTaskTest { // Given task = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL); when(mockEvent.getProcess()).thenReturn(null); + when(mockEvent.shouldIncreaseTimestamps()).thenReturn(true); // When task.run(); @@ -283,6 +290,7 @@ class VSTaskTest { VSMessageReceiveEvent messageEvent = mock(VSMessageReceiveEvent.class); task = new VSTask(1000L, mockProcess, messageEvent, VSTask.LOCAL); when(messageEvent.getProcess()).thenReturn(mockProcess); + when(messageEvent.shouldIncreaseTimestamps()).thenReturn(false); // When task.run(); @@ -298,6 +306,7 @@ class VSTaskTest { // Given task = new VSTask(1000L, mockProcess, mockProtocol, VSTask.LOCAL); when(mockProtocol.getProcess()).thenReturn(mockProcess); + when(mockProtocol.shouldIncreaseTimestamps()).thenReturn(false); // When task.run(); @@ -412,6 +421,8 @@ class VSTaskTest { void testCompareToProcessRecoverEventPriority() { // Given VSProcessRecoverEvent recoverEvent = mock(VSProcessRecoverEvent.class); + when(recoverEvent.getEventPriority()).thenReturn(-3); // Highest priority + when(mockEvent.getEventPriority()).thenReturn(0); // Normal priority VSTask recoverTask = new VSTask(1000L, mockProcess, recoverEvent, VSTask.LOCAL); VSTask normalTask = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL); @@ -425,6 +436,8 @@ class VSTaskTest { void testCompareToProcessCrashEventPriority() { // Given VSProcessCrashEvent crashEvent = mock(VSProcessCrashEvent.class); + when(crashEvent.getEventPriority()).thenReturn(-2); // Second highest priority + when(mockEvent.getEventPriority()).thenReturn(0); // Normal priority VSTask crashTask = new VSTask(1000L, mockProcess, crashEvent, VSTask.LOCAL); VSTask normalTask = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL); @@ -438,6 +451,8 @@ class VSTaskTest { void testCompareToProtocolEventPriority() { // Given VSProtocolEvent protocolEvent = mock(VSProtocolEvent.class); + when(protocolEvent.getEventPriority()).thenReturn(-1); // Third highest priority + when(mockEvent.getEventPriority()).thenReturn(0); // Normal priority VSTask protocolTask = new VSTask(1000L, mockProcess, protocolEvent, VSTask.LOCAL); VSTask normalTask = new VSTask(1000L, mockProcess, mockEvent, VSTask.LOCAL); -- cgit v1.2.3