java/sql-dk/test/info/globalcode/sql/dk/FunctionsTest.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 24 Dec 2013 00:15:04 +0100
branchv_0
changeset 46 0b05bc13aadd
child 51 6730214fab41
permissions -rw-r--r--
function escapeRegEx()
franta-hg@46
     1
/**
franta-hg@46
     2
 * SQL-DK
franta-hg@46
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@46
     4
 *
franta-hg@46
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@46
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@46
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@46
     8
 * (at your option) any later version.
franta-hg@46
     9
 *
franta-hg@46
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@46
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@46
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@46
    13
 * GNU General Public License for more details.
franta-hg@46
    14
 *
franta-hg@46
    15
 * You should have received a copy of the GNU General Public License
franta-hg@46
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@46
    17
 */
franta-hg@46
    18
package info.globalcode.sql.dk;
franta-hg@46
    19
franta-hg@46
    20
import java.util.regex.Matcher;
franta-hg@46
    21
import java.util.regex.Pattern;
franta-hg@46
    22
import org.testng.annotations.Test;
franta-hg@46
    23
import static org.testng.Assert.*;
franta-hg@46
    24
franta-hg@46
    25
/**
franta-hg@46
    26
 *
franta-hg@46
    27
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@46
    28
 */
franta-hg@46
    29
public class FunctionsTest {
franta-hg@46
    30
franta-hg@46
    31
	@Test
franta-hg@46
    32
	public void testEscapeRegEx() {
franta-hg@46
    33
		for (String original : new String[]{"abcd", "1234", "xxx", "\\Eescape\\Q", "\\Qescape\\E", "abc\\Eescape\\Qdef.", ".", ""}) {
franta-hg@46
    34
			String patternString = Functions.escapeRegEx(original);
franta-hg@46
    35
			System.out.println(original + " → " + patternString);
franta-hg@46
    36
franta-hg@46
    37
			Pattern pattern = Pattern.compile(patternString);
franta-hg@46
    38
franta-hg@46
    39
			String testString;
franta-hg@46
    40
			Matcher m;
franta-hg@46
    41
franta-hg@46
    42
			testString = original;
franta-hg@46
    43
			m = pattern.matcher(testString);
franta-hg@46
    44
			assertTrue(m.matches(), "Pattern does not match original string: " + testString);
franta-hg@46
    45
franta-hg@46
    46
			testString = original + "x";
franta-hg@46
    47
			m = pattern.matcher(testString);
franta-hg@46
    48
			assertFalse(m.matches(), "Pattern matches wrong string: " + testString);
franta-hg@46
    49
franta-hg@46
    50
			testString = "x" + original;
franta-hg@46
    51
			m = pattern.matcher(testString);
franta-hg@46
    52
			assertFalse(m.matches(), "Pattern matches wrong string: " + testString);
franta-hg@46
    53
franta-hg@46
    54
			testString = (original + "ab").substring(1);
franta-hg@46
    55
			m = pattern.matcher(testString);
franta-hg@46
    56
			assertFalse(m.matches(), "Pattern matches wrong string: " + testString);
franta-hg@46
    57
		}
franta-hg@46
    58
	}
franta-hg@46
    59
}