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 org.apache.log4j.Logger;
19import org.mockftpserver.core.util.AssertFailedException;
20import org.mockftpserver.test.AbstractTestCase;
21
22import java.util.Date;
23import java.util.HashSet;
24import java.util.Set;
25
26/**
27 * Tests for InvocationRecord
28 *
29 * @version $Revision$ - $Date$
30 *
31 * @author Chris Mair
32 */
33public final class InvocationRecordTest extends AbstractTestCase {
34
35    private static final Logger LOG = Logger.getLogger(InvocationRecordTest.class);
36    private static final Command COMMAND = new Command("command", EMPTY);
37    private static final String KEY1 = "key1";
38    private static final String KEY2 = "key2";
39    private static final String STRING = "abc123";
40    private static final Integer INT = new Integer(77);
41
42    private InvocationRecord invocationRecord;
43
44    /**
45     * Test the Constructor
46     */
47    public void testConstructor() {
48        final Command COMMAND = new Command("ABC", EMPTY);
49        long beforeTime = System.currentTimeMillis();
50        InvocationRecord commandInvocation = new InvocationRecord(COMMAND, DEFAULT_HOST);
51        long afterTime = System.currentTimeMillis();
52        LOG.info(commandInvocation);
53        assertEquals("Command", COMMAND, commandInvocation.getCommand());
54        assertTrue("time", commandInvocation.getTime().getTime() >= beforeTime
55                && commandInvocation.getTime().getTime() <= afterTime);
56        assertEquals("host", DEFAULT_HOST, commandInvocation.getClientHost());
57    }
58
59    /**
60     * Test the set() method, passing in a null key
61     */
62    public void testSet_NullKey() {
63        try {
64            invocationRecord.set(null, STRING);
65            fail("Expected AssertFailedException");
66        }
67        catch (AssertFailedException expected) {
68            LOG.info("Expected: " + expected);
69        }
70    }
71
72    /**
73     * Test the set() method, passing in a null value
74     */
75    public void testSet_NullValue() {
76        invocationRecord.set(KEY1, null);
77        assertNull(KEY1, invocationRecord.getObject(KEY1));
78    }
79
80    /**
81     * Test the containsKey() method
82     */
83    public void testContainsKey() {
84        invocationRecord.set(KEY1, STRING);
85        assertTrue(KEY1, invocationRecord.containsKey(KEY1));
86        assertFalse(KEY2, invocationRecord.containsKey(KEY2));
87    }
88
89    /**
90     * Test the containsKey() method, passing in a null key
91     */
92    public void testContainsKey_Null() {
93        try {
94            invocationRecord.containsKey(null);
95            fail("Expected AssertFailedException");
96        }
97        catch (AssertFailedException expected) {
98            LOG.info("Expected: " + expected);
99        }
100    }
101
102    /**
103     * Test the getString() method
104     */
105    public void testGetString() {
106        assertNull("undefined", invocationRecord.getString("UNDEFINED"));
107        invocationRecord.set(KEY1, STRING);
108        assertEquals(KEY1, STRING, invocationRecord.getString(KEY1));
109    }
110
111    /**
112     * Test the getString() method, passing in a null key
113     */
114    public void testGetString_Null() {
115        try {
116            invocationRecord.getString(null);
117            fail("Expected AssertFailedException");
118        }
119        catch (AssertFailedException expected) {
120            LOG.info("Expected: " + expected);
121        }
122    }
123
124    /**
125     * Test the getString() method, when the value for the key is not a String
126     */
127    public void testGetString_NotAString() {
128
129        invocationRecord.set(KEY1, INT);
130        try {
131            invocationRecord.getString(KEY1);
132            fail("Expected ClassCastException");
133        }
134        catch (ClassCastException expected) {
135            LOG.info("Expected: " + expected);
136        }
137    }
138
139    /**
140     * Test the getObject() method
141     */
142    public void testGetObject() {
143        assertNull("undefined", invocationRecord.getObject("UNDEFINED"));
144        invocationRecord.set(KEY1, STRING);
145        assertEquals(KEY1, STRING, invocationRecord.getObject(KEY1));
146    }
147
148    /**
149     * Test the keySet() method
150     */
151    public void testKeySet() {
152        Set set = new HashSet();
153        assertEquals("empty", set, invocationRecord.keySet());
154        invocationRecord.set(KEY1, STRING);
155        invocationRecord.set(KEY2, STRING);
156        set.add(KEY1);
157        set.add(KEY2);
158        assertEquals("2", set, invocationRecord.keySet());
159    }
160
161    /**
162     * Test that the keySet() return value does not allow breaking immutability
163     */
164    public void testKeySet_Immutability() {
165        Set keySet = invocationRecord.keySet();
166        try {
167            keySet.add("abc");
168            fail("Expected UnsupportedOperationException");
169        }
170        catch (UnsupportedOperationException expected) {
171            LOG.info("Expected: " + expected);
172        }
173    }
174
175    /**
176     * Test the getObject() method, passing in a null key
177     */
178    public void testGetObject_Null() {
179        try {
180            invocationRecord.getObject(null);
181            fail("Expected AssertFailedException");
182        }
183        catch (AssertFailedException expected) {
184            LOG.info("Expected: " + expected);
185        }
186    }
187
188    /**
189     * Test the lock() method
190     */
191    public void testLock() {
192        assertFalse("locked - before", invocationRecord.isLocked());
193        invocationRecord.set(KEY1, STRING);
194        invocationRecord.lock();
195        assertTrue("locked - after", invocationRecord.isLocked());
196        try {
197            invocationRecord.set(KEY2, "abc");
198            fail("Expected AssertFailedException");
199        }
200        catch (AssertFailedException expected) {
201            LOG.info("Expected: " + expected);
202        }
203    }
204
205    /**
206     * Test that the getTime() return value does not break immutability
207     */
208    public void testGetTime_Immutability() {
209
210        Date timestamp = invocationRecord.getTime();
211        long timeInMillis = timestamp.getTime();
212        timestamp.setTime(12345L);
213        assertEquals("time", timeInMillis, invocationRecord.getTime().getTime());
214    }
215
216    /**
217     * Perform initialization before each test
218     *
219     * @see org.mockftpserver.test.AbstractTestCase#setUp()
220     */
221    protected void setUp() throws Exception {
222        super.setUp();
223        invocationRecord = new InvocationRecord(COMMAND, DEFAULT_HOST);
224    }
225}
226