Commit 6f3ad2c9 authored by Yiming Liu's avatar Yiming Liu

Fix UT file resource issue

parent 5ebe366e
package com.ctrip.apollo.integration; package com.ctrip.apollo.integration;
import com.google.common.collect.ImmutableMap; import static org.hamcrest.core.IsEqual.equalTo;
import com.google.common.collect.Lists; import static org.junit.Assert.assertEquals;
import com.google.common.collect.Maps; import static org.junit.Assert.assertThat;
import com.ctrip.apollo.Config;
import com.ctrip.apollo.ConfigChangeListener;
import com.ctrip.apollo.ConfigService;
import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.ctrip.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.apollo.model.ConfigChangeEvent;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
...@@ -31,9 +17,23 @@ import javax.servlet.ServletException; ...@@ -31,9 +17,23 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import static org.hamcrest.core.IsEqual.equalTo; import org.eclipse.jetty.server.Request;
import static org.junit.Assert.assertEquals; import org.eclipse.jetty.server.handler.AbstractHandler;
import static org.junit.Assert.assertThat; import org.eclipse.jetty.server.handler.ContextHandler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.ctrip.apollo.Config;
import com.ctrip.apollo.ConfigChangeListener;
import com.ctrip.apollo.ConfigService;
import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.ctrip.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.apollo.model.ConfigChangeEvent;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
/** /**
* @author Jason Song(song_s@ctrip.com) * @author Jason Song(song_s@ctrip.com)
...@@ -56,8 +56,8 @@ public class ConfigIntegrationTest extends BaseIntegrationTest { ...@@ -56,8 +56,8 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
@Override @Override
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
super.tearDown();
recursiveDelete(configDir); recursiveDelete(configDir);
super.tearDown();
} }
private void recursiveDelete(File file) { private void recursiveDelete(File file) {
...@@ -69,7 +69,12 @@ public class ConfigIntegrationTest extends BaseIntegrationTest { ...@@ -69,7 +69,12 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
recursiveDelete(f); recursiveDelete(f);
} }
} }
file.delete(); try {
Files.deleteIfExists(file.toPath());
} catch (IOException e) {
e.printStackTrace();
}
} }
@Test @Test
...@@ -108,8 +113,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest { ...@@ -108,8 +113,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
@Test @Test
public void testGetConfigWithNoLocalFileAndRemoteConfigError() throws Exception { public void testGetConfigWithNoLocalFileAndRemoteConfigError() throws Exception {
ContextHandler ContextHandler handler =
handler =
mockConfigServerHandler(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null); mockConfigServerHandler(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
startServerWithHandlers(handler); startServerWithHandlers(handler);
...@@ -129,13 +133,11 @@ public class ConfigIntegrationTest extends BaseIntegrationTest { ...@@ -129,13 +133,11 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
properties.put(someKey, someValue); properties.put(someKey, someValue);
createLocalCachePropertyFile(properties); createLocalCachePropertyFile(properties);
ContextHandler ContextHandler handler =
handler =
mockConfigServerHandler(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null); mockConfigServerHandler(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
startServerWithHandlers(handler); startServerWithHandlers(handler);
Config config = ConfigService.getConfig(); Config config = ConfigService.getConfig();
assertEquals(someValue, config.getProperty(someKey, null)); assertEquals(someValue, config.getProperty(someKey, null));
} }
...@@ -166,7 +168,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest { ...@@ -166,7 +168,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
assertEquals(1, changeEvent.getChanges().size()); assertEquals(1, changeEvent.getChanges().size());
assertEquals(someValue, changeEvent.getChange(someKey).getOldValue()); assertEquals(someValue, changeEvent.getChange(someKey).getOldValue());
assertEquals(anotherValue, changeEvent.getChange(someKey).getNewValue()); assertEquals(anotherValue, changeEvent.getChange(someKey).getNewValue());
//if there is any assertion failed above, this line won't be executed // if there is any assertion failed above, this line won't be executed
changeEvents.add(changeEvent); changeEvents.add(changeEvent);
} }
}); });
...@@ -186,7 +188,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest { ...@@ -186,7 +188,7 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
context.setHandler(new AbstractHandler() { context.setHandler(new AbstractHandler() {
@Override @Override
public void handle(String target, Request baseRequest, HttpServletRequest request, public void handle(String target, Request baseRequest, HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException { HttpServletResponse response) throws IOException, ServletException {
response.setContentType("application/json;charset=UTF-8"); response.setContentType("application/json;charset=UTF-8");
response.setStatus(statusCode); response.setStatus(statusCode);
...@@ -210,12 +212,19 @@ public class ConfigIntegrationTest extends BaseIntegrationTest { ...@@ -210,12 +212,19 @@ public class ConfigIntegrationTest extends BaseIntegrationTest {
private File createLocalCachePropertyFile(Properties properties) throws IOException { private File createLocalCachePropertyFile(Properties properties) throws IOException {
File file = new File(configDir, assembleLocalCacheFileName()); File file = new File(configDir, assembleLocalCacheFileName());
properties.store(new FileOutputStream(file), "Persisted by ConfigIntegrationTest"); FileOutputStream in = null;
try {
in = new FileOutputStream(file);
properties.store(in, "Persisted by ConfigIntegrationTest");
} finally {
if (in != null) {
in.close();
}
}
return file; return file;
} }
private String assembleLocalCacheFileName() { private String assembleLocalCacheFileName() {
return String.format("%s-%s-%s.properties", someAppId, return String.format("%s-%s-%s.properties", someAppId, someClusterName, someNamespace);
someClusterName, someNamespace);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment