Skip to content

Commit fde6141

Browse files
yanhom1314claude
andcommitted
test: add unit tests for TaskWrappers
Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6b5cbb7 commit fde6141

1 file changed

Lines changed: 179 additions & 0 deletions

File tree

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
18+
package org.dromara.dynamictp.test.core.support.task.wrapper;
19+
20+
import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper;
21+
import org.dromara.dynamictp.core.support.task.wrapper.TaskWrappers;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.util.HashSet;
25+
import java.util.List;
26+
import java.util.Set;
27+
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertNotNull;
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
32+
/**
33+
* TaskWrappers test
34+
*
35+
* @author yanhom
36+
* @since 1.2.2
37+
*/
38+
class TaskWrappersTest {
39+
40+
@Test
41+
void testGetInstanceNotNull() {
42+
TaskWrappers instance = TaskWrappers.getInstance();
43+
assertNotNull(instance);
44+
}
45+
46+
@Test
47+
void testGetInstanceSingleton() {
48+
TaskWrappers a = TaskWrappers.getInstance();
49+
TaskWrappers b = TaskWrappers.getInstance();
50+
assertTrue(a == b);
51+
}
52+
53+
@Test
54+
void testGetByNamesTtl() {
55+
TaskWrappers instance = TaskWrappers.getInstance();
56+
Set<String> names = new HashSet<>();
57+
names.add("ttl");
58+
59+
List<TaskWrapper> wrappers = instance.getByNames(names);
60+
assertEquals(1, wrappers.size());
61+
assertEquals("ttl", wrappers.get(0).name());
62+
}
63+
64+
@Test
65+
void testGetByNamesMdc() {
66+
TaskWrappers instance = TaskWrappers.getInstance();
67+
Set<String> names = new HashSet<>();
68+
names.add("mdc");
69+
70+
List<TaskWrapper> wrappers = instance.getByNames(names);
71+
assertEquals(1, wrappers.size());
72+
assertEquals("mdc", wrappers.get(0).name());
73+
}
74+
75+
@Test
76+
void testGetByNamesBoth() {
77+
TaskWrappers instance = TaskWrappers.getInstance();
78+
Set<String> names = new HashSet<>();
79+
names.add("ttl");
80+
names.add("mdc");
81+
82+
List<TaskWrapper> wrappers = instance.getByNames(names);
83+
assertEquals(2, wrappers.size());
84+
}
85+
86+
@Test
87+
void testGetByNamesEmptyReturnsEmpty() {
88+
TaskWrappers instance = TaskWrappers.getInstance();
89+
List<TaskWrapper> wrappers = instance.getByNames(null);
90+
assertTrue(wrappers.isEmpty());
91+
92+
wrappers = instance.getByNames(new HashSet<>());
93+
assertTrue(wrappers.isEmpty());
94+
}
95+
96+
@Test
97+
void testGetByNamesUnknownReturnsEmpty() {
98+
TaskWrappers instance = TaskWrappers.getInstance();
99+
Set<String> names = new HashSet<>();
100+
names.add("nonexistent");
101+
102+
List<TaskWrapper> wrappers = instance.getByNames(names);
103+
assertTrue(wrappers.isEmpty());
104+
}
105+
106+
@Test
107+
void testRegisterCustomWrapper() {
108+
TaskWrappers.register(new TaskWrapper() {
109+
@Override
110+
public String name() {
111+
return "custom-test";
112+
}
113+
114+
@Override
115+
public Runnable wrap(Runnable runnable) {
116+
return runnable;
117+
}
118+
});
119+
120+
TaskWrappers instance = TaskWrappers.getInstance();
121+
Set<String> names = new HashSet<>();
122+
names.add("custom-test");
123+
124+
List<TaskWrapper> wrappers = instance.getByNames(names);
125+
assertEquals(1, wrappers.size());
126+
assertEquals("custom-test", wrappers.get(0).name());
127+
}
128+
129+
@Test
130+
void testRegisterDuplicateIgnored() {
131+
// Register TTL again - should be ignored since it already exists
132+
TaskWrappers.register(new TaskWrapper() {
133+
@Override
134+
public String name() {
135+
return "ttl";
136+
}
137+
138+
@Override
139+
public Runnable wrap(Runnable runnable) {
140+
return runnable;
141+
}
142+
});
143+
144+
TaskWrappers instance = TaskWrappers.getInstance();
145+
Set<String> names = new HashSet<>();
146+
names.add("ttl");
147+
148+
List<TaskWrapper> wrappers = instance.getByNames(names);
149+
assertEquals(1, wrappers.size());
150+
}
151+
152+
@Test
153+
void testTtlWrapperWraps() {
154+
TaskWrappers instance = TaskWrappers.getInstance();
155+
Set<String> names = new HashSet<>();
156+
names.add("ttl");
157+
158+
List<TaskWrapper> wrappers = instance.getByNames(names);
159+
TaskWrapper ttlWrapper = wrappers.get(0);
160+
161+
Runnable original = () -> { };
162+
Runnable wrapped = ttlWrapper.wrap(original);
163+
assertNotNull(wrapped);
164+
}
165+
166+
@Test
167+
void testMdcWrapperWraps() {
168+
TaskWrappers instance = TaskWrappers.getInstance();
169+
Set<String> names = new HashSet<>();
170+
names.add("mdc");
171+
172+
List<TaskWrapper> wrappers = instance.getByNames(names);
173+
TaskWrapper mdcWrapper = wrappers.get(0);
174+
175+
Runnable original = () -> { };
176+
Runnable wrapped = mdcWrapper.wrap(original);
177+
assertNotNull(wrapped);
178+
}
179+
}

0 commit comments

Comments
 (0)