@Test public void lastModifiedWorksWithResourceThatDoesNotSupportFileBasedAccessAtAll() throws Exception { Resource resource = new ByteArrayResource(new byte[0]); ResourceScriptSource scriptSource = new ResourceScriptSource(resource); assertTrue("ResourceScriptSource must start off in the 'isModified' state (it obviously isn't).", scriptSource.isModified()); scriptSource.getScriptAsString(); assertFalse("ResourceScriptSource must not report back as being modified if the underlying File resource is not reporting a changed lastModified time.", scriptSource.isModified()); // Must now continue to report back as not having been modified 'cos the Resource does not support access as a File (and so the lastModified date cannot be determined). assertFalse("ResourceScriptSource must not report back as being modified if the underlying File resource is not reporting a changed lastModified time.", scriptSource.isModified()); }
@Test public void lastModifiedWorksWithResourceThatDoesNotSupportFileBasedReading() throws Exception { Resource resource = mock(Resource.class); // underlying File is asked for so that the last modified time can be checked... // And then mock the file changing; i.e. the File says it has been modified given(resource.lastModified()).willReturn(100L, 100L, 200L); // does not support File-based reading; delegates to InputStream-style reading... //resource.getFile(); //mock.setThrowable(new FileNotFoundException()); given(resource.getInputStream()).willReturn(StreamUtils.emptyInput()); ResourceScriptSource scriptSource = new ResourceScriptSource(resource); assertTrue("ResourceScriptSource must start off in the 'isModified' state (it obviously isn't).", scriptSource.isModified()); scriptSource.getScriptAsString(); assertFalse("ResourceScriptSource must not report back as being modified if the underlying File resource is not reporting a changed lastModified time.", scriptSource.isModified()); // Must now report back as having been modified assertTrue("ResourceScriptSource must report back as being modified if the underlying File resource is reporting a changed lastModified time.", scriptSource.isModified()); }
public RefreshableResourceScriptSource(Resource resource, long refreshDelay) { this.refreshDelay = refreshDelay; this.source = new ResourceScriptSource(resource); try { this.script = this.source.getScriptAsString(); } catch (IOException e) { this.lastModifiedChecked.set(0); } }
public String getScriptAsString() throws IOException { if (this.script == null || this.isModified()) { this.lastModifiedChecked.set(System.currentTimeMillis()); this.script = this.source.getScriptAsString(); } return this.script; }
public RefreshableResourceScriptSource(Resource resource, long refreshDelay) { this.refreshDelay = refreshDelay; this.source = new ResourceScriptSource(resource); try { this.script = this.source.getScriptAsString(); } catch (IOException e) { this.lastModifiedChecked.set(0); } }
public String getScriptAsString() throws IOException { if (this.script == null || this.isModified()) { this.lastModifiedChecked.set(System.currentTimeMillis()); this.script = this.source.getScriptAsString(); } return this.script; }