Search in sources :

Example 1 with TranslatorSource

use of org.apache.tapestry5.services.TranslatorSource in project tapestry-5 by apache.

the class TranslatorSourceImplTest method unknown_translator_is_failure.

@Test
public void unknown_translator_is_failure() {
    Translator fred = mockTranslator("fred", String.class);
    Translator barney = mockTranslator("barney", Long.class);
    Map<Class, Translator> configuration = CollectionFactory.newMap();
    configuration.put(String.class, fred);
    configuration.put(Long.class, barney);
    replay();
    TranslatorSource source = new TranslatorSourceImpl(configuration);
    try {
        source.get("wilma");
        unreachable();
    } catch (UnknownValueException ex) {
        assertMessageContains(ex, "Unknown translator type 'wilma'.");
    }
}
Also used : TranslatorSource(org.apache.tapestry5.services.TranslatorSource) Translator(org.apache.tapestry5.Translator) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) BeforeClass(org.testng.annotations.BeforeClass) Test(org.testng.annotations.Test)

Example 2 with TranslatorSource

use of org.apache.tapestry5.services.TranslatorSource in project tapestry-5 by apache.

the class TranslatorSourceImplTest method get_by_type_not_found.

@Test
public void get_by_type_not_found() {
    Translator string = mockTranslator("string", String.class);
    Translator bool = mockTranslator("bool", Boolean.class);
    Map<Class, Translator> configuration = CollectionFactory.newMap();
    configuration.put(String.class, string);
    configuration.put(Boolean.class, bool);
    replay();
    TranslatorSource source = new TranslatorSourceImpl(configuration);
    try {
        source.getByType(Integer.class);
        unreachable();
    } catch (IllegalArgumentException ex) {
        assertEquals(ex.getMessage(), "No translator is defined for type java.lang.Integer.  Registered types: java.lang.Boolean, java.lang.String.");
    }
    verify();
}
Also used : TranslatorSource(org.apache.tapestry5.services.TranslatorSource) Translator(org.apache.tapestry5.Translator) BeforeClass(org.testng.annotations.BeforeClass) Test(org.testng.annotations.Test)

Example 3 with TranslatorSource

use of org.apache.tapestry5.services.TranslatorSource in project tapestry-5 by apache.

the class TranslatorSourceImplTest method name_collision_with_standard_translators.

@Test
public void name_collision_with_standard_translators() {
    Translator t1 = mockTranslator("fred", Integer.class);
    Translator t2 = mockTranslator("fred", Long.class);
    Map<Class, Translator> configuration = CollectionFactory.newMap();
    configuration.put(Integer.class, t1);
    configuration.put(Long.class, t2);
    replay();
    try {
        new TranslatorSourceImpl(configuration);
        unreachable();
    } catch (RuntimeException ex) {
        assertMessageContains(ex, "Two different Translators contributed to the TranslatorSource service use the same translator name: 'fred'.", "Translator names must be unique.");
    }
    verify();
}
Also used : Translator(org.apache.tapestry5.Translator) BeforeClass(org.testng.annotations.BeforeClass) Test(org.testng.annotations.Test)

Example 4 with TranslatorSource

use of org.apache.tapestry5.services.TranslatorSource in project tapestry-5 by apache.

the class TranslatorSourceImplTest method get_alternate_translator_by_name.

@Test
public void get_alternate_translator_by_name() {
    Translator t1 = mockTranslator("fred", Integer.class);
    Translator t2 = mockTranslator();
    Map<Class, Translator> configuration = newConfiguration(Integer.class, t1);
    Map<String, Translator> alternates = CollectionFactory.newMap();
    alternates.put("barney", t2);
    replay();
    TranslatorSource source = new TranslatorSourceImpl(configuration, alternates);
    assertSame(source.get("barney"), t2);
    verify();
}
Also used : TranslatorSource(org.apache.tapestry5.services.TranslatorSource) Translator(org.apache.tapestry5.Translator) BeforeClass(org.testng.annotations.BeforeClass) Test(org.testng.annotations.Test)

Example 5 with TranslatorSource

use of org.apache.tapestry5.services.TranslatorSource in project tapestry-5 by apache.

the class FieldTranslatorSourceImplTest method create_default_translator_with_name_and_null_key.

@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();
}
Also used : Locale(java.util.Locale) Messages(org.apache.tapestry5.commons.Messages) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) FieldTranslator(org.apache.tapestry5.FieldTranslator) MessageFormatter(org.apache.tapestry5.commons.MessageFormatter) MarkupWriter(org.apache.tapestry5.MarkupWriter) FormSupport(org.apache.tapestry5.services.FormSupport) TranslatorSource(org.apache.tapestry5.services.TranslatorSource) FieldTranslatorSource(org.apache.tapestry5.services.FieldTranslatorSource) Field(org.apache.tapestry5.Field) FieldTranslator(org.apache.tapestry5.FieldTranslator) Translator(org.apache.tapestry5.Translator) FieldTranslatorSource(org.apache.tapestry5.services.FieldTranslatorSource) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)14 Translator (org.apache.tapestry5.Translator)13 TranslatorSource (org.apache.tapestry5.services.TranslatorSource)12 FieldTranslatorSource (org.apache.tapestry5.services.FieldTranslatorSource)7 Map (java.util.Map)6 FieldTranslator (org.apache.tapestry5.FieldTranslator)6 AnnotationProvider (org.apache.tapestry5.commons.AnnotationProvider)6 Messages (org.apache.tapestry5.commons.Messages)6 Locale (java.util.Locale)5 Field (org.apache.tapestry5.Field)5 MarkupWriter (org.apache.tapestry5.MarkupWriter)5 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)5 FormSupport (org.apache.tapestry5.services.FormSupport)5 BeforeClass (org.testng.annotations.BeforeClass)5 BigInteger (java.math.BigInteger)1 ComponentResources (org.apache.tapestry5.ComponentResources)1 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)1 FieldComponent (org.apache.tapestry5.root.FieldComponent)1