@Theory public void theoryNonDirectoriesPersistData(String path) throws Exception { Resource res = getResource(path); assumeThat(res, not(directory())); byte[] test = {42, 29, 32, 120, 69, 0, 1}; try (OutputStream ostream = res.out()) { ostream.write(test); } byte[] result = new byte[test.length]; try (InputStream istream = res.in()) { istream.read(result); assertThat(istream.read(), is(-1)); } assertThat(result, equalTo(test)); }
@Theory public void theoryNonDirectoriesHaveFileWithSameContents(String path) throws Exception { Resource res = getResource(path); assumeThat(res, not(directory())); byte[] test = {42, 29, 32, 120, 69, 0, 1}; try (OutputStream ostream = res.out()) { ostream.write(test); } byte[] result = new byte[test.length]; try (InputStream istream = new FileInputStream(res.file())) { istream.read(result); assertThat(istream.read(), is(-1)); } assertThat(result, equalTo(test)); }
@Theory public void theoryDirectoriesHaveNoIstreams(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); exception.expect(IllegalStateException.class); res.in().close(); }
@Theory public void theoryDirectoriesHaveNoOstream(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); exception.expect(IllegalStateException.class); res.out().close(); }
@Theory public void theoryDirectoriesHaveChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); Collection<Resource> result = res.list(); assertThat(result, notNullValue()); }
@Theory public void theoryChildrenKnowTheirParents(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); Collection<Resource> children = res.list(); assumeThat(children, not(empty())); // Make sure this resource has children for (Resource child : children) { Resource parent = child.parent(); assertThat(parent, equalTo(res)); } }
@Theory public void theoryParentIsDirectory(String path) throws Exception { Resource res = getResource(path); Resource parent = res.parent(); assumeThat(path + " not root", parent, notNullValue()); if (res.getType() != Type.UNDEFINED) { assertThat(path + " directory", parent, is(directory())); } }
@Theory public void theoryHaveDir(String path) throws Exception { Resource res = getResource(path); assumeThat(res, directory()); File result = res.dir(); assertThat(result, notNullValue()); }
@Theory public void theoryAlteringFileAltersResource(String path) throws Exception { Resource res = getResource(path); assumeThat(res, not(directory())); byte[] testResource = {42, 29, 32, 120, 69, 0, 1}; byte[] testFile = {27, 3, 5, 90, -120, -3}; // Write to resource try (OutputStream ostream = res.out()) { ostream.write(testResource); } // Write to file try (OutputStream ostream = new FileOutputStream(res.file())) { ostream.write(testFile); } // Read from resource byte[] result = new byte[testFile.length]; try (InputStream istream = res.in()) { istream.read(result); assertThat(istream.read(), is(-1)); } // Should be what was written to the file assertThat(result, equalTo(testFile)); }
@Theory public void theoryParentsKnowTheirChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); Resource parent = res.parent(); assumeThat(path, parent, notNullValue()); // Make sure this resource has a parent Collection<Resource> result = parent.list(); assertThat(path, result, hasItem(res)); // this assumed equals was written! }
@Theory public void theoryAddingFileToDirectoryAddsResource(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); File dir = res.dir(); File file = new File(dir, "newFileCreatedDirectly"); assumeTrue(file.createNewFile()); Resource child = getResource(Paths.path(res.path(), "newFileCreatedDirectly")); Collection<Resource> children = res.list(); assertThat(child, is(defined())); assertThat(children, hasItem(child)); }
@Theory public void theoryDirectoriesHaveFileWithSameNamedChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); File dir = res.dir(); Collection<Resource> resChildren = res.list(); String[] fileChildrenNames = dir.list(); String[] resChildrenNames = new String[resChildren.size()]; int i = 0; for (Resource child : resChildren) { resChildrenNames[i] = child.name(); i++; } assertThat(fileChildrenNames, arrayContainingInAnyOrder(resChildrenNames)); }
@Theory public void theoryDirectoriesHaveChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); Collection<Resource> result = res.list(); assertThat(result, notNullValue()); }
@Theory public void theoryDirectoriesHaveNoOstream(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); exception.expect(IllegalStateException.class); res.out().close(); }
@Theory public void theoryParentsKnowTheirChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); Resource parent = res.parent(); assumeThat(path, parent, notNullValue()); // Make sure this resource has a parent Collection<Resource> result = parent.list(); assertThat(path, result, hasItem(res)); // this assumed equals was written! }
@Theory public void theoryModifiedUpdated(String path) throws Exception { Resource res = getResource(path); assumeThat(res, not(directory())); long lastmod = res.lastmodified(); OutputStream out = res.out(); out.write(1234); out.close(); assertTrue(res.lastmodified() > lastmod); } }
@Theory public void theoryParentIsDirectory(String path) throws Exception { Resource res = getResource(path); Resource parent = res.parent(); assumeThat(path + " not root", parent, notNullValue()); if (res.getType() != Type.UNDEFINED) { assertThat(path + " directory", parent, is(directory())); } }
@Theory public void theoryAddingFileToDirectoryAddsResource(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(directory())); File dir = res.dir(); File file = new File(dir, "newFileCreatedDirectly"); assumeTrue(file.createNewFile()); Resource child = getResource(Paths.path(res.path(), "newFileCreatedDirectly")); Collection<Resource> children = res.list(); assertThat(child, is(defined())); assertThat(children, hasItem(child)); }