### Question:
FileSizeFileFilterRule implements FileFilterRule { @Override public boolean match(File file) throws FilterRuleNotAppliableException { long filesize = file.getSize(); if (-1 == filesize) { throw new FilterRuleNotAppliableException("The file doesn't have any size attribute"); } switch (op) { case OP_IS: return filesize == size.getBytes(); case OP_ISNT: return filesize != size.getBytes(); case OP_IS_GREATER_THAN: return filesize > size.getBytes(); case OP_IS_LESS_THAN: return filesize < size.getBytes(); default: return false; } } FileSizeFileFilterRule(SizeValue size, int operator); @Override String getRuleType(); static String[] getAllOperators(); @Override int getOperator(); @Override String getOperatorName(); @Override OperandValue getValue(); @Override boolean match(File file); @Override String toString(); static final String TYPE_NAME; static final int OP_IS; static final int OP_ISNT; static final int OP_IS_GREATER_THAN; static final int OP_IS_LESS_THAN; }### Answer:
@Test public void testOpIs() throws Exception { FileSizeFileFilterRule filterRule = new FileSizeFileFilterRule(new SizeValue("1000 Bytes"), FileSizeFileFilterRule.OP_IS); assertTrue(filterRule.match(foobarTxt)); foobarTxt.setSize(2000); assertFalse(filterRule.match(foobarTxt)); }
@Test public void testOpIsnt() throws Exception { FileSizeFileFilterRule filterRule = new FileSizeFileFilterRule(new SizeValue("1000 Bytes"), FileSizeFileFilterRule.OP_ISNT); assertFalse(filterRule.match(foobarTxt)); foobarTxt.setSize(2000); assertTrue(filterRule.match(foobarTxt)); }
@Test public void testOpIsGreaterThan() throws Exception { FileSizeFileFilterRule filterRule = new FileSizeFileFilterRule(new SizeValue("1000 Bytes"), FileSizeFileFilterRule.OP_IS_GREATER_THAN); assertFalse(filterRule.match(foobarTxt)); foobarTxt.setSize(2000); assertTrue(filterRule.match(foobarTxt)); foobarTxt.setSize(999); assertFalse(filterRule.match(foobarTxt)); }
@Test public void testOpIsLessThan() throws Exception { FileSizeFileFilterRule filterRule = new FileSizeFileFilterRule(new SizeValue("1000 Bytes"), FileSizeFileFilterRule.OP_IS_LESS_THAN); assertFalse(filterRule.match(foobarTxt)); foobarTxt.setSize(2000); assertFalse(filterRule.match(foobarTxt)); foobarTxt.setSize(999); assertTrue(filterRule.match(foobarTxt)); }
@Test public void fileSizeNegative() throws Exception { FileSizeFileFilterRule filterRule = new FileSizeFileFilterRule(new SizeValue("1000 Bytes"), FileSizeFileFilterRule.OP_IS); foobarTxt.setSize(-1); assertThrows(FilterRuleNotAppliableException.class, () -> filterRule.match(foobarTxt)); }
@Test public void testOpDefault() throws Exception { FileSizeFileFilterRule filterRule = new FileSizeFileFilterRule(new SizeValue("1000 Bytes"), -1); assertFalse(filterRule.match(foobarTxt)); }