Eclipse: Flip arguments using regular expressions

Symbolic picture of a flip-action A,T tp T,A
Symbolic picture of a flip-action | © Wikimedia Commons | CC0 1.0 Deed

Lately I have migrated a project from JUnit to TestNG. With the help of an Ecplipse plugin this is a one-click task. Every assert-method is replaced by its counterpart in class org.testng.AssertJUnit. This is only an intermediate solution. The final goal is to use org.testng.Assert. This will present a small challenge. JUnit’s two arg assert-methods, e.g. assertEquals or assertSame define order expected, actual. TestNG defines the opposite. Therefore argument order needs to be flipped.

Flipping argument order is achived with the help of Find/Replace and regular expressions. To find all assertEquals-methods and flip their arguments, follow 5 steps:

  1. Within the respective test class call the Find/Replace-dialog, either via menu entry Edit or via [Strg]-F.
  2. Check Regular expressions.
  3. Enter assertEquals\(([^,]+),\s*([^)]+)\s*\) into text field Find.
  4. Enter assertEquals(\2, \1) into text field Replace.
  5. Click button Replace All.
Screenshot of Find/Replace-dialog in Eclipse, filled with necessary values
Screenshot of Find/Replace-dialog in Eclipse, filled with necessary values

The regular expression in text field Find makes use of two capturing groups. Capturing groups are created with ([^,]+) and ([^)]+). They are referenced in text field Replace in reverse order with \2 and \1.