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 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.
|
||||
@ -30,20 +43,14 @@ public class ClassBasedTranslator implements Translator {
|
||||
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
|
||||
public String get(String key) {
|
||||
if (className == null) {
|
||||
className = findClassName();
|
||||
}
|
||||
|
||||
String classKey = className + "." + key;
|
||||
|
||||
try {
|
||||
@ -63,7 +70,6 @@ public class ClassBasedTranslator implements Translator {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String get(String base, String text) {
|
||||
return translator.get(base, text);
|
||||
@ -76,11 +82,10 @@ public class ClassBasedTranslator implements Translator {
|
||||
|
||||
|
||||
|
||||
|
||||
private static String getStackClass(int levels) {
|
||||
private String findClassName() {
|
||||
TraceException trace = new TraceException();
|
||||
StackTraceElement stack[] = trace.getStackTrace();
|
||||
final int index = levels + 2;
|
||||
final int index = 2 + levels;
|
||||
if (stack.length <= index) {
|
||||
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) {
|
||||
cn = cn.substring(pos + 1);
|
||||
}
|
||||
|
||||
pos = cn.indexOf('$');
|
||||
if (pos >= 0) {
|
||||
cn = cn.substring(0, pos);
|
||||
}
|
||||
|
||||
return cn;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// For unit testing purposes
|
||||
String getClassName() {
|
||||
return className;
|
||||
|
@ -26,11 +26,11 @@ public final class Application {
|
||||
private static MotorDatabase motorSetDatabase;
|
||||
|
||||
private static ComponentPresetDao componentPresetDao;
|
||||
|
||||
|
||||
private static Preferences preferences;
|
||||
|
||||
private static ExceptionHandler exceptionHandler;
|
||||
|
||||
|
||||
// Initialize the logger to something sane for testing without executing Startup
|
||||
static {
|
||||
setLogOutputLevel(LogLevel.DEBUG);
|
||||
@ -66,7 +66,7 @@ public final class Application {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Return the log buffer.
|
||||
*
|
||||
@ -120,35 +120,35 @@ public final class Application {
|
||||
Application.baseTranslator = translator;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return the preferences
|
||||
*/
|
||||
public static Preferences getPreferences() {
|
||||
return preferences;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param preferences the preferences to set
|
||||
*/
|
||||
public static void setPreferences(Preferences preferences) {
|
||||
Application.preferences = preferences;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return the exceptionHandler
|
||||
*/
|
||||
public static ExceptionHandler getExceptionHandler() {
|
||||
return exceptionHandler;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param exceptionHandler the exceptionHandler to set
|
||||
*/
|
||||
public static void setExceptionHandler(ExceptionHandler exceptionHandler) {
|
||||
Application.exceptionHandler = exceptionHandler;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
Application.motorSetDatabase = motorSetDatabase;
|
||||
}
|
||||
|
||||
|
||||
public static ComponentPresetDao getComponentPresetDao() {
|
||||
return componentPresetDao;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void setComponentPresetDao(ComponentPresetDao componentPresetDao) {
|
||||
Application.componentPresetDao = componentPresetDao;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -22,10 +22,17 @@ public class TestClassBasedTranslator {
|
||||
|
||||
@Test
|
||||
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());
|
||||
|
||||
cbt = new ClassBasedTranslator(null, "foobar");
|
||||
cbt = new ClassBasedTranslator(translator, "foobar");
|
||||
assertEquals("foobar", cbt.getClassName());
|
||||
}
|
||||
|
||||
@ -74,6 +81,28 @@ public class TestClassBasedTranslator {
|
||||
} catch (MissingResourceException e) {
|
||||
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