Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion app/email/ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,31 @@
<artifactId>core-ui</artifactId>
<version>5.0.3-SNAPSHOT</version>
</dependency>
</dependencies>
<dependency>
<groupId>org.testfx</groupId>
<artifactId>testfx-core</artifactId>
<version>4.0.18</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testfx</groupId>
<artifactId>testfx-junit5</artifactId>
<version>4.0.18</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import org.phoebus.applications.email.EmailApp;
import org.phoebus.email.EmailPreferences;
import org.phoebus.email.EmailService;
import org.phoebus.framework.macros.MacroHandler;
import org.phoebus.framework.macros.MacroOrSystemProvider;
import org.phoebus.framework.macros.Macros;
import org.phoebus.framework.preferences.PhoebusPreferenceService;
import org.phoebus.ui.javafx.FilesTab;
import org.phoebus.ui.javafx.ImagesTab;
Expand Down Expand Up @@ -58,6 +61,8 @@ public class EmailDialogController {

final Preferences prefs = PhoebusPreferenceService.userNodeForClass(EmailApp.class);

private final MacroOrSystemProvider systemMacros = new MacroOrSystemProvider(new Macros());

private static final String TEXT_PLAIN = "text/plain";
private static final String TEXT_HTML = "text/html";

Expand Down Expand Up @@ -183,12 +188,17 @@ public void send(Event event) {

@FXML
public void initialize() {
String to = EmailPreferences.to;
if(to == null || to.isBlank())
to = prefs.get(LAST_TO, "");

txtTo.setText(prefs.get(LAST_TO, ""));
if(EmailPreferences.from == null || EmailPreferences.from.isBlank())
txtFrom.setText(prefs.get(LAST_FROM, ""));
else
txtFrom.setText(EmailPreferences.from);
txtTo.setText(substituteSystemMacros(to));

String from = EmailPreferences.from;
if(from == null || from.isBlank())
from = prefs.get(LAST_FROM, "");

txtFrom.setText(substituteSystemMacros(from));

txtFrom.setPromptText("Enter your email address");
txtTo.setPromptText("Enter receipient's email address(es)");
Expand Down Expand Up @@ -277,6 +287,18 @@ public void setAttachmets(final List<File> files)
att_files.setFiles(files);
}

private String substituteSystemMacros(String input) {
String output;
try {
output = MacroHandler.replace(systemMacros, input);
} catch (Exception ignored) {
logger.warning("Failed macro expansion in email address field. Using original string");
output = input;
}

return output;
}

private void recomputeTextArea() {
simpleTextVBox.setVisible(choiceBox.getValue().equals(TEXT_PLAIN));
simpleTextVBox.setManaged(choiceBox.getValue().equals(TEXT_PLAIN));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.phoebus.applications.email.ui;

import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import org.phoebus.applications.email.EmailApp;
import org.phoebus.email.EmailPreferences;
import org.testfx.framework.junit5.ApplicationTest;

import java.io.IOException;

class EmailDialogControllerTestUI extends ApplicationTest {

EmailDialogController controller;

@Override
public void start(Stage stage) throws IOException
{
final FXMLLoader loader = new FXMLLoader();
loader.setLocation(EmailApp.class.getResource("ui/EmailDialog.fxml"));
loader.load();
controller = loader.getController();
}

@BeforeEach
public void setup() {
System.setProperty("test_key1", "test_value1");
System.setProperty("test_key2", "test_value2");
System.setProperty("test_recursive1", "$(test_recursive2)");
System.setProperty("test_recursive2", "$(test_recursive1)");
}

@AfterEach
public void tearDown() {
System.clearProperty("test_key1");
System.clearProperty("test_key2");
System.clearProperty("test_recursive1");
System.clearProperty("test_recursive2");
}

@Test
void testEmailAddressSubstitutions() {
EmailPreferences.to = "$(test_key1)@place";
EmailPreferences.from = "$(test_key2)@other";
controller.initialize();

Assertions.assertEquals("test_value1@place", controller.txtTo.getText());
Assertions.assertEquals("test_value2@other", controller.txtFrom.getText());
}

@Test
void testEmailAddressSubstitutionsWithIncorrectProperties() {
EmailPreferences.to = "$(nonexistent)@place";
EmailPreferences.from = "$(test_key2@other";
controller.initialize();

Assertions.assertEquals("$(nonexistent)@place", controller.txtTo.getText());
Assertions.assertEquals("$(test_key2@other", controller.txtFrom.getText());
}

@Test
void testEmailAddressSubstitutionsWithRecursiveProperties() {
EmailPreferences.to = "$(test_recursive1)@place";
EmailPreferences.from = "$(test_recursive2)@other";
controller.initialize();

Assertions.assertEquals("$(test_recursive1)@place", controller.txtTo.getText());
Assertions.assertEquals("$(test_recursive2)@other", controller.txtFrom.getText());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class EmailPreferences
@Preference public static String username;
@Preference public static String password;
@Preference public static String from;
@Preference public static String to;

/** @return Is email supported? */
public static final boolean isEmailSupported()
Expand Down
9 changes: 8 additions & 1 deletion core/email/src/main/resources/email_preferences.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,12 @@ password=

# Default address to be used for {mailheader}`From:`.
#
# If left empty then the last used {mailheader}`From` address is used.
# If left empty then the last used {mailheader}`From` address is used. System or environment
# variable macro substitution is applied to this field.
from=

# Default address to be used for {mailheader}`To:`.
#
# If left empty then the last used {mailheader}`To` address is used. System or environment
# variable macro substitution is applied to this field.
to=