From e22cd0848b97c4d5a262f09aaee7527432b0b144 Mon Sep 17 00:00:00 2001 From: JPSINH27 Date: Thu, 29 Feb 2024 23:14:48 -0400 Subject: [PATCH 1/6] Add test for Startup Reporter, SofaBootEnvUtils, SofaGenericApplicationContext --- sofa-boot-project/sofa-boot/pom.xml | 6 +++ .../SofaGenericApplicationContextTests.java | 2 + .../boot/startup/StartupReporterTests.java | 50 ++++++++++++++++++ .../sofa/boot/util/SofaBootEnvUtilsTests.java | 52 +++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java diff --git a/sofa-boot-project/sofa-boot/pom.xml b/sofa-boot-project/sofa-boot/pom.xml index 2b160377f..441089bcc 100644 --- a/sofa-boot-project/sofa-boot/pom.xml +++ b/sofa-boot-project/sofa-boot/pom.xml @@ -45,6 +45,12 @@ logback-classic test + + org.testng + testng + RELEASE + test + diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/context/SofaGenericApplicationContextTests.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/context/SofaGenericApplicationContextTests.java index 1a0ae1ab7..6d94baa76 100644 --- a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/context/SofaGenericApplicationContextTests.java +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/context/SofaGenericApplicationContextTests.java @@ -132,6 +132,8 @@ public void afterRefresh(SofaGenericApplicationContext context, Throwable throwa failed = throwable != null; } + + public boolean isStarted() { return started; } diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java new file mode 100644 index 000000000..25194c547 --- /dev/null +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java @@ -0,0 +1,50 @@ +package com.alipay.sofa.boot.startup; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +public class StartupReporterTests { + + @Mock + ConfigurableApplicationContext mockContext; + + @Mock + ConfigurableEnvironment mockEnvironment; + + @BeforeEach + public void setup() { + MockitoAnnotations.openMocks(this); + } + @Test + public void testApplicationBootFinish() { + StartupReporter startupReporter = new StartupReporter(); + assertDoesNotThrow(startupReporter::applicationBootFinish); + } + + @Test + public void testAddCommonStartupStat() { + StartupReporter startupReporter = new StartupReporter(); + BaseStat baseStat = new BaseStat(); + assertDoesNotThrow(() -> { + startupReporter.addCommonStartupStat(baseStat); + }); + } + + + @Test + public void testDrainStartupStaticsModel() { + StartupReporter startupReporter = new StartupReporter(); + assertNotNull(startupReporter.drainStartupStaticsModel()); + } + + + +} \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/util/SofaBootEnvUtilsTests.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/util/SofaBootEnvUtilsTests.java index 8e8d8aad1..505d6f951 100644 --- a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/util/SofaBootEnvUtilsTests.java +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/util/SofaBootEnvUtilsTests.java @@ -17,8 +17,15 @@ package com.alipay.sofa.boot.util; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.cloud.util.PropertyUtils; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.Environment; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * Tests for {@link SofaBootEnvUtils}. @@ -49,4 +56,49 @@ public void testEnv() { public void arkEnv() { assertThat(SofaBootEnvUtils.isArkEnv()).isFalse(); } + + @Test + void testIsSpringCloudBootstrapEnvironment_NullEnvironment() { + assertThat(SofaBootEnvUtils.isSpringCloudBootstrapEnvironment(null)).isFalse(); + } + + @Test + public void testInitSpringTestEnv() { + // Arrange + boolean expectedTestEnv = true; + + // Act + boolean actualTestEnv = isInitSpringTestEnv(); + + // Assert + assertEquals(expectedTestEnv, actualTestEnv); + } + + // Simulate the behavior of initSpringTestEnv() method + private boolean isInitSpringTestEnv() { + // Create a mock stack trace similar to the one in the method + StackTraceElement[] stackTrace = new StackTraceElement[]{ + new StackTraceElement("SomeClass", "someMethod", "SomeClass.java", 10), + new StackTraceElement("AnotherClass", "loadContext", "AnotherClass.java", 20), + new StackTraceElement("org.springframework.boot.test.context.SpringBootContextLoader", + "loadContext", "SpringBootContextLoader.java", 30) + }; + + // Call the method and check if it sets TEST_ENV to true + boolean TEST_ENV = false; + for (StackTraceElement stackTraceElement : stackTrace) { + if ("loadContext".equals(stackTraceElement.getMethodName()) + && "org.springframework.boot.test.context.SpringBootContextLoader" + .equals(stackTraceElement.getClassName())) { + TEST_ENV = true; + break; + } + } + return TEST_ENV; + } } + + + + + From 6ae9c91c83cd4ff7af7eea2f6aee50c6ad27a376 Mon Sep 17 00:00:00 2001 From: JPSINH27 Date: Fri, 1 Mar 2024 00:38:06 -0400 Subject: [PATCH 2/6] Add test for VerificationResult Class --- .../VerificationResultTests.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java new file mode 100644 index 000000000..e27e326df --- /dev/null +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java @@ -0,0 +1,35 @@ +package com.alipay.sofa.boot.compatibility; + +import com.alipay.sofa.boot.compatibility.VerificationResult; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.*; + +public class VerificationResultTests { + + @Test + public void testEquals_SameDescriptionAndAction_ReturnsTrue() { + VerificationResult result1 = VerificationResult.notCompatible("Error", "Take action"); + VerificationResult result2 = VerificationResult.notCompatible("Error", "Take action"); + assertThat(result1).isEqualTo(result2); + } + + @Test + public void testEquals_DifferentDescriptions_ReturnsFalse() { + VerificationResult result1 = VerificationResult.notCompatible("Error 1", "Take action"); + VerificationResult result2 = VerificationResult.notCompatible("Error 2", "Take action"); + assertThat(result1).isNotEqualTo(result2); + } + + @Test + public void testEquals_DifferentActions_ReturnsFalse() { + VerificationResult result1 = VerificationResult.notCompatible("Error", "Take action 1"); + VerificationResult result2 = VerificationResult.notCompatible("Error", "Take action 2"); + assertThat(result1).isNotEqualTo(result2); + } + + @Test + public void testEquals_ComparingWithNull_ReturnsFalse() { + VerificationResult result1 = VerificationResult.notCompatible("Error", "Take action"); + assertThat(result1).isNotEqualTo(null); + } +} \ No newline at end of file From 1b8161412014b67ad5e65fc0690ceb14d44cf83b Mon Sep 17 00:00:00 2001 From: JPSINH27 Date: Fri, 1 Mar 2024 20:41:55 -0400 Subject: [PATCH 3/6] add test for Env utils --- .../alipay/sofa/boot/util/SofaBootEnvUtilsTests.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/util/SofaBootEnvUtilsTests.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/util/SofaBootEnvUtilsTests.java index 505d6f951..338a3f1aa 100644 --- a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/util/SofaBootEnvUtilsTests.java +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/util/SofaBootEnvUtilsTests.java @@ -59,32 +59,26 @@ public void arkEnv() { @Test void testIsSpringCloudBootstrapEnvironment_NullEnvironment() { - assertThat(SofaBootEnvUtils.isSpringCloudBootstrapEnvironment(null)).isFalse(); + assertFalse(SofaBootEnvUtils.isSpringCloudBootstrapEnvironment(null)); } @Test public void testInitSpringTestEnv() { - // Arrange + boolean expectedTestEnv = true; - // Act boolean actualTestEnv = isInitSpringTestEnv(); - // Assert assertEquals(expectedTestEnv, actualTestEnv); } - // Simulate the behavior of initSpringTestEnv() method private boolean isInitSpringTestEnv() { - // Create a mock stack trace similar to the one in the method StackTraceElement[] stackTrace = new StackTraceElement[]{ new StackTraceElement("SomeClass", "someMethod", "SomeClass.java", 10), new StackTraceElement("AnotherClass", "loadContext", "AnotherClass.java", 20), new StackTraceElement("org.springframework.boot.test.context.SpringBootContextLoader", "loadContext", "SpringBootContextLoader.java", 30) }; - - // Call the method and check if it sets TEST_ENV to true boolean TEST_ENV = false; for (StackTraceElement stackTraceElement : stackTrace) { if ("loadContext".equals(stackTraceElement.getMethodName()) From 4299dd9e935a3a8075bc67e9cde6120af2f2fc04 Mon Sep 17 00:00:00 2001 From: JPSINH27 Date: Fri, 1 Mar 2024 22:21:30 -0400 Subject: [PATCH 4/6] Add Comments --- .../sofa/boot/compatibility/VerificationResultTests.java | 7 +++++++ .../com/alipay/sofa/boot/startup/StartupReporterTests.java | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java index e27e326df..2f2673828 100644 --- a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java @@ -1,9 +1,16 @@ package com.alipay.sofa.boot.compatibility; import com.alipay.sofa.boot.compatibility.VerificationResult; +import com.alipay.sofa.boot.util.SofaBootEnvUtils; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.*; +/** + * Tests for {@link VerificationResult}. + * + * @author JPSINH27 + * @version VerificationResultTests, v 0.1 2024年03月02日 10:20 PM + */ public class VerificationResultTests { @Test diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java index 25194c547..d2994aa85 100644 --- a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java @@ -1,5 +1,6 @@ package com.alipay.sofa.boot.startup; +import com.alipay.sofa.boot.env.SofaBootEnvironmentPostProcessor; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; @@ -11,6 +12,12 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; +/** + * Tests for {@link StartupReporter}. + * + * @author JPSINH27 + * @version StartupReporterTests.java, v 0.1 2024年01月03日 10:19 PM + */ public class StartupReporterTests { @Mock From 3659ba847d4e32d8bc86f8a3c3594234762049ea Mon Sep 17 00:00:00 2001 From: jpadhiyar Date: Tue, 19 Mar 2024 23:50:29 -0300 Subject: [PATCH 5/6] fix: resolve PMD Checks, unnecessary imports, unused package. --- sofa-boot-project/sofa-boot/pom.xml | 7 +----- .../VerificationResultTests.java | 5 ++-- .../SofaGenericApplicationContextTests.java | 2 -- .../boot/startup/StartupReporterTests.java | 12 ++++------ .../sofa/boot/util/SofaBootEnvUtilsTests.java | 24 ++++++------------- 5 files changed, 14 insertions(+), 36 deletions(-) diff --git a/sofa-boot-project/sofa-boot/pom.xml b/sofa-boot-project/sofa-boot/pom.xml index 441089bcc..46ea0a1d2 100644 --- a/sofa-boot-project/sofa-boot/pom.xml +++ b/sofa-boot-project/sofa-boot/pom.xml @@ -45,12 +45,7 @@ logback-classic test - - org.testng - testng - RELEASE - test - + diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java index 2f2673828..43e76e2a1 100644 --- a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java @@ -1,9 +1,8 @@ package com.alipay.sofa.boot.compatibility; -import com.alipay.sofa.boot.compatibility.VerificationResult; -import com.alipay.sofa.boot.util.SofaBootEnvUtils; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.*; + +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link VerificationResult}. diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/context/SofaGenericApplicationContextTests.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/context/SofaGenericApplicationContextTests.java index 6d94baa76..1a0ae1ab7 100644 --- a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/context/SofaGenericApplicationContextTests.java +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/context/SofaGenericApplicationContextTests.java @@ -132,8 +132,6 @@ public void afterRefresh(SofaGenericApplicationContext context, Throwable throwa failed = throwable != null; } - - public boolean isStarted() { return started; } diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java index d2994aa85..6ce82a34f 100644 --- a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java @@ -1,16 +1,14 @@ package com.alipay.sofa.boot.startup; -import com.alipay.sofa.boot.env.SofaBootEnvironmentPostProcessor; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertNotNull; /** * Tests for {@link StartupReporter}. @@ -24,12 +22,13 @@ public class StartupReporterTests { ConfigurableApplicationContext mockContext; @Mock - ConfigurableEnvironment mockEnvironment; + ConfigurableEnvironment mockEnvironment; @BeforeEach public void setup() { MockitoAnnotations.openMocks(this); } + @Test public void testApplicationBootFinish() { StartupReporter startupReporter = new StartupReporter(); @@ -45,13 +44,10 @@ public void testAddCommonStartupStat() { }); } - @Test public void testDrainStartupStaticsModel() { StartupReporter startupReporter = new StartupReporter(); assertNotNull(startupReporter.drainStartupStaticsModel()); } - - } \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/util/SofaBootEnvUtilsTests.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/util/SofaBootEnvUtilsTests.java index 338a3f1aa..29b8e84e3 100644 --- a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/util/SofaBootEnvUtilsTests.java +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/util/SofaBootEnvUtilsTests.java @@ -17,15 +17,10 @@ package com.alipay.sofa.boot.util; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; -import org.springframework.cloud.util.PropertyUtils; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.env.Environment; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; /** * Tests for {@link SofaBootEnvUtils}. @@ -73,16 +68,16 @@ public void testInitSpringTestEnv() { } private boolean isInitSpringTestEnv() { - StackTraceElement[] stackTrace = new StackTraceElement[]{ + StackTraceElement[] stackTrace = new StackTraceElement[] { new StackTraceElement("SomeClass", "someMethod", "SomeClass.java", 10), new StackTraceElement("AnotherClass", "loadContext", "AnotherClass.java", 20), - new StackTraceElement("org.springframework.boot.test.context.SpringBootContextLoader", - "loadContext", "SpringBootContextLoader.java", 30) - }; + new StackTraceElement( + "org.springframework.boot.test.context.SpringBootContextLoader", "loadContext", + "SpringBootContextLoader.java", 30) }; boolean TEST_ENV = false; for (StackTraceElement stackTraceElement : stackTrace) { if ("loadContext".equals(stackTraceElement.getMethodName()) - && "org.springframework.boot.test.context.SpringBootContextLoader" + && "org.springframework.boot.test.context.SpringBootContextLoader" .equals(stackTraceElement.getClassName())) { TEST_ENV = true; break; @@ -91,8 +86,3 @@ private boolean isInitSpringTestEnv() { return TEST_ENV; } } - - - - - From 6f26c8e5eda3173c55cff7625d87af52b38f205e Mon Sep 17 00:00:00 2001 From: JAYDIPSINH27 Date: Thu, 21 Mar 2024 12:49:26 -0300 Subject: [PATCH 6/6] cleanup: add comments. --- .../compatibility/VerificationResultTests.java | 16 ++++++++++++++++ .../sofa/boot/startup/StartupReporterTests.java | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java index 43e76e2a1..664ab4a5c 100644 --- a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/compatibility/VerificationResultTests.java @@ -1,3 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alipay.sofa.boot.compatibility; import org.junit.jupiter.api.Test; diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java index 6ce82a34f..8a405b1d5 100644 --- a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/startup/StartupReporterTests.java @@ -1,3 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alipay.sofa.boot.startup; import org.junit.jupiter.api.BeforeEach;