aboutsummaryrefslogtreecommitdiffstats
path: root/ncomp-utils-journaling/src/main/java/org/openecomp/ncomp/utils/journaling/JournalingTest.java
blob: dba03abbcf12e72f5321b718f36e8f68330dc387 (plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*-
 * ============LICENSE_START==========================================
 * OPENECOMP - DCAE
 * ===================================================================
 * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 */
	
package org.openecomp.ncomp.utils.journaling;

import java.io.File;
import java.io.Serializable;

import junit.framework.TestCase;

public class JournalingTest extends TestCase {
	private static File clean(File f) {
		if (f.isDirectory()) {
			for (File x: f.listFiles()) {
				clean(x);
			}
		}
		f.delete();
		return(f);
	}
	@SuppressWarnings("unchecked")
	public void test_case1() {
		File f = clean(new File("testdata/journal/case1"));
		JournalingHashMap<String> m = JournalingHashMap.create(f);
		m.put("foo", "bar");
		m.save();
		m.close();
		for (int i = 0; i < 3; i++) {
			// overwrite object with new. Can test that it has the correct
			// state.
			m = JournalingHashMap.create(f);
			assertEquals("bar", m.get("foo"));
			m.close();
		}
	}

	public void test_case2() {
		File f = clean(new File("testdata/journal/case2"));
		MyTestJObject o = MyTestJObject.create(f);
		o.setS("foo");
		o.setS("foo2");
		o.m.put("a", "b");
		o.l.add("foo1");
		o.l.add("foo2");
		o.l.add("foo3");
		o.l.remove(0);
		o.save();
		o.setS("bar");
		o.close();
		for (int i = 0; i < 3; i++) {
			o = MyTestJObject.create(f);
			assertEquals("foo2", o.getS());
			assertEquals("b", o.m.get("a"));
			assertEquals("foo2", o.l.get(0));
			o.close();
		}
	}

	public void test_case3() {
		File f = clean(new File("testdata/journal/case3"));
		MyTestJObject2 o = MyTestJObject2.create(f);
		o.setS("foo");
		// hashmap
		MyTestJObject oo = o.m.newKey("x", MyTestJObject.class);
		oo.m.put("a", "b");
		oo.setS("jjj");
		// list
		MyTestJObject ooo = o.l.addNew(MyTestJObject.class);
		ooo = o.l.addNew(MyTestJObject.class);
		ooo.m.put("aa", "bb");
		ooo.setS("kkk");
		// hashmaplist
		JournalingList<MyTestJObject> x = o.ml.newList("x");
		MyTestJObject xx = x.addNew(MyTestJObject.class);
		xx.m.put("aa", "bb");
		xx.setS("kkk");
		o.save();
		o.setS("bar");
		o.close();
		for (int i = 0; i < 3; i++) {
			// System.out.println("Round: " + i);
			o = MyTestJObject2.create(f);
			oo = o.m.get("x");
			assertEquals("foo", o.getS());
			assertEquals("jjj", oo.getS());
			assertEquals("b", oo.m.get("a"));
			assertEquals(2, o.l.size());
			ooo = o.l.get(1);
			assertEquals("kkk", ooo.getS());
			assertEquals("bb", ooo.m.get("aa"));
			assertEquals(0, o.l.get(0).m.size());
			xx = o.ml.get("x").get(0);
			assertEquals("kkk", xx.getS());
			assertEquals("bb", xx.m.get("aa"));
			assertEquals(0, o.l.get(0).m.size());

			o.close();
		}
	}

	public void test_case4() {
		File f = clean(new File("testdata/journal/case4"));
		MyTestJObject o = MyTestJObject.create(f);
		// o.setSnapshotInterval(1); // 1ms snapshots. This should force
		// snapshots on each save.
		o.setS("foo:1");
		o.setS("foo:2");
		o.save();
		// assertEquals(2,o.getLogSize());
		o.createSnapshot();
		// assertEquals(0,o.getLogSize());
		o.setS("foo:3");
		o.save();
		o.createSnapshot();
		o.l.add("foo1");
		o.save();
		o.close();

		for (int i = 0; i < 3; i++) {
			o = MyTestJObject.create(f);
			System.out.println("Round: " + i + " " + o);
			assertEquals("foo:3", o.getS());
			assertEquals("foo1", o.l.get(0));
			o.close();
		}
	}

	public void test_case5() {
		File f = clean(new File("testdata/journal/case5"));
		MyTestJObject2 o = MyTestJObject2.create(f);
		// o.setSnapshotInterval(1); // 1ms snapshots. This should force
		// snapshots on each save.
		o.setS("foo");
		o.save();
		for (int i = 0; i < 10; i++) {
			o.setS("foo:" + i);
		}
		assertTrue(o.getLogSize() == 12);
		o.close();

		for (int i = 0; i < 3; i++) {
			o = MyTestJObject2.create(f);
			System.out.println("Round: " + i + " " + o);
			assertEquals("foo", o.getS());
			o.close();
		}
	}
}

class MyTestJObject extends JournalingObject implements Serializable {
	@Override
	public String toString() {
		return "MyTestJObject [s=" + s + ", m=" + m + ", l=" + l + "]";
	}

	private static final long serialVersionUID = 1L;
	private String s;
	JournalingHashMap<String> m;
	JournalingList<String> l;

	public MyTestJObject(String context, JournalingObject parent) {
		super(context, parent);
	}

	public MyTestJObject() {
		// TODO Auto-generated constructor stub
	}

	public String getS() {
		return s;
	}

	public void setS(String str) {
		logAttributeValue("s", str);
		this.s = str;
	}

	@Override
	public void init() {
		if (m == null)
			m = new JournalingHashMap<String>("m", this);
		else
			initChild("m", m);
		if (l == null)
			l = new JournalingList<String>("l", this);
		else
			initChild("l", l);
	}

	static public MyTestJObject create(File dir) {
		return (MyTestJObject) create2(dir, new MyTestJObject());
	}
}

class MyTestJObject2 extends JournalingObject implements Serializable {
	@Override
	public String toString() {
		return "MyTestJObject2 [s=" + s + ", m=" + m + ", l=" + l + ", ml="
				+ ml + "]";
	}

	private static final long serialVersionUID = 1L;
	private String s;
	JournalingHashMap<MyTestJObject> m;
	JournalingList<MyTestJObject> l;
	JournalingHashMap<JournalingList<MyTestJObject>> ml;

	public String getS() {
		return s;
	}

	public void setS(String str) {
		logAttributeValue("s", str);
		this.s = str;
	}

	@Override
	public void init() {
		if (m == null)
			m = new JournalingHashMap<MyTestJObject>("m", this);
		else
			initChild("m", m);
		if (l == null)
			l = new JournalingList<MyTestJObject>("l", this);
		else 
			initChild("l", l);
		if (ml == null)
			ml = new JournalingHashMap<JournalingList<MyTestJObject>>("ml", this);
		else 
			initChild("ml", ml);
	}

	static public MyTestJObject2 create(File dir) {
		return (MyTestJObject2) create2(dir, new MyTestJObject2());
	}

}