001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.util;
005:
006: import com.tc.exception.ImplementMe;
007: import com.tc.io.TCFile;
008: import com.tc.io.TCFileChannel;
009: import com.tc.io.TCFileLock;
010: import com.tc.io.TCRandomFileAccess;
011: import com.tc.test.TCTestCase;
012: import com.tc.util.startuplock.FileNotCreatedException;
013: import com.tc.util.startuplock.LocationNotCreatedException;
014:
015: import java.io.File;
016: import java.io.IOException;
017: import java.nio.channels.OverlappingFileLockException;
018:
019: public class StartupLockTest extends TCTestCase {
020:
021: private final int lockedAlreadyOnThisVM = 0;
022: private final int lockCanBeAquired = 1;
023:
024: public void testBasics() throws Throwable {
025: TestTCRandomFileAccessImpl randomFileAccess = new TestTCRandomFileAccessImpl();
026:
027: boolean locationIsMakable = false;
028: boolean fileIsmakable = false;
029: TestTCFileImpl location = new TestTCFileImpl(locationIsMakable);
030: location.setNewFileIsMakable(fileIsmakable);
031: StartupLock startupLock = new StartupLock(location);
032: try {
033: startupLock.canProceed(randomFileAccess, true);
034: fail("Expected LocationNotCreatedException not thrown.");
035: } catch (LocationNotCreatedException se) {
036: // ok
037: }
038: Assert.assertFalse(location.exists());
039:
040: locationIsMakable = true;
041: fileIsmakable = false;
042: location = new TestTCFileImpl(locationIsMakable);
043: location.setNewFileIsMakable(fileIsmakable);
044: startupLock = new StartupLock(location);
045: try {
046: startupLock.canProceed(randomFileAccess, true);
047: fail("Expected FileNotCreatedException not thrown.");
048: } catch (FileNotCreatedException se) {
049: // ok
050: }
051: Assert.assertTrue(location.exists());
052:
053: randomFileAccess.setLockAvailability(lockedAlreadyOnThisVM);
054: locationIsMakable = true;
055: fileIsmakable = true;
056: location = new TestTCFileImpl(locationIsMakable);
057: location.setNewFileIsMakable(fileIsmakable);
058: startupLock = new StartupLock(location);
059: boolean result = startupLock.canProceed(randomFileAccess, true);
060: Assert.assertFalse(result);
061:
062: randomFileAccess.setLockAvailability(lockCanBeAquired);
063: locationIsMakable = true;
064: fileIsmakable = true;
065: location = new TestTCFileImpl(locationIsMakable);
066: location.setNewFileIsMakable(fileIsmakable);
067: startupLock = new StartupLock(location);
068: result = startupLock.canProceed(randomFileAccess, true);
069: Assert.assertTrue(location.exists());
070: Assert.assertTrue(result);
071: }
072:
073: private class TestTCRandomFileAccessImpl implements
074: TCRandomFileAccess {
075: private int lockAvailability;
076:
077: public void setLockAvailability(int lockAvailability) {
078: this .lockAvailability = lockAvailability;
079: }
080:
081: public TCFileChannel getChannel(TCFile tcFile, String mode) {
082: return new TestTCFileChannelImpl(tcFile, mode,
083: lockAvailability);
084: }
085: }
086:
087: private class TestTCFileChannelImpl implements TCFileChannel {
088:
089: private int lockAvailability;
090:
091: public TestTCFileChannelImpl(TCFile file, String mode,
092: int lockAvailability) {
093: this .lockAvailability = lockAvailability;
094: }
095:
096: public TCFileLock lock() throws OverlappingFileLockException {
097: if (lockAvailability == lockedAlreadyOnThisVM) {
098: throw new OverlappingFileLockException();
099: }
100: return new TestTCFileLockImpl();
101: }
102:
103: public void close() {
104: //method is not used in test
105: }
106:
107: public TCFileLock tryLock() throws IOException,
108: OverlappingFileLockException {
109: throw new ImplementMe();
110: }
111:
112: }
113:
114: private class TestTCFileLockImpl implements TCFileLock {
115:
116: public void release() {
117: //method not used in test
118: }
119:
120: }
121:
122: private class TestTCFileImpl implements TCFile {
123:
124: private boolean fileIsMakable;
125: private boolean fileExists;
126: private boolean newFileIsMakable;
127:
128: public TestTCFileImpl(boolean isMakable) {
129: fileIsMakable = isMakable;
130: fileExists = false;
131: }
132:
133: public boolean exists() {
134: return fileExists;
135: }
136:
137: public void forceMkdir() throws IOException {
138: if (!fileIsMakable) {
139: throw new IOException(
140: "Could not create indicated directory.");
141: }
142:
143: fileExists = true;
144: }
145:
146: public File getFile() {
147: return null;
148: }
149:
150: public TCFile createNewTCFile(TCFile location, String fileName) {
151: return new TestTCFileImpl(newFileIsMakable);
152: }
153:
154: public boolean createNewFile() throws IOException {
155: if (!fileIsMakable) {
156: throw new IOException(
157: "Could not create indicated directory.");
158: }
159:
160: fileExists = true;
161: return fileExists;
162: }
163:
164: public void setNewFileIsMakable(boolean val) {
165: newFileIsMakable = val;
166: }
167:
168: public String toString() {
169: String s = "TestTCFileImpl: ";
170: if (fileIsMakable) {
171: s += "file is makable, ";
172: } else {
173: s += "file is not makable, ";
174: }
175: if (fileExists) {
176: s += "file exists.";
177: } else {
178: s += "file does not exist.";
179: }
180: return s;
181: }
182: }
183:
184: }
|