1   package com.atlassian.bonnie;
2   
3   import junit.framework.TestCase;
4   import org.apache.lucene.document.DateField;
5   import org.apache.lucene.document.Document;
6   import org.apache.lucene.document.Field;
7   
8   import java.util.Date;
9   import java.util.Map;
10  
11  public class TestLuceneUtils extends TestCase
12  {
13      public void testBuildMapFromDocument()
14      {
15          Document d = new Document();
16          d.add(new Field("foo", "foo", Field.Store.YES, Field.Index.TOKENIZED));
17  
18          Map result = LuceneUtils.buildMapFromDocument(d);
19          assertEquals("foo", result.get("foo"));
20  
21          d.add(new Field("bar.car", "bar.car",Field.Store.YES, Field.Index.TOKENIZED));
22          result = LuceneUtils.buildMapFromDocument(d);
23          assertEquals("bar.car", ((Map)result.get("bar")).get("car"));
24  
25          d.add(new Field("car.do.eve", "car.do.eve", Field.Store.YES, Field.Index.TOKENIZED));
26          result = LuceneUtils.buildMapFromDocument(d);
27          assertEquals("car.do.eve", ((Map) ((Map)result.get("car")).get("do")).get("eve"));
28      }
29  
30      public void testDateConversion()
31      {
32          //The date itself is not important, but we want to ensure that the date is at the very minimum a millisecond
33          //different from what the default value of dateToString() returns, which is a new Date() object
34          Date d = new Date(1287099250277l);
35          String stringDate = LuceneUtils.dateToString(d);
36          assertEquals("Could not convert string to date!",d,LuceneUtils.stringToDate(stringDate));
37          String oldFormat = DateField.dateToString(d);
38          assertEquals("Could not convert string to date!",d,LuceneUtils.stringToDate(oldFormat));
39      }
40  }