Difference between revisions of "Java Debugging Tips"
imported>Aeric |
imported>Aeric |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
===Debug Messages=== | ===Add Debug Messages=== | ||
To debug a Java [[class]]: | To debug a Java [[class]]: | ||
:* Add messages to the debug log | :* Add messages to the debug log to trace code execution: | ||
:: <tt>Logger.info("message text", "message type");</tt> | :: <tt>Logger.info("message text", "message type");</tt> | ||
:: (The message type can be used for searching, when needed.) | :: (The message type can be used for searching, when needed. Using the class name works pretty well.) | ||
:* Examine the contents of a hashmap or list: | |||
:: <tt>Parameters params;</tt> | |||
:: <tt> ...</tt> | |||
:: <tt>Logger.info("params: " + params, "message type");</tt> | |||
: or pretty=print the string by substituting linebreaks for the comma-separators: | |||
:: <tt>Logger.info("params: " + params.toString().replace(",","\n"), "message type");</tt> | |||
:* Open the debug log in a separate page: | :* Open the debug log in a separate page: | ||
Line 18: | Line 25: | ||
::* See the [[Class Template]] for code that uses many of those techniques. | ::* See the [[Class Template]] for code that uses many of those techniques. | ||
=== | ===Invoke a Method from a Macro or Rule=== | ||
The [[Unit Test Framework]] is great for automated testing of Java methods, but note that ''database changes don't persist''. That's a highly desirable feature in a test harness, but it means you can't run a test and then look for changes interactively, in the GUI. | The [[Unit Test Framework]] is great for automated testing of Java methods, but note that ''database changes don't persist''. That's a highly desirable feature in a test harness, but it means you can't run a test and then look for changes interactively, in the GUI. | ||
Line 25: | Line 32: | ||
:* Use an [[Event Rule]] that runs when a record is added. (As a precaution, you can set up a condition so that the rule is triggered only when some flag is set.) | :* Use an [[Event Rule]] that runs when a record is added. (As a precaution, you can set up a condition so that the rule is triggered only when some flag is set.) | ||
=== | {{Note|There are subtle differences in the parameters available in the different contexts.<br/>Learn more: [[Incoming Method Parameters]] }} | ||
=== Set up a "Debug Mode" === | |||
To ensure that your rule fires only when you are actively debugging, you can add a "Debug" flag to the User object, set the default to false, and then set it to true for yourself. The condition add-record Rule condition can then test for <tt>Owner.Debug equals true</tt>. | To ensure that your rule fires only when you are actively debugging, you can add a "Debug" flag to the User object, set the default to false, and then set it to true for yourself. The condition add-record Rule condition can then test for <tt>Owner.Debug equals true</tt>. | ||
Or, more simply, change the value of your title field to "Developer", and then set the Rule condition to <tt>Owner.Title equals Developer</tt>. Anyone who creates a record is automatically its owner, so anyone who has the title "Developer" will execute the method when they add a record. | Or, more simply, change the value of your title field to "Developer", and then set the Rule condition to <tt>Owner.Title equals Developer</tt>. Anyone who creates a record is automatically its owner, so anyone who has the title "Developer" will execute the method when they add a record. | ||
===Test a Code Fragment=== | ===Use a JSP Page to Test a Code Fragment=== | ||
To test a code fragment, you can create a JSP [[Page]]. | To test a code fragment, you can create a JSP [[Page]]. | ||
Line 41: | Line 50: | ||
You can create <tt>Test.jsp</tt> for example, and visit <tt>{{platformURL}}/pages/Test.jsp</tt> to execute the code. | You can create <tt>Test.jsp</tt> for example, and visit <tt>{{platformURL}}/pages/Test.jsp</tt> to execute the code. | ||
===Use the Unit Test Framework=== | |||
Use the [[Unit Test Framework]] to add unit tests to your Java classes. | |||
<noinclude> | |||
[[Category:Development]] | |||
</noinclude> |
Latest revision as of 23:47, 21 May 2015
Add Debug Messages
To debug a Java class:
- Add messages to the debug log to trace code execution:
- Logger.info("message text", "message type");
- (The message type can be used for searching, when needed. Using the class name works pretty well.)
- Examine the contents of a hashmap or list:
- Parameters params;
- ...
- Logger.info("params: " + params, "message type");
- or pretty=print the string by substituting linebreaks for the comma-separators:
- Logger.info("params: " + params.toString().replace(",","\n"), "message type");
- Open the debug log in a separate page:
- Display a note at the top of the user's page:
- Functions.showMessage("message text");
- Abort processing and display an error message:
- Functions.throwException("message text");
- Learn more:
- See the Class Template for code that uses many of those techniques.
Invoke a Method from a Macro or Rule
The Unit Test Framework is great for automated testing of Java methods, but note that database changes don't persist. That's a highly desirable feature in a test harness, but it means you can't run a test and then look for changes interactively, in the GUI.
To view changes interactively, the code can be launched using a Macro or a Rule. For example:
- Open the object and create a macro that invokes a method. Then, when viewing a record, select the macro from the list of Actions.
- Use an Event Rule that runs when a record is added. (As a precaution, you can set up a condition so that the rule is triggered only when some flag is set.)
Note: There are subtle differences in the parameters available in the different contexts.
Learn more: Incoming Method Parameters
Set up a "Debug Mode"
To ensure that your rule fires only when you are actively debugging, you can add a "Debug" flag to the User object, set the default to false, and then set it to true for yourself. The condition add-record Rule condition can then test for Owner.Debug equals true.
Or, more simply, change the value of your title field to "Developer", and then set the Rule condition to Owner.Title equals Developer. Anyone who creates a record is automatically its owner, so anyone who has the title "Developer" will execute the method when they add a record.
Use a JSP Page to Test a Code Fragment
To test a code fragment, you can create a JSP Page.
The page need not have anything on it but the tags needed to embed the code:
- <syntaxhighlight lang="java" enclose="div">
<%
...your code goes here...
%> </syntaxhighlight>
You can create Test.jsp for example, and visit https://{yourDomain}/networking/pages/Test.jsp to execute the code.
Use the Unit Test Framework
Use the Unit Test Framework to add unit tests to your Java classes.