Skip to content

Commit 26aea21

Browse files
JAYDIPSINH27远轩
authored andcommitted
Test: add new test cases. (sofastack#1299)
* Add test for Startup Reporter, SofaBootEnvUtils, SofaGenericApplicationContext * Add test for VerificationResult Class * add test for Env utils * Add Comments * fix: resolve PMD Checks, unnecessary imports, unused package. * cleanup: add comments.
1 parent ebbddb3 commit 26aea21

4 files changed

Lines changed: 163 additions & 0 deletions

File tree

sofa-boot-project/sofa-boot/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<artifactId>logback-classic</artifactId>
4646
<scope>test</scope>
4747
</dependency>
48+
4849
</dependencies>
4950

5051
<build>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.boot.compatibility;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
/**
24+
* Tests for {@link VerificationResult}.
25+
*
26+
* @author JPSINH27
27+
* @version VerificationResultTests, v 0.1 2024年03月02日 10:20 PM
28+
*/
29+
public class VerificationResultTests {
30+
31+
@Test
32+
public void testEquals_SameDescriptionAndAction_ReturnsTrue() {
33+
VerificationResult result1 = VerificationResult.notCompatible("Error", "Take action");
34+
VerificationResult result2 = VerificationResult.notCompatible("Error", "Take action");
35+
assertThat(result1).isEqualTo(result2);
36+
}
37+
38+
@Test
39+
public void testEquals_DifferentDescriptions_ReturnsFalse() {
40+
VerificationResult result1 = VerificationResult.notCompatible("Error 1", "Take action");
41+
VerificationResult result2 = VerificationResult.notCompatible("Error 2", "Take action");
42+
assertThat(result1).isNotEqualTo(result2);
43+
}
44+
45+
@Test
46+
public void testEquals_DifferentActions_ReturnsFalse() {
47+
VerificationResult result1 = VerificationResult.notCompatible("Error", "Take action 1");
48+
VerificationResult result2 = VerificationResult.notCompatible("Error", "Take action 2");
49+
assertThat(result1).isNotEqualTo(result2);
50+
}
51+
52+
@Test
53+
public void testEquals_ComparingWithNull_ReturnsFalse() {
54+
VerificationResult result1 = VerificationResult.notCompatible("Error", "Take action");
55+
assertThat(result1).isNotEqualTo(null);
56+
}
57+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.boot.startup;
18+
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
import org.mockito.Mock;
22+
import org.mockito.MockitoAnnotations;
23+
import org.springframework.context.ConfigurableApplicationContext;
24+
import org.springframework.core.env.ConfigurableEnvironment;
25+
26+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
27+
import static org.junit.jupiter.api.Assertions.assertNotNull;
28+
29+
/**
30+
* Tests for {@link StartupReporter}.
31+
*
32+
* @author JPSINH27
33+
* @version StartupReporterTests.java, v 0.1 2024年01月03日 10:19 PM
34+
*/
35+
public class StartupReporterTests {
36+
37+
@Mock
38+
ConfigurableApplicationContext mockContext;
39+
40+
@Mock
41+
ConfigurableEnvironment mockEnvironment;
42+
43+
@BeforeEach
44+
public void setup() {
45+
MockitoAnnotations.openMocks(this);
46+
}
47+
48+
@Test
49+
public void testApplicationBootFinish() {
50+
StartupReporter startupReporter = new StartupReporter();
51+
assertDoesNotThrow(startupReporter::applicationBootFinish);
52+
}
53+
54+
@Test
55+
public void testAddCommonStartupStat() {
56+
StartupReporter startupReporter = new StartupReporter();
57+
BaseStat baseStat = new BaseStat();
58+
assertDoesNotThrow(() -> {
59+
startupReporter.addCommonStartupStat(baseStat);
60+
});
61+
}
62+
63+
@Test
64+
public void testDrainStartupStaticsModel() {
65+
StartupReporter startupReporter = new StartupReporter();
66+
assertNotNull(startupReporter.drainStartupStaticsModel());
67+
}
68+
69+
}

sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/util/SofaBootEnvUtilsTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.junit.jupiter.api.Test;
2020

2121
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertFalse;
2224

2325
/**
2426
* Tests for {@link SofaBootEnvUtils}.
@@ -49,4 +51,38 @@ public void testEnv() {
4951
public void arkEnv() {
5052
assertThat(SofaBootEnvUtils.isArkEnv()).isFalse();
5153
}
54+
55+
@Test
56+
void testIsSpringCloudBootstrapEnvironment_NullEnvironment() {
57+
assertFalse(SofaBootEnvUtils.isSpringCloudBootstrapEnvironment(null));
58+
}
59+
60+
@Test
61+
public void testInitSpringTestEnv() {
62+
63+
boolean expectedTestEnv = true;
64+
65+
boolean actualTestEnv = isInitSpringTestEnv();
66+
67+
assertEquals(expectedTestEnv, actualTestEnv);
68+
}
69+
70+
private boolean isInitSpringTestEnv() {
71+
StackTraceElement[] stackTrace = new StackTraceElement[] {
72+
new StackTraceElement("SomeClass", "someMethod", "SomeClass.java", 10),
73+
new StackTraceElement("AnotherClass", "loadContext", "AnotherClass.java", 20),
74+
new StackTraceElement(
75+
"org.springframework.boot.test.context.SpringBootContextLoader", "loadContext",
76+
"SpringBootContextLoader.java", 30) };
77+
boolean TEST_ENV = false;
78+
for (StackTraceElement stackTraceElement : stackTrace) {
79+
if ("loadContext".equals(stackTraceElement.getMethodName())
80+
&& "org.springframework.boot.test.context.SpringBootContextLoader"
81+
.equals(stackTraceElement.getClassName())) {
82+
TEST_ENV = true;
83+
break;
84+
}
85+
}
86+
return TEST_ENV;
87+
}
5288
}

0 commit comments

Comments
 (0)