| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.connector.file; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.RandomAccessFile; |
| 37 | | | import java.util.Properties; |
| 38 | | | import java.util.logging.Level; |
| 39 | | | import java.util.logging.Logger; |
| 40 | | | import javax.resource.ResourceException; |
| 41 | | | import junit.framework.Test; |
| 42 | | | import junit.framework.TestSuite; |
| 43 | | | import org.jcoderz.commons.InternalErrorException; |
| 44 | | | import org.jcoderz.commons.ServerTestCase; |
| 45 | | | import org.jcoderz.commons.util.IoUtil; |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | | |
| 53 | 0 | (1) | public class FsConnectionServerTest |
| 54 | | | extends ServerTestCase |
| 55 | | | { |
| 56 | | | |
| 57 | 0 | | private static final String CLASSNAME |
| 58 | | | = FsConnectionServerTest.class.getName(); |
| 59 | | | |
| 60 | 0 | | private static final transient Logger logger = Logger.getLogger(CLASSNAME); |
| 61 | | | |
| 62 | | | private static final int MAX_CONNECTIONS = 100; |
| 63 | | | |
| 64 | | | private static final String MSG_LOOKUP_FAILED |
| 65 | | | = "FsConnection lookup failed."; |
| 66 | | | private static final String MSG_ERROR_CLOSING |
| 67 | | | = "Error while connection closing."; |
| 68 | | | |
| 69 | | | private static final long CHUNK_SIZE = 1000; |
| 70 | | | |
| 71 | | | |
| 72 | | | |
| 73 | | | @return |
| 74 | | | |
| 75 | | | public static Test suite () |
| 76 | | | { |
| 77 | | | final TestSuite suite; |
| 78 | 0 | | if (hasTestCases()) |
| 79 | | | { |
| 80 | 0 | | suite = getSuite(FsConnectionServerTest.class); |
| 81 | | | } |
| 82 | | | else |
| 83 | | | { |
| 84 | 0 | | suite = new TestSuite(FsConnectionServerTest.class); |
| 85 | | | } |
| 86 | 0 | | return suite; |
| 87 | | | } |
| 88 | | | |
| 89 | | | |
| 90 | | | |
| 91 | | | |
| 92 | | | public void testConnectionLookup () |
| 93 | | | { |
| 94 | | | try |
| 95 | | | { |
| 96 | 0 | | final FsConnection fc = FsConnectionUtil.getFileSystemConnection(); |
| 97 | 0 | | assertNotNull(MSG_LOOKUP_FAILED, fc); |
| 98 | 0 | | FsConnectionUtil.close(fc); |
| 99 | | | } |
| 100 | 0 | | catch (InternalErrorException iee) |
| 101 | | | { |
| 102 | 0 | | iee.log(); |
| 103 | 0 | | fail(MSG_LOOKUP_FAILED + ", caught exception " + iee.getMessage()); |
| 104 | 0 | | } |
| 105 | 0 | | } |
| 106 | | | |
| 107 | | | |
| 108 | | | |
| 109 | | | |
| 110 | | | |
| 111 | | | public void testCloseConnection () |
| 112 | | | { |
| 113 | 0 | | for (int i = 0; i < MAX_CONNECTIONS; i++) |
| 114 | | | { |
| 115 | 0 | | final FsConnection fc = FsConnectionUtil.getFileSystemConnection(); |
| 116 | | | try |
| 117 | | | { |
| 118 | 0 | | fc.close(); |
| 119 | | | } |
| 120 | 0 | | catch (ResourceException e) |
| 121 | | | { |
| 122 | 0 | | logger.log(Level.SEVERE, MSG_ERROR_CLOSING, e); |
| 123 | 0 | | fail(MSG_ERROR_CLOSING); |
| 124 | 0 | | } |
| 125 | | | } |
| 126 | 0 | | } |
| 127 | | | |
| 128 | | | |
| 129 | | | {@link } |
| 130 | | | |
| 131 | | | public void testCloseConnection3 () |
| 132 | | | { |
| 133 | 0 | | for (int i = 0; i < MAX_CONNECTIONS; i++) |
| 134 | | | { |
| 135 | 0 | | final FsConnection fc1 = FsConnectionUtil.getFileSystemConnection(); |
| 136 | 0 | | final FsConnection fc2 = FsConnectionUtil.getFileSystemConnection(); |
| 137 | 0 | | final FsConnection fc3 = FsConnectionUtil.getFileSystemConnection(); |
| 138 | | | try |
| 139 | | | { |
| 140 | 0 | | fc1.close(); |
| 141 | 0 | | fc2.close(); |
| 142 | 0 | | fc3.close(); |
| 143 | | | } |
| 144 | 0 | | catch (ResourceException e) |
| 145 | | | { |
| 146 | 0 | | logger.log(Level.SEVERE, MSG_ERROR_CLOSING, e); |
| 147 | 0 | | fail(MSG_ERROR_CLOSING); |
| 148 | 0 | | } |
| 149 | | | } |
| 150 | 0 | | } |
| 151 | | | |
| 152 | | | |
| 153 | | | {@link } |
| 154 | | | |
| 155 | | | |
| 156 | | | public void testCreateTempFile () |
| 157 | | | { |
| 158 | 0 | | final FsConnection fc = FsConnectionUtil.getFileSystemConnection(); |
| 159 | | | try |
| 160 | | | { |
| 161 | 0 | | final String f = fc.createTempFile(); |
| 162 | 0 | | logger.finer("Created temp file " + f); |
| 163 | 0 | | fc.deleteFile(f); |
| 164 | 0 | | fc.close(); |
| 165 | | | } |
| 166 | 0 | | catch (ResourceException e) |
| 167 | | | { |
| 168 | 0 | | final String msg = "testCreateTempFile failed"; |
| 169 | 0 | | logger.log(Level.SEVERE, msg, e); |
| 170 | 0 | | fail(msg); |
| 171 | 0 | | } |
| 172 | 0 | | } |
| 173 | | | |
| 174 | | | |
| 175 | | | |
| 176 | | | |
| 177 | | | public void testBigFile () |
| 178 | | | { |
| 179 | 0 | | final String msg = "testBigFile failed"; |
| 180 | 0 | | final FsConnection fc = FsConnectionUtil.getFileSystemConnection(); |
| 181 | 0 | | RandomAccessFile raf = null; |
| 182 | | | try |
| 183 | | | { |
| 184 | 0 | | final String f = fc.createTempFile(); |
| 185 | 0 | | logger.finer("Created temp file " + f); |
| 186 | 0 | | final String copyF = f + "Copy"; |
| 187 | 0 | | raf = fc.getRandomAccessFile(f, "rw"); |
| 188 | | | |
| 189 | 0 | | raf.seek(FsConnectionFactory.FILE_TRANSFER_CHUNK_SIZE_DEF_VALUE + 1); |
| 190 | 0 | | raf.writeChars("Just a teststring!"); |
| 191 | 0 | | logger.finer("Writen " + raf.length() + " bytes into the file " + f); |
| 192 | 0 | | IoUtil.close(raf); |
| 193 | 0 | | raf = null; |
| 194 | | | |
| 195 | 0 | | fc.moveFile(f, copyF); |
| 196 | 0 | | logger.finer("Moved " + f + " to " + copyF); |
| 197 | 0 | | assertTrue("The destination file " + copyF |
| 198 | | | + " does not exist after the file moving.", fc.isExists(copyF)); |
| 199 | 0 | | assertFalse("Just moved file " + copyF + " does exits", |
| 200 | | | fc.isExists(f)); |
| 201 | | | |
| 202 | 0 | | fc.deleteFile(copyF); |
| 203 | 0 | | logger.finer("Deleted " + copyF); |
| 204 | 0 | | assertFalse("Deleted file " + copyF + " exists.", fc.isExists(copyF)); |
| 205 | | | } |
| 206 | 0 | | catch (ResourceException r) |
| 207 | | | { |
| 208 | 0 | | logger.log(Level.SEVERE, msg, r); |
| 209 | 0 | | fail(msg); |
| 210 | | | } |
| 211 | 0 | | catch (Exception e) |
| 212 | | | { |
| 213 | 0 | | logger.log(Level.SEVERE, msg, e); |
| 214 | 0 | | fail(msg); |
| 215 | 0 | | } |
| 216 | | | |
| 217 | 0 | | IoUtil.close(raf); |
| 218 | 0 | | FsConnectionUtil.close(fc); |
| 219 | 0 | | } |
| 220 | | | |
| 221 | | | |
| 222 | | | |
| 223 | | | |
| 224 | | | public void testUserProperties () |
| 225 | | | { |
| 226 | 0 | | final String msg = "testUserProperties failed"; |
| 227 | 0 | | final Properties props = new Properties(); |
| 228 | 0 | | FsConnection fc = FsConnectionUtil.getFileSystemConnection(); |
| 229 | 0 | | RandomAccessFile raf = null; |
| 230 | | | try |
| 231 | | | { |
| 232 | 0 | | final String tmpDir = fc.createTempFile(); |
| 233 | 0 | | fc.deleteFile(tmpDir); |
| 234 | 0 | | fc.close(); |
| 235 | 0 | | final File dir = new File(tmpDir); |
| 236 | 0 | (2) | dir.mkdirs(); |
| 237 | | | |
| 238 | 0 | | props.setProperty(FsConnectionFactory.PROP_TEMP_DIR, dir.toString()); |
| 239 | 0 | | props.setProperty(FsConnectionFactory.PROP_FILE_TRANSFER_CHUNK_SIZE, |
| 240 | | | String.valueOf(CHUNK_SIZE)); |
| 241 | 0 | | fc = FsConnectionUtil.getFileSystemConnection(props); |
| 242 | | | |
| 243 | 0 | | final String tmp = fc.createTempFile(); |
| 244 | 0 | | final File tmpFile = new File(tmp); |
| 245 | 0 | | assertEquals("Newly created temp file " + tmpFile |
| 246 | | | + " has a wrong parent dir. Expected " + dir, |
| 247 | | | tmpFile.getParentFile(), dir); |
| 248 | | | |
| 249 | 0 | | raf = fc.getRandomAccessFile(tmp, "rw"); |
| 250 | | | |
| 251 | 0 | | raf.seek(CHUNK_SIZE + CHUNK_SIZE); |
| 252 | 0 | | raf.writeChars("Just a teststring!"); |
| 253 | 0 | | logger.finer("Writen " + raf.length() + " bytes into the file " |
| 254 | | | + tmpFile); |
| 255 | 0 | | IoUtil.close(raf); |
| 256 | | | |
| 257 | 0 | | final String copyF = tmp + "Copy"; |
| 258 | | | |
| 259 | 0 | | fc.moveFile(tmp, copyF); |
| 260 | 0 | | fc.deleteFile(copyF); |
| 261 | 0 | (3) | dir.delete(); |
| 262 | | | } |
| 263 | 0 | | catch (Exception e) |
| 264 | | | { |
| 265 | 0 | | logger.log(Level.SEVERE, msg, e); |
| 266 | 0 | | fail(msg); |
| 267 | 0 | | } |
| 268 | | | |
| 269 | 0 | | IoUtil.close(raf); |
| 270 | 0 | | FsConnectionUtil.close(fc); |
| 271 | 0 | | } |
| 272 | | | } |