Skip to content

Commit f437974

Browse files
committed
Improving unit test coverage
1 parent 1b2f145 commit f437974

33 files changed

Lines changed: 2570 additions & 148 deletions

.github/workflows/gradle.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,13 @@ jobs:
3737
# Note: Assumes we're running on Ubuntu
3838
# https://github.com/actions/runner-images/blob/main/images/ubuntu/Ubuntu2404-Readme.md
3939
- name: Build with Gradle
40-
run: xvfb-run ./gradlew build -xsign -xpublish --warning-mode all
40+
run: xvfb-run ./gradlew build jacocoTestReport -xsign -xpublish --warning-mode all
41+
42+
- name: Submit coverage data to codecov
43+
uses: codecov/codecov-action@v5
44+
if: matrix.java == '17'
45+
with:
46+
token: ${{ secrets.CODECOV_TOKEN }}
47+
disable_search: true
48+
files: ./RSTAUI/build/reports/jacoco/test/jacocoTestReport.xml
49+
name: codecov

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# RSTAUI
22
![Java Build](https://github.com/bobbylight/RSTAUI/actions/workflows/gradle.yml/badge.svg)
33
![Java Build](https://github.com/bobbylight/RSTAUI/actions/workflows/codeql-analysis.yml/badge.svg)
4+
![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.fifesoft/rstaui/badge.svg)
5+
[![codecov](https://codecov.io/gh/bobbylight/RSTAUI/branch/master/graph/badge.svg?token=Hktq3vfINy)](https://codecov.io/gh/bobbylight/RSTAUI)
46

57
This is a library for adding the following dialogs to an application using `RSyntaxTextArea` as an
68
editor:

RSTAUI/build.gradle

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ plugins {
22
id 'java-library'
33
}
44

5-
['base', 'distribution', 'maven-publish', 'signing'].each { apply plugin: it }
5+
['base', 'jacoco', 'distribution', 'maven-publish', 'signing'].each { apply plugin: it }
66

77
base {
88
archivesName = 'rstaui'
@@ -14,10 +14,18 @@ dependencies {
1414
testImplementation platform('org.junit:junit-bom:5.12.1')
1515
testImplementation 'org.junit.jupiter:junit-jupiter'
1616
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
17+
testImplementation 'org.mockito:mockito-core:5.15.2'
1718
}
1819

1920
ext.isReleaseVersion = !project.version.endsWith('SNAPSHOT')
2021

22+
jacocoTestReport {
23+
reports {
24+
xml.required = true // codecov depends on xml format report
25+
html.required = true
26+
}
27+
}
28+
2129
java {
2230
withSourcesJar()
2331
withJavadocJar()

RSTAUI/src/main/java/org/fife/rsta/ui/CollapsibleSectionPanel.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ private void focusMainComponent() {
168168
if (center instanceof JScrollPane) {
169169
center = ((JScrollPane)center).getViewport().getView();
170170
}
171-
center.requestFocusInWindow();
171+
if (center != null) {
172+
center.requestFocusInWindow();
173+
}
172174
}
173175

174176

@@ -188,6 +190,16 @@ public JComponent getDisplayedBottomComponent() {
188190
}
189191

190192

193+
/**
194+
* Returns the number of ticks for the animation.
195+
*
196+
* @return The total number of ticks.
197+
*/
198+
protected int getTotalTicks() {
199+
return totalTicks;
200+
}
201+
202+
191203
/**
192204
* Hides the currently displayed "bottom" component with a slide-out
193205
* animation.
@@ -238,6 +250,16 @@ private void installKeystrokes() {
238250
}
239251

240252

253+
/**
254+
* Toggles whether this component animates when showing or hiding.
255+
*
256+
* @param animate Whether the display is animated.
257+
*/
258+
public void setAnimate(boolean animate) {
259+
this.animate = animate;
260+
}
261+
262+
241263
/**
242264
* Sets the amount of time, in milliseconds, it should take for a
243265
* "collapsible panel" to show or hide. The default is <code>120</code>.

RSTAUI/src/main/java/org/fife/rsta/ui/GoToDialog.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,22 @@ private boolean attemptToGetGoToLine() {
172172
}
173173

174174

175+
/**
176+
* Programmatically clicks the Cancel button. Used for testing purposes.
177+
*/
178+
void clickCancelButton() {
179+
cancelButton.doClick(0);
180+
}
181+
182+
183+
/**
184+
* Programmatically clicks the OK button. Used for testing purposes.
185+
*/
186+
protected void clickOkButton() {
187+
okButton.doClick(0);
188+
}
189+
190+
175191
/**
176192
* Returns a panel containing the OK and Cancel buttons. This panel is
177193
* added to the bottom of this dialog. Applications that don't like these
@@ -238,6 +254,7 @@ public String getErrorDialogTitle() {
238254
* @return The line number the user decided to go to, or <code>-1</code>
239255
* if the dialog was canceled. If valid, this will be 1-based,
240256
* not 0-based.
257+
* @see #setLineNumber(int)
241258
*/
242259
public int getLineNumber() {
243260
return lineNumber;
@@ -267,6 +284,18 @@ public void setErrorDialogTitle(String title) {
267284
}
268285

269286

287+
/**
288+
* Sets the line number displayed in this dialog.
289+
*
290+
* @param lineNumber The new line number.
291+
* @see #getLineNumber()
292+
*/
293+
protected void setLineNumber(int lineNumber) {
294+
this.lineNumber = lineNumber;
295+
lineNumberField.setText(String.valueOf(lineNumber));
296+
}
297+
298+
270299
/**
271300
* Sets the maximum line number for them to enter.
272301
*

RSTAUI/src/main/java/org/fife/rsta/ui/TextFilePropertiesDialog.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.awt.event.ActionListener;
1919
import java.io.File;
2020
import java.nio.charset.Charset;
21-
import java.nio.charset.StandardCharsets;
2221
import java.text.BreakIterator;
2322
import java.text.CharacterIterator;
2423
import java.text.MessageFormat;
@@ -75,7 +74,7 @@ public class TextFilePropertiesDialog extends EscapableDialog
7574
};
7675

7776
private static final String[] LINE_TERMINATORS = {
78-
System.getProperty("line.separator"), "\r", "\n", "\r\n"
77+
System.lineSeparator(), "\r", "\n", "\r\n"
7978
};
8079

8180
/**
@@ -258,7 +257,6 @@ private String getSelectedLineTerminator() {
258257
return LINE_TERMINATORS[terminatorCombo.getSelectedIndex()];
259258
}
260259

261-
262260
private void init(TextEditorPane textArea) {
263261

264262
this.textArea = textArea;
@@ -385,13 +383,22 @@ private void init(TextEditorPane textArea) {
385383
}
386384

387385

386+
/**
387+
* Returns whether this modal is dirty and will persist changes.
388+
*
389+
* @return Whether the modal is dirty.
390+
*/
391+
boolean isCloseable() {
392+
return okButton != null && okButton.isEnabled();
393+
}
394+
395+
388396
/**
389397
* Sets the encoding selected by this dialog.
390398
*
391-
* @param encoding The desired encoding. If this value is invalid or not
392-
* supported by this OS, <code>US-ASCII</code> is used.
399+
* @param encoding The desired encoding.
393400
*/
394-
private void setEncoding(String encoding) {
401+
protected void setEncoding(String encoding) {
395402

396403
Charset cs1 = Charset.forName(encoding);
397404

@@ -404,22 +411,10 @@ private void setEncoding(String encoding) {
404411
return;
405412
}
406413
}
407-
408-
// Encoding not found: select default.
409-
cs1 = StandardCharsets.US_ASCII;
410-
for (int i=0; i<count; i++) {
411-
String item = encodingCombo.getItemAt(i);
412-
Charset cs2 = Charset.forName(item);
413-
if (cs1.equals(cs2)) {
414-
encodingCombo.setSelectedIndex(i);
415-
return;
416-
}
417-
}
418-
419414
}
420415

421416

422-
private void setSelectedLineTerminator(String terminator) {
417+
protected void setSelectedLineTerminator(String terminator) {
423418
for (int i=0; i<LINE_TERMINATORS.length; i++) {
424419
if (LINE_TERMINATORS[i].equals(terminator)) {
425420
terminatorCombo.setSelectedIndex(i);

RSTAUI/src/main/java/org/fife/rsta/ui/UIUtil.java

Lines changed: 14 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
*/
88
package org.fife.rsta.ui;
99

10-
import java.awt.Color;
11-
import java.awt.Component;
12-
import java.awt.ComponentOrientation;
13-
import java.awt.Container;
14-
import java.lang.reflect.Method;
10+
import java.awt.*;
11+
import java.io.IOException;
1512
import java.net.URI;
1613
import java.net.URISyntaxException;
1714
import java.util.*;
15+
import java.util.List;
1816
import javax.swing.BorderFactory;
1917
import javax.swing.JButton;
2018
import javax.swing.JComboBox;
@@ -35,10 +33,6 @@
3533
*/
3634
public final class UIUtil {
3735

38-
private static boolean desktopCreationAttempted;
39-
private static Object desktop;
40-
private static final Object LOCK_DESKTOP_CREATION = new Object();
41-
4236
/**
4337
* A very common border that can be shared across many components.
4438
*/
@@ -59,7 +53,7 @@ private UIUtil() {
5953
* @param uri The URI to open. If this is <code>null</code>, nothing
6054
* happens and this method returns <code>false</code>.
6155
* @return Whether the operation was successful. This will be
62-
* <code>false</code> on JRE's older than 1.6.
56+
* <code>false</code> on systems without desktop support.
6357
* @see #browse(URI)
6458
*/
6559
public static boolean browse(String uri) {
@@ -80,24 +74,20 @@ public static boolean browse(String uri) {
8074
* @param uri The URI to open. If this is <code>null</code>, nothing
8175
* happens and this method returns <code>false</code>.
8276
* @return Whether the operation was successful. This will be
83-
* <code>false</code> on JRE's older than 1.6.
77+
* <code>false</code> on systems without desktop support.
8478
* @see #browse(String)
8579
*/
8680
public static boolean browse(URI uri) {
8781

8882
boolean success = false;
8983

90-
if (uri!=null) {
91-
Object desktop = getDesktop();
92-
if (desktop!=null) {
84+
if (uri!=null && Desktop.isDesktopSupported()) {
85+
Desktop desktop = Desktop.getDesktop();
86+
if (desktop.isSupported(Desktop.Action.BROWSE)) {
9387
try {
94-
Method m = desktop.getClass().getDeclaredMethod(
95-
"browse", URI.class);
96-
m.invoke(desktop, uri);
88+
desktop.browse(uri);
9789
success = true;
98-
} catch (RuntimeException re) {
99-
throw re; // Keep FindBugs happy
100-
} catch (Exception e) {
90+
} catch (IOException ioe) {
10191
// Ignore, just return "false" below.
10292
}
10393
}
@@ -170,47 +160,6 @@ public static <T> List<T> getDescendantsOfType(Container comp, Class<T> clazz) {
170160
}
171161

172162

173-
/**
174-
* Returns the singleton <code>java.awt.Desktop</code> instance, or
175-
* <code>null</code> if it is unsupported on this platform (or the JRE
176-
* is older than 1.6).
177-
*
178-
* @return The desktop, as an {@link Object}.
179-
*/
180-
private static Object getDesktop() {
181-
182-
synchronized (LOCK_DESKTOP_CREATION) {
183-
184-
if (!desktopCreationAttempted) {
185-
186-
desktopCreationAttempted = true;
187-
188-
try {
189-
Class<?> desktopClazz = Class.forName("java.awt.Desktop");
190-
Method m = desktopClazz.
191-
getDeclaredMethod("isDesktopSupported");
192-
193-
boolean supported = (Boolean) m.invoke(null);
194-
if (supported) {
195-
m = desktopClazz.getDeclaredMethod("getDesktop");
196-
desktop = m.invoke(null);
197-
}
198-
199-
} catch (RuntimeException re) {
200-
throw re; // Keep FindBugs happy
201-
} catch (Exception e) {
202-
// Ignore; keeps desktop as null.
203-
}
204-
205-
}
206-
207-
}
208-
209-
return desktop;
210-
211-
}
212-
213-
214163
/**
215164
* Returns an empty border of width 5 on all sides. Since this is a
216165
* very common border in GUI's, the border returned is a singleton.
@@ -248,13 +197,11 @@ public static Color getErrorTextForeground() {
248197
*/
249198
public static int getMnemonic(ResourceBundle msg, String key) {
250199
int mnemonic = 0;
251-
try {
200+
if (msg.containsKey(key)) {
252201
Object value = msg.getObject(key);
253202
if (value instanceof String) {
254203
mnemonic = ((String)value).charAt(0);
255204
}
256-
} catch (MissingResourceException mre) {
257-
// Swallow. TODO: When we drop 1.4/1.5 support, use containsKey().
258205
}
259206
return mnemonic;
260207
}
@@ -302,7 +249,7 @@ public static void makeSpringCompactGrid(Container parent, int rows,
302249
"must use SpringLayout.", cce);
303250
}
304251

305-
//Align all cells in each column and make them the same width.
252+
// Align all cells in each column and make them the same width.
306253
Spring x = Spring.constant(initialX);
307254
for (int c = 0; c < cols; c++) {
308255
Spring width = Spring.constant(0);
@@ -320,7 +267,7 @@ public static void makeSpringCompactGrid(Container parent, int rows,
320267
x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
321268
}
322269

323-
//Align all cells in each row and make them the same height.
270+
// Align all cells in each row and make them the same height.
324271
Spring y = Spring.constant(initialY);
325272
for (int r = 0; r < rows; r++) {
326273
Spring height = Spring.constant(0);
@@ -337,7 +284,7 @@ public static void makeSpringCompactGrid(Container parent, int rows,
337284
y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
338285
}
339286

340-
//Set the parent's size.
287+
// Set the parent's size.
341288
SpringLayout.Constraints pCons = layout.getConstraints(parent);
342289
pCons.setConstraint(SpringLayout.SOUTH, y);
343290
pCons.setConstraint(SpringLayout.EAST, x);

0 commit comments

Comments
 (0)