use of org.commonjava.cdi.util.weft.SignallingLock in project partyline by Commonjava.
the class JoinableFileTest method lockDirectoryJoinFails.
@Test(expected = IOException.class)
public void lockDirectoryJoinFails() throws IOException, InterruptedException {
File dir = temp.newFolder();
dir.mkdirs();
final JoinableFile jf = new RandomAccessJFS().getFile(dir, newLockOwner(dir.getAbsolutePath(), LockLevel.read), null, false, new SignallingLock());
assertThat(jf.isWriteLocked(), equalTo(true));
jf.joinStream();
}
use of org.commonjava.cdi.util.weft.SignallingLock in project partyline by Commonjava.
the class JoinableFileTest method assertReadOfExistingFileOfSize.
private void assertReadOfExistingFileOfSize(String s) throws IOException, InterruptedException {
File f = temp.newFile("read-target.txt");
int sz = (int) FileSize.valueOf("11mb").getSize();
byte[] src = new byte[sz];
Random rand = new Random();
rand.nextBytes(src);
try (OutputStream out = new FileOutputStream(f)) {
IOUtils.write(src, out);
}
final JoinableFile jf = new RandomAccessJFS().getFile(f, newLockOwner(f.getAbsolutePath(), LockLevel.read), null, false, new SignallingLock());
try (InputStream stream = jf.joinStream()) {
byte[] result = IOUtils.toByteArray(stream);
assertThat(result, equalTo(src));
}
}
use of org.commonjava.cdi.util.weft.SignallingLock in project partyline by Commonjava.
the class JoinableFileTest method lockDirectory.
@Test
public void lockDirectory() throws IOException {
File dir = temp.newFolder();
dir.mkdirs();
final JoinableFile jf = new RandomAccessJFS().getFile(dir, newLockOwner(dir.getAbsolutePath(), LockLevel.read), null, false, new SignallingLock());
assertThat(jf.isWriteLocked(), equalTo(true));
jf.close();
}
use of org.commonjava.cdi.util.weft.SignallingLock in project partyline by Commonjava.
the class JoinableFileTest method overwriteFile_SmallerReplacementTruncates.
@Test
public void overwriteFile_SmallerReplacementTruncates() throws Exception {
File f = temp.newFile();
JoinableFile jf = new RandomAccessJFS().getFile(f, newLockOwner(f.getAbsolutePath(), LockLevel.write), null, true, new SignallingLock());
OutputStream stream = jf.getOutputStream();
String longer = "This is a really really really long string";
stream.write(longer.getBytes());
stream.close();
jf = new RandomAccessJFS().getFile(f, newLockOwner(f.getAbsolutePath(), LockLevel.write), null, true, new SignallingLock());
stream = jf.getOutputStream();
String shorter = "This is a short string";
stream.write(shorter.getBytes());
stream.close();
final File file = new File(jf.getPath());
System.out.println("File length: " + file.length());
assertThat(file.length(), equalTo((long) shorter.getBytes().length));
String content = FileUtils.readFileToString(f);
assertThat(content, equalTo(shorter));
}
use of org.commonjava.cdi.util.weft.SignallingLock in project partyline by Commonjava.
the class BinaryFileTest method shouldReadBinaryFile.
@Test
public void shouldReadBinaryFile() throws IOException, InterruptedException {
List<String> failures = new ArrayList<>();
File binaryFile = temp.newFile("binary-file.bin");
ReentrantLock lock = new ReentrantLock();
JoinableFile jf = new RandomAccessJFS().getFile(binaryFile, new LocalLockOwner(binaryFile.getAbsolutePath(), name.getMethodName(), LockLevel.write), null, true, new SignallingLock());
OutputStream jos = jf.getOutputStream();
InputStream actual = jf.joinStream();
ByteArrayOutputStream written = new ByteArrayOutputStream();
writeBinaryFile(jos, written);
int pos = 0;
int exp, act;
ByteArrayInputStream expected = new ByteArrayInputStream(written.toByteArray());
while ((exp = expected.read()) != -1) {
act = actual.read();
pos++;
if (act != exp) {
failures.add(String.format("Failure at position %d. Expected %d, got %d", pos, exp, act));
}
}
actual.close();
if (!failures.isEmpty()) {
fail("Failures: " + failures);
}
}
Aggregations