1   /*
2    * Copyright 2002-2006 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.binding.form.support;
17  
18  import java.util.Arrays;
19  
20  import junit.framework.TestCase;
21  
22  import org.springframework.util.ObjectUtils;
23  
24  /**
25   * @author Mathias Broekelmann
26   * 
27   */
28  public class DefaultMessageCodeStrategyTests extends TestCase {
29  
30      private DefaultMessageCodeStrategy strategy;
31  
32      protected void setUp() throws Exception {
33          strategy = new DefaultMessageCodeStrategy();
34      }
35  
36      protected void tearDown() throws Exception {
37          strategy = null;
38      }
39  
40      public final void testGetMessageCodesEmptyArgs() {
41          String[] emptyValues = new String[0];
42          assertEquals(emptyValues, strategy.getMessageCodes(null, null, null));
43          assertEquals(emptyValues, strategy.getMessageCodes(null, "", null));
44          assertEquals(emptyValues, strategy.getMessageCodes("", "", null));
45          assertEquals(emptyValues, strategy.getMessageCodes("", "", emptyValues));
46          assertEquals(emptyValues, strategy.getMessageCodes("", "", new String[] { "" }));
47          assertEquals(emptyValues, strategy.getMessageCodes("", "", new String[] { "", "" }));
48      }
49  
50      public final void testGetMessageCodesNullContextNullSuffixes() {
51          assertEquals(new String[] { "simpleField" }, strategy.getMessageCodes(null, "simpleField", null));
52          assertEquals(new String[] { "fieldbase.simpleField", "simpleField" }, strategy.getMessageCodes(null,
53                  "fieldbase.simpleField", null));
54      }
55  
56      public final void testGetMessageCodesWithContext() {
57          assertEquals(new String[] { "context.fieldbase.simpleField", "context.simpleField", "fieldbase.simpleField",
58                  "simpleField" }, strategy.getMessageCodes("context", "fieldbase.simpleField", null));
59      }
60  
61      public final void testGetMessageCodesWithSuffix() {
62          assertEquals(new String[] { "simpleField.suffix" }, strategy.getMessageCodes(null, "simpleField",
63                  new String[] { "suffix" }));
64          assertEquals(new String[] { "simpleField.suffix1", "simpleField" }, strategy.getMessageCodes(null,
65                  "simpleField", new String[] { "suffix1", "" }));
66          assertEquals(new String[] { "simpleField.suffix1", "simpleField.suffix2" }, strategy.getMessageCodes(null,
67                  "simpleField", new String[] { "suffix1", "suffix2" }));
68      }
69  
70      public final void testGetMessageCodesWithContextAndSuffix() {
71          assertEquals(new String[] { "context.fieldbase.simpleField.suffix", "context.simpleField.suffix",
72                  "fieldbase.simpleField.suffix", "simpleField.suffix" }, strategy.getMessageCodes("context",
73                  "fieldbase.simpleField", new String[] { "suffix" }));
74      }
75  
76      protected void assertEquals(Object[] expected, Object[] actual) {
77          if (!Arrays.equals(expected, actual)) {
78              fail(buildMessage(expected, actual));
79          }
80      }
81  
82      private String buildMessage(Object[] expected, Object[] actual) {
83          return "expected " + ObjectUtils.nullSafeToString(expected) + ", got " + ObjectUtils.nullSafeToString(actual);
84      }
85  
86      protected void assertEquals(String message, Object[] expected, Object[] actual) {
87          if (!Arrays.equals(expected, actual)) {
88              fail(message);
89          }
90      }
91  }