Coverage Report - org.kuali.student.core.messages.service.impl.MessageServiceMock
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageServiceMock
0%
0/85
0%
0/26
2.308
MessageServiceMock$1
N/A
N/A
2.308
MessageServiceMock$LocaleMessages
0%
0/10
0%
0/2
2.308
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.core.messages.service.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashMap;
 20  
 import java.util.HashSet;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 import java.util.Set;
 25  
 
 26  
 import org.apache.log4j.Logger;
 27  
 import org.kuali.student.common.messages.dto.LocaleKeyList;
 28  
 import org.kuali.student.common.messages.dto.Message;
 29  
 import org.kuali.student.common.messages.dto.MessageGroupKeyList;
 30  
 import org.kuali.student.common.messages.dto.MessageList;
 31  
 import org.kuali.student.common.messages.service.MessageService;
 32  
 import org.kuali.student.core.messages.entity.MessageEntity;
 33  
 import org.springframework.beans.factory.xml.XmlBeanFactory;
 34  
 import org.springframework.core.io.ClassPathResource;
 35  
 import org.springframework.core.io.FileSystemResource;
 36  
 import org.springframework.core.io.Resource;
 37  
 
 38  
 /**
 39  
  * This is poor man's implementation of a mock messages service to be used for
 40  
  * UI testing, it will load gwt-messages.xml configured via the set method.
 41  
  * 
 42  
  * @author Kuali Student Team
 43  
  * 
 44  
  */
 45  0
 public class MessageServiceMock implements MessageService {
 46  
 
 47  0
         final Logger logger = Logger.getLogger(MessageServiceMock.class);
 48  
 
 49  
         List<String> messageFiles;
 50  
 
 51  0
         Map<String, LocaleMessages> messages = new HashMap<String, LocaleMessages>();
 52  
 
 53  0
         private static class LocaleMessages {
 54  0
                 Map<String, Map<String, String>> localeMessages = new HashMap<String, Map<String, String>>();
 55  
 
 56  
                 public Map<String, String> getMessages(String groupKey) {
 57  0
                         return localeMessages.get(groupKey);
 58  
                 }
 59  
 
 60  
                 public Set<String> getGroups() {
 61  0
                         return localeMessages.keySet();
 62  
                 }
 63  
 
 64  
                 public void putMessage(String groupKey, String messageKey,
 65  
                                 String message) {
 66  
                         Map<String, String> groupMessages;
 67  
 
 68  0
                         groupMessages = localeMessages.get(groupKey);
 69  0
                         if (groupMessages == null) {
 70  0
                                 groupMessages = new HashMap<String, String>();
 71  
                         }
 72  0
                         groupMessages.put(messageKey, message);
 73  0
                         this.localeMessages.put(groupKey, groupMessages);
 74  0
                 }
 75  
         }
 76  
 
 77  
         private void putMessage(String locale, String group, String key,
 78  
                         String value) {
 79  
 
 80  0
                 LocaleMessages localeMessages = messages.get(locale);
 81  0
                 if (localeMessages == null) {
 82  0
                         localeMessages = new LocaleMessages();
 83  
                 }
 84  0
                 localeMessages.putMessage(group, key, value);
 85  0
                 this.messages.put(locale, localeMessages);
 86  0
         }
 87  
 
 88  
         /**
 89  
          * @see org.kuali.student.common.messages.service.MessageService#addMessage(org.kuali.student.common.messages.dto.Message)
 90  
          */
 91  
         @Override
 92  
         public Message addMessage(Message messageInfo) {
 93  
 
 94  0
                 LocaleMessages localeMessages = this.messages.get(messageInfo
 95  
                                 .getLocale());
 96  0
                 if (localeMessages == null) {
 97  0
                         localeMessages = new LocaleMessages();
 98  
                 }
 99  0
                 localeMessages.putMessage(messageInfo.getGroupName(), messageInfo
 100  
                                 .getId(), messageInfo.getValue());
 101  
 
 102  0
                 return messageInfo;
 103  
         }
 104  
 
 105  
         /**
 106  
          * @see org.kuali.student.common.messages.service.MessageService#getLocales()
 107  
          */
 108  
         @Override
 109  
         public LocaleKeyList getLocales() {
 110  
 
 111  0
                 LocaleKeyList locales = new LocaleKeyList();
 112  
 
 113  0
                 if (!this.messages.isEmpty()) {
 114  0
                         locales.setLocales(new ArrayList<String>(this.messages.keySet()));
 115  
                 }
 116  
 
 117  0
                 return locales;
 118  
         }
 119  
 
 120  
         /**
 121  
          * @see org.kuali.student.common.messages.service.MessageService#getMessage(java.lang.String,
 122  
          *      java.lang.String, java.lang.String)
 123  
          */
 124  
         @Override
 125  
         public Message getMessage(String localeKey, String messageGroupKey,
 126  
                         String messageKey) {
 127  
 
 128  0
                 Message m = null;
 129  0
                 LocaleMessages lMsgs = this.messages.get(localeKey);
 130  0
                 if (null != lMsgs && null != lMsgs.getMessages(messageGroupKey)) {
 131  0
                         m = new Message();
 132  0
                         m.setGroupName(messageGroupKey);
 133  0
                         m.setLocale(localeKey);
 134  0
                         m.setId(messageKey);
 135  0
                         m.setValue(lMsgs.getMessages(messageGroupKey).get(messageKey));
 136  
                 }
 137  0
                 return m;
 138  
         }
 139  
 
 140  
         /**
 141  
          * @see org.kuali.student.common.messages.service.MessageService#getMessageGroups()
 142  
          */
 143  
         @Override
 144  
         public MessageGroupKeyList getMessageGroups() {
 145  
 
 146  0
                 MessageGroupKeyList groupKeys = new MessageGroupKeyList();
 147  0
                 Iterator<String> i = messages.keySet().iterator();
 148  0
                 Set<String> groupKeySet = new HashSet<String>();
 149  0
                 while (i.hasNext()) {
 150  0
                         String locale = i.next();
 151  0
                         groupKeySet.addAll(messages.get(locale).getGroups());
 152  0
                 }
 153  0
                 groupKeys.setMessageGroupKeys(new ArrayList<String>(groupKeySet));
 154  
 
 155  0
                 return groupKeys;
 156  
         }
 157  
 
 158  
         /**
 159  
          * @see org.kuali.student.common.messages.service.MessageService#getMessages(java.lang.String,
 160  
          *      java.lang.String)
 161  
          */
 162  
         @Override
 163  
         public MessageList getMessages(String localeKey, String messageGroupKey) {
 164  
 
 165  0
                 MessageList messageList = new MessageList();
 166  
 
 167  0
                 Map<String, String> groupMessages = ((LocaleMessages) messages
 168  
                                 .get(localeKey)).getMessages(messageGroupKey);
 169  0
                 List<Message> messageArrayList = new ArrayList<Message>();
 170  
 
 171  0
                 Iterator<String> i = groupMessages.keySet().iterator();
 172  0
                 while (i.hasNext()) {
 173  0
                         String id = i.next();
 174  0
                         Message m = new Message();
 175  0
                         m.setGroupName(messageGroupKey);
 176  0
                         m.setId(id);
 177  0
                         m.setValue(groupMessages.get(id));
 178  0
                         m.setLocale(localeKey);
 179  0
                         messageArrayList.add(m);
 180  0
                 }
 181  0
                 messageList.setMessages(messageArrayList);
 182  
 
 183  0
                 return messageList;
 184  
         }
 185  
 
 186  
         /**
 187  
          * @see org.kuali.student.common.messages.service.MessageService#getMessagesByGroups(java.lang.String,
 188  
          *      org.kuali.student.common.messages.dto.MessageGroupKeyList)
 189  
          */
 190  
         @Override
 191  
         public MessageList getMessagesByGroups(String localeKey,
 192  
                         MessageGroupKeyList messageGroupKeyList) {
 193  
 
 194  0
                 MessageList groupMessageList = new MessageList();
 195  0
                 List<Message> messageArrayList = new ArrayList<Message>();
 196  0
                 for (String groupKey : messageGroupKeyList.getMessageGroupKeys()) {
 197  0
                         MessageList messageList = getMessages(localeKey, groupKey);
 198  0
                         messageArrayList.addAll(messageList.getMessages());
 199  0
                 }
 200  0
                 groupMessageList.setMessages(messageArrayList);
 201  
 
 202  0
                 return groupMessageList;
 203  
         }
 204  
 
 205  
         /**
 206  
          * @see org.kuali.student.common.messages.service.MessageService#updateMessage(java.lang.String,
 207  
          *      java.lang.String, java.lang.String,
 208  
          *      org.kuali.student.common.messages.dto.Message)
 209  
          */
 210  
         @Override
 211  
         public Message updateMessage(String localeKey, String messageGroupKey,
 212  
                         String messageKey, Message messageInfo) {
 213  0
                 if (getMessage(localeKey, messageGroupKey, messageKey) != null) {
 214  0
                         putMessage(localeKey, messageGroupKey, messageKey, messageInfo
 215  
                                         .getValue());
 216  
                 }
 217  0
                 return messageInfo;
 218  
         }
 219  
 
 220  
         public List<String> getMessageFiles() {
 221  0
                 return messageFiles;
 222  
         }
 223  
 
 224  
         public void setMessageFiles(List<String> messageFiles) {
 225  0
                 this.messageFiles = messageFiles;
 226  
 
 227  
                 // bootstrap the data
 228  0
                 logger.debug("Bootstrap messages started.");
 229  0
                 if (this.messages.isEmpty()) {
 230  0
                         for (String messageFileName : messageFiles) {
 231  
                                 Resource res;
 232  
                                 try {
 233  0
                                         if (messageFileName.startsWith("classpath:")) {
 234  0
                                                 res = new ClassPathResource(messageFileName
 235  
                                                                 .substring("classpath:".length()));
 236  
                                         } else {
 237  0
                                                 res = new FileSystemResource(messageFileName);
 238  
                                         }
 239  0
                                 } catch (Exception e) {
 240  0
                                         throw new RuntimeException(e);
 241  0
                                 }
 242  
 
 243  0
                                 XmlBeanFactory factory = new XmlBeanFactory(res);
 244  
 
 245  
                                 try {
 246  0
                                         Object[] beanArray = factory.getBeansOfType(
 247  
                                                         MessageEntity.class).values().toArray();
 248  0
                                         for (Object o : beanArray) {
 249  0
                                                 MessageEntity m = (MessageEntity) o;
 250  0
                                                 putMessage(m.getLocale(), m.getGroupName(), m.getMessageId(),
 251  
                                                                 m.getValue());
 252  
                                         }
 253  0
                                 } catch (Exception e) {
 254  0
                                         logger.debug(e);
 255  0
                                 }
 256  0
                         }
 257  
                 }
 258  
 
 259  0
                 logger.debug("Bootstrap messages finished.");
 260  0
         }
 261  
 
 262  
 }