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