matcher

When to use ** (double star) in glob syntax within JAVA

放肆的年华 提交于 2019-11-28 16:11:25
Directly from this Java Oracle tutorial: Two asterisks, **, works like * but crosses directory boundaries. This syntax is generally used for matching complete paths. Could anybody do a real example out of it? What do they mean with "crosses directory boundary"? Crossing the directory boundary, I imagine something like checking the file from root to getNameCount()-1 . Again a real example explaining the difference between * and ** in practice would be great. The javadoc for FileSystem#getPathMatcher() has some pretty good examples and explanations *.java Matches a path that represents a file

Mockito match any class argument

不想你离开。 提交于 2019-11-28 15:28:55
问题 Is there a way to match any class argument of the below sample routine? class A { public B method(Class<? extends A> a) {} } How can I always return a new B() regardless of which class is passed into method ? The following attempt only works for the specific case where A is matched. A a = new A(); B b = new B(); when(a.method(eq(A.class))).thenReturn(b); EDIT : One solution is (Class<?>) any(Class.class) 回答1: Two more ways to do it (see my comment on the previous answer by @Tomasz Nurkiewicz)

Mockito: List Matchers with generics

狂风中的少年 提交于 2019-11-28 15:26:30
问题 Mockito offers: when(mock.process(Matchers.any(List.class))); How to avoid warning if process takes a List<Bar> instead? 回答1: For Java 8 and above, it's easy: when(mock.process(Matchers.anyList())); For Java 7 and below, the compiler needs a bit of help. Use anyListOf(Class<T> clazz) : when(mock.process(Matchers.anyListOf(Bar.class))); 回答2: In addition to anyListOf above, you can always specify generics explicitly using this syntax: when(mock.process(Matchers.<List<Bar>>any(List.class)));

How Matcher.find() works [duplicate]

不羁的心 提交于 2019-11-28 10:55:18
This question already has an answer here: SCJP6 regex issue 1 answer I am testing a small stub of Matcher and Pattern class...see the following small stub.. package scjp2.escape.sequence.examples; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Sample_19 { public static void main(String a[]){ String stream = "ab34ef"; Pattern pattern = Pattern.compile("\\d*"); //HERE * IS GREEDY QUANTIFIER THAT LOOKS FOR ZERO TO MANY COMBINATION THAT //START WITH NUMBER Matcher matcher = pattern.matcher(stream); while(matcher.find()){ System.out.print(matcher.start()+matcher.group(

What's the difference between Mockito Matchers isA, any, eq, and same?

倖福魔咒の 提交于 2019-11-28 05:46:11
I am confused on what's the difference between them, and which one to choose in which case. Some difference might be obvious, like any and eq , but I'm including them all just to be sure. I wonder about their differences because I came across this problem: I have this POST method in a Controller class public Response doSomething(@ResponseBody Request request) { return someService.doSomething(request); } And would like to perform a unit test on that controller. I have two versions. The first one is the simple one, like this @Test public void testDoSomething() { //initialize ObjectMapper mapper

All overlapping substrings matching a java regex

僤鯓⒐⒋嵵緔 提交于 2019-11-28 04:35:04
问题 Is there an API method that returns all (possibly overlapping) substrings that match a regular expression? For example, I have a text string: String t = 04/31 412-555-1235; , and I have a pattern: Pattern p = new Pattern("\\d\\d+"); that matches strings of two or more characters. The matches I get are: 04, 31, 412, 555, 1235. How do I get overlapping matches? I want the code to return: 04, 31, 41, 412, 12, 55, 555, 55, 12, 123, 1235, 23, 235, 35. Theoretically it should be possible -- there

Regular expression to extract SQL query

蓝咒 提交于 2019-11-28 01:48:12
问题 Is there a regex which extracts SQL queries from a string? I'm NOT interested to validate any SQL syntax, rather and only extracting a selection of SQL commands. This to parse a given SQL file/string in a flexible manner. Given is the following SQL file/string example: SELECT * FROM test_table WHERE test_row = 'Testing ; semicolon'; SELECT * FROM another_test_table; INSERT INTO table_name VALUES (value1,'value which contains semicolon ;;;;',value3,...); Some pseudocode example would be: ^

Java RegEx Matcher.groupCount returns 0

ε祈祈猫儿з 提交于 2019-11-27 23:51:07
I know this has been asked but I am unable to fix it For a book object with body (spanish): "quiero mas dinero" (actually quite a bit longer) My Matcher keeps returning 0 for: String s="mas"; // this is for testing, comes from a List<String> int hit=0; Pattern p=Pattern.compile(s,Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(mybooks.get(i).getBody()); m.find(); System.out.println(s+" "+m.groupCount()+" " +mybooks.get(i).getBody()); hit+=m.groupCount(); I keep getting "mas 0 quiero mas dinero" on console. Why oh why? From the javadoc of Matcher.groupCount() : Returns the number of capturing

How to write a matcher that is not equal to something

試著忘記壹切 提交于 2019-11-27 23:24:52
I am trying to create a mock for a call. Say I have this method I am trying to stub out: class ClassA { public String getString(String a) { return a + "hey"; } } What I am trying to mock out is: 1st instance is when(classA.getString(eq("a")).thenReturn(...);` in the same test case when(classA.getString([anything that is not a])).thenReturn(somethingelse); The 2nd case is my question: How do I match anyString() other than "a"? With Mockito framework, you can use AdditionalMatchers ClassA classA = Mockito.mock(ClassA.class); Mockito.when(classA.getString(Matchers.eq("a"))).thenReturn("something"

get unique regex matcher results (without using maps or lists)

北战南征 提交于 2019-11-27 21:50:21
问题 Is there a way to get only the unique matches? without using a list or a map after the matching, I want the matcher output to be unique right away. Sample input/output: String input = "This is a question from [userName] about finding unique regex matches for [inputString] without using any lists or maps. -[userName]."; Pattern pattern = Pattern.compile("\\[[^\\[\\]]*\\]"); Matcher matcher = pattern.matcher(rawText); while (matcher.find()) { String tokenName = matcher.group(0); System.out