1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  
21  package org.onemind.commons.java.datastructure;
22  
23  import java.util.TreeSet;
24  import org.onemind.commons.java.datastructure.MruList;
25  import junit.framework.TestCase;
26  /***
27   * Test for MruList
28   * @author TiongHiang Lee (thlee@onemindsoft.org)
29   * @version $Id: MruListTest.java,v 1.2 2004/08/26 12:33:09 thlee Exp $ $Name:  $
30   */
31  public class MruListTest extends TestCase
32  {
33  
34      /***
35       * Test the MruEntry implementation
36       * @throws Exception if there's problem
37       */
38      public void testMruEntry() throws Exception
39      {
40          TreeSet set = new TreeSet();
41          Object first = new String("first");
42          Object second = new String("second");
43          MruList.MruEntry entry1 = new MruList.MruEntry(first, System.currentTimeMillis());
44          Thread.currentThread().sleep(20);
45          MruList.MruEntry entry2 = new MruList.MruEntry(second, System.currentTimeMillis());
46          set.add(entry1);
47          set.add(entry2);
48          assertEquals(set.iterator().next(), entry2);
49          System.out.println(set);
50          Thread.currentThread().sleep(20);
51          entry1.setLastAccessTime(System.currentTimeMillis());
52          set.remove(entry1);
53          set.add(entry1);
54          System.out.println(set);
55          assertEquals(set.iterator().next(), entry1);
56      }
57  
58      /***
59       * Test the MruList
60       * @throws Exception if there's problem
61       */
62      public void testMruList() throws Exception
63      {
64          MruList list = new MruList();
65          Object first = new String("first");
66          Object second = new String("second");
67          list.add(first);
68          Thread.currentThread().sleep(20);
69          list.add(second);
70          System.out.println(list.iterator().next());
71          assertEquals(list.iterator().next(), second);
72          Thread.currentThread().sleep(20);
73          list.add(first);
74          assertEquals(list.iterator().next(), first);
75      }
76  
77      /***
78       * Test truncation
79       */
80      public void testTruncate()
81      {
82          MruList list  = new MruList(3, 0);
83          for (int i = 0; i < 10; i++)
84          {
85              Integer obj = new Integer(i);
86              list.add(obj);
87          }
88          assertEquals(list.size(), 3);
89      }
90  }