Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javatools/util/test/TestCache.java


1   /*
2       Javatools (modified version) - Some useful general classes.
3       Copyright (C) 2002  Chris Bitmead (original) Antonio Petrelli (modified)
4   
5       This program is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation; either version 2 of the License, or
8       (at your option) any later version.
9   
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  
19      Contact me at: brenmcguire@users.sourceforge.net
20   */
21  package javatools.util.test;
22  import java.util.*;
23  import javatools.util.Cache;
24  
25  public class TestCache {
26    Cache c;
27  
28    public static void main(String argv[]) {
29      TestCache t = new TestCache();
30      t.run();
31    }
32  
33    void run() {
34      try {
35        c = new Cache();
36        test();
37        c = new Cache();
38        c.setCacheMaxAgeSeconds(10);
39        test();
40        try {
41          Thread.currentThread().sleep(12 * 1000);
42        } catch (InterruptedException e) {
43          throw new TestFailedException("Interrupted Exception");
44        }
45        test2();
46  
47        System.out.println("SUCCESS");
48      } catch (TestFailedException e) {
49        System.out.println("FAIL: " + e.getMessage());
50      }
51    }
52  
53    void test() throws TestFailedException {
54      c.setCacheMaxObj(3);
55      c.put(new Integer(2), new Integer(4));
56      c.put(new Integer(3), new Integer(6));
57      c.put(new Integer(4), new Integer(8));
58      Integer i;
59  
60      i = (Integer) c.get(new Integer(2));
61      if (i.intValue() != 4) {
62        throw new TestFailedException("2");
63      }
64      i = (Integer) c.get(new Integer(4));
65      if (i.intValue() != 8) {
66        throw new TestFailedException("4");
67      }
68      i = (Integer) c.get(new Integer(3));
69      if (i.intValue() != 6) {
70        throw new TestFailedException("3");
71      }
72      i = (Integer) c.get(new Integer(8));
73      if (i != null) {
74        throw new TestFailedException("8");
75      }
76      c.put(new Integer(5), new Integer(10));
77      i = (Integer) c.get(new Integer(2));
78      if (i != null) {
79        throw new TestFailedException("2.2");
80      }
81      i = (Integer) c.get(new Integer(3));
82      if (i.intValue() != 6) {
83        throw new TestFailedException("3.2");
84      }
85    }
86  
87    void test2() throws TestFailedException {
88      Integer i;
89      i = (Integer) c.get(new Integer(5));
90      if (i != null) {
91        throw new TestFailedException("5.3");
92      }
93      i = (Integer) c.get(new Integer(2));
94      if (i != null) {
95        throw new TestFailedException("2.3");
96      }
97      i = (Integer) c.get(new Integer(3));
98      if (i != null) {
99        throw new TestFailedException("3.3");
100     }
101     i = (Integer) c.get(new Integer(4));
102     if (i != null) {
103       throw new TestFailedException("4.3");
104     }
105   }
106 
107 
108   class TestFailedException extends Exception {
109     TestFailedException(String desc) {
110       super(desc);
111     }
112   }
113 }
114