Author: nbelaevski
Date: 2010-08-31 09:13:39 -0400 (Tue, 31 Aug 2010)
New Revision: 19039
Added:
trunk/core/impl/src/test/java/org/richfaces/application/MessageFactoryImplTest.java
Removed:
trunk/core/impl/src/test/java/org/richfaces/application/MessageFactoryImpltest.java
Modified:
trunk/
trunk/core/api/src/main/java/org/richfaces/application/MessageFactory.java
trunk/core/impl/src/main/java/org/richfaces/application/MessageFactoryImpl.java
Log:
Merged revisions 19030-19031,19034 via svnmerge from
https://svn.jboss.org/repos/richfaces/branches/RFPL-434
Property changes on: trunk
___________________________________________________________________
Name: svnmerge-integrated
- /branches/RFPL-434:1-19024 /branches/RFPL-754:1-18909
+ /branches/RFPL-754:1-18909 /branches/RFPL-434:1-19038
Modified: trunk/core/api/src/main/java/org/richfaces/application/MessageFactory.java
===================================================================
--- trunk/core/api/src/main/java/org/richfaces/application/MessageFactory.java 2010-08-31
12:41:57 UTC (rev 19038)
+++ trunk/core/api/src/main/java/org/richfaces/application/MessageFactory.java 2010-08-31
13:13:39 UTC (rev 19039)
@@ -35,4 +35,6 @@
public FacesMessage createMessage(FacesContext facesContext, Severity severity,
Enum<?> messageKey, Object... args);
+ public String getMessageText(FacesContext facesContext, Enum<?> messageKey,
Object... args);
+
}
Modified: trunk/core/impl/src/main/java/org/richfaces/application/MessageFactoryImpl.java
===================================================================
---
trunk/core/impl/src/main/java/org/richfaces/application/MessageFactoryImpl.java 2010-08-31
12:41:57 UTC (rev 19038)
+++
trunk/core/impl/src/main/java/org/richfaces/application/MessageFactoryImpl.java 2010-08-31
13:13:39 UTC (rev 19039)
@@ -40,6 +40,53 @@
*/
public class MessageFactoryImpl implements MessageFactory {
+ protected static interface Factory<T> {
+
+ public T create(ResourceBundle bundle, Enum<?> messageKey, Object... args)
throws MissingResourceException;
+
+ }
+
+ private static final Factory<FacesMessage> MESSAGE_FACTORY = new
Factory<FacesMessage>() {
+
+ public FacesMessage create(ResourceBundle bundle, Enum<?> messageKey,
Object... args)
+ throws MissingResourceException {
+
+ String messageId = messageKey.toString();
+
+ String summary = null;
+ String detail = null;
+
+ try {
+ summary = bundle.getString(messageId);
+ detail = bundle.getString(messageId + "_detail");
+ } catch (MissingResourceException e) {
+ // do nothing
+ }
+
+ if (summary != null) {
+ String formattedSummary = MessageFormat.format(summary, args);
+ String formattedDetail = null;
+
+ if (detail != null) {
+ formattedDetail = MessageFormat.format(detail, args);
+ }
+
+ return new FacesMessage(formattedSummary, formattedDetail);
+ }
+
+ return null;
+ }
+ };
+
+ private static final Factory<String> LABEL_FACTORY = new
Factory<String>() {
+
+ public String create(ResourceBundle bundle, Enum<?> messageKey, Object...
args) throws MissingResourceException {
+ String pattern = bundle.getString(messageKey.toString());
+ return MessageFormat.format(pattern, args);
+ }
+
+ };
+
private BundleLoader bundleLoader;
public MessageFactoryImpl(BundleLoader bundleLoader) {
@@ -73,26 +120,46 @@
throw new NullPointerException("messageKey");
}
- FacesMessage result = null;
+ FacesMessage result = detectLocalesAndCreate(facesContext, MESSAGE_FACTORY,
messageKey, args);
- Locale locale = detectLocale(facesContext);
+ if (result != null) {
+ result.setSeverity(severity);
+ }
+
+ return result;
+ }
+
+ public String getMessageText(FacesContext facesContext, Enum<?> messageKey,
Object... args) {
+ String text = detectLocalesAndCreate(facesContext, LABEL_FACTORY, messageKey,
args);
+ if (text == null) {
+ text = "???" + messageKey + "???";
+ }
+
+ return text;
+ }
+
+ protected <T> T detectLocalesAndCreate(FacesContext context, Factory<T>
factory, Enum<?> messageKey, Object... args) {
+
+ T result = null;
+
+ Locale locale = detectLocale(context);
if (locale != null) {
- result = createMessage(facesContext, severity, locale, messageKey, args);
+ result = create(context, factory, locale, messageKey, args);
}
if (result == null) {
Locale defaultLocale = Locale.getDefault();
if (!defaultLocale.equals(locale)) {
- result = createMessage(facesContext, severity, defaultLocale, messageKey,
args);
+ result = create(context, factory, defaultLocale, messageKey, args);
}
}
-
+
return result;
}
-
- protected FacesMessage createMessage(FacesContext context, Severity severity, Locale
locale, Enum<?> messageKey,
+
+ protected <T> T create(FacesContext context, Factory<T> factory, Locale
locale, Enum<?> messageKey,
Object... args) {
MessageBundle messageBundle =
messageKey.getClass().getAnnotation(MessageBundle.class);
@@ -101,41 +168,25 @@
return null;
}
- String messageId = messageKey.toString();
+ T result = null;
- String summary = null;
- String detail = null;
-
try {
ResourceBundle bundle = bundleLoader.getApplicationBundle(context,
messageKey, locale);
- summary = bundle.getString(messageId);
- detail = bundle.getString(messageId + "_detail");
+ result = factory.create(bundle, messageKey, args);
} catch (MissingResourceException e) {
// do nothing
}
- if (summary == null) {
+ if (result == null) {
try {
ResourceBundle bundle = bundleLoader.getBundle(messageKey, locale);
- summary = bundle.getString(messageId);
- detail = bundle.getString(messageId + "_detail");
+ result = factory.create(bundle, messageKey, args);
} catch (MissingResourceException e) {
// do nothing
}
}
- if (summary == null) {
- return null;
- }
-
- String formattedSummary = MessageFormat.format(summary, args);
- String formattedDetail = null;
-
- if (detail != null) {
- formattedDetail = MessageFormat.format(detail, args);
- }
-
- return new FacesMessage(severity, formattedSummary, formattedDetail);
+ return result;
}
}
Copied:
trunk/core/impl/src/test/java/org/richfaces/application/MessageFactoryImplTest.java (from
rev 19034,
branches/RFPL-434/core/impl/src/test/java/org/richfaces/application/MessageFactoryImplTest.java)
===================================================================
--- trunk/core/impl/src/test/java/org/richfaces/application/MessageFactoryImplTest.java
(rev 0)
+++
trunk/core/impl/src/test/java/org/richfaces/application/MessageFactoryImplTest.java 2010-08-31
13:13:39 UTC (rev 19039)
@@ -0,0 +1,162 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.application;
+
+import static org.easymock.EasyMock.expect;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Locale;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIViewRoot;
+
+import org.jboss.test.faces.mock.MockFacesEnvironment;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.richfaces.l10n.BundleLoader;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class MessageFactoryImplTest {
+
+ private MockFacesEnvironment facesEnvironment;
+
+ private MessageFactory messageFactory;
+
+ @Before
+ public void setUp() throws Exception {
+ Locale.setDefault(Locale.US);
+
+ facesEnvironment = MockFacesEnvironment.createEnvironment().withApplication();
+
+ messageFactory = new MessageFactoryImpl(new BundleLoader());
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ facesEnvironment.verify();
+ facesEnvironment.release();
+ facesEnvironment = null;
+
+ messageFactory = null;
+ }
+
+ @Test
+ public void testCreateMessageFromFacesBundle() throws Exception {
+ expect(facesEnvironment.getFacesContext().getViewRoot()).andStubReturn(null);
+
expect(facesEnvironment.getApplication().getMessageBundle()).andStubReturn(null);
+ facesEnvironment.replay();
+
+ // {1}: Could not convert ''{0}'' to a string.
+ FacesMessage stringConverterMessage =
messageFactory.createMessage(facesEnvironment.getFacesContext(),
+ FacesMessages.CONVERTER_STRING, "something", "Message");
+
+ assertNotNull(stringConverterMessage);
+ assertEquals(FacesMessage.SEVERITY_INFO, stringConverterMessage.getSeverity());
+ assertEquals("Message: Could not convert 'something' to a
string.", stringConverterMessage.getSummary());
+ assertEquals(stringConverterMessage.getSummary(),
stringConverterMessage.getDetail());
+
+ // javax.faces.converter.EnumConverter.ENUM={2}: ''{0}'' must be
convertible to an enum.
+ // javax.faces.converter.EnumConverter.ENUM_detail={2}: ''{0}''
must be convertible to an enum from the enum that contains the constant
''{1}''.
+ FacesMessage longConverterMessage =
messageFactory.createMessage(facesEnvironment.getFacesContext(),
+ FacesMessage.SEVERITY_ERROR, FacesMessages.ENUM_CONVERTER_ENUM,
"field", "anotherField", "Failed");
+ assertNotNull(longConverterMessage);
+ assertEquals(FacesMessage.SEVERITY_ERROR, longConverterMessage.getSeverity());
+ assertEquals("Failed: 'field' must be convertible to an enum.",
longConverterMessage.getSummary());
+ assertEquals(
+ "Failed: 'field' must be convertible to an enum from the enum
that contains the constant 'anotherField'.",
+ longConverterMessage.getDetail());
+ }
+
+ @Test
+ public void testGetMessageTextFromFacesBundle() throws Exception {
+ expect(facesEnvironment.getFacesContext().getViewRoot()).andStubReturn(null);
+
expect(facesEnvironment.getApplication().getMessageBundle()).andStubReturn(null);
+ facesEnvironment.replay();
+
+ // {1}: Could not convert ''{0}'' to a string.
+ String message =
messageFactory.getMessageText(facesEnvironment.getFacesContext(),
+ FacesMessages.CONVERTER_STRING, "something", "Message");
+
+ assertEquals("Message: Could not convert 'something' to a
string.", message);
+ }
+
+ @Test
+ public void testCreateMessageFromApplicationBundle() throws Exception {
+ UIViewRoot mockViewRoot = facesEnvironment.createMock(UIViewRoot.class);
+ expect(mockViewRoot.getLocale()).andStubReturn(new Locale("ru",
"RU"));
+
expect(facesEnvironment.getFacesContext().getViewRoot()).andStubReturn(mockViewRoot);
+
expect(facesEnvironment.getApplication().getMessageBundle()).andStubReturn("org.richfaces.application.MessageFactoryImplTest");
+ facesEnvironment.replay();
+
+ // {1}: ''{0}'' ne konvertiruyetsia v stroku.
+ FacesMessage stringConverterMessage =
messageFactory.createMessage(facesEnvironment.getFacesContext(),
+ FacesMessages.CONVERTER_STRING, "something", "Message");
+
+ assertNotNull(stringConverterMessage);
+ assertEquals(FacesMessage.SEVERITY_INFO, stringConverterMessage.getSeverity());
+ assertEquals("Message: 'something' ne konvertiruyetsia v
stroku.", stringConverterMessage.getSummary());
+ assertEquals(stringConverterMessage.getSummary(),
stringConverterMessage.getDetail());
+
+ // javax.faces.converter.EnumConverter.ENUM={2}: ''{0}'' dolzhno
konvertirovat''sia v enum.
+ // javax.faces.converter.EnumConverter.ENUM_detail={2}: ''{0}''
dolzhno konvertirovat''sia v enum iz enum s konstantoj ''{1}''.
+ FacesMessage longConverterMessage =
messageFactory.createMessage(facesEnvironment.getFacesContext(),
+ FacesMessage.SEVERITY_ERROR, FacesMessages.ENUM_CONVERTER_ENUM,
"field", "anotherField", "Failed");
+ assertNotNull(longConverterMessage);
+ assertEquals(FacesMessage.SEVERITY_ERROR, longConverterMessage.getSeverity());
+ assertEquals("Failed: 'field' dolzhno konvertirovat'sia v
enum.", longConverterMessage.getSummary());
+ assertEquals(
+ "Failed: 'field' dolzhno konvertirovat'sia v enum iz enum s
konstantoj 'anotherField'.",
+ longConverterMessage.getDetail());
+
+ // javax.faces.component.UIInput.CONVERSION={0}: Conversion error occurred.
+ FacesMessage inputConversionMessage =
messageFactory.createMessage(facesEnvironment.getFacesContext(),
+ FacesMessages.UIINPUT_CONVERSION, "Failure message");
+ assertNotNull(inputConversionMessage);
+ assertEquals(FacesMessage.SEVERITY_INFO, inputConversionMessage.getSeverity());
+ assertEquals("Failure message: Conversion error occurred.",
inputConversionMessage.getSummary());
+ }
+
+ @Test
+ public void testGetMessageTextFromApplicationBundle() throws Exception {
+ UIViewRoot mockViewRoot = facesEnvironment.createMock(UIViewRoot.class);
+ expect(mockViewRoot.getLocale()).andStubReturn(new Locale("ru",
"RU"));
+
expect(facesEnvironment.getFacesContext().getViewRoot()).andStubReturn(mockViewRoot);
+
expect(facesEnvironment.getApplication().getMessageBundle()).andStubReturn("org.richfaces.application.MessageFactoryImplTest");
+ facesEnvironment.replay();
+
+ // {1}: ''{0}'' ne konvertiruyetsia v stroku.
+ String message =
messageFactory.getMessageText(facesEnvironment.getFacesContext(),
+ FacesMessages.CONVERTER_STRING, "something", "Message");
+
+ assertEquals("Message: 'something' ne konvertiruyetsia v
stroku.", message);
+
+ // javax.faces.component.UIInput.CONVERSION={0}: Conversion error occurred.
+ String otherMessage =
messageFactory.getMessageText(facesEnvironment.getFacesContext(),
+ FacesMessages.UIINPUT_CONVERSION, "Failure message");
+ assertEquals("Failure message: Conversion error occurred.",
otherMessage);
+ }
+}
Deleted:
trunk/core/impl/src/test/java/org/richfaces/application/MessageFactoryImpltest.java
===================================================================
---
trunk/core/impl/src/test/java/org/richfaces/application/MessageFactoryImpltest.java 2010-08-31
12:41:57 UTC (rev 19038)
+++
trunk/core/impl/src/test/java/org/richfaces/application/MessageFactoryImpltest.java 2010-08-31
13:13:39 UTC (rev 19039)
@@ -1,129 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.richfaces.application;
-
-import static org.easymock.EasyMock.expect;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-
-import java.util.Locale;
-
-import javax.faces.application.FacesMessage;
-import javax.faces.component.UIViewRoot;
-
-import org.jboss.test.faces.mock.MockFacesEnvironment;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.richfaces.l10n.BundleLoader;
-
-/**
- * @author Nick Belaevski
- *
- */
-public class MessageFactoryImpltest {
-
- private MockFacesEnvironment facesEnvironment;
-
- private MessageFactory messageFactory;
-
- @Before
- public void setUp() throws Exception {
- Locale.setDefault(Locale.US);
-
- facesEnvironment = MockFacesEnvironment.createEnvironment().withApplication();
-
- messageFactory = new MessageFactoryImpl(new BundleLoader());
- }
-
- @After
- public void tearDown() throws Exception {
- facesEnvironment.verify();
- facesEnvironment.release();
- facesEnvironment = null;
-
- messageFactory = null;
- }
-
- @Test
- public void testCreateMessageFromFacesBundle() throws Exception {
- expect(facesEnvironment.getFacesContext().getViewRoot()).andStubReturn(null);
-
expect(facesEnvironment.getApplication().getMessageBundle()).andStubReturn(null);
- facesEnvironment.replay();
-
- // {1}: Could not convert ''{0}'' to a string.
- FacesMessage stringConverterMessage =
messageFactory.createMessage(facesEnvironment.getFacesContext(),
- FacesMessages.CONVERTER_STRING, "something", "Message");
-
- assertNotNull(stringConverterMessage);
- assertEquals(FacesMessage.SEVERITY_INFO, stringConverterMessage.getSeverity());
- assertEquals("Message: Could not convert 'something' to a
string.", stringConverterMessage.getSummary());
- assertEquals(stringConverterMessage.getSummary(),
stringConverterMessage.getDetail());
-
- // javax.faces.converter.EnumConverter.ENUM={2}: ''{0}'' must be
convertible to an enum.
- // javax.faces.converter.EnumConverter.ENUM_detail={2}: ''{0}''
must be convertible to an enum from the enum that contains the constant
''{1}''.
- FacesMessage longConverterMessage =
messageFactory.createMessage(facesEnvironment.getFacesContext(),
- FacesMessage.SEVERITY_ERROR, FacesMessages.ENUM_CONVERTER_ENUM,
"field", "anotherField", "Failed");
- assertNotNull(longConverterMessage);
- assertEquals(FacesMessage.SEVERITY_ERROR, longConverterMessage.getSeverity());
- assertEquals("Failed: 'field' must be convertible to an enum.",
longConverterMessage.getSummary());
- assertEquals(
- "Failed: 'field' must be convertible to an enum from the enum
that contains the constant 'anotherField'.",
- longConverterMessage.getDetail());
- }
-
- @Test
- public void testCreateMessageFromApplicationBundle() throws Exception {
- UIViewRoot mockViewRoot = facesEnvironment.createMock(UIViewRoot.class);
- expect(mockViewRoot.getLocale()).andStubReturn(new Locale("ru",
"RU"));
-
expect(facesEnvironment.getFacesContext().getViewRoot()).andStubReturn(mockViewRoot);
-
expect(facesEnvironment.getApplication().getMessageBundle()).andStubReturn("org.richfaces.application.MessageFactoryImplTest");
- facesEnvironment.replay();
-
- // {1}: ''{0}'' ne konvertiruyetsia v stroku.
- FacesMessage stringConverterMessage =
messageFactory.createMessage(facesEnvironment.getFacesContext(),
- FacesMessages.CONVERTER_STRING, "something", "Message");
-
- assertNotNull(stringConverterMessage);
- assertEquals(FacesMessage.SEVERITY_INFO, stringConverterMessage.getSeverity());
- assertEquals("Message: 'something' ne konvertiruyetsia v
stroku.", stringConverterMessage.getSummary());
- assertEquals(stringConverterMessage.getSummary(),
stringConverterMessage.getDetail());
-
- // javax.faces.converter.EnumConverter.ENUM={2}: ''{0}'' dolzhno
konvertirovat''sia v enum.
- // javax.faces.converter.EnumConverter.ENUM_detail={2}: ''{0}''
dolzhno konvertirovat''sia v enum iz enum s konstantoj ''{1}''.
- FacesMessage longConverterMessage =
messageFactory.createMessage(facesEnvironment.getFacesContext(),
- FacesMessage.SEVERITY_ERROR, FacesMessages.ENUM_CONVERTER_ENUM,
"field", "anotherField", "Failed");
- assertNotNull(longConverterMessage);
- assertEquals(FacesMessage.SEVERITY_ERROR, longConverterMessage.getSeverity());
- assertEquals("Failed: 'field' dolzhno konvertirovat'sia v
enum.", longConverterMessage.getSummary());
- assertEquals(
- "Failed: 'field' dolzhno konvertirovat'sia v enum iz enum s
konstantoj 'anotherField'.",
- longConverterMessage.getDetail());
-
- // javax.faces.component.UIInput.CONVERSION={0}: Conversion error occurred.
- FacesMessage inputConversionMessage =
messageFactory.createMessage(facesEnvironment.getFacesContext(),
- FacesMessages.UIINPUT_CONVERSION, "Failure message");
- assertNotNull(inputConversionMessage);
- assertEquals(FacesMessage.SEVERITY_INFO, inputConversionMessage.getSeverity());
- assertEquals("Failure message: Conversion error occurred.",
inputConversionMessage.getSummary());
- }
-}