1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   */
18  package org.apache.commons.i18n;
19  
20  import junit.framework.TestCase;
21  
22  import java.util.Locale;
23  import java.util.Map;
24  import java.util.HashMap;
25  import java.text.MessageFormat;
26  
27  /**
28   * The <code>MockProviderTestBase</code> class serves as a base class for test cases using a mock
29   * <code>MessageProvider</code>. After every test, it will remove the mock message provider to prepare
30   * for other tests.
31   * @author Mattias Jiderhamn
32   */
33  public abstract class MockProviderTestBase extends TestCase {
34  
35      public void tearDown() {
36          /* Remove mock provider after each test, to allow for MessageNotFoundExceptions */
37          MessageManager.clearMessageProviders();
38      }
39  
40      /**
41       * Add mock provider to <code>MessageManager</code>.
42       */
43      protected void addMockProvider() {
44          addMockProvider("mock");
45      }
46  
47      /**
48       * Add mock provider to <code>MessageManager</code>.
49       */
50      protected void addMockProvider(final String providerId) {
51           //  Mock message provider that returns a string made up of the arguments passed to it.
52          MessageProvider mockMessageProvider = new MessageProvider() {
53              public String getText(String id, String entry, Locale locale) throws MessageNotFoundException {
54                  return MockProviderTestBase.getMockString(providerId, id, entry, locale);
55              }
56  
57              public Map getEntries(String id, Locale locale) throws MessageNotFoundException {
58                  Map output = new HashMap();
59                  output.put("entry1", MockProviderTestBase.getMockString(providerId,id,"entry1",locale));
60                  output.put("entry2", MockProviderTestBase.getMockString(providerId,id,"entry2",locale));
61                  return output;
62              }
63          };
64  
65          MessageManager.addMessageProvider(providerId, mockMessageProvider);
66      }
67  
68      /**
69       * Add provider that always throws error to <code>MessageManager</code>.
70       */
71      protected void addThrowingMockProvider() {
72          MessageManager.addMessageProvider("throwingMock", new MessageProvider() {
73              public String getText(String id, String entry, Locale locale) throws MessageNotFoundException {
74                  return null;
75              }
76  
77              public Map getEntries(String id, Locale locale) throws MessageNotFoundException {
78                  throw new MessageNotFoundException("Mock exception from getEntries()");
79              }
80          });
81      }
82  
83      protected void removeThrowingMockProvider() {
84          MessageManager.removeMessageProvider("throwingMock");
85      }
86  
87      ////////////////////////////////////////////////////////////////////////
88      // Utility methods
89      ////////////////////////////////////////////////////////////////////////
90  
91      public static String getMockString(String providerId, String id, String entry, Locale locale) throws MessageNotFoundException {
92          return ((providerId != null) ? "Source=" + providerId + " " : "") +
93                  "Id=" + id + " Entry=" + entry + " Locale=" + locale + "";
94      }
95  
96      public static String getMockString(String id, String entry, Locale locale) throws MessageNotFoundException {
97          return getMockString("mock", id, entry, locale);
98      }
99  
100     public static String getFormattedMockString(String providerId, String id, String entry, String[] arguments, Locale locale) {
101         return MessageFormat.format(getMockString(providerId, id, entry, locale), arguments);
102     }
103 
104     public static String getFormattedMockString(String id, String entry, String[] arguments, Locale locale) {
105         return MessageFormat.format(getMockString(id, entry, locale), arguments);
106     }
107 }