View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.util;
17  
18  import java.util.Collection;
19  import java.util.Collections;
20  
21  import org.kuali.rice.krad.messages.Message;
22  import org.kuali.rice.krad.messages.MessageProvider;
23  import org.kuali.rice.krad.messages.MessageService;
24  import org.kuali.rice.krad.messages.MessageServiceImpl;
25  import org.kuali.rice.krad.messages.providers.ResourceMessageProvider;
26  
27  /**
28   * Provides mock messages for UIF unit tests.
29   * 
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class MockMessageService implements MessageService {
33  
34      private MessageService delegate;
35  
36      private MessageService getDelegate() {
37          if (delegate != null) {
38              return delegate;
39          }
40  
41          ResourceMessageProvider provider = new ResourceMessageProvider();
42          
43          MessageServiceImpl messageServiceDelegate = new MessageServiceImpl();
44          messageServiceDelegate.setMessageProviders(Collections.<MessageProvider> singletonList(provider));
45          delegate = messageServiceDelegate;
46          return delegate;
47      }
48      
49      /**
50       * @see org.kuali.rice.krad.messages.MessageService#getMessage(java.lang.String,
51       *      java.lang.String, java.lang.String)
52       */
53      @Override
54      public Message getMessage(String namespace, String component, String key) {
55          Message rv = getDelegate().getMessage(namespace, component, key);
56          if (rv != null) {
57              return rv;
58          }
59          
60          rv = new Message();
61          rv.setNamespaceCode(namespace);
62          rv.setComponentCode(component);
63          rv.setKey(key);
64          rv.setText(getMessageText(namespace, component, key));
65          return rv;
66      }
67  
68      /**
69       * @see org.kuali.rice.krad.messages.MessageService#getMessage(java.lang.String,
70       *      java.lang.String, java.lang.String, java.lang.String)
71       */
72      @Override
73      public Message getMessage(String namespace, String component, String key, String locale) {
74          Message rv = getDelegate().getMessage(namespace, component, key, locale);
75          if (rv != null) {
76              return rv;
77          }
78  
79          rv = new Message();
80          rv.setNamespaceCode(namespace);
81          rv.setComponentCode(component);
82          rv.setKey(key);
83          rv.setLocale(locale);
84          rv.setText(namespace + ":" + component + ":" + key + ":" + locale);
85          return rv;
86      }
87  
88      /**
89       * @see org.kuali.rice.krad.messages.MessageService#getMessageText(java.lang.String,
90       *      java.lang.String, java.lang.String)
91       */
92      @Override
93      public String getMessageText(String namespace, String component, String key) {
94          String rv = getDelegate().getMessageText(namespace, component, key);
95          if (rv != null) {
96              return rv;
97          }
98  
99          return namespace + ":" + component + ":" + key;
100     }
101 
102     /**
103      * @see org.kuali.rice.krad.messages.MessageService#getMessageText(java.lang.String,
104      *      java.lang.String, java.lang.String, java.lang.String)
105      */
106     @Override
107     public String getMessageText(String namespace, String component, String key, String locale) {
108         String rv = getDelegate().getMessageText(namespace, component, key, locale);
109         if (rv != null) {
110             return rv;
111         }
112 
113         return namespace + ":" + component + ":" + key + ":" + locale;
114     }
115 
116     /**
117      * @see org.kuali.rice.krad.messages.MessageService#getMessageText(java.lang.String)
118      */
119     @Override
120     public String getMessageText(String key) {
121         String rv = getDelegate().getMessageText(key);
122         if (rv != null) {
123             return rv;
124         }
125 
126         return key;
127     }
128 
129     /**
130      * @see org.kuali.rice.krad.messages.MessageService#getMessageText(java.lang.String,
131      *      java.lang.String)
132      */
133     @Override
134     public String getMessageText(String key, String locale) {
135         String rv = getDelegate().getMessageText(key, locale);
136         if (rv != null) {
137             return rv;
138         }
139 
140         return key + ":" + locale;
141     }
142 
143     /**
144      * @see org.kuali.rice.krad.messages.MessageService#getAllMessagesForComponent(java.lang.String,
145      *      java.lang.String)
146      */
147     @Override
148     public Collection<Message> getAllMessagesForComponent(String namespace, String component) {
149         Collection<Message> rv = getDelegate().getAllMessagesForComponent(namespace, component);
150         if (rv != null) {
151             return rv;
152         }
153 
154         Message rm = new Message();
155         rm.setNamespaceCode(namespace);
156         rm.setComponentCode(component);
157         rm.setText(namespace + ":" + component);
158         return Collections.singletonList(rm);
159     }
160 
161     /**
162      * @see org.kuali.rice.krad.messages.MessageService#getAllMessagesForComponent(java.lang.String,
163      *      java.lang.String, java.lang.String)
164      */
165     @Override
166     public Collection<Message> getAllMessagesForComponent(String namespace, String component, String locale) {
167         Collection<Message> rv = getDelegate().getAllMessagesForComponent(namespace, component, locale);
168         if (rv != null) {
169             return rv;
170         }
171 
172         Message rm = new Message();
173         rm.setNamespaceCode(namespace);
174         rm.setComponentCode(component);
175         rm.setLocale(locale);
176         rm.setText(namespace + ":" + component + ":" + locale);
177         return Collections.singletonList(rm);
178     }
179 
180 }