File: SerializationTest.java

package info (click to toggle)
backport-util-concurrent 2.2%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 4,684 kB
  • ctags: 7,005
  • sloc: java: 51,261; xml: 405; makefile: 50; perl: 27
file content (205 lines) | stat: -rw-r--r-- 6,698 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

import java.rmi.MarshalledObject;
import java.io.*;
import java.util.List;
import java.util.ArrayList;

import edu.emory.mathcs.backport.java.util.*;
import edu.emory.mathcs.backport.java.util.PriorityQueue;
import edu.emory.mathcs.backport.java.util.concurrent.*;
import edu.emory.mathcs.backport.java.util.concurrent.atomic.*;
import edu.emory.mathcs.backport.java.util.concurrent.locks.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Collection;

/**
 * Class to test serial compatibility between backport versions
 */
public class SerializationTest {

    final static List ldata;
    final static Map mdata;

    static {
        ldata = new ArrayList();
        ldata.add("s1");
        ldata.add("s2");
        ldata.add("s3");
        mdata = new HashMap();
        mdata.put("key 1", "value 1");
        mdata.put("key 2", new Long(4));
    }

    public static void main(String[] args) throws IOException {
        if (args.length < 2) throw new IllegalArgumentException("Need 2 arguments");
        String op       = args[0];
        String filename = args[1];
        File f = new File(filename);
        if ("-serialize".equals(op) || "serialize".equals(op)) {
            FileOutputStream fout = new FileOutputStream(f);
            OutputStream out = new BufferedOutputStream(fout);
            List objs = createObjects();
            for (Iterator itr = objs.iterator(); itr.hasNext(); ) {
                serializeObject(itr.next(), out);
            }
            out.flush();
            out.close();
        }
        else {
            FileInputStream fin = new FileInputStream(f);
            InputStream in = new BufferedInputStream(fin);
            deserializeObjects(in);
        }
    }

    private static List createObjects() {
        List objs = new ArrayList();

        // collections

        objs.add(new ArrayBlockingQueue(100, false, ldata));
        objs.add(new ArrayDeque(ldata));
        objs.add(new LinkedBlockingDeque(ldata));
        objs.add(new LinkedBlockingQueue(ldata));
        objs.add(new LinkedList(ldata));
        objs.add(new PriorityQueue(ldata));
        objs.add(new PriorityBlockingQueue(ldata));
        CopyOnWriteArrayList cowl = new CopyOnWriteArrayList(ldata);
        objs.add(cowl);
        objs.add(cowl.subList(1, 2));
        objs.add(new CopyOnWriteArraySet(ldata));
        objs.add(new SynchronousQueue(false));
        objs.add(new SynchronousQueue(true));

        ConcurrentHashMap m = new ConcurrentHashMap(mdata);
        objs.add(m);
        //objs.add(m.keySet());
        //objs.add(m.values());
        objs.add(new ConcurrentLinkedQueue(ldata));
        NavigableMap nm = new ConcurrentSkipListMap(mdata);
        objs.add(nm);
        objs.add(nm.subMap("key 0", "key 3"));
        NavigableSet ns = new ConcurrentSkipListSet(mdata.keySet());
        objs.add(ns);
        objs.add(ns.subSet("key 0", "key 3"));
        nm = new TreeMap(mdata);
        objs.add(nm);
        objs.add(nm.subMap("key 0", "key 3"));
        ns = new TreeSet(mdata.keySet());
        objs.add(ns);
        objs.add(ns.subSet("key 0", "key 3"));

        // atomics

        objs.add(new AtomicBoolean(true));
        objs.add(new AtomicInteger(123));
        objs.add(new AtomicIntegerArray(new int[] { 1, 2, 3}));
        objs.add(new AtomicLong(123L));
        objs.add(new AtomicLongArray(new long[] { 1L, 2L, 3L}));
        objs.add(new AtomicReference(new Integer(3)));
        objs.add(new AtomicReferenceArray(new Integer[] {
            new Integer(1), new Integer(2), new Integer(3)}));

        // locks

        serializeLock(objs, new ReentrantLock(false));
        serializeLock(objs, new ReentrantLock(true));
        ReentrantReadWriteLock rr = new ReentrantReadWriteLock();
        objs.add(rr);
        serializeLock(objs, rr.readLock());
        serializeLock(objs, rr.writeLock());
        serializeSemaphore(objs, new Semaphore(10, false));
        serializeSemaphore(objs, new Semaphore(10, true));

        // other
        objs.add(TimeUnit.DAYS);
        objs.add(TimeUnit.HOURS);
        objs.add(TimeUnit.MINUTES);
        objs.add(TimeUnit.SECONDS);
        objs.add(TimeUnit.MILLISECONDS);
        objs.add(TimeUnit.MICROSECONDS);
        objs.add(TimeUnit.NANOSECONDS);

        return objs;
    }

    private static void serializeLock(List objs, Lock l) {
        l.lock();
        try {
            objs.add(l);
            objs.add(l.newCondition());
        }
        catch (UnsupportedOperationException e) {}
        finally {
            l.unlock();
        }
    }

    private static void serializeSemaphore(List objs, Semaphore s) {
        s.acquireUninterruptibly();
        try {
            objs.add(s);
        }
        finally {
            s.release();
        }
    }

    private static void serializeObject(Object obj, OutputStream out) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.flush();
            oos.close();
            int size = bos.size();
            DataOutputStream dout = new DataOutputStream(out);
            dout.writeInt(size);
            bos.writeTo(dout);
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static void deserializeObjects(InputStream in) throws IOException {
        DataInput din = new DataInputStream(in);
        while (true) {
            int size;
            try {
                size = din.readInt();
            }
            catch (EOFException e) {
                return;
            }
            byte[] arr = new byte[size];
            din.readFully(arr);
            ByteArrayInputStream bin = new ByteArrayInputStream(arr);
            ObjectInputStream oin = new ObjectInputStream(bin);
            try {
                Object obj = oin.readObject();
                System.out.println(obj);
                if (obj instanceof Lock) {
                    Lock l = (Lock)obj;
                    l.lock();
                    l.unlock();
                }
                else if (obj instanceof ReadWriteLock) {
                    ReadWriteLock rl = (ReadWriteLock)obj;
                    Lock r = rl.readLock();
                    Lock w = rl.writeLock();
                    r.lock();
                    r.unlock();
                    w.newCondition();
                    w.lock();
                    w.unlock();
                }
            }
            catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
}