Make ClassBasedTranslator compatible with Guice
This commit is contained in:
parent
e78a7fe027
commit
811292286e
@ -17,7 +17,20 @@ public class ClassBasedTranslator implements Translator {
|
|||||||
|
|
||||||
|
|
||||||
private final Translator translator;
|
private final Translator translator;
|
||||||
private final String className;
|
private String className;
|
||||||
|
private int levels;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a translator by obtaining the base class name from the stack
|
||||||
|
* on the first execution of the get() method.
|
||||||
|
*
|
||||||
|
* @param translator the translator from which to obtain the translations.
|
||||||
|
*/
|
||||||
|
public ClassBasedTranslator(Translator translator, int levels) {
|
||||||
|
this.translator = translator;
|
||||||
|
this.levels = levels;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a translator using a specified class name.
|
* Construct a translator using a specified class name.
|
||||||
@ -30,20 +43,14 @@ public class ClassBasedTranslator implements Translator {
|
|||||||
this.className = className;
|
this.className = className;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct a translator by obtaining the base class name from the stack.
|
|
||||||
*
|
|
||||||
* @param translator the translator from which to obtain the translations.
|
|
||||||
* @param levels the number of levels to move upwards in the stack from the point where this method is called.
|
|
||||||
*/
|
|
||||||
public ClassBasedTranslator(Translator translator, int levels) {
|
|
||||||
this(translator, getStackClass(levels));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String get(String key) {
|
public String get(String key) {
|
||||||
|
if (className == null) {
|
||||||
|
className = findClassName();
|
||||||
|
}
|
||||||
|
|
||||||
String classKey = className + "." + key;
|
String classKey = className + "." + key;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -63,7 +70,6 @@ public class ClassBasedTranslator implements Translator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String get(String base, String text) {
|
public String get(String base, String text) {
|
||||||
return translator.get(base, text);
|
return translator.get(base, text);
|
||||||
@ -76,11 +82,10 @@ public class ClassBasedTranslator implements Translator {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private String findClassName() {
|
||||||
private static String getStackClass(int levels) {
|
|
||||||
TraceException trace = new TraceException();
|
TraceException trace = new TraceException();
|
||||||
StackTraceElement stack[] = trace.getStackTrace();
|
StackTraceElement stack[] = trace.getStackTrace();
|
||||||
final int index = levels + 2;
|
final int index = 2 + levels;
|
||||||
if (stack.length <= index) {
|
if (stack.length <= index) {
|
||||||
throw new BugException("Stack trace is too short, length=" + stack.length + ", expected=" + index, trace);
|
throw new BugException("Stack trace is too short, length=" + stack.length + ", expected=" + index, trace);
|
||||||
}
|
}
|
||||||
@ -91,12 +96,17 @@ public class ClassBasedTranslator implements Translator {
|
|||||||
if (pos >= 0) {
|
if (pos >= 0) {
|
||||||
cn = cn.substring(pos + 1);
|
cn = cn.substring(pos + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pos = cn.indexOf('$');
|
||||||
|
if (pos >= 0) {
|
||||||
|
cn = cn.substring(0, pos);
|
||||||
|
}
|
||||||
|
|
||||||
return cn;
|
return cn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// For unit testing purposes
|
// For unit testing purposes
|
||||||
String getClassName() {
|
String getClassName() {
|
||||||
return className;
|
return className;
|
||||||
|
@ -26,11 +26,11 @@ public final class Application {
|
|||||||
private static MotorDatabase motorSetDatabase;
|
private static MotorDatabase motorSetDatabase;
|
||||||
|
|
||||||
private static ComponentPresetDao componentPresetDao;
|
private static ComponentPresetDao componentPresetDao;
|
||||||
|
|
||||||
private static Preferences preferences;
|
private static Preferences preferences;
|
||||||
|
|
||||||
private static ExceptionHandler exceptionHandler;
|
private static ExceptionHandler exceptionHandler;
|
||||||
|
|
||||||
// Initialize the logger to something sane for testing without executing Startup
|
// Initialize the logger to something sane for testing without executing Startup
|
||||||
static {
|
static {
|
||||||
setLogOutputLevel(LogLevel.DEBUG);
|
setLogOutputLevel(LogLevel.DEBUG);
|
||||||
@ -66,7 +66,7 @@ public final class Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the log buffer.
|
* Return the log buffer.
|
||||||
*
|
*
|
||||||
@ -120,35 +120,35 @@ public final class Application {
|
|||||||
Application.baseTranslator = translator;
|
Application.baseTranslator = translator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the preferences
|
* @return the preferences
|
||||||
*/
|
*/
|
||||||
public static Preferences getPreferences() {
|
public static Preferences getPreferences() {
|
||||||
return preferences;
|
return preferences;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param preferences the preferences to set
|
* @param preferences the preferences to set
|
||||||
*/
|
*/
|
||||||
public static void setPreferences(Preferences preferences) {
|
public static void setPreferences(Preferences preferences) {
|
||||||
Application.preferences = preferences;
|
Application.preferences = preferences;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the exceptionHandler
|
* @return the exceptionHandler
|
||||||
*/
|
*/
|
||||||
public static ExceptionHandler getExceptionHandler() {
|
public static ExceptionHandler getExceptionHandler() {
|
||||||
return exceptionHandler;
|
return exceptionHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param exceptionHandler the exceptionHandler to set
|
* @param exceptionHandler the exceptionHandler to set
|
||||||
*/
|
*/
|
||||||
public static void setExceptionHandler(ExceptionHandler exceptionHandler) {
|
public static void setExceptionHandler(ExceptionHandler exceptionHandler) {
|
||||||
Application.exceptionHandler = exceptionHandler;
|
Application.exceptionHandler = exceptionHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the database of all thrust curves loaded into the system.
|
* Return the database of all thrust curves loaded into the system.
|
||||||
*/
|
*/
|
||||||
@ -162,15 +162,15 @@ public final class Application {
|
|||||||
public static void setMotorSetDatabase(MotorDatabase motorSetDatabase) {
|
public static void setMotorSetDatabase(MotorDatabase motorSetDatabase) {
|
||||||
Application.motorSetDatabase = motorSetDatabase;
|
Application.motorSetDatabase = motorSetDatabase;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ComponentPresetDao getComponentPresetDao() {
|
public static ComponentPresetDao getComponentPresetDao() {
|
||||||
return componentPresetDao;
|
return componentPresetDao;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setComponentPresetDao(ComponentPresetDao componentPresetDao) {
|
public static void setComponentPresetDao(ComponentPresetDao componentPresetDao) {
|
||||||
Application.componentPresetDao = componentPresetDao;
|
Application.componentPresetDao = componentPresetDao;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -22,10 +22,17 @@ public class TestClassBasedTranslator {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testClassName() {
|
public void testClassName() {
|
||||||
ClassBasedTranslator cbt = new ClassBasedTranslator(null, 0);
|
// @formatter:off
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
oneOf(translator).get("TestClassBasedTranslator.fake.key1"); will(returnValue("foobar"));
|
||||||
|
}});
|
||||||
|
// @formatter:on
|
||||||
|
|
||||||
|
ClassBasedTranslator cbt = new ClassBasedTranslator(translator, 0);
|
||||||
|
cbt.get("fake.key1");
|
||||||
assertEquals("TestClassBasedTranslator", cbt.getClassName());
|
assertEquals("TestClassBasedTranslator", cbt.getClassName());
|
||||||
|
|
||||||
cbt = new ClassBasedTranslator(null, "foobar");
|
cbt = new ClassBasedTranslator(translator, "foobar");
|
||||||
assertEquals("foobar", cbt.getClassName());
|
assertEquals("foobar", cbt.getClassName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,6 +81,28 @@ public class TestClassBasedTranslator {
|
|||||||
} catch (MissingResourceException e) {
|
} catch (MissingResourceException e) {
|
||||||
assertEquals("Neither key 'TestClassBasedTranslator.fake.key3' nor 'fake.key3' could be found", e.getMessage());
|
assertEquals("Neither key 'TestClassBasedTranslator.fake.key3' nor 'fake.key3' could be found", e.getMessage());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetWithSubClass() {
|
||||||
|
ClassBasedTranslator cbt = new ClassBasedTranslator(translator, 0);
|
||||||
|
|
||||||
|
// @formatter:off
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
oneOf(translator).get("TestClassBasedTranslator.fake.key1"); will(returnValue("foobar"));
|
||||||
|
}});
|
||||||
|
// @formatter:on
|
||||||
|
|
||||||
|
assertEquals("foobar", new Subclass().get(cbt, "fake.key1"));
|
||||||
|
assertEquals("TestClassBasedTranslator", cbt.getClassName());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private class Subclass {
|
||||||
|
private String get(Translator trans, String key) {
|
||||||
|
return trans.get(key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user