FileEntryTest.groovy revision d150a70ca8f8ad6cc300cf56174c8846066a4ad4
1/* 2 * Copyright 2008 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.fake.filesystem 17 18import org.apache.log4j.Logger 19import org.mockftpserver.core.util.IoUtil 20 21 22/** 23 * Tests for FileEntry 24 * 25 * @version $Revision$ - $Date$ 26 * 27 * @author Chris Mair 28 */ 29public class FileEntryTest extends AbstractFileSystemEntryTest { 30 31 private static final LOG = Logger.getLogger(FileEntryTest) 32 private static final CONTENTS = "abc 123 %^& xxx" 33 34 private FileEntry entry 35 36 void testConstructorWithStringContents() { 37 entry = new FileEntry(PATH, CONTENTS) 38 verifyContents(CONTENTS) 39 } 40 41 void testSettingContentsFromString() { 42 entry.setContents(CONTENTS) 43 verifyContents(CONTENTS) 44 } 45 46 void testSettingContentsFromBytes() { 47 byte[] contents = CONTENTS.getBytes() 48 entry.setContents(contents) 49 // Now corrupt the original byte array to make sure the file entry is not affected 50 contents[1] = (byte) '#' 51 verifyContents(CONTENTS) 52 } 53 54 void testSetContents_NullString() { 55 entry.setContents((String) null) 56 assert entry.size == 0 57 } 58 59 void testSetContents_NullBytes() { 60 entry.setContents((byte[]) null) 61 assert entry.size == 0 62 } 63 64 void testCreateOutputStream() { 65 // New, empty file 66 OutputStream out = entry.createOutputStream(false) 67 out.write(CONTENTS.getBytes()) 68 verifyContents(CONTENTS) 69 70 // Another OutputStream, append=false 71 out = entry.createOutputStream(false) 72 out.write(CONTENTS.getBytes()) 73 verifyContents(CONTENTS) 74 75 // Another OutputStream, append=true 76 out = entry.createOutputStream(true) 77 out.write(CONTENTS.getBytes()) 78 verifyContents(CONTENTS + CONTENTS) 79 80 // Set contents directly 81 final String NEW_CONTENTS = ",./'\t\r[]-\n=" 82 entry.setContents(NEW_CONTENTS) 83 verifyContents(NEW_CONTENTS) 84 85 // New OutputStream, append=true (so should append to contents we set directly) 86 out = entry.createOutputStream(true) 87 out.write(CONTENTS.getBytes()) 88 verifyContents(NEW_CONTENTS + CONTENTS) 89 90 // Yet another OutputStream, append=true (so should append to accumulated contents) 91 OutputStream out2 = entry.createOutputStream(true) 92 out2.write(CONTENTS.getBytes()) 93 out2.close() // should have no effect 94 verifyContents(NEW_CONTENTS + CONTENTS + CONTENTS) 95 96 // Write with the previous OutputStream (simulate 2 OututStreams writing "concurrently") 97 out.write(NEW_CONTENTS.getBytes()) 98 verifyContents(NEW_CONTENTS + CONTENTS + CONTENTS + NEW_CONTENTS) 99 } 100 101 void testCreateInputStream_NullContents() { 102 verifyContents("") 103 } 104 105 void testCloneWithNewPath() { 106 entry.lastModified = LAST_MODIFIED 107 entry.owner = USER 108 entry.group = GROUP 109 entry.permissions = PERMISSIONS 110 entry.setContents('abc') 111 def clone = entry.cloneWithNewPath(NEW_PATH) 112 113 assert !clone.is(entry) 114 assert clone.path == NEW_PATH 115 assert clone.lastModified == LAST_MODIFIED 116 assert clone.owner == USER 117 assert clone.group == GROUP 118 assert clone.permissions == PERMISSIONS 119 assert Arrays.equals(clone.bytes, entry.bytes) 120 assert !clone.directory 121 } 122 123// void testEquals() { 124// assert entry.equals(entry) 125// assert entry.equals(new FileEntry(path:PATH, lastModified:LAST_MODIFIED)) 126// assert entry.equals(new FileEntry(path:PATH, lastModified:new Date())) // lastModified ignored 127// 128// assert !entry.equals(new FileEntry("xyz", lastModified:LAST_MODIFIED)) 129// assert !entry.equals(new FileEntry(path:PATH, contents:'abc', lastModified:LAST_MODIFIED)) 130// assert !entry.equals("ABC") 131// assert !entry.equals(null) 132// } 133// 134// void testHashCode() { 135// assert entry.hashCode() == entry.hashCode() 136// assert entry.hashCode() == new FileEntry(path:PATH, contents:'abc', lastModified:LAST_MODIFIED).hashCode() 137// assert entry.hashCode() == new FileEntry(path:PATH, contents:'abc', new Date()).hashCode() // lastModified ignored 138// 139// assert entry.hashCode() != new FileEntry(path:PATH, contents:'abc', lastModified:LAST_MODIFIED).hashCode() 140// assert entry.hashCode() != new FileEntry(path:PATH, contents:'abcdef', lastModified:LAST_MODIFIED).hashCode() 141// 142// assert entry.hashCode() == new DirectoryEntry(path:PATH, lastModified:LAST_MODIFIED).hashCode() 143// } 144 145 //------------------------------------------------------------------------- 146 // Implementation of Required Abstract Methods 147 //------------------------------------------------------------------------- 148 149 /** 150 * @see org.mockftpserver.fake.filesystem.AbstractFileSystemEntryTest#getImplementationClass() 151 */ 152 protected Class getImplementationClass() { 153 return FileEntry.class 154 } 155 156 /** 157 * @see org.mockftpserver.fake.filesystem.AbstractFileSystemEntryTest#isDirectory() 158 */ 159 protected boolean isDirectory() { 160 return false 161 } 162 163 //------------------------------------------------------------------------- 164 // Test setup 165 //------------------------------------------------------------------------- 166 167 /** 168 * @see org.mockftpserver.test.AbstractTest#setUp() 169 */ 170 void setUp() { 171 super.setUp() 172 entry = new FileEntry(PATH) 173 } 174 175 //------------------------------------------------------------------------- 176 // Internal Helper Methods 177 //------------------------------------------------------------------------- 178 179 /** 180 * Verify the expected contents of the file entry, read from its InputSteam 181 * @param expectedContents - the expected contents 182 * @throws IOException 183 */ 184 private void verifyContents(String expectedContents) { 185 byte[] bytes = IoUtil.readBytes(entry.createInputStream()) 186 LOG.info("bytes=[" + new String(bytes) + "]") 187 assertEquals("contents: actual=[" + new String(bytes) + "]", expectedContents.getBytes(), bytes) 188 assert entry.getSize() == expectedContents.length() 189 } 190 191} 192