Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c62cbee2446186bc17bebafa4d40ef89366b4a0a (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
/*******************************************************************************
 * Copyright (c) 2010, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 ******************************************************************************/

package org.eclipse.equinox.bidi.internal.tests;

import org.eclipse.equinox.bidi.STextStringRecord;
import org.eclipse.equinox.bidi.STextTypeHandlerFactory;

/**
 *	Tests the StringRecord class	
 */
public class STextStringRecordTest extends STextTestBase {
	public void testStringRecord() {
		STextStringRecord sr;
		boolean catchFlag;
		// check handling of invalid arguments
		catchFlag = false;
		try {
			sr = STextStringRecord.addRecord(null, 1, STextTypeHandlerFactory.EMAIL, 0, 1);
		} catch (IllegalArgumentException e) {
			catchFlag = true;
		}
		assertTrue("Catch null string argument", catchFlag);
		catchFlag = false;
		try {
			sr = STextStringRecord.addRecord("abc", 1, null, 0, 1);
		} catch (IllegalArgumentException e) {
			catchFlag = true;
		}
		assertTrue("Catch null handler argument", catchFlag);
		catchFlag = false;
		try {
			sr = STextStringRecord.addRecord("abc", 0, STextTypeHandlerFactory.EMAIL, 0, 1);
		} catch (IllegalArgumentException e) {
			catchFlag = true;
		}
		assertTrue("Catch invalid segment count argument", catchFlag);
		catchFlag = false;
		try {
			sr = STextStringRecord.addRecord("abc", 1, STextTypeHandlerFactory.EMAIL, -1, 1);
		} catch (IllegalArgumentException e) {
			catchFlag = true;
		}
		assertTrue("Catch invalid start argument", catchFlag);
		catchFlag = false;
		try {
			sr = STextStringRecord.addRecord("abc", 1, STextTypeHandlerFactory.EMAIL, 4, 1);
		} catch (IllegalArgumentException e) {
			catchFlag = true;
		}
		assertTrue("Catch invalid start argument", catchFlag);
		catchFlag = false;
		try {
			sr = STextStringRecord.addRecord("abc", 1, STextTypeHandlerFactory.EMAIL, 0, 0);
		} catch (IllegalArgumentException e) {
			catchFlag = true;
		}
		assertTrue("Catch invalid limit argument", catchFlag);
		catchFlag = false;
		try {
			sr = STextStringRecord.addRecord("abc", 1, STextTypeHandlerFactory.EMAIL, 0, 5);
		} catch (IllegalArgumentException e) {
			catchFlag = true;
		}
		assertTrue("Catch invalid limit argument", catchFlag);

		int poolSize = STextStringRecord.POOLSIZE;
		int lim = poolSize / 2;
		sr = STextStringRecord.getRecord("XXX");
		assertEquals(null, sr);
		for (int i = 0; i < lim; i++) {
			String str = Integer.toString(i);
			sr = STextStringRecord.addRecord(str, 1, STextTypeHandlerFactory.EMAIL, 0, 1);
		}
		sr = STextStringRecord.getRecord(null);
		assertEquals(null, sr);
		sr = STextStringRecord.getRecord("");
		assertEquals(null, sr);

		for (int i = 0; i < poolSize; i++) {
			String str = Integer.toString(i);
			sr = STextStringRecord.getRecord(str);
			if (i < lim)
				assertFalse(null == sr);
			else
				assertTrue(null == sr);
		}

		for (int i = lim; i <= poolSize; i++) {
			String str = Integer.toString(i);
			sr = STextStringRecord.addRecord(str, 1, STextTypeHandlerFactory.EMAIL, 0, 1);
		}
		for (int i = 1; i <= poolSize; i++) {
			String str = Integer.toString(i);
			sr = STextStringRecord.getRecord(str);
			assertFalse(null == sr);
		}
		sr = STextStringRecord.getRecord("0");
		assertEquals(null, sr);
		sr = STextStringRecord.addRecord("thisisalongstring", 3, STextTypeHandlerFactory.EMAIL, 0, 2);
		sr.addSegment(STextTypeHandlerFactory.JAVA, 4, 5);
		sr.addSegment(STextTypeHandlerFactory.FILE, 6, 7);
		catchFlag = false;
		try {
			sr.addSegment(STextTypeHandlerFactory.EMAIL, 10, 13);
		} catch (IllegalStateException e) {
			catchFlag = true;
		}
		assertTrue("Catch too many segments", catchFlag);
		assertEquals(3, sr.getSegmentCount());
		assertEquals(STextTypeHandlerFactory.EMAIL, sr.getHandler(0));
		assertEquals(STextTypeHandlerFactory.JAVA, sr.getHandler(1));
		assertEquals(STextTypeHandlerFactory.FILE, sr.getHandler(2));
		assertEquals(0, sr.getStart(0));
		assertEquals(4, sr.getStart(1));
		assertEquals(6, sr.getStart(2));
		assertEquals(2, sr.getLimit(0));
		assertEquals(5, sr.getLimit(1));
		assertEquals(7, sr.getLimit(2));
		catchFlag = false;
		try {
			sr.getLimit(3);
		} catch (IllegalArgumentException e) {
			catchFlag = true;
		}
		assertTrue("Catch segment number too large", catchFlag);

		STextStringRecord.clear();
		for (int i = 0; i <= poolSize; i++) {
			String str = Integer.toString(i);
			sr = STextStringRecord.getRecord(str);
			assertEquals(null, sr);
		}
	}
}

Back to the top