james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject svn commit: r633666 - in /james/server/trunk: core-library/src/main/java/org/apache/james/mailboxmanager/ torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/ torque-mailboxmanager-function/src/test/java/org/apache/james...
Date Tue, 04 Mar 2008 22:02:26 GMT
Author: rdonkin
Date: Tue Mar  4 14:02:26 2008
New Revision: 633666

URL: http://svn.apache.org/viewvc?rev=633666&view=rev
Log:
Torque search query.

Added:
    james/server/trunk/torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/SearchUtils.java
    james/server/trunk/torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/UnsupportedSearchException.java
    james/server/trunk/torque-mailboxmanager-function/src/test/java/org/apache/james/mailboxmanager/torque/SearchUtilsTest.java
Modified:
    james/server/trunk/core-library/src/main/java/org/apache/james/mailboxmanager/SearchQuery.java

Modified: james/server/trunk/core-library/src/main/java/org/apache/james/mailboxmanager/SearchQuery.java
URL: http://svn.apache.org/viewvc/james/server/trunk/core-library/src/main/java/org/apache/james/mailboxmanager/SearchQuery.java?rev=633666&r1=633665&r2=633666&view=diff
==============================================================================
--- james/server/trunk/core-library/src/main/java/org/apache/james/mailboxmanager/SearchQuery.java
(original)
+++ james/server/trunk/core-library/src/main/java/org/apache/james/mailboxmanager/SearchQuery.java
Tue Mar  4 14:02:26 2008
@@ -59,6 +59,16 @@
     }
     
     /**
+     * Creates a filter for message size equal to the given value
+     * @param value messages with size equal to this value will be selected
+     * by the returned criterion
+     * @return <code>Criterion</code>, not null
+     */
+    public static final Criterion sizeEquals(long value) {
+        return new SizeCriterion(new NumericOperator(value, NumericOperator.EQUALS));
+    }
+    
+    /**
      * Creates a filter matching messages with internal date after the given date.
      * @param day one based day of the month
      * @param month one based month of the year

Added: james/server/trunk/torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/SearchUtils.java
URL: http://svn.apache.org/viewvc/james/server/trunk/torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/SearchUtils.java?rev=633666&view=auto
==============================================================================
--- james/server/trunk/torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/SearchUtils.java
(added)
+++ james/server/trunk/torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/SearchUtils.java
Tue Mar  4 14:02:26 2008
@@ -0,0 +1,217 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailboxmanager.torque;
+
+import java.io.StringReader;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import org.apache.james.mailboxmanager.SearchQuery;
+import org.apache.james.mailboxmanager.torque.om.MessageHeader;
+import org.apache.james.mailboxmanager.torque.om.MessageRow;
+import org.apache.james.mime4j.field.datetime.DateTime;
+import org.apache.james.mime4j.field.datetime.parser.DateTimeParser;
+import org.apache.james.mime4j.field.datetime.parser.ParseException;
+import org.apache.torque.TorqueException;
+
+/**
+ * Uility methods to help perform search operations.
+ */
+class SearchUtils {
+    
+    public static boolean matches(SearchQuery.Criterion criterion, MessageRow row) throws
TorqueException {
+        final boolean result;
+        if (criterion instanceof SearchQuery.InternalDateCriterion) {
+            result = matches((SearchQuery.InternalDateCriterion) criterion, row);
+        } else if (criterion instanceof SearchQuery.SizeCriterion) {
+            result = matches((SearchQuery.SizeCriterion) criterion, row);
+        } else if (criterion instanceof SearchQuery.HeaderCriterion) {
+            result = matches((SearchQuery.HeaderCriterion) criterion, row);
+        } else {
+            throw new UnsupportedSearchException();
+        }
+        return result;
+    }
+    
+    private static boolean matches(SearchQuery.HeaderCriterion criterion, MessageRow row)
throws TorqueException {
+        final SearchQuery.HeaderOperator operator = criterion.getOperator();
+        final String headerName = criterion.getHeaderName();
+        final boolean result;
+        if (operator instanceof SearchQuery.DateOperator) {
+            result = matches((SearchQuery.DateOperator) operator, headerName, row);
+        } else if (operator instanceof SearchQuery.ContainsOperator) {
+            result = matches((SearchQuery.ContainsOperator)operator, headerName, row);
+        } else if (operator instanceof SearchQuery.ExistsOperator) {
+            result = exists(headerName, row);
+        } else {
+            throw new UnsupportedSearchException();
+        }
+        return result;
+    }
+
+    private static boolean exists(String headerName, MessageRow row) throws TorqueException
{
+        boolean result = false;
+        final List headers = row.getMessageHeaders();
+        for (Iterator it = headers.iterator(); it.hasNext();) {
+            final MessageHeader header = (MessageHeader) it.next();
+            final String name = header.getField();
+            if (headerName.equalsIgnoreCase(name)) {
+                result = true;
+                break;
+            }
+        }
+        return result;
+    }
+
+    private static boolean matches(final SearchQuery.ContainsOperator operator, final String
headerName, final MessageRow row) throws TorqueException {
+        final String text = operator.getValue();
+        boolean result = false;
+        final List headers = row.getMessageHeaders();
+        for (Iterator it = headers.iterator(); it.hasNext();) {
+            final MessageHeader header = (MessageHeader) it.next();
+            final String name = header.getField();
+            if (headerName.equalsIgnoreCase(name)) {
+                final String value = header.getValue();
+                if (value.contains(text)) {
+                    result = true;
+                    break;
+                }
+            }
+        }
+        return result;
+    }
+
+    private static boolean matches(final SearchQuery.DateOperator operator, final String
headerName, final MessageRow row) throws TorqueException {
+        final int day = operator.getDay();
+        final int month = operator.getMonth();
+        final int year = operator.getYear();
+        final int iso = toISODate(day, month, year);
+        final String value = headerValue(headerName, row);
+        if (value == null) {
+            return false;
+        } else {
+            try {
+                final int isoFieldValue = toISODate(value);
+                final int type = operator.getType();
+                switch (type) {
+                    case SearchQuery.DateOperator.AFTER: return iso < isoFieldValue; 
+                    case SearchQuery.DateOperator.BEFORE: return iso > isoFieldValue;

+                    case SearchQuery.DateOperator.ON: return iso == isoFieldValue;
+                    default: throw new UnsupportedSearchException();
+                }
+            } catch (ParseException e) {
+                return false;
+            } 
+        }
+    }
+
+    private static String headerValue(final String headerName, final MessageRow row) throws
TorqueException {
+        final List headers = row.getMessageHeaders();
+        String value = null;
+        for (Iterator it = headers.iterator(); it.hasNext();) {
+            final MessageHeader header = (MessageHeader) it.next();
+            final String name = header.getField();
+            if (headerName.equalsIgnoreCase(name)) {
+                value = header.getValue();
+                break;
+            }
+        }
+        return value;
+    }
+
+    private static int toISODate(String value) throws ParseException {
+        final StringReader reader = new StringReader(value);
+        final DateTime dateTime = new DateTimeParser(reader).parseAll();
+        final int isoFieldValue = toISODate(dateTime.getDay(), dateTime.getMonth(), dateTime.getYear());
+        return isoFieldValue;
+    }
+    
+    private static boolean matches(SearchQuery.SizeCriterion criterion, MessageRow row) throws
UnsupportedSearchException {
+        final SearchQuery.NumericOperator operator = criterion.getOperator();
+        final int size = row.getSize();
+        final long value = operator.getValue();
+        final int type = operator.getType();
+        switch (type) {
+            case SearchQuery.NumericOperator.LESS_THAN: return size < value;
+            case SearchQuery.NumericOperator.GREATER_THAN: return size > value;
+            case SearchQuery.NumericOperator.EQUALS: return size == value;
+            default: throw new UnsupportedSearchException();
+        }
+    }
+    
+    private static boolean matches(SearchQuery.InternalDateCriterion criterion, MessageRow
row) throws UnsupportedSearchException {
+        final SearchQuery.DateOperator operator = criterion.getOperator();
+        final boolean result = matchesInternalDate(operator, row);
+        return result;
+    }
+
+    private static boolean matchesInternalDate(final SearchQuery.DateOperator operator, final
MessageRow row) throws UnsupportedSearchException {
+        final int day = operator.getDay();
+        final int month = operator.getMonth();
+        final int year = operator.getYear();
+        final Date internalDate = row.getInternalDate();
+        final int type = operator.getType();
+        switch (type) {
+            case SearchQuery.DateOperator.ON: return on(day, month, year, internalDate);
+            case SearchQuery.DateOperator.BEFORE: return before(day, month, year, internalDate);
+            case SearchQuery.DateOperator.AFTER: return after(day, month, year, internalDate);
+            default: throw new UnsupportedSearchException();
+        }
+    }
+    
+    private static boolean on(final int day, final int month, final int year, final Date
date) {
+        final Calendar gmt = getGMT();
+        gmt.setTime(date);
+        return day == gmt.get(Calendar.DAY_OF_MONTH) 
+                    && month == (gmt.get(Calendar.MONTH) + 1) 
+                            && year == gmt.get(Calendar.YEAR);
+    }
+    
+    private static boolean before(final int day, final int month, final int year, final Date
date) {
+        return toISODate(date) < toISODate(day, month, year);
+    }
+    
+    private static boolean after(final int day, final int month, final int year, final Date
date) {
+        return toISODate(date) > toISODate(day, month, year);
+    }
+    
+    private static int toISODate(final Date date) {
+        final Calendar gmt = getGMT();
+        gmt.setTime(date);
+        final int day = gmt.get(Calendar.DAY_OF_MONTH);
+        final int month = (gmt.get(Calendar.MONTH) + 1); 
+        final int year = gmt.get(Calendar.YEAR);
+        final int result = toISODate(day, month, year);
+        return result;
+    }
+    
+    private static Calendar getGMT() {
+        return Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.UK);
+    }
+    
+    private static int toISODate(final int day, final int month, final int year) {
+        final int result = (year * 10000) + (month * 100) + day;
+        return result;
+    }
+}

Added: james/server/trunk/torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/UnsupportedSearchException.java
URL: http://svn.apache.org/viewvc/james/server/trunk/torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/UnsupportedSearchException.java?rev=633666&view=auto
==============================================================================
--- james/server/trunk/torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/UnsupportedSearchException.java
(added)
+++ james/server/trunk/torque-mailboxmanager-function/src/main/java/org/apache/james/mailboxmanager/torque/UnsupportedSearchException.java
Tue Mar  4 14:02:26 2008
@@ -0,0 +1,34 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailboxmanager.torque;
+
+import org.apache.torque.TorqueException;
+
+/**
+ * Indicates that the requested search is not supported by this implementation.
+ */
+public class UnsupportedSearchException extends TorqueException {
+
+    private static final long serialVersionUID = -7442949630563672557L;
+
+    public UnsupportedSearchException() {
+        super("Unsupported search.");
+    }
+}

Added: james/server/trunk/torque-mailboxmanager-function/src/test/java/org/apache/james/mailboxmanager/torque/SearchUtilsTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/torque-mailboxmanager-function/src/test/java/org/apache/james/mailboxmanager/torque/SearchUtilsTest.java?rev=633666&view=auto
==============================================================================
--- james/server/trunk/torque-mailboxmanager-function/src/test/java/org/apache/james/mailboxmanager/torque/SearchUtilsTest.java
(added)
+++ james/server/trunk/torque-mailboxmanager-function/src/test/java/org/apache/james/mailboxmanager/torque/SearchUtilsTest.java
Tue Mar  4 14:02:26 2008
@@ -0,0 +1,247 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mailboxmanager.torque;
+
+import java.util.Date;
+
+import org.apache.james.mailboxmanager.SearchQuery;
+import org.apache.james.mailboxmanager.torque.om.MessageHeader;
+import org.apache.james.mailboxmanager.torque.om.MessageRow;
+import org.apache.mailet.RFC2822Headers;
+import org.apache.torque.TorqueException;
+
+import junit.framework.TestCase;
+
+public class SearchUtilsTest extends TestCase {
+
+    private static final String RHUBARD = "Rhubard";
+    private static final String CUSTARD = "Custard";
+    private static final Date SUN_SEP_9TH_2001 = new Date(1000000000000L);
+    private static final int SIZE = 1729;
+    private static final String DATE_FIELD = RFC2822Headers.DATE;
+    private static final String SUBJECT_FIELD = RFC2822Headers.SUBJECT;
+    private static final String RFC822_SUN_SEP_9TH_2001 = "Sun, 9 Sep 2001 09:10:48 +0000
(GMT)";
+    private static final String TEXT = RHUBARD + RHUBARD + RHUBARD;
+    
+    MessageRow row;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        row = new MessageRow();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+    
+    public void testMatchSizeLessThan() throws Exception {
+        row.setSize(SIZE);
+        assertFalse(SearchUtils.matches(SearchQuery.sizeLessThan(SIZE - 1), row));
+        assertFalse(SearchUtils.matches(SearchQuery.sizeLessThan(SIZE), row));
+        assertTrue(SearchUtils.matches(SearchQuery.sizeLessThan(SIZE + 1), row));
+        assertTrue(SearchUtils.matches(SearchQuery.sizeLessThan(Integer.MAX_VALUE), row));
+    }
+    
+    public void testMatchSizeMoreThan() throws Exception {
+        row.setSize(SIZE);
+        assertTrue(SearchUtils.matches(SearchQuery.sizeGreaterThan(SIZE - 1), row));
+        assertFalse(SearchUtils.matches(SearchQuery.sizeGreaterThan(SIZE), row));
+        assertFalse(SearchUtils.matches(SearchQuery.sizeGreaterThan(SIZE + 1), row));
+        assertFalse(SearchUtils.matches(SearchQuery.sizeGreaterThan(Integer.MAX_VALUE), row));
+    }
+    
+    public void testMatchSizeEquals() throws Exception {
+        row.setSize(SIZE);
+        assertFalse(SearchUtils.matches(SearchQuery.sizeEquals(SIZE - 1), row));
+        assertTrue(SearchUtils.matches(SearchQuery.sizeEquals(SIZE), row));
+        assertFalse(SearchUtils.matches(SearchQuery.sizeEquals(SIZE + 1), row));
+        assertFalse(SearchUtils.matches(SearchQuery.sizeEquals(Integer.MAX_VALUE), row));
+    }
+    
+    public void testMatchInternalDateEquals() throws Exception {
+        row.setInternalDate(SUN_SEP_9TH_2001);
+        assertFalse(SearchUtils.matches(SearchQuery.internalDateOn(9, 9, 2000), row));
+        assertFalse(SearchUtils.matches(SearchQuery.internalDateOn(8, 9, 2001), row));
+        assertTrue(SearchUtils.matches(SearchQuery.internalDateOn(9, 9, 2001), row));
+        assertFalse(SearchUtils.matches(SearchQuery.internalDateOn(10, 9, 2001), row));
+        assertFalse(SearchUtils.matches(SearchQuery.internalDateOn(9, 9, 2002), row));
+    }
+    
+    public void testMatchInternalDateBefore() throws Exception {
+        row.setInternalDate(SUN_SEP_9TH_2001);
+        assertFalse(SearchUtils.matches(SearchQuery.internalDateBefore(9, 9, 2000), row));
+        assertFalse(SearchUtils.matches(SearchQuery.internalDateBefore(8, 9, 2001), row));
+        assertFalse(SearchUtils.matches(SearchQuery.internalDateBefore(9, 9, 2001), row));
+        assertTrue(SearchUtils.matches(SearchQuery.internalDateBefore(10, 9, 2001), row));
+        assertTrue(SearchUtils.matches(SearchQuery.internalDateBefore(9, 9, 2002), row));
+    }
+        
+    public void testMatchInternalDateAfter() throws Exception {
+        row.setInternalDate(SUN_SEP_9TH_2001);
+        assertTrue(SearchUtils.matches(SearchQuery.internalDateAfter(9, 9, 2000), row));
+        assertTrue(SearchUtils.matches(SearchQuery.internalDateAfter(8, 9, 2001), row));
+        assertFalse(SearchUtils.matches(SearchQuery.internalDateAfter(9, 9, 2001), row));
+        assertFalse(SearchUtils.matches(SearchQuery.internalDateAfter(10, 9, 2001), row));
+        assertFalse(SearchUtils.matches(SearchQuery.internalDateAfter(9, 9, 2002), row));
+    }
+    
+    public void testMatchHeaderDateAfter() throws Exception {
+        addHeader(DATE_FIELD, RFC822_SUN_SEP_9TH_2001);
+        assertTrue(SearchUtils.matches(SearchQuery.headerDateAfter(DATE_FIELD, 9, 9, 2000),
row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerDateAfter(DATE_FIELD,8, 9, 2001),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateAfter(DATE_FIELD,9, 9, 2001),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateAfter(DATE_FIELD,10, 9, 2001),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateAfter(DATE_FIELD,9, 9, 2002),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateAfter("BOGUS",9, 9, 2001),
row));
+    }
+    
+    public void testShouldMatchCapsHeaderDateAfter() throws Exception {
+        addHeader(DATE_FIELD.toUpperCase(), RFC822_SUN_SEP_9TH_2001);
+        assertTrue(SearchUtils.matches(SearchQuery.headerDateAfter(DATE_FIELD, 9, 9, 2000),
row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerDateAfter(DATE_FIELD,8, 9, 2001),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateAfter(DATE_FIELD,9, 9, 2001),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateAfter(DATE_FIELD,10, 9, 2001),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateAfter(DATE_FIELD,9, 9, 2002),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateAfter("BOGUS",9, 9, 2001),
row));
+    }
+    
+    public void testShouldMatchLowersHeaderDateAfter() throws Exception {
+        addHeader(DATE_FIELD.toLowerCase(), RFC822_SUN_SEP_9TH_2001);
+        assertTrue(SearchUtils.matches(SearchQuery.headerDateAfter(DATE_FIELD, 9, 9, 2000),
row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerDateAfter(DATE_FIELD,8, 9, 2001),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateAfter(DATE_FIELD,9, 9, 2001),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateAfter(DATE_FIELD,10, 9, 2001),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateAfter(DATE_FIELD,9, 9, 2002),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateAfter("BOGUS",9, 9, 2001),
row));
+    }
+    
+    public void testMatchHeaderDateOn() throws Exception {
+        addHeader(DATE_FIELD, RFC822_SUN_SEP_9TH_2001);
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateOn(DATE_FIELD, 9, 9, 2000),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateOn(DATE_FIELD,8, 9, 2001),
row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerDateOn(DATE_FIELD,9, 9, 2001), row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateOn(DATE_FIELD,10, 9, 2001),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateOn(DATE_FIELD,9, 9, 2002),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateOn("BOGUS",9, 9, 2001), row));
+    }
+    
+    public void testShouldMatchCapsHeaderDateOn() throws Exception {
+        addHeader(DATE_FIELD.toUpperCase(), RFC822_SUN_SEP_9TH_2001);
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateOn(DATE_FIELD, 9, 9, 2000),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateOn(DATE_FIELD,8, 9, 2001),
row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerDateOn(DATE_FIELD,9, 9, 2001), row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateOn(DATE_FIELD,10, 9, 2001),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateOn(DATE_FIELD,9, 9, 2002),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateOn("BOGUS",9, 9, 2001), row));
+     }
+    
+    public void testShouldMatchLowersHeaderDateOn() throws Exception {
+        addHeader(DATE_FIELD.toLowerCase(), RFC822_SUN_SEP_9TH_2001);
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateOn(DATE_FIELD, 9, 9, 2000),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateOn(DATE_FIELD,8, 9, 2001),
row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerDateOn(DATE_FIELD,9, 9, 2001), row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateOn(DATE_FIELD,10, 9, 2001),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateOn(DATE_FIELD,9, 9, 2002),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateOn("BOGUS",9, 9, 2001), row));
+    }
+    
+    
+    public void testMatchHeaderDateBefore() throws Exception {
+        addHeader(DATE_FIELD, RFC822_SUN_SEP_9TH_2001);
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateBefore(DATE_FIELD, 9, 9, 2000),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateBefore(DATE_FIELD,8, 9, 2001),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateBefore(DATE_FIELD,9, 9, 2001),
row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerDateBefore(DATE_FIELD,10, 9, 2001),
row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerDateBefore(DATE_FIELD,9, 9, 2002),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateBefore("BOGUS",9, 9, 2001),
row));
+    }
+    
+    public void testShouldMatchCapsHeaderDateBefore() throws Exception {
+        addHeader(DATE_FIELD.toUpperCase(), RFC822_SUN_SEP_9TH_2001);
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateBefore(DATE_FIELD, 9, 9, 2000),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateBefore(DATE_FIELD,8, 9, 2001),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateBefore(DATE_FIELD,9, 9, 2001),
row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerDateBefore(DATE_FIELD,10, 9, 2001),
row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerDateBefore(DATE_FIELD,9, 9, 2002),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateBefore("BOGUS",9, 9, 2001),
row));
+    }
+    
+    public void testShouldMatchLowersHeaderDateBefore() throws Exception {
+        addHeader(DATE_FIELD.toLowerCase(), RFC822_SUN_SEP_9TH_2001);
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateBefore(DATE_FIELD, 9, 9, 2000),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateBefore(DATE_FIELD,8, 9, 2001),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateBefore(DATE_FIELD,9, 9, 2001),
row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerDateBefore(DATE_FIELD,10, 9, 2001),
row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerDateBefore(DATE_FIELD,9, 9, 2002),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerDateBefore("BOGUS",9, 9, 2001),
row));
+    }
+    
+    public void testMatchHeaderContains() throws Exception {
+        addHeader(SUBJECT_FIELD, TEXT);
+        assertFalse(SearchUtils.matches(SearchQuery.headerContains(DATE_FIELD, CUSTARD),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerContains(DATE_FIELD, TEXT), row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerContains(SUBJECT_FIELD, TEXT), row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerContains(SUBJECT_FIELD, RHUBARD),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerContains(SUBJECT_FIELD, CUSTARD),
row)); 
+    }
+    
+    public void testShouldMatchLowerHeaderContains() throws Exception {
+        addHeader(SUBJECT_FIELD.toLowerCase(), TEXT);
+        assertFalse(SearchUtils.matches(SearchQuery.headerContains(DATE_FIELD, CUSTARD),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerContains(DATE_FIELD, TEXT), row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerContains(SUBJECT_FIELD, TEXT), row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerContains(SUBJECT_FIELD, RHUBARD),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerContains(SUBJECT_FIELD, CUSTARD),
row)); 
+    }
+    
+    public void testShouldMatchCapsHeaderContains() throws Exception {
+        addHeader(SUBJECT_FIELD.toUpperCase(), TEXT);
+        assertFalse(SearchUtils.matches(SearchQuery.headerContains(DATE_FIELD, CUSTARD),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerContains(DATE_FIELD, TEXT), row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerContains(SUBJECT_FIELD, TEXT), row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerContains(SUBJECT_FIELD, RHUBARD),
row));
+        assertFalse(SearchUtils.matches(SearchQuery.headerContains(SUBJECT_FIELD, CUSTARD),
row)); 
+    }
+    
+    public void testMatchHeaderExists() throws Exception {
+        addHeader(SUBJECT_FIELD, TEXT);
+        assertFalse(SearchUtils.matches(SearchQuery.headerExists(DATE_FIELD), row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerExists(SUBJECT_FIELD), row));
+    }
+
+    public void testShouldMatchLowersHeaderExists() throws Exception {
+        addHeader(SUBJECT_FIELD.toLowerCase(), TEXT);
+        assertFalse(SearchUtils.matches(SearchQuery.headerExists(DATE_FIELD), row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerExists(SUBJECT_FIELD), row));
+    }
+    
+    public void testShouldMatchUppersHeaderExists() throws Exception {
+        addHeader(SUBJECT_FIELD.toUpperCase(), TEXT);
+        assertFalse(SearchUtils.matches(SearchQuery.headerExists(DATE_FIELD), row));
+        assertTrue(SearchUtils.matches(SearchQuery.headerExists(SUBJECT_FIELD), row));
+    }
+    
+    private void addHeader(String fieldName, String value) throws TorqueException {
+        final MessageHeader messageHeader = new MessageHeader();
+        messageHeader.setField(fieldName);
+        messageHeader.setValue(value);
+        row.addMessageHeader(messageHeader);
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message