Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add junit test for text fields check #12057

Merged
merged 13 commits into from
Oct 27, 2024

Conversation

ShunL12324
Copy link
Contributor

Closes #11939

This is an approach try to test if FieldEditor has the appropriate usage of TextArea or TextField by using Java Parser to analyze FieldEditors.java. However, there are currently many limitations.

This test does the following:

  • Step one: Use Java Parser to analyze all if statements in FieldEditors.getForField and create a map.
    • The key: The .java file path of the FieldEditor class (for example: src/.../UrlEditor.java)
    • The value: The possible FieldProperties by analyzing the if statement conditions (for example: [FieldProperty.EXTERNAL])
  • Step two: Analyze each entry. If the properties include FieldProperty.MULTILINE_TEXT, then perform the following tests:
    • Since the class implements FieldEditorFX and calls the establishBinding method, get the name of the first parameter.
    • Check the corresponding field declaration to see if the field is an instance of TextInputControl.
    • If yes, check if there is an EditorTextArea creation in this class.

Limitations:

  • We cannot test if the class uses an EditorTextField field correctly because we cannot determine if the FieldEditor class has multiple lines from FieldEditors.java. For example, the PersonsEditor should be able to use TextArea, but from FieldEditors.java we can't determine if the PersonEditor will have the MULTILINE_TEXT property. In fact, none of these FieldEditor classes can be determined to have the MULTILINE_TEXT property if we only analyze FieldEditors.java.
  • This test seems to be unreliable and not robust, but I have no other way to do it.

Suggestions:

  • I think we should change the design first. We should not use the parameter final boolean isMultiLine but instead check if the field parameter holds the MULTILINE_TEXT property.
  • Maybe use a field to mark if this editor can be multi-line, then check if it holds the correct GUI component (TextArea or TextField).
  • At this moment, maybe just add some comments or documentation?

Mandatory checks

  • I own the copyright of the code submitted and I licence it under the MIT license
  • Change in CHANGELOG.md described in a way that is understandable for the average user (if applicable)
  • Tests created for changes (if applicable)
  • Manually tested changed features in running JabRef (always required)
  • Screenshots added in PR description (for UI changes)
  • Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • Checked documentation: Is the information available and up to date? If not, I created an issue at https://github.com/JabRef/user-documentation/issues or, even better, I submitted a pull request to the documentation repository.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code currently does not meet JabRef's code guidelines.
We use Checkstyle to identify issues.
The tool reviewdog already placed comments on GitHub to indicate the places. See the tab "Files" in you PR.
Please carefully follow the setup guide for the codestyle.
Afterwards, please run checkstyle locally and fix the issues.

You can check review dog's comments at the tab "Files changed" of your pull request.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code currently does not meet JabRef's code guidelines.
We use Checkstyle to identify issues.
The tool reviewdog already placed comments on GitHub to indicate the places. See the tab "Files" in you PR.
Please carefully follow the setup guide for the codestyle.
Afterwards, please run checkstyle locally and fix the issues.

You can check review dog's comments at the tab "Files changed" of your pull request.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code currently does not meet JabRef's code guidelines.
We use OpenRewrite to ensure "modern" Java coding practices.
The issues found can be automatically fixed.
Please execute the gradle task rewriteRun, check the results, commit, and push.

You can check the detailed error output by navigating to your pull request, selecting the tab "Checks", section "Tests" (on the left), subsection "OpenRewrite".

Copy link
Member

@koppor koppor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small comments, otherwise, looks OK.

* b) Has an EditorTextArea object creation
*/
@Test
public void fieldEditorTextAreaTest() throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More self-descriptive test name please. I think fieldEditorsMatchMultilineProperty could be a good name.

*/
@Test
public void fieldEditorTextAreaTest() throws IOException {
// get all field editors and their properties in FieldEditors.java
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this comment - it is exactly the method name (which is good)


CompilationUnit cu = PARSER.parse(filePath).getResult().orElse(null);
if (cu == null) {
throw new RuntimeException("Failed to analyze " + filePath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No RuntimeExeptions in tests. Use normal Exceptions. And use throws clause.

NullPointerExceptions are also OK.

The text Failed to analyze is too generic. Is it because of a logic issue? The NullPointerException is more specific than a general failure


try {
CompilationUnit cu = PARSER.parse(Paths.get(filePath)).getResult().orElse(null);
if (cu == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

Comment on lines 177 to 179
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Error parsing file: " + filePath, e);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are crafting a JUnit test - just let exceptions flow - no need to handle or log them.

@koppor
Copy link
Member

koppor commented Oct 23, 2024

  • This test seems to be unreliable and not robust, but I have no other way to do it.

However, it is green now. What do you mean by "unreliable".

* I think we should change the design first. We should not use the parameter `final boolean isMultiLine` but instead check if the `field` parameter holds the `MULTILINE_TEXT` property.

No. Please keep as is. The statement isMultiline allows for configuring multiline fields by the user. See for #11897 configuration.

boolean isMultiLine = FieldFactory.isMultiLineField(field, preferences.getFieldPreferences().getNonWrappableFields());

* At this moment, maybe just add some comments or documentation?

We can do as follow up.

@koppor koppor added the status: changes required Pull requests that are not yet complete label Oct 23, 2024
2. Rename the test method and file.
3. Use normal exceptions
4. Remove logger
@FeiLi-lab
Copy link

I noticed that the holdEditorTextField method appears to be unused within the codebase. Should we consider removing it to simplify the code and improve maintainability? Unless there’s a planned usage or if it’s reserved for future features, removing unused methods could help keep the code clean and focused.

@ShunL12324
Copy link
Contributor Author

  • This test seems to be unreliable and not robust, but I have no other way to do it.

However, it is green now. What do you mean by "unreliable".

* I think we should change the design first. We should not use the parameter `final boolean isMultiLine` but instead check if the `field` parameter holds the `MULTILINE_TEXT` property.

No. Please keep as is. The statement isMultiline allows for configuring multiline fields by the user. See for #11897 configuration.

boolean isMultiLine = FieldFactory.isMultiLineField(field, preferences.getFieldPreferences().getNonWrappableFields());

* At this moment, maybe just add some comments or documentation?

We can do as follow up.

It feels somewhat fragile since the check relies heavily on the current structure of FieldEditors.java. If the method name changes or if statements are replaced, the test could break or become ineffective.

@ShunL12324
Copy link
Contributor Author

ShunL12324 commented Oct 26, 2024

I noticed that the holdEditorTextField method appears to be unused within the codebase. Should we consider removing it to simplify the code and improve maintainability? Unless there’s a planned usage or if it’s reserved for future features, removing unused methods could help keep the code clean and focused.

I think we could keep it for now, as it might be useful once we can specifically check whether the FieldEditor should use a TextField.

@koppor
Copy link
Member

koppor commented Oct 26, 2024

holdEditorTextField

Where is this variable? IntelliJ doesn't find it.

image

@koppor
Copy link
Member

koppor commented Oct 26, 2024

It feels somewhat fragile since the check relies heavily on the current structure of FieldEditors.java. If the method name changes or if statements are replaced, the test could break or become ineffective.

Then, we can delete it.

You can add a JavaDoc to the text just stating your text. You can be stronger: "This test is somewhat fragile ..."

Copy link
Member

@koppor koppor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add JavaDoc (see above) and small indent fix.

@koppor koppor added this pull request to the merge queue Oct 27, 2024
Merged via the queue into JabRef:main with commit 28fb91e Oct 27, 2024
27 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: changes required Pull requests that are not yet complete
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consistency check for text fields
4 participants