ReplyTextBundleUtilTest.java revision 77391c2a01ce1fed085906743cc240a4d58edd92
1/*
2 * Copyright 2007 the original author or authors.
3 *
4 * Licensed under the Apache 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.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,
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 */
16package org.mockftpserver.core.command;
17
18import java.util.ListResourceBundle;
19import java.util.ResourceBundle;
20
21import org.apache.log4j.Logger;
22import org.mockftpserver.core.util.AssertFailedException;
23import org.mockftpserver.test.AbstractTest;
24
25/**
26 * Tests for the ReplyTextBundleUtil class.
27 *
28 * @version $Revision$ - $Date$
29 *
30 * @author Chris Mair
31 */
32public final class ReplyTextBundleUtilTest extends AbstractTest {
33
34    private static final Logger LOG = Logger.getLogger(ReplyTextBundleUtilTest.class);
35
36    private ResourceBundle resourceBundle1;
37    private ResourceBundle resourceBundle2;
38
39    /**
40     * Test the setReplyTextBundleIfAppropriate() method, when the CommandHandler implements
41     * the ResourceBundleAware interface, and the replyTextBundle has not yet been set.
42     */
43    public void testSetReplyTextBundleIfAppropriate_ReplyTextBundleAware_NotSetYet() {
44        AbstractCommandHandler commandHandler = new StaticReplyCommandHandler();
45        ReplyTextBundleUtil.setReplyTextBundleIfAppropriate(commandHandler, resourceBundle1);
46        assertSame(resourceBundle1, commandHandler.getReplyTextBundle());
47    }
48
49    /**
50     * Test the setReplyTextBundleIfAppropriate() method, when the CommandHandler implements
51     * the ResourceBundleAware interface, and the replyTextBundle has already been set.
52     */
53    public void testSetReplyTextBundleIfAppropriate_ReplyTextBundleAware_AlreadySet() {
54        AbstractCommandHandler commandHandler = new StaticReplyCommandHandler();
55        commandHandler.setReplyTextBundle(resourceBundle2);
56        ReplyTextBundleUtil.setReplyTextBundleIfAppropriate(commandHandler, resourceBundle1);
57        assertSame(resourceBundle2, commandHandler.getReplyTextBundle());
58    }
59
60    /**
61     * Test the setReplyTextBundleIfAppropriate() method, when the CommandHandler does not
62     * implement the ResourceBundleAware interface.
63     */
64    public void testSetReplyTextBundleIfAppropriate_NotReplyTextBundleAware() {
65        CommandHandler commandHandler = (CommandHandler) createMock(CommandHandler.class);
66        replay(commandHandler);
67        ReplyTextBundleUtil.setReplyTextBundleIfAppropriate(commandHandler, resourceBundle1);
68        verify(commandHandler);         // expect no method calls
69    }
70
71    /**
72     * Test the setReplyTextBundleIfAppropriate() method, when the CommandHandler is null.
73     */
74    public void testSetReplyTextBundleIfAppropriate_NullCommandHandler() {
75        try {
76            ReplyTextBundleUtil.setReplyTextBundleIfAppropriate(null, resourceBundle1);
77            fail("Expected AssertFailedException");
78        }
79        catch (AssertFailedException expected) {
80            LOG.info("Expected: " + expected);
81        }
82    }
83
84    /**
85     * @see org.mockftpserver.test.AbstractTest#setUp()
86     */
87    protected void setUp() throws Exception {
88        super.setUp();
89
90        resourceBundle1 = new ListResourceBundle() {
91            protected Object[][] getContents() {
92                return null;
93            }
94        };
95
96        resourceBundle2 = new ListResourceBundle() {
97            protected Object[][] getContents() {
98                return new Object[][] { { "a", "b" } };
99            }
100        };
101    }
102
103}
104