From 8ae8fcbd03f077b743886da5a46b9f06d1f8b940 Mon Sep 17 00:00:00 2001 From: Fabrice Bacchella Date: Wed, 2 Nov 2011 15:11:16 +0100 Subject: [PATCH] Better PageCache unit tests --- junit/jrds/caching/TestPageCache.java | 62 ++++++++++++++++----------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/junit/jrds/caching/TestPageCache.java b/junit/jrds/caching/TestPageCache.java index 9fe490d77..120e56775 100644 --- a/junit/jrds/caching/TestPageCache.java +++ b/junit/jrds/caching/TestPageCache.java @@ -8,7 +8,6 @@ import javax.xml.parsers.ParserConfigurationException; import jrds.Tools; -import jrds.Util; import org.apache.log4j.Level; import org.apache.log4j.Logger; @@ -18,52 +17,65 @@ public class TestPageCache { static final private Logger logger = Logger.getLogger(TestRrdCachedFileBackend.class); + static final private int numpages = 4; + static final private File testFile = new File("tmp/testpagecache"); @BeforeClass static public void configure() throws IOException, ParserConfigurationException { Tools.configure(); Tools.setLevel(logger, Level.TRACE, "jrds.caching.RrdCachedFileBackend", "jrds.caching.FilePage", "jrds.caching.PageCache"); } + + private void checkcontent() throws IOException { + Assert.assertEquals("File size invalid", PageCache.PAGESIZE * numpages, testFile.length()); + FileInputStream in = new FileInputStream(testFile); + byte[] bufferin = new byte[PageCache.PAGESIZE * numpages ]; + in.read(bufferin); + for(int i=0 ; i < PageCache.PAGESIZE * numpages; i++ ) { + Assert.assertEquals(String.format("Invalid content at offset %d", i), (byte)Math.floor(i / PageCache.PAGESIZE), bufferin[i]); + } + } @Test - public void test1() throws IOException { - String outString = getClass().getName(); - PageCache pc = new PageCache(4,3600); - File testFile = new File("tmp/testpagecache"); + public void fillSequential() throws IOException { + PageCache pc = new PageCache(numpages,3600); if(testFile.exists()) testFile.delete(); - byte[] buffer = outString.getBytes(); - pc.write(testFile, 0, buffer); + byte[] buffer = new byte[PageCache.PAGESIZE]; + for(byte i= 0; i < numpages; i++) { + Arrays.fill(buffer, i); + pc.write(testFile, i * PageCache.PAGESIZE, buffer); + } pc.sync(); - - FileInputStream in = new FileInputStream(testFile); - byte[] b = new byte[(int) testFile.length()]; - in.read(b); - Assert.assertEquals("read does not match write", outString.trim(), new String(b).trim()); - testFile.delete(); + checkcontent(); } - + @Test - public void test2() throws IOException { - int numpages = 3; + public void fillReverse() throws IOException { PageCache pc = new PageCache(numpages,3600); - File testFile = new File("tmp/testpagecache"); if(testFile.exists()) testFile.delete(); byte[] buffer = new byte[PageCache.PAGESIZE]; - for(byte i= 0; i < numpages; i++) { + for(byte i = (byte) (numpages - 1) ; i > 0; i--) { Arrays.fill(buffer, i); pc.write(testFile, i * PageCache.PAGESIZE, buffer); } pc.sync(); - Assert.assertEquals("File size invalid", PageCache.PAGESIZE * numpages, testFile.length()); - FileInputStream in = new FileInputStream(testFile); - byte[] bufferin = new byte[PageCache.PAGESIZE * numpages ]; - in.read(bufferin); - for(int i=0 ; i < PageCache.PAGESIZE * numpages; i++ ) { - Assert.assertEquals(String.format("Invalid content at offset %d", i), (byte)Math.floor(i / PageCache.PAGESIZE), bufferin[i]); - } + checkcontent(); + } + @Test + public void fillOnce() throws IOException { + PageCache pc = new PageCache(numpages,3600); + if(testFile.exists()) + testFile.delete(); + byte[] buffer = new byte[PageCache.PAGESIZE * numpages]; + for(byte i= 0; i < numpages; i++) { + Arrays.fill(buffer, PageCache.PAGESIZE * i , PageCache.PAGESIZE * (i + 1), i); + } + pc.write(testFile, 0, buffer); + pc.sync(); + checkcontent(); } }