Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 352ffbfe3185e052c28ffc0af581979b509b28a4 (plain) (blame)
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
package org.eclipse.ecf.tests.core.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.ecf.core.util.OSGIObjectInputStream;
import org.eclipse.ecf.core.util.OSGIObjectOutputStream;
import org.eclipse.ecf.internal.tests.core.Activator;
import org.osgi.dto.DTO;
import org.osgi.framework.Bundle;
import org.osgi.framework.Version;

import junit.framework.TestCase;

public class OSGIObjectStreamTest extends TestCase {

	public static class MyDTO extends DTO {
		public double d1;
		public String s;
		public MyDTO dto;
	}
	
	public static class MySerializable implements Serializable {
		private static final long serialVersionUID = 7671873163370195115L;
		private String first;
		private long second;
		private byte[] bytes;
		
		public MySerializable(String f, long s, byte[] b) {
			this.first = f;
			this.second = s;
			this.bytes = b;
		}
		
		public String getFirst() {
			return first;
		}
		
		public long getSecond() {
			return second;
		}
		
		public byte[] getBytes() {
			return bytes;
		}
	}

	MyDTO dto1;
	MyDTO dto2;
	Version v1;
	Version v2;
	String s;
	MySerializable ser;
	Bundle b;
	TestEnumImpl testEnum;
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		MyDTO dto1 = new MyDTO();
		dto1.d1 = 1.0;
		dto1.s = "test";
		dto1.dto = null;

		MyDTO dto2 = new MyDTO();
		dto2.d1 = 2.0;
		dto2.s = "test2";
		dto2.dto = new MyDTO();
		dto2.dto.s = "test3";

		v1 = Version.valueOf("1.0.1.myfirstversion");
		v2 = Version.valueOf("2.0.2.mysecondversion");
		
		s = "my string is as it is";
		
		ser = new MySerializable("first string", 100, new byte[] { 1, 2, 3 });
		
		b = Activator.getContext().getBundle();
		
		testEnum = new TestEnumImpl();
	}
	
	public void testOSGIObjectOutputStream() throws Exception {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		OSGIObjectOutputStream oos = new OSGIObjectOutputStream(bos);
		oos.writeObject(ser);
		oos.close();

		ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
		OSGIObjectInputStream ois = new OSGIObjectInputStream(Activator.getContext().getBundle(),bis);
		Object result = ois.readObject();
		ois.close();

	}

	public void testObjectOutputStream() throws Exception {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		oos.writeObject(ser);
		oos.close();

		ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
		ObjectInputStream ois = new ObjectInputStream(bis);
		Object result = ois.readObject();
		ois.close();

	}
	

	public void testDTOMap() throws Exception {
		Map<String, DTO> dtos = new HashMap<String, DTO>();
		dtos.put("one", dto1);
		dtos.put("two", dto2);
		assertTrue(dtos.equals(serializeDeserialize(dtos)));
	}
	
	public void testVersionArray() throws Exception {
		Version[] versions = new Version[2];
		versions[0] = v1;
		versions[1] = v2;
		assertTrue(Arrays.equals(versions, (Version[]) serializeDeserialize(versions)));
	}
	
	public void testString() throws Exception {
		assertEquals(s, (String) serializeDeserialize(s));
	}
	
	private Object serializeDeserialize(Object o) throws IOException, ClassNotFoundException {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		OSGIObjectOutputStream oos = new OSGIObjectOutputStream(bos);
		oos.writeObject(o);
		oos.close();

		ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
		OSGIObjectInputStream ois = new OSGIObjectInputStream(b,bis);
		Object result = ois.readObject();
		ois.close();
		return result;
	}
	
	public void testSerializable() throws Exception {
		MySerializable r = (MySerializable) serializeDeserialize(ser);
		assertEquals(r.getFirst(),ser.getFirst());
		assertEquals(r.getSecond(),ser.getSecond());
		assertTrue(Arrays.equals(r.getBytes(),ser.getBytes()));
	}

	public void testEnum() throws Exception {
		ITestEnum desTestEnum = (ITestEnum) serializeDeserialize(testEnum);
		assertEquals(desTestEnum.getColumnType().name(),testEnum.getColumnType().name());
	}
}

Back to the top