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