Skip to content

Commit 01e3b6a

Browse files
kamilkrzywanskiskybber
authored andcommitted
Another fix for tests
1 parent e6941a0 commit 01e3b6a

File tree

8 files changed

+82
-13
lines changed

8 files changed

+82
-13
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.hotswap.agent.annotation;
2+
3+
import java.lang.annotation.*;
4+
5+
/**
6+
* Indicates that the visibility of a field, method, or constructor
7+
* has been relaxed to make the code testable.
8+
* <p>
9+
* This annotation serves purely as documentation — it does not change
10+
* visibility or behavior at runtime. It helps other developers understand
11+
* that a particular element is exposed for testing purposes and should not
12+
* be used by production code directly.
13+
* </p>
14+
*
15+
* <p><b>Typical usage:</b></p>
16+
* <pre>{@code
17+
* class MyService {
18+
*
19+
* @VisibleForTesting
20+
* void computeInternalLogic() {
21+
* // Exposed only for test visibility
22+
* }
23+
* }
24+
* }</pre>
25+
*
26+
* <p><b>Best practice:</b></p>
27+
* Keep such methods <i>package-private</i> and place test classes
28+
* in the same package so that tests can access them.
29+
*/
30+
@Documented
31+
@Retention(RetentionPolicy.SOURCE)
32+
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.TYPE})
33+
public @interface TestOnly {
34+
/**
35+
* Optional note describing why this element is visible for testing.
36+
*
37+
* @return explanation or context for the increased visibility
38+
*/
39+
String value() default "";
40+
}

plugin/hotswap-agent-mybatis-plugin/src/main/java/org/hotswap/agent/plugin/mybatis/proxy/ConfigurationProxy.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
2727
import org.apache.ibatis.session.Configuration;
28+
import org.hotswap.agent.annotation.TestOnly;
2829
import org.hotswap.agent.javassist.util.proxy.MethodHandler;
2930
import org.hotswap.agent.javassist.util.proxy.ProxyFactory;
3031
import org.hotswap.agent.logging.AgentLogger;
@@ -41,7 +42,7 @@ public class ConfigurationProxy {
4142

4243
private static AgentLogger LOGGER = AgentLogger.getLogger(ConfigurationProxy.class);
4344

44-
private static Map<XMLConfigBuilder, ConfigurationProxy> proxiedConfigurations = new HashMap<>();
45+
private static final Map<XMLConfigBuilder, ConfigurationProxy> proxiedConfigurations = new HashMap<>();
4546

4647
public static ConfigurationProxy getWrapper(XMLConfigBuilder configBuilder) {
4748
/*
@@ -53,10 +54,10 @@ public static ConfigurationProxy getWrapper(XMLConfigBuilder configBuilder) {
5354
return new ConfigurationProxy(configBuilder);
5455
}
5556

56-
if (!proxiedConfigurations.containsKey(configBuilder)) {
57-
proxiedConfigurations.put(configBuilder, new ConfigurationProxy(configBuilder));
58-
}
59-
return proxiedConfigurations.get(configBuilder);
57+
return proxiedConfigurations.computeIfAbsent(
58+
configBuilder,
59+
ConfigurationProxy::new
60+
);
6061
}
6162

6263
public static void refreshProxiedConfigurations() {
@@ -77,7 +78,7 @@ public void refreshProxiedConfiguration() throws NoSuchMethodException, Invocati
7778
ReflectionHelper.invoke(configBuilder, MyBatisTransformers.REFRESH_METHOD);
7879
}
7980

80-
private XMLConfigBuilder configBuilder;
81+
private final XMLConfigBuilder configBuilder;
8182
private Configuration configuration;
8283
private Configuration proxyInstance;
8384

@@ -113,4 +114,9 @@ public static boolean isMybatisEntity(Class<?> clazz) {
113114

114115
return false;
115116
}
116-
}
117+
118+
@TestOnly
119+
protected static void reset() {
120+
proxiedConfigurations.clear();
121+
}
122+
}

plugin/hotswap-agent-mybatis-plugin/src/main/java/org/hotswap/agent/plugin/mybatis/proxy/SpringMybatisConfigurationProxy.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.hotswap.agent.plugin.mybatis.proxy;
22

33
import org.apache.ibatis.session.Configuration;
4+
import org.hotswap.agent.annotation.TestOnly;
45
import org.hotswap.agent.plugin.mybatis.transformers.ConfigurationCaller;
56
import org.hotswap.agent.util.ReflectionHelper;
67

@@ -16,10 +17,10 @@ public SpringMybatisConfigurationProxy(Object sqlSessionFactoryBean) {
1617
}
1718

1819
public static SpringMybatisConfigurationProxy getWrapper(Object sqlSessionFactoryBean) {
19-
if (!proxiedConfigurations.containsKey(sqlSessionFactoryBean)) {
20-
proxiedConfigurations.put(sqlSessionFactoryBean, new SpringMybatisConfigurationProxy(sqlSessionFactoryBean));
21-
}
22-
return proxiedConfigurations.get(sqlSessionFactoryBean);
20+
return proxiedConfigurations.computeIfAbsent(
21+
sqlSessionFactoryBean,
22+
SpringMybatisConfigurationProxy::new
23+
);
2324
}
2425

2526
public static boolean runningBySpringMybatis() {
@@ -60,4 +61,9 @@ public static boolean isMybatisEntity(Class<?> clazz) {
6061

6162
return false;
6263
}
64+
65+
@TestOnly
66+
protected static void reset() {
67+
proxiedConfigurations.clear();
68+
}
6369
}

plugin/hotswap-agent-mybatis-plugin/src/test/java/org/hotswap/agent/plugin/mybatis/BaseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected static void swapMapper(String mapperNew, String dstMapperName) throws
5050
public boolean result() throws Exception {
5151
return !MyBatisRefreshCommands.reloadFlag;
5252
}
53-
}, 4000 )); // Repository is regenerated within 2*DeltaSpikePlugin.WAIT_ON_REDEFINE
53+
}, 7000 )); // Repository is regenerated within 2*DeltaSpikePlugin.WAIT_ON_REDEFINE
5454

5555
// TODO do not know why sleep is needed, maybe a separate thread in owb refresh?
5656
Thread.sleep(100);
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.ibatis.session.SqlSession;
2323
import org.apache.ibatis.session.SqlSessionFactory;
2424
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
25+
import org.hotswap.agent.plugin.mybatis.proxy.ProxyReset;
2526
import org.junit.AfterClass;
2627
import org.junit.BeforeClass;
2728
import org.junit.Test;
@@ -32,7 +33,7 @@
3233

3334
import static org.junit.Assert.assertEquals;
3435

35-
public class AMyBatisPluginTest extends BaseTest {
36+
public class MyBatisPluginTest extends BaseTest {
3637

3738
private static SqlSessionFactory sqlSessionFactory;
3839

@@ -52,6 +53,7 @@ public static void setup() throws Exception {
5253

5354
@AfterClass
5455
public static void tearDown() throws Exception {
56+
ProxyReset.reset();
5557
File tmp = Resources.getResourceAsFile("org/hotswap/agent/plugin/mybatis/Mapper.xml");
5658
tmp.delete();
5759
}

plugin/hotswap-agent-mybatis-plugin/src/test/java/org/hotswap/agent/plugin/mybatis/MybatisSpringBootTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.apache.ibatis.io.Resources;
44
import org.hotswap.agent.logging.AgentLogger;
5+
import org.hotswap.agent.plugin.mybatis.proxy.ProxyReset;
56
import org.hotswap.agent.plugin.mybatis.springboot.*;
67
import org.junit.AfterClass;
78
import org.junit.BeforeClass;
@@ -40,6 +41,7 @@ public static void setup() throws Exception {
4041

4142
@AfterClass
4243
public static void tearDown() throws Exception {
44+
ProxyReset.reset();
4345
File f = Resources.getResourceAsFile("swapXML/BootUserMapper1.xml");
4446
Files.copy(f.toPath(), f.toPath().getParent().resolve("BootUserMapper.xml"), StandardCopyOption.REPLACE_EXISTING);
4547
}

plugin/hotswap-agent-mybatis-plugin/src/test/java/org/hotswap/agent/plugin/mybatis/MybatisSpringTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.hotswap.agent.plugin.mybatis;
22

33
import org.apache.ibatis.io.Resources;
4+
import org.hotswap.agent.plugin.mybatis.proxy.ProxyReset;
45
import org.junit.AfterClass;
56
import org.junit.Before;
67
import org.junit.BeforeClass;
@@ -38,6 +39,7 @@ public static void setup() throws Exception {
3839

3940
@AfterClass
4041
public static void tearDown() throws Exception {
42+
ProxyReset.reset();
4143
File tmp = Resources.getResourceAsFile("org/hotswap/agent/plugin/mybatis/Mapper.xml");
4244
tmp.delete();
4345
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.hotswap.agent.plugin.mybatis.proxy;
2+
3+
/**
4+
* Utility method to access reset methods for Proxy Configurations
5+
*/
6+
public class ProxyReset {
7+
public static void reset() {
8+
ConfigurationProxy.reset();
9+
SpringMybatisConfigurationProxy.reset();
10+
}
11+
}

0 commit comments

Comments
 (0)