FieldTranslatorSourceImpl implements FieldTranslatorSource { public FieldTranslator createDefaultTranslator(ComponentResources resources, String parameterName) { assert resources != null; assert InternalUtils.isNonBlank(parameterName); Field field = (Field) resources.getComponent(); Class propertyType = resources.getBoundType(parameterName); return createDefaultTranslator(field, resources.getId(), resources.getContainerMessages(), null, propertyType, resources.getAnnotationProvider(parameterName)); } FieldTranslatorSourceImpl(TranslatorSource translatorSource, Messages globalMessages,
            FormSupport formSupport); FieldTranslator createDefaultTranslator(ComponentResources resources, String parameterName); FieldTranslator createDefaultTranslator(Field field, String overrideId, Messages overrideMessages,
            Locale locale, Class propertyType, AnnotationProvider propertyAnnotations); FieldTranslator createTranslator(Field field, String overrideId, Messages overrideMessages, Locale locale,
            Translator translator); FieldTranslator createTranslator(ComponentResources resources, String translatorName);  }
@Test public void create_default_property_type_null() { Field field = mockField(); Messages messages = mockMessages(); Locale locale = Locale.ENGLISH; replay(); FieldTranslatorSource source = new FieldTranslatorSourceImpl(null, null, null); assertNull(source.createDefaultTranslator(field, "override", messages, locale, null, null)); verify(); }
@Test public void create_default_translator_not_found_for_type() { Field field = mockField(); Messages messages = mockMessages(); Locale locale = Locale.ENGLISH; Class propertyType = Map.class; TranslatorSource ts = mockTranslatorSource(); AnnotationProvider ap = mockAnnotationProvider(null); train_findByType(ts, propertyType, null); replay(); FieldTranslatorSource source = new FieldTranslatorSourceImpl(ts, null, null); assertNull(source.createDefaultTranslator(field, "override", messages, locale, propertyType, ap)); verify(); }
@Test public void create_default_translator_with_name_and_null_key() { Field field = mockField(); Messages messages = mockMessages(); Locale locale = Locale.ENGLISH; Class propertyType = Map.class; TranslatorSource ts = mockTranslatorSource(); FormSupport fs = mockFormSupport(); Translator translator = mockTranslator("maptrans", Map.class); Messages globalMessages = mockMessages(); MessageFormatter formatter = mockMessageFormatter(); MarkupWriter writer = mockMarkupWriter(); String label = "Field Label"; String message = "Woops, did it again."; AnnotationProvider ap = mockAnnotationProvider(null); train_findByType(ts, propertyType, translator); train_getFormValidationId(fs, "myform"); train_contains(messages, "myform-myfield-maptrans-message", false); train_contains(messages, "myfield-maptrans-message", false); train_getMessageKey(translator, null); train_getMessageFormatter(globalMessages, "maptrans-message", formatter); train_getLabel(field, label); train_format(formatter, message, label); translator.render(field, message, writer, fs); replay(); FieldTranslatorSource source = new FieldTranslatorSourceImpl(ts, globalMessages, fs); FieldTranslator ft = source.createDefaultTranslator(field, "myfield", messages, locale, propertyType, ap); assertEquals(ft.getType(), Map.class); ft.render(writer); verify(); }
@Test public void create_default_translator_with_name() { Field field = mockField(); Messages messages = mockMessages(); Locale locale = Locale.ENGLISH; Class propertyType = Map.class; TranslatorSource ts = mockTranslatorSource(); FormSupport fs = mockFormSupport(); Translator translator = mockTranslator("maptrans", Map.class); Messages globalMessages = mockMessages(); MessageFormatter formatter = mockMessageFormatter(); MarkupWriter writer = mockMarkupWriter(); String label = "Field Label"; String message = "Woops, did it again."; AnnotationProvider ap = mockAnnotationProvider(null); train_findByType(ts, propertyType, translator); train_getFormValidationId(fs, "myform"); train_contains(messages, "myform-myfield-maptrans-message", false); train_contains(messages, "myfield-maptrans-message", false); train_getMessageKey(translator, "mykey"); train_getMessageFormatter(globalMessages, "mykey", formatter); train_getLabel(field, label); train_format(formatter, message, label); translator.render(field, message, writer, fs); replay(); FieldTranslatorSource source = new FieldTranslatorSourceImpl(ts, globalMessages, fs); FieldTranslator ft = source.createDefaultTranslator(field, "myfield", messages, locale, propertyType, ap); assertEquals(ft.getType(), Map.class); ft.render(writer); verify(); }
@Test public void create_default_translator_with_override_message() { Field field = mockField(); Messages messages = mockMessages(); Locale locale = Locale.ENGLISH; Class propertyType = Map.class; TranslatorSource ts = mockTranslatorSource(); FormSupport fs = mockFormSupport(); Translator translator = mockTranslator("maptrans", Map.class); MessageFormatter formatter = mockMessageFormatter(); MarkupWriter writer = mockMarkupWriter(); String label = "My Label"; String message = "Formatted Message"; AnnotationProvider ap = mockAnnotationProvider(null); train_findByType(ts, propertyType, translator); train_getFormValidationId(fs, "myform"); train_contains(messages, "myform-myfield-maptrans-message", false); train_contains(messages, "myfield-maptrans-message", true); train_getMessageFormatter(messages, "myfield-maptrans-message", formatter); train_getLabel(field, label); train_format(formatter, message, label); translator.render(field, message, writer, fs); replay(); FieldTranslatorSource source = new FieldTranslatorSourceImpl(ts, null, fs); FieldTranslator ft = source.createDefaultTranslator(field, "myfield", messages, locale, propertyType, ap); assertEquals(ft.getType(), Map.class); ft.render(writer); verify(); }
@Test public void create_default_translator_with_per_form_override_message() { Field field = mockField(); Messages messages = mockMessages(); Locale locale = Locale.ENGLISH; Class propertyType = Map.class; TranslatorSource ts = mockTranslatorSource(); FormSupport fs = mockFormSupport(); Translator translator = mockTranslator("maptrans", Map.class); MessageFormatter formatter = mockMessageFormatter(); MarkupWriter writer = mockMarkupWriter(); String label = "My Label"; String message = "Formatted Message"; AnnotationProvider ap = mockAnnotationProvider(null); train_findByType(ts, propertyType, translator); train_getFormValidationId(fs, "myform"); train_contains(messages, "myform-myfield-maptrans-message", true); train_getMessageFormatter(messages, "myform-myfield-maptrans-message", formatter); train_getLabel(field, label); train_format(formatter, message, label); translator.render(field, message, writer, fs); replay(); FieldTranslatorSource source = new FieldTranslatorSourceImpl(ts, null, fs); FieldTranslator ft = source.createDefaultTranslator(field, "myfield", messages, locale, propertyType, ap); assertEquals(ft.getType(), Map.class); ft.render(writer); verify(); }