You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileNameMatcherTest.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*
  2. * Copyright (C) 2008, Florian Koeberle <florianskarten@web.de>
  3. * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.fnmatch;
  45. import static org.eclipse.jgit.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import org.eclipse.jgit.errors.InvalidPatternException;
  50. import org.junit.Test;
  51. public class FileNameMatcherTest {
  52. private static void assertMatch(final String pattern, final String input,
  53. final boolean matchExpected, final boolean appendCanMatchExpected)
  54. throws InvalidPatternException {
  55. final FileNameMatcher matcher = new FileNameMatcher(pattern, null);
  56. matcher.append(input);
  57. assertEquals(matchExpected, matcher.isMatch());
  58. assertEquals(appendCanMatchExpected, matcher.canAppendMatch());
  59. }
  60. private static void assertFileNameMatch(final String pattern,
  61. final String input,
  62. final char excludedCharacter, final boolean matchExpected,
  63. final boolean appendCanMatchExpected)
  64. throws InvalidPatternException {
  65. final FileNameMatcher matcher = new FileNameMatcher(pattern,
  66. Character.valueOf(excludedCharacter));
  67. matcher.append(input);
  68. assertEquals(matchExpected, matcher.isMatch());
  69. assertEquals(appendCanMatchExpected, matcher.canAppendMatch());
  70. }
  71. @Test
  72. public void testVerySimplePatternCase0() throws Exception {
  73. assertMatch("", "", true, false);
  74. }
  75. @Test
  76. public void testVerySimplePatternCase1() throws Exception {
  77. assertMatch("ab", "a", false, true);
  78. }
  79. @Test
  80. public void testVerySimplePatternCase2() throws Exception {
  81. assertMatch("ab", "ab", true, false);
  82. }
  83. @Test
  84. public void testVerySimplePatternCase3() throws Exception {
  85. assertMatch("ab", "ac", false, false);
  86. }
  87. @Test
  88. public void testVerySimplePatternCase4() throws Exception {
  89. assertMatch("ab", "abc", false, false);
  90. }
  91. @Test
  92. public void testVerySimpleWirdcardCase0() throws Exception {
  93. assertMatch("?", "a", true, false);
  94. }
  95. @Test
  96. public void testVerySimpleWildCardCase1() throws Exception {
  97. assertMatch("??", "a", false, true);
  98. }
  99. @Test
  100. public void testVerySimpleWildCardCase2() throws Exception {
  101. assertMatch("??", "ab", true, false);
  102. }
  103. @Test
  104. public void testVerySimpleWildCardCase3() throws Exception {
  105. assertMatch("??", "abc", false, false);
  106. }
  107. @Test
  108. public void testVerySimpleStarCase0() throws Exception {
  109. assertMatch("*", "", true, true);
  110. }
  111. @Test
  112. public void testVerySimpleStarCase1() throws Exception {
  113. assertMatch("*", "a", true, true);
  114. }
  115. @Test
  116. public void testVerySimpleStarCase2() throws Exception {
  117. assertMatch("*", "ab", true, true);
  118. }
  119. @Test
  120. public void testSimpleStarCase0() throws Exception {
  121. assertMatch("a*b", "a", false, true);
  122. }
  123. @Test
  124. public void testSimpleStarCase1() throws Exception {
  125. assertMatch("a*c", "ac", true, true);
  126. }
  127. @Test
  128. public void testSimpleStarCase2() throws Exception {
  129. assertMatch("a*c", "ab", false, true);
  130. }
  131. @Test
  132. public void testSimpleStarCase3() throws Exception {
  133. assertMatch("a*c", "abc", true, true);
  134. }
  135. @Test
  136. public void testManySolutionsCase0() throws Exception {
  137. assertMatch("a*a*a", "aaa", true, true);
  138. }
  139. @Test
  140. public void testManySolutionsCase1() throws Exception {
  141. assertMatch("a*a*a", "aaaa", true, true);
  142. }
  143. @Test
  144. public void testManySolutionsCase2() throws Exception {
  145. assertMatch("a*a*a", "ababa", true, true);
  146. }
  147. @Test
  148. public void testManySolutionsCase3() throws Exception {
  149. assertMatch("a*a*a", "aaaaaaaa", true, true);
  150. }
  151. @Test
  152. public void testManySolutionsCase4() throws Exception {
  153. assertMatch("a*a*a", "aaaaaaab", false, true);
  154. }
  155. @Test
  156. public void testVerySimpleGroupCase0() throws Exception {
  157. assertMatch("[ab]", "a", true, false);
  158. }
  159. @Test
  160. public void testVerySimpleGroupCase1() throws Exception {
  161. assertMatch("[ab]", "b", true, false);
  162. }
  163. @Test
  164. public void testVerySimpleGroupCase2() throws Exception {
  165. assertMatch("[ab]", "ab", false, false);
  166. }
  167. @Test
  168. public void testVerySimpleGroupRangeCase0() throws Exception {
  169. assertMatch("[b-d]", "a", false, false);
  170. }
  171. @Test
  172. public void testVerySimpleGroupRangeCase1() throws Exception {
  173. assertMatch("[b-d]", "b", true, false);
  174. }
  175. @Test
  176. public void testVerySimpleGroupRangeCase2() throws Exception {
  177. assertMatch("[b-d]", "c", true, false);
  178. }
  179. @Test
  180. public void testVerySimpleGroupRangeCase3() throws Exception {
  181. assertMatch("[b-d]", "d", true, false);
  182. }
  183. @Test
  184. public void testVerySimpleGroupRangeCase4() throws Exception {
  185. assertMatch("[b-d]", "e", false, false);
  186. }
  187. @Test
  188. public void testVerySimpleGroupRangeCase5() throws Exception {
  189. assertMatch("[b-d]", "-", false, false);
  190. }
  191. @Test
  192. public void testTwoGroupsCase0() throws Exception {
  193. assertMatch("[b-d][ab]", "bb", true, false);
  194. }
  195. @Test
  196. public void testTwoGroupsCase1() throws Exception {
  197. assertMatch("[b-d][ab]", "ca", true, false);
  198. }
  199. @Test
  200. public void testTwoGroupsCase2() throws Exception {
  201. assertMatch("[b-d][ab]", "fa", false, false);
  202. }
  203. @Test
  204. public void testTwoGroupsCase3() throws Exception {
  205. assertMatch("[b-d][ab]", "bc", false, false);
  206. }
  207. @Test
  208. public void testTwoRangesInOneGroupCase0() throws Exception {
  209. assertMatch("[b-ce-e]", "a", false, false);
  210. }
  211. @Test
  212. public void testTwoRangesInOneGroupCase1() throws Exception {
  213. assertMatch("[b-ce-e]", "b", true, false);
  214. }
  215. @Test
  216. public void testTwoRangesInOneGroupCase2() throws Exception {
  217. assertMatch("[b-ce-e]", "c", true, false);
  218. }
  219. @Test
  220. public void testTwoRangesInOneGroupCase3() throws Exception {
  221. assertMatch("[b-ce-e]", "d", false, false);
  222. }
  223. @Test
  224. public void testTwoRangesInOneGroupCase4() throws Exception {
  225. assertMatch("[b-ce-e]", "e", true, false);
  226. }
  227. @Test
  228. public void testTwoRangesInOneGroupCase5() throws Exception {
  229. assertMatch("[b-ce-e]", "f", false, false);
  230. }
  231. @Test
  232. public void testIncompleteRangesInOneGroupCase0() throws Exception {
  233. assertMatch("a[b-]", "ab", true, false);
  234. }
  235. @Test
  236. public void testIncompleteRangesInOneGroupCase1() throws Exception {
  237. assertMatch("a[b-]", "ac", false, false);
  238. }
  239. @Test
  240. public void testIncompleteRangesInOneGroupCase2() throws Exception {
  241. assertMatch("a[b-]", "a-", true, false);
  242. }
  243. @Test
  244. public void testCombinedRangesInOneGroupCase0() throws Exception {
  245. assertMatch("[a-c-e]", "b", true, false);
  246. }
  247. /**
  248. * The c belongs to the range a-c. "-e" is no valid range so d should not
  249. * match.
  250. *
  251. * @throws Exception
  252. * for some reasons
  253. */
  254. @Test
  255. public void testCombinedRangesInOneGroupCase1() throws Exception {
  256. assertMatch("[a-c-e]", "d", false, false);
  257. }
  258. @Test
  259. public void testCombinedRangesInOneGroupCase2() throws Exception {
  260. assertMatch("[a-c-e]", "e", true, false);
  261. }
  262. @Test
  263. public void testInversedGroupCase0() throws Exception {
  264. assertMatch("[!b-c]", "a", true, false);
  265. }
  266. @Test
  267. public void testInversedGroupCase1() throws Exception {
  268. assertMatch("[!b-c]", "b", false, false);
  269. }
  270. @Test
  271. public void testInversedGroupCase2() throws Exception {
  272. assertMatch("[!b-c]", "c", false, false);
  273. }
  274. @Test
  275. public void testInversedGroupCase3() throws Exception {
  276. assertMatch("[!b-c]", "d", true, false);
  277. }
  278. @Test
  279. public void testAlphaGroupCase0() throws Exception {
  280. assertMatch("[[:alpha:]]", "d", true, false);
  281. }
  282. @Test
  283. public void testAlphaGroupCase1() throws Exception {
  284. assertMatch("[[:alpha:]]", ":", false, false);
  285. }
  286. @Test
  287. public void testAlphaGroupCase2() throws Exception {
  288. // \u00f6 = 'o' with dots on it
  289. assertMatch("[[:alpha:]]", "\u00f6", true, false);
  290. }
  291. @Test
  292. public void test2AlphaGroupsCase0() throws Exception {
  293. // \u00f6 = 'o' with dots on it
  294. assertMatch("[[:alpha:]][[:alpha:]]", "a\u00f6", true, false);
  295. assertMatch("[[:alpha:]][[:alpha:]]", "a1", false, false);
  296. }
  297. @Test
  298. public void testAlnumGroupCase0() throws Exception {
  299. assertMatch("[[:alnum:]]", "a", true, false);
  300. }
  301. @Test
  302. public void testAlnumGroupCase1() throws Exception {
  303. assertMatch("[[:alnum:]]", "1", true, false);
  304. }
  305. @Test
  306. public void testAlnumGroupCase2() throws Exception {
  307. assertMatch("[[:alnum:]]", ":", false, false);
  308. }
  309. @Test
  310. public void testBlankGroupCase0() throws Exception {
  311. assertMatch("[[:blank:]]", " ", true, false);
  312. }
  313. @Test
  314. public void testBlankGroupCase1() throws Exception {
  315. assertMatch("[[:blank:]]", "\t", true, false);
  316. }
  317. @Test
  318. public void testBlankGroupCase2() throws Exception {
  319. assertMatch("[[:blank:]]", "\r", false, false);
  320. }
  321. @Test
  322. public void testBlankGroupCase3() throws Exception {
  323. assertMatch("[[:blank:]]", "\n", false, false);
  324. }
  325. @Test
  326. public void testBlankGroupCase4() throws Exception {
  327. assertMatch("[[:blank:]]", "a", false, false);
  328. }
  329. @Test
  330. public void testCntrlGroupCase0() throws Exception {
  331. assertMatch("[[:cntrl:]]", "a", false, false);
  332. }
  333. @Test
  334. public void testCntrlGroupCase1() throws Exception {
  335. assertMatch("[[:cntrl:]]", String.valueOf((char) 7), true, false);
  336. }
  337. @Test
  338. public void testDigitGroupCase0() throws Exception {
  339. assertMatch("[[:digit:]]", "0", true, false);
  340. }
  341. @Test
  342. public void testDigitGroupCase1() throws Exception {
  343. assertMatch("[[:digit:]]", "5", true, false);
  344. }
  345. @Test
  346. public void testDigitGroupCase2() throws Exception {
  347. assertMatch("[[:digit:]]", "9", true, false);
  348. }
  349. @Test
  350. public void testDigitGroupCase3() throws Exception {
  351. // \u06f9 = EXTENDED ARABIC-INDIC DIGIT NINE
  352. assertMatch("[[:digit:]]", "\u06f9", true, false);
  353. }
  354. @Test
  355. public void testDigitGroupCase4() throws Exception {
  356. assertMatch("[[:digit:]]", "a", false, false);
  357. }
  358. @Test
  359. public void testDigitGroupCase5() throws Exception {
  360. assertMatch("[[:digit:]]", "]", false, false);
  361. }
  362. @Test
  363. public void testGraphGroupCase0() throws Exception {
  364. assertMatch("[[:graph:]]", "]", true, false);
  365. }
  366. @Test
  367. public void testGraphGroupCase1() throws Exception {
  368. assertMatch("[[:graph:]]", "a", true, false);
  369. }
  370. @Test
  371. public void testGraphGroupCase2() throws Exception {
  372. assertMatch("[[:graph:]]", ".", true, false);
  373. }
  374. @Test
  375. public void testGraphGroupCase3() throws Exception {
  376. assertMatch("[[:graph:]]", "0", true, false);
  377. }
  378. @Test
  379. public void testGraphGroupCase4() throws Exception {
  380. assertMatch("[[:graph:]]", " ", false, false);
  381. }
  382. @Test
  383. public void testGraphGroupCase5() throws Exception {
  384. // \u00f6 = 'o' with dots on it
  385. assertMatch("[[:graph:]]", "\u00f6", true, false);
  386. }
  387. @Test
  388. public void testLowerGroupCase0() throws Exception {
  389. assertMatch("[[:lower:]]", "a", true, false);
  390. }
  391. @Test
  392. public void testLowerGroupCase1() throws Exception {
  393. assertMatch("[[:lower:]]", "h", true, false);
  394. }
  395. @Test
  396. public void testLowerGroupCase2() throws Exception {
  397. assertMatch("[[:lower:]]", "A", false, false);
  398. }
  399. @Test
  400. public void testLowerGroupCase3() throws Exception {
  401. assertMatch("[[:lower:]]", "H", false, false);
  402. }
  403. @Test
  404. public void testLowerGroupCase4() throws Exception {
  405. // \u00e4 = small 'a' with dots on it
  406. assertMatch("[[:lower:]]", "\u00e4", true, false);
  407. }
  408. @Test
  409. public void testLowerGroupCase5() throws Exception {
  410. assertMatch("[[:lower:]]", ".", false, false);
  411. }
  412. @Test
  413. public void testPrintGroupCase0() throws Exception {
  414. assertMatch("[[:print:]]", "]", true, false);
  415. }
  416. @Test
  417. public void testPrintGroupCase1() throws Exception {
  418. assertMatch("[[:print:]]", "a", true, false);
  419. }
  420. @Test
  421. public void testPrintGroupCase2() throws Exception {
  422. assertMatch("[[:print:]]", ".", true, false);
  423. }
  424. @Test
  425. public void testPrintGroupCase3() throws Exception {
  426. assertMatch("[[:print:]]", "0", true, false);
  427. }
  428. @Test
  429. public void testPrintGroupCase4() throws Exception {
  430. assertMatch("[[:print:]]", " ", true, false);
  431. }
  432. @Test
  433. public void testPrintGroupCase5() throws Exception {
  434. // \u00f6 = 'o' with dots on it
  435. assertMatch("[[:print:]]", "\u00f6", true, false);
  436. }
  437. @Test
  438. public void testPunctGroupCase0() throws Exception {
  439. assertMatch("[[:punct:]]", ".", true, false);
  440. }
  441. @Test
  442. public void testPunctGroupCase1() throws Exception {
  443. assertMatch("[[:punct:]]", "@", true, false);
  444. }
  445. @Test
  446. public void testPunctGroupCase2() throws Exception {
  447. assertMatch("[[:punct:]]", " ", false, false);
  448. }
  449. @Test
  450. public void testPunctGroupCase3() throws Exception {
  451. assertMatch("[[:punct:]]", "a", false, false);
  452. }
  453. @Test
  454. public void testSpaceGroupCase0() throws Exception {
  455. assertMatch("[[:space:]]", " ", true, false);
  456. }
  457. @Test
  458. public void testSpaceGroupCase1() throws Exception {
  459. assertMatch("[[:space:]]", "\t", true, false);
  460. }
  461. @Test
  462. public void testSpaceGroupCase2() throws Exception {
  463. assertMatch("[[:space:]]", "\r", true, false);
  464. }
  465. @Test
  466. public void testSpaceGroupCase3() throws Exception {
  467. assertMatch("[[:space:]]", "\n", true, false);
  468. }
  469. @Test
  470. public void testSpaceGroupCase4() throws Exception {
  471. assertMatch("[[:space:]]", "a", false, false);
  472. }
  473. @Test
  474. public void testUpperGroupCase0() throws Exception {
  475. assertMatch("[[:upper:]]", "a", false, false);
  476. }
  477. @Test
  478. public void testUpperGroupCase1() throws Exception {
  479. assertMatch("[[:upper:]]", "h", false, false);
  480. }
  481. @Test
  482. public void testUpperGroupCase2() throws Exception {
  483. assertMatch("[[:upper:]]", "A", true, false);
  484. }
  485. @Test
  486. public void testUpperGroupCase3() throws Exception {
  487. assertMatch("[[:upper:]]", "H", true, false);
  488. }
  489. @Test
  490. public void testUpperGroupCase4() throws Exception {
  491. // \u00c4 = 'A' with dots on it
  492. assertMatch("[[:upper:]]", "\u00c4", true, false);
  493. }
  494. @Test
  495. public void testUpperGroupCase5() throws Exception {
  496. assertMatch("[[:upper:]]", ".", false, false);
  497. }
  498. @Test
  499. public void testXDigitGroupCase0() throws Exception {
  500. assertMatch("[[:xdigit:]]", "a", true, false);
  501. }
  502. @Test
  503. public void testXDigitGroupCase1() throws Exception {
  504. assertMatch("[[:xdigit:]]", "d", true, false);
  505. }
  506. @Test
  507. public void testXDigitGroupCase2() throws Exception {
  508. assertMatch("[[:xdigit:]]", "f", true, false);
  509. }
  510. @Test
  511. public void testXDigitGroupCase3() throws Exception {
  512. assertMatch("[[:xdigit:]]", "0", true, false);
  513. }
  514. @Test
  515. public void testXDigitGroupCase4() throws Exception {
  516. assertMatch("[[:xdigit:]]", "5", true, false);
  517. }
  518. @Test
  519. public void testXDigitGroupCase5() throws Exception {
  520. assertMatch("[[:xdigit:]]", "9", true, false);
  521. }
  522. @Test
  523. public void testXDigitGroupCase6() throws Exception {
  524. assertMatch("[[:xdigit:]]", "۹", false, false);
  525. }
  526. @Test
  527. public void testXDigitGroupCase7() throws Exception {
  528. assertMatch("[[:xdigit:]]", ".", false, false);
  529. }
  530. @Test
  531. public void testWordroupCase0() throws Exception {
  532. assertMatch("[[:word:]]", "g", true, false);
  533. }
  534. @Test
  535. public void testWordroupCase1() throws Exception {
  536. // \u00f6 = 'o' with dots on it
  537. assertMatch("[[:word:]]", "\u00f6", true, false);
  538. }
  539. @Test
  540. public void testWordroupCase2() throws Exception {
  541. assertMatch("[[:word:]]", "5", true, false);
  542. }
  543. @Test
  544. public void testWordroupCase3() throws Exception {
  545. assertMatch("[[:word:]]", "_", true, false);
  546. }
  547. @Test
  548. public void testWordroupCase4() throws Exception {
  549. assertMatch("[[:word:]]", " ", false, false);
  550. }
  551. @Test
  552. public void testWordroupCase5() throws Exception {
  553. assertMatch("[[:word:]]", ".", false, false);
  554. }
  555. @Test
  556. public void testMixedGroupCase0() throws Exception {
  557. assertMatch("[A[:lower:]C3-5]", "A", true, false);
  558. }
  559. @Test
  560. public void testMixedGroupCase1() throws Exception {
  561. assertMatch("[A[:lower:]C3-5]", "C", true, false);
  562. }
  563. @Test
  564. public void testMixedGroupCase2() throws Exception {
  565. assertMatch("[A[:lower:]C3-5]", "e", true, false);
  566. }
  567. @Test
  568. public void testMixedGroupCase3() throws Exception {
  569. assertMatch("[A[:lower:]C3-5]", "3", true, false);
  570. }
  571. @Test
  572. public void testMixedGroupCase4() throws Exception {
  573. assertMatch("[A[:lower:]C3-5]", "4", true, false);
  574. }
  575. @Test
  576. public void testMixedGroupCase5() throws Exception {
  577. assertMatch("[A[:lower:]C3-5]", "5", true, false);
  578. }
  579. @Test
  580. public void testMixedGroupCase6() throws Exception {
  581. assertMatch("[A[:lower:]C3-5]", "B", false, false);
  582. }
  583. @Test
  584. public void testMixedGroupCase7() throws Exception {
  585. assertMatch("[A[:lower:]C3-5]", "2", false, false);
  586. }
  587. @Test
  588. public void testMixedGroupCase8() throws Exception {
  589. assertMatch("[A[:lower:]C3-5]", "6", false, false);
  590. }
  591. @Test
  592. public void testMixedGroupCase9() throws Exception {
  593. assertMatch("[A[:lower:]C3-5]", ".", false, false);
  594. }
  595. @Test
  596. public void testSpecialGroupCase0() throws Exception {
  597. assertMatch("[[]", "[", true, false);
  598. }
  599. @Test
  600. public void testSpecialGroupCase1() throws Exception {
  601. assertMatch("[]]", "]", true, false);
  602. }
  603. @Test
  604. public void testSpecialGroupCase2() throws Exception {
  605. assertMatch("[]a]", "]", true, false);
  606. }
  607. @Test
  608. public void testSpecialGroupCase3() throws Exception {
  609. assertMatch("[a[]", "[", true, false);
  610. }
  611. @Test
  612. public void testSpecialGroupCase4() throws Exception {
  613. assertMatch("[a[]", "a", true, false);
  614. }
  615. @Test
  616. public void testSpecialGroupCase5() throws Exception {
  617. assertMatch("[!]]", "]", false, false);
  618. }
  619. @Test
  620. public void testSpecialGroupCase6() throws Exception {
  621. assertMatch("[!]]", "x", true, false);
  622. }
  623. @Test
  624. public void testSpecialGroupCase7() throws Exception {
  625. assertMatch("[:]]", ":]", true, false);
  626. }
  627. @Test
  628. public void testSpecialGroupCase8() throws Exception {
  629. assertMatch("[:]]", ":", false, true);
  630. }
  631. @Test
  632. public void testSpecialGroupCase9() throws Exception {
  633. try {
  634. assertMatch("[[:]", ":", true, true);
  635. fail("InvalidPatternException expected");
  636. } catch (InvalidPatternException e) {
  637. // expected
  638. }
  639. }
  640. @Test
  641. public void testUnsupportedGroupCase0() throws Exception {
  642. try {
  643. assertMatch("[[=a=]]", "b", false, false);
  644. fail("InvalidPatternException expected");
  645. } catch (InvalidPatternException e) {
  646. assertTrue(e.getMessage().contains("[=a=]"));
  647. }
  648. }
  649. @Test
  650. public void testUnsupportedGroupCase1() throws Exception {
  651. try {
  652. assertMatch("[[.a.]]", "b", false, false);
  653. fail("InvalidPatternException expected");
  654. } catch (InvalidPatternException e) {
  655. assertTrue(e.getMessage().contains("[.a.]"));
  656. }
  657. }
  658. @Test
  659. public void testEscapedBracket1() throws Exception {
  660. assertMatch("\\[", "[", true, false);
  661. }
  662. @Test
  663. public void testEscapedBracket2() throws Exception {
  664. assertMatch("\\[[a]", "[", false, true);
  665. }
  666. @Test
  667. public void testEscapedBracket3() throws Exception {
  668. assertMatch("\\[[a]", "a", false, false);
  669. }
  670. @Test
  671. public void testEscapedBracket4() throws Exception {
  672. assertMatch("\\[[a]", "[a", true, false);
  673. }
  674. @Test
  675. public void testEscapedBracket5() throws Exception {
  676. assertMatch("[a\\]]", "]", true, false);
  677. }
  678. @Test
  679. public void testEscapedBracket6() throws Exception {
  680. assertMatch("[a\\]]", "a", true, false);
  681. }
  682. @Test
  683. public void testEscapedBackslash() throws Exception {
  684. assertMatch("a\\\\b", "a\\b", true, false);
  685. }
  686. @Test
  687. public void testMultipleEscapedCharacters1() throws Exception {
  688. assertMatch("\\]a?c\\*\\[d\\?\\]", "]abc*[d?]", true, false);
  689. }
  690. @Test
  691. public void testFilePathSimpleCase() throws Exception {
  692. assertFileNameMatch("a/b", "a/b", '/', true, false);
  693. }
  694. @Test
  695. public void testFilePathCase0() throws Exception {
  696. assertFileNameMatch("a*b", "a/b", '/', false, false);
  697. }
  698. @Test
  699. public void testFilePathCase1() throws Exception {
  700. assertFileNameMatch("a?b", "a/b", '/', false, false);
  701. }
  702. @Test
  703. public void testFilePathCase2() throws Exception {
  704. assertFileNameMatch("a*b", "a\\b", '\\', false, false);
  705. }
  706. @Test
  707. public void testFilePathCase3() throws Exception {
  708. assertFileNameMatch("a?b", "a\\b", '\\', false, false);
  709. }
  710. @Test
  711. public void testReset() throws Exception {
  712. final String pattern = "helloworld";
  713. final FileNameMatcher matcher = new FileNameMatcher(pattern, null);
  714. matcher.append("helloworld");
  715. assertTrue(matcher.isMatch());
  716. assertFalse(matcher.canAppendMatch());
  717. matcher.reset();
  718. matcher.append("hello");
  719. assertFalse(matcher.isMatch());
  720. assertTrue(matcher.canAppendMatch());
  721. matcher.append("world");
  722. assertTrue(matcher.isMatch());
  723. assertFalse(matcher.canAppendMatch());
  724. matcher.append("to much");
  725. assertFalse(matcher.isMatch());
  726. assertFalse(matcher.canAppendMatch());
  727. matcher.reset();
  728. matcher.append("helloworld");
  729. assertTrue(matcher.isMatch());
  730. assertFalse(matcher.canAppendMatch());
  731. }
  732. @Test
  733. public void testCreateMatcherForSuffix() throws Exception {
  734. final String pattern = "helloworld";
  735. final FileNameMatcher matcher = new FileNameMatcher(pattern, null);
  736. matcher.append("hello");
  737. final FileNameMatcher childMatcher = matcher.createMatcherForSuffix();
  738. assertFalse(matcher.isMatch());
  739. assertTrue(matcher.canAppendMatch());
  740. assertFalse(childMatcher.isMatch());
  741. assertTrue(childMatcher.canAppendMatch());
  742. matcher.append("world");
  743. assertTrue(matcher.isMatch());
  744. assertFalse(matcher.canAppendMatch());
  745. assertFalse(childMatcher.isMatch());
  746. assertTrue(childMatcher.canAppendMatch());
  747. childMatcher.append("world");
  748. assertTrue(matcher.isMatch());
  749. assertFalse(matcher.canAppendMatch());
  750. assertTrue(childMatcher.isMatch());
  751. assertFalse(childMatcher.canAppendMatch());
  752. childMatcher.reset();
  753. assertTrue(matcher.isMatch());
  754. assertFalse(matcher.canAppendMatch());
  755. assertFalse(childMatcher.isMatch());
  756. assertTrue(childMatcher.canAppendMatch());
  757. childMatcher.append("world");
  758. assertTrue(matcher.isMatch());
  759. assertFalse(matcher.canAppendMatch());
  760. assertTrue(childMatcher.isMatch());
  761. assertFalse(childMatcher.canAppendMatch());
  762. }
  763. @Test
  764. public void testCopyConstructor() throws Exception {
  765. final String pattern = "helloworld";
  766. final FileNameMatcher matcher = new FileNameMatcher(pattern, null);
  767. matcher.append("hello");
  768. final FileNameMatcher copy = new FileNameMatcher(matcher);
  769. assertFalse(matcher.isMatch());
  770. assertTrue(matcher.canAppendMatch());
  771. assertFalse(copy.isMatch());
  772. assertTrue(copy.canAppendMatch());
  773. matcher.append("world");
  774. assertTrue(matcher.isMatch());
  775. assertFalse(matcher.canAppendMatch());
  776. assertFalse(copy.isMatch());
  777. assertTrue(copy.canAppendMatch());
  778. copy.append("world");
  779. assertTrue(matcher.isMatch());
  780. assertFalse(matcher.canAppendMatch());
  781. assertTrue(copy.isMatch());
  782. assertFalse(copy.canAppendMatch());
  783. copy.reset();
  784. assertTrue(matcher.isMatch());
  785. assertFalse(matcher.canAppendMatch());
  786. assertFalse(copy.isMatch());
  787. assertTrue(copy.canAppendMatch());
  788. copy.append("helloworld");
  789. assertTrue(matcher.isMatch());
  790. assertFalse(matcher.canAppendMatch());
  791. assertTrue(copy.isMatch());
  792. assertFalse(copy.canAppendMatch());
  793. }
  794. }