Browse Source

reset mock inject in tear down phase to make sure the context is clean

Jason Song 4 years ago
parent
commit
4c2c12b7c8
17 changed files with 73 additions and 29 deletions
  1. 8 9
      apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java
  2. 6 2
      apollo-client/src/test/java/com/ctrip/framework/apollo/ConfigServiceTest.java
  3. 1 1
      apollo-client/src/test/java/com/ctrip/framework/apollo/build/MockInjector.java
  4. 6 1
      apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigManagerTest.java
  5. 1 1
      apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java
  6. 1 1
      apollo-client/src/test/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepositoryTest.java
  7. 6 1
      apollo-client/src/test/java/com/ctrip/framework/apollo/internals/PropertiesConfigFileTest.java
  8. 6 2
      apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollServiceTest.java
  9. 6 1
      apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java
  10. 6 0
      apollo-client/src/test/java/com/ctrip/framework/apollo/internals/SimpleConfigTest.java
  11. 1 0
      apollo-client/src/test/java/com/ctrip/framework/apollo/internals/XmlConfigFileTest.java
  12. 6 1
      apollo-client/src/test/java/com/ctrip/framework/apollo/internals/YamlConfigFileTest.java
  13. 6 1
      apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryManagerTest.java
  14. 6 1
      apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryTest.java
  15. 0 1
      apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigRegistryTest.java
  16. 1 1
      apollo-client/src/test/java/com/ctrip/framework/apollo/spring/AbstractSpringIntegrationTest.java
  17. 6 5
      apollo-client/src/test/java/com/ctrip/framework/apollo/util/yaml/YamlParserTest.java

+ 8 - 9
apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java

@@ -67,12 +67,18 @@ public abstract class BaseIntegrationTest {
     refreshTimeUnit = TimeUnit.MINUTES;
     propertiesOrderEnabled = false;
 
+    MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil());
+  }
+
+  @After
+  public void tearDown() throws Exception {
     //as ConfigService is singleton, so we must manually clear its container
     ConfigService.reset();
     MockInjector.reset();
-    MockInjector.setDelegate(new DefaultInjector());
 
-    MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil());
+    if (server != null && server.isStarted()) {
+      server.stop();
+    }
   }
 
   /**
@@ -94,13 +100,6 @@ public abstract class BaseIntegrationTest {
     return server;
   }
 
-  @After
-  public void tearDown() throws Exception {
-    if (server != null && server.isStarted()) {
-      server.stop();
-    }
-  }
-
   protected ContextHandler mockMetaServerHandler() {
     return mockMetaServerHandler(false);
   }

+ 6 - 2
apollo-client/src/test/java/com/ctrip/framework/apollo/ConfigServiceTest.java

@@ -5,6 +5,7 @@ import static org.junit.Assert.assertEquals;
 import com.ctrip.framework.apollo.enums.ConfigSourceType;
 import java.util.Set;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -26,11 +27,14 @@ public class ConfigServiceTest {
   public void setUp() throws Exception {
     someAppId = "someAppId";
 
+    MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil());
+  }
+
+  @After
+  public void tearDown() throws Exception {
     //as ConfigService is singleton, so we must manually clear its container
     ConfigService.reset();
     MockInjector.reset();
-    MockInjector.setDelegate(new DefaultInjector());
-    MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil());
   }
 
   @Test

+ 1 - 1
apollo-client/src/test/java/com/ctrip/framework/apollo/build/MockInjector.java

@@ -59,6 +59,6 @@ public class MockInjector implements Injector {
   public static void reset() {
     classMap.clear();
     classTable.clear();
-    delegate = null;
+    delegate = new DefaultInjector();
   }
 }

+ 6 - 1
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigManagerTest.java

@@ -9,6 +9,7 @@ import com.ctrip.framework.apollo.enums.ConfigSourceType;
 import java.util.Properties;
 import java.util.Set;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -29,13 +30,17 @@ public class DefaultConfigManagerTest {
 
   @Before
   public void setUp() throws Exception {
-    MockInjector.reset();
     MockInjector.setInstance(ConfigFactoryManager.class, new MockConfigFactoryManager());
     MockInjector.setInstance(ConfigUtil.class, new ConfigUtil());
     defaultConfigManager = new DefaultConfigManager();
     someConfigContent = "someContent";
   }
 
+  @After
+  public void tearDown() throws Exception {
+    MockInjector.reset();
+  }
+
   @Test
   public void testGetConfig() throws Exception {
     String someNamespace = "someName";

+ 1 - 1
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java

@@ -60,7 +60,6 @@ public class DefaultConfigTest {
 
   @Before
   public void setUp() throws Exception {
-    MockInjector.reset();
     MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil());
 
     propertiesFactory = mock(PropertiesFactory.class);
@@ -80,6 +79,7 @@ public class DefaultConfigTest {
 
   @After
   public void tearDown() throws Exception {
+    MockInjector.reset();
     recursiveDelete(someResourceDir);
   }
 

+ 1 - 1
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepositoryTest.java

@@ -59,7 +59,6 @@ public class LocalFileConfigRepositoryTest {
     when(upstreamRepo.getConfig()).thenReturn(someProperties);
     when(upstreamRepo.getSourceType()).thenReturn(someSourceType);
 
-    MockInjector.reset();
     MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil());
     PropertiesFactory propertiesFactory = mock(PropertiesFactory.class);
     when(propertiesFactory.getPropertiesInstance()).thenAnswer(new Answer<Properties>() {
@@ -73,6 +72,7 @@ public class LocalFileConfigRepositoryTest {
 
   @After
   public void tearDown() throws Exception {
+    MockInjector.reset();
     recursiveDelete(someBaseDir);
   }
 

+ 6 - 1
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/PropertiesConfigFileTest.java

@@ -15,6 +15,7 @@ import com.google.common.util.concurrent.SettableFuture;
 import java.util.Properties;
 
 import java.util.concurrent.TimeUnit;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -40,7 +41,6 @@ public class PropertiesConfigFileTest {
   @Before
   public void setUp() throws Exception {
     someNamespace = "someName";
-    MockInjector.reset();
     when(propertiesFactory.getPropertiesInstance()).thenAnswer(new Answer<Properties>() {
       @Override
       public Properties answer(InvocationOnMock invocation) {
@@ -50,6 +50,11 @@ public class PropertiesConfigFileTest {
     MockInjector.setInstance(PropertiesFactory.class, propertiesFactory);
   }
 
+  @After
+  public void tearDown() throws Exception {
+    MockInjector.reset();
+  }
+
   @Test
   public void testWhenHasContent() throws Exception {
     Properties someProperties = new Properties();

+ 6 - 2
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollServiceTest.java

@@ -30,6 +30,7 @@ import java.util.Map;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 import javax.servlet.http.HttpServletResponse;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -61,8 +62,6 @@ public class RemoteConfigLongPollServiceTest {
 
   @Before
   public void setUp() throws Exception {
-    MockInjector.reset();
-
     MockInjector.setInstance(HttpUtil.class, httpUtil);
 
     someServerUrl = "http://someServer";
@@ -82,6 +81,11 @@ public class RemoteConfigLongPollServiceTest {
     someCluster = "someCluster";
   }
 
+  @After
+  public void tearDown() throws Exception {
+    MockInjector.reset();
+  }
+
   @Test
   public void testSubmitLongPollNamespaceWith304Response() throws Exception {
     RemoteConfigRepository someRepository = mock(RemoteConfigRepository.class);

+ 6 - 1
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java

@@ -41,6 +41,7 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.concurrent.TimeUnit;
 import javax.servlet.http.HttpServletResponse;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -80,7 +81,6 @@ public class RemoteConfigRepositoryTest {
 
     when(pollResponse.getStatusCode()).thenReturn(HttpServletResponse.SC_NOT_MODIFIED);
 
-    MockInjector.reset();
     configUtil = new MockConfigUtil();
     MockInjector.setInstance(ConfigUtil.class, configUtil);
 
@@ -111,6 +111,11 @@ public class RemoteConfigRepositoryTest {
     someCluster = "someCluster";
   }
 
+  @After
+  public void tearDown() throws Exception {
+    MockInjector.reset();
+  }
+
   @Test
   public void testLoadConfig() throws Exception {
     String someKey = "someKey";

+ 6 - 0
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/SimpleConfigTest.java

@@ -10,6 +10,7 @@ import com.ctrip.framework.apollo.util.factory.PropertiesFactory;
 import java.util.Properties;
 import java.util.concurrent.TimeUnit;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -52,6 +53,11 @@ public class SimpleConfigTest {
     MockInjector.setInstance(PropertiesFactory.class, propertiesFactory);
   }
 
+  @After
+  public void tearDown() throws Exception {
+    MockInjector.reset();
+  }
+
   @Test
   public void testGetProperty() throws Exception {
     Properties someProperties = new Properties();

+ 1 - 0
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/XmlConfigFileTest.java

@@ -54,6 +54,7 @@ public class XmlConfigFileTest {
 
   @After
   public void tearDown() throws Exception {
+    MockInjector.reset();
     System.clearProperty(PropertiesFactory.APOLLO_PROPERTY_ORDER_ENABLE);
   }
 

+ 6 - 1
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/YamlConfigFileTest.java

@@ -11,6 +11,7 @@ import com.ctrip.framework.apollo.util.OrderedProperties;
 import com.ctrip.framework.apollo.util.factory.PropertiesFactory;
 import com.ctrip.framework.apollo.util.yaml.YamlParser;
 import java.util.Properties;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -36,7 +37,6 @@ public class YamlConfigFileTest {
   public void setUp() throws Exception {
     someNamespace = "someName";
 
-    MockInjector.reset();
     MockInjector.setInstance(YamlParser.class, yamlParser);
 
     when(propertiesFactory.getPropertiesInstance()).thenAnswer(new Answer<Properties>() {
@@ -48,6 +48,11 @@ public class YamlConfigFileTest {
     MockInjector.setInstance(PropertiesFactory.class, propertiesFactory);
   }
 
+  @After
+  public void tearDown() throws Exception {
+    MockInjector.reset();
+  }
+
   @Test
   public void testWhenHasContent() throws Exception {
     Properties someProperties = new Properties();

+ 6 - 1
apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryManagerTest.java

@@ -5,6 +5,7 @@ import static org.hamcrest.core.IsInstanceOf.instanceOf;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertThat;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -21,11 +22,15 @@ public class DefaultConfigFactoryManagerTest {
 
   @Before
   public void setUp() throws Exception {
-    MockInjector.reset();
     MockInjector.setInstance(ConfigRegistry.class, new MockConfigRegistry());
     defaultConfigFactoryManager = new DefaultConfigFactoryManager();
   }
 
+  @After
+  public void tearDown() throws Exception {
+    MockInjector.reset();
+  }
+
   @Test
   public void testGetFactoryFromRegistry() throws Exception {
     ConfigFactory result =

+ 6 - 1
apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigFactoryTest.java

@@ -13,6 +13,7 @@ import static org.mockito.Mockito.when;
 import com.ctrip.framework.apollo.internals.PropertiesCompatibleFileConfigRepository;
 import java.util.Properties;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.springframework.test.util.ReflectionTestUtils;
@@ -43,11 +44,15 @@ public class DefaultConfigFactoryTest {
   public void setUp() throws Exception {
     someAppId = "someId";
     someEnv = Env.DEV;
-    MockInjector.reset();
     MockInjector.setInstance(ConfigUtil.class, new MockConfigUtil());
     defaultConfigFactory = spy(new DefaultConfigFactory());
   }
 
+  @After
+  public void tearDown() throws Exception {
+    MockInjector.reset();
+  }
+
   @Test
   public void testCreate() throws Exception {
     String someNamespace = "someName";

+ 0 - 1
apollo-client/src/test/java/com/ctrip/framework/apollo/spi/DefaultConfigRegistryTest.java

@@ -20,7 +20,6 @@ public class DefaultConfigRegistryTest {
 
   @Before
   public void setUp() throws Exception {
-    MockInjector.reset();
     defaultConfigRegistry = new DefaultConfigRegistry();
   }
 

+ 1 - 1
apollo-client/src/test/java/com/ctrip/framework/apollo/spring/AbstractSpringIntegrationTest.java

@@ -147,12 +147,12 @@ public abstract class AbstractSpringIntegrationTest {
     ReflectionUtils.invokeMethod(PROPERTY_SOURCES_PROCESSOR_RESET, null);
     DefaultInjector defaultInjector = new DefaultInjector();
     ConfigManager defaultConfigManager = defaultInjector.getInstance(ConfigManager.class);
-    MockInjector.reset();
     MockInjector.setInstance(ConfigManager.class, new MockConfigManager(defaultConfigManager));
     MockInjector.setDelegate(defaultInjector);
   }
 
   protected static void doTearDown() {
+    MockInjector.reset();
     CONFIG_REGISTRY.clear();
   }
 

+ 6 - 5
apollo-client/src/test/java/com/ctrip/framework/apollo/util/yaml/YamlParserTest.java

@@ -14,6 +14,7 @@ import com.google.common.io.Files;
 import java.io.File;
 import java.io.IOException;
 import java.util.Properties;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.invocation.InvocationOnMock;
@@ -28,12 +29,14 @@ public class YamlParserTest {
 
   @Before
   public void setUp() throws Exception {
-    MockInjector.reset();
-    MockInjector.setDelegate(new DefaultInjector());
-
     parser = new YamlParser();
   }
 
+  @After
+  public void tearDown() throws Exception {
+    MockInjector.reset();
+  }
+
   @Test
   public void testValidCases() throws Exception {
     test("case1.yaml");
@@ -60,8 +63,6 @@ public class YamlParserTest {
 
     Properties nonOrderedProperties = parser.yamlToProperties(yamlContent);
 
-    MockInjector.reset();
-
     PropertiesFactory propertiesFactory = mock(PropertiesFactory.class);;
     when(propertiesFactory.getPropertiesInstance()).thenAnswer(new Answer<Properties>() {
       @Override