Fix "lastModified" test to allow for the fact that Unix only keeps timestamps to the nearest second.
This commit is contained in:
parent
7fd7fbcc27
commit
4d59966bd0
1 changed files with 13 additions and 52 deletions
|
@ -2,13 +2,13 @@
|
|||
|
||||
package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
|
@ -55,7 +55,6 @@ public class FlatteningTemplateLoaderTest extends AbstractTestClass {
|
|||
private static final String TEMPLATE_NAME_LOWER = "another.ftl";
|
||||
private static final String TEMPLATE_LOWER_CONTENTS = "Another template file.";
|
||||
|
||||
private static long setupTime;
|
||||
private static File tempDir;
|
||||
private static File notADirectory;
|
||||
private static File upperTemplate;
|
||||
|
@ -65,8 +64,6 @@ public class FlatteningTemplateLoaderTest extends AbstractTestClass {
|
|||
|
||||
@BeforeClass
|
||||
public static void setUpFiles() throws IOException {
|
||||
setupTime = System.currentTimeMillis();
|
||||
|
||||
notADirectory = File.createTempFile(
|
||||
FlatteningTemplateLoader.class.getSimpleName(), "");
|
||||
|
||||
|
@ -150,20 +147,21 @@ public class FlatteningTemplateLoaderTest extends AbstractTestClass {
|
|||
}
|
||||
|
||||
/**
|
||||
* We may not know exactly when the file was last modified, but it should
|
||||
* fall into a known range.
|
||||
* Some systems only record last-modified times to the nearest second, so we
|
||||
* can't rely on them changing during the course of the test. Force the
|
||||
* change, and test for it.
|
||||
*/
|
||||
@Test
|
||||
public void lastModified() throws IOException {
|
||||
public void teplateLastModified() throws IOException {
|
||||
Object source = loader.findTemplateSource(TEMPLATE_NAME_UPPER);
|
||||
long modified = loader.getLastModified(source);
|
||||
long firstBoundary = System.currentTimeMillis();
|
||||
assertInRange("created", setupTime, firstBoundary, modified);
|
||||
long now = System.currentTimeMillis();
|
||||
assertTrue("near to now: modified=" + formatTimeStamp(modified)
|
||||
+ ", now=" + formatTimeStamp(now),
|
||||
2000 > Math.abs(modified - now));
|
||||
|
||||
rewriteFile(upperTemplate, TEMPLATE_UPPER_CONTENTS);
|
||||
long secondBoundary = System.currentTimeMillis();
|
||||
modified = loader.getLastModified(source);
|
||||
assertInRange("modified", firstBoundary, secondBoundary, modified);
|
||||
upperTemplate.setLastModified(5000);
|
||||
assertEquals("modified modified", 5000, loader.getLastModified(source));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -176,43 +174,6 @@ public class FlatteningTemplateLoaderTest extends AbstractTestClass {
|
|||
// helper methods
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Fill an existing file with new contents.
|
||||
*/
|
||||
private void rewriteFile(File file, String contents) throws IOException {
|
||||
Writer writer = null;
|
||||
try {
|
||||
writer = new FileWriter(file);
|
||||
writer.write(contents);
|
||||
} finally {
|
||||
if (writer != null) {
|
||||
try {
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the modified time falls between (or on) the two boundary
|
||||
* times.
|
||||
*/
|
||||
private void assertInRange(String message, long lowerBound,
|
||||
long upperBound, long modified) {
|
||||
if (modified < lowerBound) {
|
||||
fail(message + ": " + formatTimeStamp(modified)
|
||||
+ " is less than the lower bound "
|
||||
+ formatTimeStamp(lowerBound));
|
||||
}
|
||||
if (modified > upperBound) {
|
||||
fail(message + ": " + formatTimeStamp(modified)
|
||||
+ " is greater than the upper bound "
|
||||
+ formatTimeStamp(upperBound));
|
||||
}
|
||||
}
|
||||
|
||||
private String formatTimeStamp(long time) {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat(
|
||||
"yyyy-MM-dd HH:mm:ss.SSS");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue