1   
2   import java.io.Externalizable;
3   import java.io.IOException;
4   import java.io.ObjectInput;
5   import java.io.ObjectOutput;
6   import java.util.Iterator;
7   import java.util.Map;
8   import java.util.Set;
9   import javax.util.jcache.CacheAccessFactory;
10  import javax.util.jcache.CacheAttributes;
11  import javax.util.jcache.CacheException;
12  
13  public class Bug1097240 {
14      public static void main(String[] args) {
15          try {
16              CacheAttributes ca = CacheAttributes.getDefaultCacheAttributes();
17              ca.setDiskPath(System.getProperty("java.io.tmpdir"));
18              ca.setDiskCacheSize(1000);
19              ca.setMemoryCacheSize(10);
20              ca.setMaxObjects(10000);
21              ca.setLocal();
22              CacheAccessFactory factory = CacheAccessFactory.getInstance();
23              javax.util.jcache.Cache cache = factory.getCache();
24              cache.close();
25              cache.init(ca);
26              System.out.println(ca.toString());
27              Map cacheMap = factory.getMapAccess();
28              long startTime = System.currentTimeMillis();
29              System.out.println("writing 25,000 values");
30              for (int loop = 0; loop < 25000; loop++) {
31                  String key = "Key:" + loop;
32                  String value = "Value:" + loop;
33                  Key keyObj = new Key(key);
34                  Value valueObj = new Value(value);
35                  cacheMap.put(keyObj, valueObj);
36              }
37              Set set = cacheMap.keySet();
38              Iterator iterator = set.iterator();
39              System.out.println("Enumeration elements ...");
40              int count = 0;
41              while (iterator.hasNext()) {
42                  count++;
43                  Key key = (Key) iterator.next();
44                  Value val = (Value) cacheMap.get(key);
45                  //System.out.println("" + count + "Enumeration key=" + key.val + "; value=" + val.val);
46              }
47              System.out.println("Enumeration " + count + " elements");
48              System.out.println("reading 25,000 values");
49              for (int loop = 0; loop < 25000; loop++) {
50                  String key = "Key:" + loop;
51                  Key keyObj = new Key(key);
52                  Object object = cacheMap.get(keyObj);
53                  Value valueObj=null;
54                  try {
55                      valueObj = (Value) object;
56                  } catch (RuntimeException e1) {
57                      e1.printStackTrace();
58                  }
59                  if (valueObj == null)
60                      throw new InternalError("Failed");
61                  //System.out.println("Key=" + key + "; value="+ valueObj.val);
62              }
63              Key keyObj = new Key("bob");
64              System.out.println("failed get=" + cacheMap.get(keyObj));
65              long duration = System.currentTimeMillis() - startTime;
66              System.out.println("Test run took " + duration);
67          } catch (CacheException e) {
68              e.printStackTrace();
69          }
70      }
71  
72      public static class Value implements Externalizable {
73          private String val;
74  
75          public Value() {
76          }
77  
78          public Value(String val) {
79              this.val = val;
80          }
81  
82          public void readExternal(ObjectInput input) throws IOException {
83              val = input.readUTF();
84          }
85  
86          public void writeExternal(ObjectOutput output) throws IOException {
87              output.writeUTF(val);
88          }
89      }
90      public static class Key implements Externalizable {
91          private String val;
92  
93          public Key() {
94          }
95  
96          public Key(String val) {
97              this.val = val;
98          }
99  
100         public void readExternal(ObjectInput input) throws IOException {
101             val = input.readUTF();
102         }
103 
104         public void writeExternal(ObjectOutput output) throws IOException {
105             output.writeUTF(val);
106         }
107 
108         public int hashCode() {
109             return val.hashCode();
110         }
111 
112         public boolean equals(Object other) {
113             if (other == null)
114                 return false;
115             if (getClass() != other.getClass())
116                 return false;
117             Key o = (Key) other;
118             return val.equals(o.val);
119         }
120     }
121 }