Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.hotswap.agent.annotation;

import java.lang.annotation.*;

/**
* Indicates that the visibility of a field, method, or constructor
* has been relaxed to make the code testable.
* <p>
* This annotation serves purely as documentation — it does not change
* visibility or behavior at runtime. It helps other developers understand
* that a particular element is exposed for testing purposes and should not
* be used by production code directly.
* </p>
*
* <p><b>Typical usage:</b></p>
* <pre>{@code
* class MyService {
*
* @VisibleForTesting
* void computeInternalLogic() {
* // Exposed only for test visibility
* }
* }
* }</pre>
*
* <p><b>Best practice:</b></p>
* Keep such methods <i>package-private</i> and place test classes
* in the same package so that tests can access them.
*/
@Documented
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.TYPE})
public @interface TestOnly {
/**
* Optional note describing why this element is visible for testing.
*
* @return explanation or context for the increased visibility
*/
String value() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.session.Configuration;
import org.hotswap.agent.annotation.TestOnly;
import org.hotswap.agent.javassist.util.proxy.MethodHandler;
import org.hotswap.agent.javassist.util.proxy.ProxyFactory;
import org.hotswap.agent.logging.AgentLogger;
Expand All @@ -41,7 +42,7 @@ public class ConfigurationProxy {

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

private static Map<XMLConfigBuilder, ConfigurationProxy> proxiedConfigurations = new HashMap<>();
private static final Map<XMLConfigBuilder, ConfigurationProxy> proxiedConfigurations = new HashMap<>();

public static ConfigurationProxy getWrapper(XMLConfigBuilder configBuilder) {
/*
Expand All @@ -53,10 +54,10 @@ public static ConfigurationProxy getWrapper(XMLConfigBuilder configBuilder) {
return new ConfigurationProxy(configBuilder);
}

if (!proxiedConfigurations.containsKey(configBuilder)) {
proxiedConfigurations.put(configBuilder, new ConfigurationProxy(configBuilder));
}
return proxiedConfigurations.get(configBuilder);
return proxiedConfigurations.computeIfAbsent(
configBuilder,
ConfigurationProxy::new
);
}

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

private XMLConfigBuilder configBuilder;
private final XMLConfigBuilder configBuilder;
private Configuration configuration;
private Configuration proxyInstance;

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

return false;
}
}

@TestOnly
protected static void reset() {
proxiedConfigurations.clear();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hotswap.agent.plugin.mybatis.proxy;

import org.apache.ibatis.session.Configuration;
import org.hotswap.agent.annotation.TestOnly;
import org.hotswap.agent.plugin.mybatis.transformers.ConfigurationCaller;
import org.hotswap.agent.util.ReflectionHelper;

Expand All @@ -16,10 +17,10 @@ public SpringMybatisConfigurationProxy(Object sqlSessionFactoryBean) {
}

public static SpringMybatisConfigurationProxy getWrapper(Object sqlSessionFactoryBean) {
if (!proxiedConfigurations.containsKey(sqlSessionFactoryBean)) {
proxiedConfigurations.put(sqlSessionFactoryBean, new SpringMybatisConfigurationProxy(sqlSessionFactoryBean));
}
return proxiedConfigurations.get(sqlSessionFactoryBean);
return proxiedConfigurations.computeIfAbsent(
sqlSessionFactoryBean,
SpringMybatisConfigurationProxy::new
);
}

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

return false;
}

@TestOnly
protected static void reset() {
proxiedConfigurations.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected static void swapMapper(String mapperNew, String dstMapperName) throws
public boolean result() throws Exception {
return !MyBatisRefreshCommands.reloadFlag;
}
}, 4000 )); // Repository is regenerated within 2*DeltaSpikePlugin.WAIT_ON_REDEFINE
}, 7000 )); // Repository is regenerated within 2*DeltaSpikePlugin.WAIT_ON_REDEFINE

// TODO do not know why sleep is needed, maybe a separate thread in owb refresh?
Thread.sleep(100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.hotswap.agent.plugin.mybatis.proxy.ProxyReset;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -32,7 +33,7 @@

import static org.junit.Assert.assertEquals;

public class AMyBatisPluginTest extends BaseTest {
public class MyBatisPluginTest extends BaseTest {

private static SqlSessionFactory sqlSessionFactory;

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

@AfterClass
public static void tearDown() throws Exception {
ProxyReset.reset();
File tmp = Resources.getResourceAsFile("org/hotswap/agent/plugin/mybatis/Mapper.xml");
tmp.delete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.ibatis.io.Resources;
import org.hotswap.agent.logging.AgentLogger;
import org.hotswap.agent.plugin.mybatis.proxy.ProxyReset;
import org.hotswap.agent.plugin.mybatis.springboot.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -40,6 +41,7 @@ public static void setup() throws Exception {

@AfterClass
public static void tearDown() throws Exception {
ProxyReset.reset();
File f = Resources.getResourceAsFile("swapXML/BootUserMapper1.xml");
Files.copy(f.toPath(), f.toPath().getParent().resolve("BootUserMapper.xml"), StandardCopyOption.REPLACE_EXISTING);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hotswap.agent.plugin.mybatis;

import org.apache.ibatis.io.Resources;
import org.hotswap.agent.plugin.mybatis.proxy.ProxyReset;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -38,6 +39,7 @@ public static void setup() throws Exception {

@AfterClass
public static void tearDown() throws Exception {
ProxyReset.reset();
File tmp = Resources.getResourceAsFile("org/hotswap/agent/plugin/mybatis/Mapper.xml");
tmp.delete();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.hotswap.agent.plugin.mybatis.proxy;

/**
* Utility method to access reset methods for Proxy Configurations
*/
public class ProxyReset {
public static void reset() {
ConfigurationProxy.reset();
SpringMybatisConfigurationProxy.reset();
}
}
Loading