1 /*******************************************************************************
2  * Copyright (c) 2010, 2011 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  ******************************************************************************/
14 
15 package org.eclipse.equinox.bidi.internal.tests;
16 
17 import static org.junit.Assert.assertEquals;
18 
19 import java.util.Locale;
20 import org.eclipse.equinox.bidi.StructuredTextProcessor;
21 import org.eclipse.equinox.bidi.StructuredTextTypeHandlerFactory;
22 import org.eclipse.equinox.bidi.advanced.IStructuredTextExpert;
23 import org.eclipse.equinox.bidi.advanced.StructuredTextExpertFactory;
24 import org.junit.*;
25 
26 /**
27  * Tests methods in BidiComplexUtil
28  */
29 
30 public class StructuredTextProcessorTest extends StructuredTextTestBase {
31 
32 	private static final String HEBREW = "iw";
33 
34 	private static final String HEBREW2 = "he";
35 
36 	private static final String ARABIC = "ar";
37 
38 	private static final String FARSI = "fa";
39 
40 	private static final String URDU = "ur";
41 
42 	private Locale locale;
43 
44 	@Before
setUp()45 	public void setUp() throws Exception {
46 		locale = Locale.getDefault();
47 	}
48 
49 	@After
tearDown()50 	public void tearDown() {
51 		Locale.setDefault(locale);
52 	}
53 
doTest1(String data, String result)54 	private void doTest1(String data, String result) {
55 		Locale.setDefault(Locale.ENGLISH);
56 		String full = StructuredTextProcessor.process(toUT16(data));
57 		// Since https://bugs.eclipse.org/467836 , processing also works in non-bidi locales:
58 		assertEquals("Util #1 full EN - ", result, toPseudo(full));
59 		Locale.setDefault(new Locale(HEBREW2));
60 		full = StructuredTextProcessor.process(toUT16(data));
61 		assertEquals("Util #1 full HE - ", result, toPseudo(full));
62 		Locale.setDefault(new Locale(ARABIC));
63 		full = StructuredTextProcessor.process(toUT16(data));
64 		assertEquals("Util #1 full AR - ", result, toPseudo(full));
65 		Locale.setDefault(new Locale(FARSI));
66 		full = StructuredTextProcessor.process(toUT16(data));
67 		assertEquals("Util #1 full FA - ", result, toPseudo(full));
68 		Locale.setDefault(new Locale(URDU));
69 		full = StructuredTextProcessor.process(toUT16(data));
70 		assertEquals("Util #1 full UR - ", result, toPseudo(full));
71 		Locale.setDefault(new Locale(HEBREW));
72 		full = StructuredTextProcessor.process(toUT16(data));
73 		String ful2 = StructuredTextProcessor.process(toUT16(data), (String) null);
74 		assertEquals("Util #1 full - ", result, toPseudo(full));
75 		assertEquals("Util #1 ful2 - ", result, toPseudo(ful2));
76 		String lean = StructuredTextProcessor.deprocess(full);
77 		assertEquals("Util #1 lean - ", data, toPseudo(lean));
78 	}
79 
doTest2(String msg, String data, String result)80 	private void doTest2(String msg, String data, String result) {
81 		doTest2(msg, data, result, data);
82 	}
83 
doTest2(String msg, String data, String result, String resLean)84 	private void doTest2(String msg, String data, String result, String resLean) {
85 		String full = StructuredTextProcessor.process(toUT16(data), "*");
86 		assertEquals(msg + "full", result, toPseudo(full));
87 		String lean = StructuredTextProcessor.deprocess(full);
88 		assertEquals(msg + "lean", resLean, toPseudo(lean));
89 	}
90 
doTest3(String msg, String data, String result)91 	private void doTest3(String msg, String data, String result) {
92 		doTest3(msg, data, result, data);
93 	}
94 
doTest3(String msg, String data, String result, String resLean)95 	private void doTest3(String msg, String data, String result, String resLean) {
96 		String full = StructuredTextProcessor.processTyped(toUT16(data), StructuredTextTypeHandlerFactory.COMMA_DELIMITED);
97 		assertEquals(msg + "full", result, toPseudo(full));
98 		String lean = StructuredTextProcessor.deprocessTyped(full, StructuredTextTypeHandlerFactory.COMMA_DELIMITED);
99 		assertEquals(msg + "lean", resLean, toPseudo(lean));
100 	}
101 
doTest4(String msg, String data, int[] offsets, int direction, int affixLength, String result)102 	private void doTest4(String msg, String data, int[] offsets, int direction, int affixLength, String result) {
103 		String txt = msg + "text=" + data + "\n    offsets=" + array_display(offsets) + "\n    direction=" + direction + "\n    affixLength=" + affixLength;
104 		String lean = toUT16(data);
105 		IStructuredTextExpert expert = StructuredTextExpertFactory.getExpert();
106 		String full = expert.insertMarks(lean, offsets, direction, affixLength);
107 		assertEquals(txt, result, toPseudo(full));
108 	}
109 
110 	@Test
testStructuredTextProcessor()111 	public void testStructuredTextProcessor() {
112 		// Test process() and deprocess() with default delimiters
113 		doTest1("ABC/DEF/G", ">@ABC@/DEF@/G@^");
114 		// Test process() and deprocess() with specified delimiters
115 		doTest2("Util #2.1 - ", "", "");
116 		doTest2("Util #2.2 - ", ">@ABC@^", ">@ABC@^", "ABC");
117 		doTest2("Util #2.3 - ", "abc", "abc");
118 		doTest2("Util #2.4 - ", "!abc", ">@!abc@^");
119 		doTest2("Util #2.5 - ", "abc!", ">@abc!@^");
120 		doTest2("Util #2.6 - ", "ABC*DEF*G", ">@ABC@*DEF@*G@^");
121 		// Test process() and deprocess() with specified expression type
122 		doTest3("Util #3.1 - ", "ABC,DEF,G", ">@ABC@,DEF@,G@^");
123 		doTest3("Util #3.2 - ", "", "");
124 		doTest3("Util #3.3 - ", ">@DEF@^", ">@DEF@^", "DEF");
125 		// Test insertMarks()
126 		doTest4("Util #4.1 - ", "ABCDEFG", new int[] {3, 6}, 0, 0, "ABC@DEF@G");
127 		doTest4("Util #4.2 - ", "ABCDEFG", new int[] {3, 6}, 0, 2, ">@ABC@DEF@G@^");
128 		doTest4("Util #4.3 - ", "ABCDEFG", new int[] {3, 6}, 1, 0, "ABC&DEF&G");
129 		doTest4("Util #4.4 - ", "ABCDEFG", new int[] {3, 6}, 1, 2, "<&ABC&DEF&G&^");
130 		doTest4("Util #4.5 - ", "", new int[] {3, 6}, 0, 0, "");
131 		doTest4("Util #4.6 - ", "", new int[] {3, 6}, 0, 2, "");
132 		doTest4("Util #4.7 - ", "ABCDEFG", null, 1, 0, "ABCDEFG");
133 		doTest4("Util #4.8 - ", "ABCDEFG", null, 1, 2, "<&ABCDEFG&^");
134 	}
135 }
136