KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > samples > prevayler > SoftMapTest


1 package com.tirsen.nanning.samples.prevayler;
2
3 import java.io.*;
4
5 import junit.framework.TestCase;
6
7 import com.tirsen.nanning.test.TestUtils;
8
9 import com.tirsen.nanning.samples.prevayler.SoftMap;
10 import com.tirsen.nanning.config.AspectSystem;
11 import com.tirsen.nanning.config.FindTargetMixinAspect;
12 import com.tirsen.nanning.Aspects;
13
14 public class SoftMapTest extends TestCase {
15     private String JavaDoc smallObject;
16     private String JavaDoc largeObject;
17
18     protected void setUp() throws Exception JavaDoc {
19         super.setUp();
20
21         smallObject = "key";
22         largeObject = "hej";
23         for (int i = 0; i < 400; i++) {
24             largeObject += "hej";
25         }
26     }
27
28     public void testSoftValues() throws InterruptedException JavaDoc {
29
30         SoftMap map = SoftMap.createSoftValuesMap();
31         map.put(smallObject, largeObject);
32
33         assertEquals(largeObject, map.get(smallObject));
34         largeObject = null;
35
36         TestUtils.gc();
37
38         assertNull("Value was not removed after GC", map.get(smallObject));
39     }
40
41     public void testSoftKeys() throws InterruptedException JavaDoc {
42
43         SoftMap map = SoftMap.createSoftKeysMap();
44         map.put(largeObject, smallObject);
45
46         assertEquals(smallObject, map.get(largeObject));
47         largeObject = null;
48
49         TestUtils.gc();
50
51         assertNull("Value was not removed after GC", map.get(largeObject));
52     }
53
54     public void testSerializationOfSoftValues() throws IOException, ClassNotFoundException JavaDoc {
55
56         SoftMap map = SoftMap.createSoftValuesMap();
57         map.put(smallObject, largeObject);
58
59         ByteArrayOutputStream data = new ByteArrayOutputStream();
60         ObjectOutputStream out = new ObjectOutputStream(data);
61         out.writeObject(map);
62         out.close();
63
64         ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data.toByteArray()));
65         SoftMap readMap = (SoftMap) in.readObject();
66         in.close();
67         in = null;
68         out = null;
69
70         assertEquals(largeObject, readMap.get(smallObject));
71         largeObject = null;
72
73         TestUtils.gc();
74         assertNull("Value was not removed after serialization and GC", readMap.get(smallObject));
75     }
76
77     public void testSerializationOfSoftKeys() throws IOException, ClassNotFoundException JavaDoc {
78
79         SoftMap map = SoftMap.createSoftKeysMap();
80         map.put(largeObject, smallObject);
81
82         ByteArrayOutputStream data = new ByteArrayOutputStream();
83         ObjectOutputStream out = new ObjectOutputStream(data);
84         out.writeObject(map);
85         out.close();
86
87         ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data.toByteArray()));
88         SoftMap readMap = (SoftMap) in.readObject();
89         in.close();
90         in = null;
91         out = null;
92
93         assertEquals(smallObject, readMap.get(largeObject));
94         largeObject = null;
95
96         TestUtils.gc();
97         assertNull("Value was not removed after serialization and GC", readMap.get(largeObject));
98     }
99     
100     public void testSerializationWithAspects() throws IOException, ClassNotFoundException JavaDoc {
101         AspectSystem system = new AspectSystem();
102         Aspects.setContextAspectFactory(system);
103         system.addAspect(new FindTargetMixinAspect());
104         Object JavaDoc o = system.newInstance(MyObject.class);
105
106         SoftMap map = SoftMap.createSoftKeysMap();
107         map.put(o, "miffo");
108         
109         ByteArrayOutputStream data = new ByteArrayOutputStream();
110         ObjectOutputStream out = new ObjectOutputStream(data);
111         out.writeObject(map);
112         out.close();
113
114         ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data.toByteArray()));
115         SoftMap readMap = (SoftMap) in.readObject();
116         in.close();
117         in = null;
118         out = null;
119
120         assertEquals("miffo", readMap.get(readMap.keySet().iterator().next()));
121     }
122 }
Popular Tags