Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eb638a8d35b0313b1d1bac0e24f30572f58ec6c6 (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
/*******************************************************************************
 * Copyright (c) 2007, 2016 Symbian Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Andrew Ferguson (Symbian) - Initial implementation
 *     Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.pdom.tests;

import java.io.File;
import java.util.Iterator;
import java.util.Properties;

import junit.framework.Test;

import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
import org.eclipse.cdt.internal.core.pdom.db.ChunkCache;
import org.eclipse.cdt.internal.core.pdom.db.DBProperties;
import org.eclipse.cdt.internal.core.pdom.db.Database;
import org.eclipse.core.runtime.CoreException;

/**
 * Tests for the {@link DBProperties} class.
 */
public class DBPropertiesTests extends BaseTestCase {
	File dbLoc;
	Database db;

	public static Test suite() {
		return suite(DBPropertiesTests.class);
	}

	@Override
	protected void setUp() throws Exception {
		dbLoc = File.createTempFile("test", "db");
		dbLoc.deleteOnExit();
		db = new Database(dbLoc, new ChunkCache(), 0, false);
		db.setExclusiveLock();
	}

	@Override
	protected void tearDown() throws Exception {
		db.close();
	}

	public void testBasic() throws CoreException {
		DBProperties properties = new DBProperties(db);
		Properties expected = System.getProperties();
		for (Iterator i = expected.keySet().iterator(); i.hasNext();) {
			String key = (String) i.next();
			String value = expected.getProperty(key);
			if (value != null) {
				properties.setProperty(key, value);
			}
		}
		for (Iterator i = expected.keySet().iterator(); i.hasNext();) {
			String key = (String) i.next();
			String aValue = properties.getProperty(key);
			assertEquals(expected.getProperty(key), aValue);
		}
		for (Iterator i = expected.keySet().iterator(); i.hasNext();) {
			String key = (String) i.next();
			properties.removeProperty(key);
		}
		assertEquals(0, properties.getKeySet().size());

		properties.delete();
	}

	public void testLong() throws Exception {
		DBProperties ps = new DBProperties(db);

		StringBuilder largeValue = new StringBuilder();
		for (int i = 0; i < Database.CHUNK_SIZE * 2; i += 64) {
			largeValue.append("********");
			ps.setProperty("key", largeValue.toString());
			ps.setProperty(largeValue.toString(), "value");
		}

		assertEquals(largeValue.toString(), ps.getProperty("key"));
		assertEquals("value", ps.getProperty(largeValue.toString()));

		ps.delete();
	}

	public void testNulls() throws Exception {
		DBProperties ps = new DBProperties(db);
		try {
			ps.setProperty(null, "val1");
			fail("NullPointerException expected");
		} catch (NullPointerException e) {
		} catch (AssertionError e) {
		}

		try {
			ps.setProperty("key", null);
			fail("NullPointerException expected");
		} catch (NullPointerException e) {
		} catch (AssertionError e) {
		}

		try {
			ps.setProperty(null, null);
			fail("NullPointerException expected");
		} catch (NullPointerException e) {
		} catch (AssertionError e) {
		}

		assertFalse(ps.removeProperty(null));

		assertNull(ps.getProperty(null));

		String s = "" + System.currentTimeMillis();
		assertEquals(s, ps.getProperty(null, s));
	}

	public void testSeq() throws Exception {
		DBProperties ps = new DBProperties(db);

		ps.setProperty("a", "b");
		assertEquals("b", ps.getProperty("a"));
		assertEquals(1, ps.getKeySet().size());

		ps.setProperty("b", "c");
		assertEquals("c", ps.getProperty("b"));
		assertEquals(2, ps.getKeySet().size());

		ps.setProperty("a", "c");
		assertEquals("c", ps.getProperty("a"));
		assertEquals(2, ps.getKeySet().size());

		boolean deleted = ps.removeProperty("c");
		assertEquals(false, deleted);
		assertEquals(2, ps.getKeySet().size());

		deleted = ps.removeProperty("a");
		assertEquals(true, deleted);
		assertEquals(1, ps.getKeySet().size());

		ps.delete();
	}
}

Back to the top