/** * Locate the specified grid file. * * <p>It will look in GEOSERVER_DATA_DIR/user_projections * * @param grid the grid name/location * @return the fully resolved URL of the grid or null, if the resource cannot be located. */ @Override public URL locateGrid(String grid) { if (grid == null) return null; GeoServerResourceLoader loader = GeoServerExtensions.bean(GeoServerResourceLoader.class); if (loader == null) { return null; // must be test case still loading } Resource gridfile = loader.get("user_projections/" + grid); if (gridfile.getType() == Type.RESOURCE) { return URLs.fileToUrl(gridfile.file()); } else { return null; } } }
private void writeCurrentVersion() throws IOException { Resource security = security(); security.dir(); Resource properties = security.get(VERSION_PROPERTIES); Properties p = new Properties(); p.put(VERSION, CURR_VERSION.toString()); try (OutputStream os = properties.out()) { p.store( os, "Current version of the security directory. Do not remove or alter this file"); } }
/** Determines if the underlying file has been modified since the last check. */ public boolean isModified() { long now = System.currentTimeMillis(); if ((now - lastCheck) > 1000) { lastCheck = now; stale = (resource.getType() != Type.UNDEFINED) && (resource.lastmodified() != lastModified); } return stale; }
@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 theoryDoubleClose(String path) throws Exception { final Resource res = getResource(path); assumeThat(res, is(resource())); OutputStream os = res.out(); os.close(); os.close(); }
@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 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 theoryLeavesHaveIstream(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(resource())); try (InputStream result = res.in()) { assertThat(result, notNullValue()); } }
@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 theoryUndefinedHaveEmptyListOfChildren(String path) throws Exception { Resource res = getResource(path); assumeThat(res, is(undefined())); Collection<Resource> result = res.list(); assertThat(result, empty()); }
@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 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 theoryRecursiveDelete(String path) throws Exception { final Resource res = getResource(path); assumeThat(res, is(directory())); assumeThat(res, is(directory())); Collection<Resource> result = res.list(); assumeThat(result.size(), greaterThan(0)); assertTrue(res.delete()); }
@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 theoryUndefinedNotDeleted(String path) throws Exception { Resource res = getResource(path); assumeThat(res, undefined()); assertThat(res.delete(), is(false)); assertThat(res, undefined()); }
@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)); }