mirror of
https://github.com/lightbend/config.git
synced 2025-01-15 23:01:05 +08:00
Move ConfigBeanFactorTest and toCamelCase into the impl package
This commit is contained in:
parent
7a74183190
commit
3681e5d427
@ -10,6 +10,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.time.Duration;
|
||||
import com.typesafe.config.impl.ConfigImplUtil;
|
||||
|
||||
/**
|
||||
* Factory for automatic creation of config classes populated with values from config.
|
||||
@ -41,8 +42,8 @@ public class ConfigBeanFactory {
|
||||
Map<String, Object> configProps = new HashMap<String, Object>();
|
||||
Map<String,String> originalNames = new HashMap<String, String>();
|
||||
for (Map.Entry<String, ?> configProp : configAsMap.entrySet()) {
|
||||
configProps.put(toCamelCase(configProp.getKey()), configProp.getValue());
|
||||
originalNames.put(toCamelCase(configProp.getKey()),configProp.getKey());
|
||||
configProps.put(ConfigImplUtil.toCamelCase(configProp.getKey()), configProp.getValue());
|
||||
originalNames.put(ConfigImplUtil.toCamelCase(configProp.getKey()),configProp.getKey());
|
||||
}
|
||||
|
||||
BeanInfo beanInfo = null;
|
||||
@ -106,22 +107,4 @@ public class ConfigBeanFactory {
|
||||
|
||||
return config.getAnyRef(configPropName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts from hyphenated name to camel case.
|
||||
*/
|
||||
static String toCamelCase(String originalName) {
|
||||
String[] words = originalName.split("-+");
|
||||
StringBuilder nameBuilder = new StringBuilder(originalName.length());
|
||||
for (int i = 0; i < words.length; i++) {
|
||||
if (nameBuilder.length() == 0) {
|
||||
nameBuilder.append(words[i]);
|
||||
} else {
|
||||
nameBuilder.append(words[i].substring(0, 1).toUpperCase());
|
||||
nameBuilder.append(words[i].substring(1));
|
||||
}
|
||||
}
|
||||
return nameBuilder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -230,4 +230,22 @@ final public class ConfigImplUtil {
|
||||
SerializedConfigValue.writeOrigin(new DataOutputStream(out), (SimpleConfigOrigin) origin,
|
||||
null);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is public ONLY for use by the "config" package, DO NOT USE this ABI
|
||||
* may change.
|
||||
*/
|
||||
public static String toCamelCase(String originalName) {
|
||||
String[] words = originalName.split("-+");
|
||||
StringBuilder nameBuilder = new StringBuilder(originalName.length());
|
||||
for (String word : words) {
|
||||
if (nameBuilder.length() == 0) {
|
||||
nameBuilder.append(word);
|
||||
} else {
|
||||
nameBuilder.append(word.substring(0, 1).toUpperCase());
|
||||
nameBuilder.append(word.substring(1));
|
||||
}
|
||||
}
|
||||
return nameBuilder.toString();
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
/**
|
||||
* Copyright (C) 2013 Typesafe Inc. <http://typesafe.com>
|
||||
*/
|
||||
package com.typesafe.config
|
||||
package com.typesafe.config.impl
|
||||
|
||||
import com.typesafe.config._
|
||||
|
||||
import java.io.{ InputStream, InputStreamReader }
|
||||
import java.time.Duration;
|
||||
@ -12,38 +14,14 @@ import org.junit._
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
|
||||
class ConfigBeanFactoryTest {
|
||||
|
||||
// TODO this is here temporarily to avoid moving to impl in this
|
||||
// same commit
|
||||
import scala.reflect.ClassTag
|
||||
import scala.reflect.classTag
|
||||
protected def intercept[E <: Throwable: ClassTag](block: => Any): E = {
|
||||
val expectedClass = classTag[E].runtimeClass
|
||||
var thrown: Option[Throwable] = None
|
||||
val result = try {
|
||||
Some(block)
|
||||
} catch {
|
||||
case t: Throwable =>
|
||||
thrown = Some(t)
|
||||
None
|
||||
}
|
||||
thrown match {
|
||||
case Some(t) if expectedClass.isAssignableFrom(t.getClass) =>
|
||||
t.asInstanceOf[E]
|
||||
case Some(t) =>
|
||||
throw new Exception(s"Expected exception ${expectedClass.getName} was not thrown, got $t", t)
|
||||
case None =>
|
||||
throw new Exception(s"Expected exception ${expectedClass.getName} was not thrown, no exception was thrown and got result $result")
|
||||
}
|
||||
}
|
||||
class ConfigBeanFactoryTest extends TestUtils {
|
||||
|
||||
@Test
|
||||
def toCamelCase() {
|
||||
assertEquals("configProp", ConfigBeanFactory.toCamelCase("config-prop"))
|
||||
assertEquals("fooBar", ConfigBeanFactory.toCamelCase("foo-----bar"))
|
||||
assertEquals("foo", ConfigBeanFactory.toCamelCase("-foo"))
|
||||
assertEquals("bar", ConfigBeanFactory.toCamelCase("bar-"))
|
||||
assertEquals("configProp", ConfigImplUtil.toCamelCase("config-prop"))
|
||||
assertEquals("fooBar", ConfigImplUtil.toCamelCase("foo-----bar"))
|
||||
assertEquals("foo", ConfigImplUtil.toCamelCase("-foo"))
|
||||
assertEquals("bar", ConfigImplUtil.toCamelCase("bar-"))
|
||||
}
|
||||
|
||||
@Test
|
Loading…
Reference in New Issue
Block a user