-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance to load mocked properties from apollo.cache-dir (#58)
* enhance to load mocked properties from apollo.cache-dir * enhance to load mocked properties from apollo.cache-dir * enhance to load mocked properties from apollo.cache-dir add UT * enhance to load mocked properties from apollo.cache-dir del test files * enhance to load mocked properties from apollo.cache-dir revert ApolloMockServerApiTest * enhance to load mocked properties from apollo.cache-dir align with review * enhance to load mocked properties from apollo.cache-dir using getDefaultLocalCacheDir instead * enhance to load mocked properties from apollo.cache-dir using getDefaultLocalCacheDir instead * enhance to load mocked properties from apollo.cache-dir optimize * use localFileConfigRepository.getConfig() instead of reflect * update CHANGES.md * update CHANGES.md * update CHANGES.md * little optimized
- Loading branch information
Showing
3 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
.../com/ctrip/framework/apollo/mockserver/ApolloMockServerApiWhileCacheDirSpecifiedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2022 Apollo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.ctrip.framework.apollo.mockserver; | ||
|
||
import com.ctrip.framework.apollo.Config; | ||
import com.ctrip.framework.apollo.ConfigService; | ||
import com.ctrip.framework.apollo.core.ApolloClientSystemConsts; | ||
import com.ctrip.framework.apollo.core.ConfigConsts; | ||
import com.ctrip.framework.apollo.internals.LocalFileConfigRepository; | ||
import com.ctrip.framework.apollo.util.ConfigUtil; | ||
import com.google.common.base.Charsets; | ||
import com.google.common.base.Joiner; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.lang.reflect.Field; | ||
import java.nio.file.Files; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.spy; | ||
|
||
public class ApolloMockServerApiWhileCacheDirSpecifiedTest { | ||
|
||
@ClassRule | ||
public static EmbeddedApollo embeddedApollo = new EmbeddedApollo(); | ||
|
||
@Test | ||
public void testLoadDefaultLocalCacheDir() throws Exception { | ||
String someCacheDir = "src/test/resources/config-cache"; | ||
String someAppId = "someAppId"; | ||
String someNamespace = "someNamespace"; | ||
String someKey = "someKey"; | ||
String someValue = "someValue"; | ||
System.setProperty(ApolloClientSystemConsts.APOLLO_CACHE_DIR, someCacheDir); | ||
|
||
ConfigUtil configUtil = spy(new ConfigUtil()); | ||
doReturn(someAppId).when(configUtil).getAppId(); | ||
String defaultLocalCacheDir = ReflectionTestUtils.invokeMethod(configUtil, "getDefaultLocalCacheDir", new Object[]{}); | ||
assertEquals(someCacheDir + "/" + someAppId, defaultLocalCacheDir); | ||
|
||
// LocalFileConfigRepository.CONFIG_DIR | ||
LocalFileConfigRepository localFileConfigRepository = new LocalFileConfigRepository(someNamespace); | ||
Field FIELD_CONFIG_DIR = localFileConfigRepository.getClass().getDeclaredField("CONFIG_DIR"); | ||
FIELD_CONFIG_DIR.setAccessible(true); | ||
String configDir = (String) FIELD_CONFIG_DIR.get(localFileConfigRepository); | ||
|
||
File someBaseDir = new File(defaultLocalCacheDir, configDir); | ||
someBaseDir.mkdirs(); | ||
File file = new File(someBaseDir, String.format("%s.properties", Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR).join( | ||
someAppId, "default", someNamespace))); | ||
try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), Charsets.UTF_8)) { | ||
writer.write(someKey + "=" + someValue); | ||
} | ||
Config config = ConfigService.getConfig(someNamespace); | ||
assertEquals(someValue, config.getProperty(someKey, null)); | ||
file.deleteOnExit(); | ||
someBaseDir.deleteOnExit(); | ||
} | ||
} |