1 4 package com.tc.util; 5 6 import com.tc.exception.ImplementMe; 7 import com.tc.io.TCFile; 8 import com.tc.io.TCFileChannel; 9 import com.tc.io.TCFileLock; 10 import com.tc.io.TCRandomFileAccess; 11 import com.tc.test.TCTestCase; 12 import com.tc.util.startuplock.FileNotCreatedException; 13 import com.tc.util.startuplock.LocationNotCreatedException; 14 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.nio.channels.OverlappingFileLockException ; 18 19 public class StartupLockTest extends TCTestCase { 20 21 private final int lockedAlreadyOnThisVM = 0; 22 private final int lockCanBeAquired = 1; 23 24 public void testBasics() throws Throwable { 25 TestTCRandomFileAccessImpl randomFileAccess = new TestTCRandomFileAccessImpl(); 26 27 boolean locationIsMakable = false; 28 boolean fileIsmakable = false; 29 TestTCFileImpl location = new TestTCFileImpl(locationIsMakable); 30 location.setNewFileIsMakable(fileIsmakable); 31 StartupLock startupLock = new StartupLock(location); 32 try { 33 startupLock.canProceed(randomFileAccess, true); 34 fail("Expected LocationNotCreatedException not thrown."); 35 } catch (LocationNotCreatedException se) { 36 } 38 Assert.assertFalse(location.exists()); 39 40 locationIsMakable = true; 41 fileIsmakable = false; 42 location = new TestTCFileImpl(locationIsMakable); 43 location.setNewFileIsMakable(fileIsmakable); 44 startupLock = new StartupLock(location); 45 try { 46 startupLock.canProceed(randomFileAccess, true); 47 fail("Expected FileNotCreatedException not thrown."); 48 } catch (FileNotCreatedException se) { 49 } 51 Assert.assertTrue(location.exists()); 52 53 randomFileAccess.setLockAvailability(lockedAlreadyOnThisVM); 54 locationIsMakable = true; 55 fileIsmakable = true; 56 location = new TestTCFileImpl(locationIsMakable); 57 location.setNewFileIsMakable(fileIsmakable); 58 startupLock = new StartupLock(location); 59 boolean result = startupLock.canProceed(randomFileAccess, true); 60 Assert.assertFalse(result); 61 62 randomFileAccess.setLockAvailability(lockCanBeAquired); 63 locationIsMakable = true; 64 fileIsmakable = true; 65 location = new TestTCFileImpl(locationIsMakable); 66 location.setNewFileIsMakable(fileIsmakable); 67 startupLock = new StartupLock(location); 68 result = startupLock.canProceed(randomFileAccess, true); 69 Assert.assertTrue(location.exists()); 70 Assert.assertTrue(result); 71 } 72 73 private class TestTCRandomFileAccessImpl implements TCRandomFileAccess { 74 private int lockAvailability; 75 76 public void setLockAvailability(int lockAvailability) { 77 this.lockAvailability = lockAvailability; 78 } 79 80 public TCFileChannel getChannel(TCFile tcFile, String mode) { 81 return new TestTCFileChannelImpl(tcFile, mode, lockAvailability); 82 } 83 } 84 85 private class TestTCFileChannelImpl implements TCFileChannel { 86 87 private int lockAvailability; 88 89 public TestTCFileChannelImpl(TCFile file, String mode, int lockAvailability) { 90 this.lockAvailability = lockAvailability; 91 } 92 93 public TCFileLock lock() throws OverlappingFileLockException { 94 if (lockAvailability == lockedAlreadyOnThisVM) { 95 throw new OverlappingFileLockException (); 96 } 97 return new TestTCFileLockImpl(); 98 } 99 100 public void close() { 101 } 103 104 public TCFileLock tryLock() throws IOException , OverlappingFileLockException { 105 throw new ImplementMe(); 106 } 107 108 } 109 110 private class TestTCFileLockImpl implements TCFileLock { 111 112 public void release() { 113 } 115 116 } 117 118 private class TestTCFileImpl implements TCFile { 119 120 private boolean fileIsMakable; 121 private boolean fileExists; 122 private boolean newFileIsMakable; 123 124 public TestTCFileImpl(boolean isMakable) { 125 fileIsMakable = isMakable; 126 fileExists = false; 127 } 128 129 public boolean exists() { 130 return fileExists; 131 } 132 133 public void forceMkdir() throws IOException { 134 if (!fileIsMakable) { 135 throw new IOException ("Could not create indicated directory."); 136 } 137 138 fileExists = true; 139 } 140 141 public File getFile() { 142 return null; 143 } 144 145 public TCFile createNewTCFile(TCFile location, String fileName) { 146 return new TestTCFileImpl(newFileIsMakable); 147 } 148 149 public boolean createNewFile() throws IOException { 150 if (!fileIsMakable) { 151 throw new IOException ("Could not create indicated directory."); 152 } 153 154 fileExists = true; 155 return fileExists; 156 } 157 158 public void setNewFileIsMakable (boolean val) { 159 newFileIsMakable = val; 160 } 161 162 public String toString() { 163 String s = "TestTCFileImpl: "; 164 if (fileIsMakable) { 165 s += "file is makable, "; 166 } else { 167 s += "file is not makable, "; 168 } 169 if (fileExists) { 170 s += "file exists."; 171 } else { 172 s += "file does not exist."; 173 } 174 return s; 175 } 176 } 177 178 } 179 | Popular Tags |