001    /*
002     * Copyright 2002-2006 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.binding.form.support;
017    
018    import java.util.Arrays;
019    
020    import junit.framework.TestCase;
021    
022    import org.springframework.util.ObjectUtils;
023    
024    /**
025     * @author Mathias Broekelmann
026     * 
027     */
028    public class DefaultMessageCodeStrategyTests extends TestCase {
029    
030        private DefaultMessageCodeStrategy strategy;
031    
032        protected void setUp() throws Exception {
033            strategy = new DefaultMessageCodeStrategy();
034        }
035    
036        protected void tearDown() throws Exception {
037            strategy = null;
038        }
039    
040        public final void testGetMessageCodesEmptyArgs() {
041            String[] emptyValues = new String[0];
042            assertEquals(emptyValues, strategy.getMessageCodes(null, null, null));
043            assertEquals(emptyValues, strategy.getMessageCodes(null, "", null));
044            assertEquals(emptyValues, strategy.getMessageCodes("", "", null));
045            assertEquals(emptyValues, strategy.getMessageCodes("", "", emptyValues));
046            assertEquals(emptyValues, strategy.getMessageCodes("", "", new String[] { "" }));
047            assertEquals(emptyValues, strategy.getMessageCodes("", "", new String[] { "", "" }));
048        }
049    
050        public final void testGetMessageCodesNullContextNullSuffixes() {
051            assertEquals(new String[] { "simpleField" }, strategy.getMessageCodes(null, "simpleField", null));
052            assertEquals(new String[] { "fieldbase.simpleField", "simpleField" }, strategy.getMessageCodes(null,
053                    "fieldbase.simpleField", null));
054        }
055    
056        public final void testGetMessageCodesWithContext() {
057            assertEquals(new String[] { "context.fieldbase.simpleField", "context.simpleField", "fieldbase.simpleField",
058                    "simpleField" }, strategy.getMessageCodes("context", "fieldbase.simpleField", null));
059        }
060    
061        public final void testGetMessageCodesWithSuffix() {
062            assertEquals(new String[] { "simpleField.suffix" }, strategy.getMessageCodes(null, "simpleField",
063                    new String[] { "suffix" }));
064            assertEquals(new String[] { "simpleField.suffix1", "simpleField" }, strategy.getMessageCodes(null,
065                    "simpleField", new String[] { "suffix1", "" }));
066            assertEquals(new String[] { "simpleField.suffix1", "simpleField.suffix2" }, strategy.getMessageCodes(null,
067                    "simpleField", new String[] { "suffix1", "suffix2" }));
068        }
069    
070        public final void testGetMessageCodesWithContextAndSuffix() {
071            assertEquals(new String[] { "context.fieldbase.simpleField.suffix", "context.simpleField.suffix",
072                    "fieldbase.simpleField.suffix", "simpleField.suffix" }, strategy.getMessageCodes("context",
073                    "fieldbase.simpleField", new String[] { "suffix" }));
074        }
075    
076        protected void assertEquals(Object[] expected, Object[] actual) {
077            if (!Arrays.equals(expected, actual)) {
078                fail(buildMessage(expected, actual));
079            }
080        }
081    
082        private String buildMessage(Object[] expected, Object[] actual) {
083            return "expected " + ObjectUtils.nullSafeToString(expected) + ", got " + ObjectUtils.nullSafeToString(actual);
084        }
085    
086        protected void assertEquals(String message, Object[] expected, Object[] actual) {
087            if (!Arrays.equals(expected, actual)) {
088                fail(message);
089            }
090        }
091    }