Skip to content

Commit 442f881

Browse files
#1892692: [Test] Pops up NPE error with invalid values for Quota and Retention Period in Create Web App dialog
1 parent bde37b5 commit 442f881

File tree

8 files changed

+90
-70
lines changed

8 files changed

+90
-70
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*/
5+
6+
package com.microsoft.azure.toolkit.intellij.common;
7+
8+
import com.google.common.collect.ImmutableMap;
9+
import com.intellij.icons.AllIcons;
10+
import com.intellij.ui.AnimatedIcon;
11+
import com.intellij.ui.components.fields.ExtendableTextField;
12+
import com.microsoft.azure.toolkit.lib.common.form.AzureValidationInfo;
13+
import com.microsoft.azure.toolkit.lib.common.utils.Debouncer;
14+
import com.microsoft.azure.toolkit.lib.common.utils.TailingDebouncer;
15+
16+
import javax.annotation.Nullable;
17+
import javax.swing.*;
18+
import java.util.Map;
19+
import java.util.Objects;
20+
import java.util.Optional;
21+
import java.util.function.Function;
22+
23+
public class AbstractAzureTextInput<T> extends ExtendableTextField
24+
implements AzureFormInputComponent<T>, TextDocumentListenerAdapter {
25+
protected static final int DEBOUNCE_DELAY = 500;
26+
private final Debouncer debouncer;
27+
private static final Extension VALIDATING = Extension.create(AnimatedIcon.Default.INSTANCE, "Validating...", null);
28+
private static final Extension SUCCESS = Extension.create(AllIcons.General.InspectionsOK, "Validation passed.", null);
29+
private static final Map<AzureValidationInfo.Type, Function<AzureValidationInfo, Extension>> extensions = ImmutableMap.of(
30+
AzureValidationInfo.Type.PENDING, (i) -> VALIDATING,
31+
AzureValidationInfo.Type.SUCCESS, (i) -> SUCCESS,
32+
AzureValidationInfo.Type.ERROR, (i) -> Extension.create(AllIcons.General.BalloonError, i.getMessage(), null),
33+
AzureValidationInfo.Type.WARNING, (i) -> Extension.create(AllIcons.General.BalloonWarning, i.getMessage(), null)
34+
);
35+
36+
public AbstractAzureTextInput() {
37+
this(null);
38+
}
39+
40+
public AbstractAzureTextInput(@Nullable JTextField comp) {
41+
super();
42+
this.debouncer = new TailingDebouncer(this::fireValueChangedEvent, DEBOUNCE_DELAY);
43+
Optional.ofNullable(comp).or(() -> Optional.of(this.getInputComponent()))
44+
.ifPresent(t -> t.getDocument().addDocumentListener(this));
45+
this.trackValidation();
46+
}
47+
48+
public void setValidationInfo(@Nullable AzureValidationInfo info) {
49+
AzureFormInputComponent.super.setValidationInfo(info);
50+
final Extension ex = Objects.isNull(info) ? null : extensions.get(info.getType()).apply(info);
51+
this.setExtensions(ex);
52+
}
53+
54+
public void onDocumentChanged() {
55+
if (this.needValidation()) {
56+
this.setExtensions(VALIDATING);
57+
}
58+
this.debouncer.debounce();
59+
}
60+
61+
@Override
62+
public JTextField getInputComponent() {
63+
return this;
64+
}
65+
66+
@Override
67+
public String toString() {
68+
return String.format("[%s]%s", this.getClass().getSimpleName(), this.getLabel());
69+
}
70+
}

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-lib/src/main/java/com/microsoft/azure/toolkit/intellij/common/AzureComboBox.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public AzureComboBox(boolean refresh) {
7070
super();
7171
this.init();
7272
this.refresher = new TailingDebouncer(this::doRefreshItems, DEBOUNCE_DELAY);
73-
this.valueDebouncer = new TailingDebouncer(() -> this.fireValueChangedEvent(this.getValue()), DEBOUNCE_DELAY);
73+
this.valueDebouncer = new TailingDebouncer(this::fireValueChangedEvent, DEBOUNCE_DELAY);
7474
if (refresh) {
7575
this.refreshItems();
7676
}

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-lib/src/main/java/com/microsoft/azure/toolkit/intellij/common/IntegerTextField.java renamed to PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-lib/src/main/java/com/microsoft/azure/toolkit/intellij/common/AzureIntegerInput.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package com.microsoft.azure.toolkit.intellij.common;
77

8-
import com.intellij.ui.components.JBTextField;
98
import com.microsoft.azure.toolkit.lib.common.form.AzureValidationInfo;
109
import lombok.Getter;
1110
import lombok.Setter;
@@ -14,7 +13,7 @@
1413
import javax.annotation.Nonnull;
1514
import javax.annotation.Nullable;
1615

17-
public class IntegerTextField extends JBTextField implements AzureFormInputComponent<Integer> {
16+
public class AzureIntegerInput extends AbstractAzureTextInput<Integer> {
1817

1918
@Setter
2019
@Getter
@@ -27,7 +26,10 @@ public class IntegerTextField extends JBTextField implements AzureFormInputCompo
2726
@Override
2827
public Integer getValue() {
2928
final String text = getText();
30-
return (StringUtils.isNotEmpty(text) && StringUtils.isNumeric(text)) ? Integer.valueOf(getText()) : null;
29+
if (StringUtils.isBlank(text) || !StringUtils.isNumeric(text)) {
30+
throw new NumberFormatException(String.format("\"%s\" is not an integer", text));
31+
}
32+
return Integer.valueOf(getText());
3133
}
3234

3335
@Override
@@ -36,9 +38,8 @@ public void setValue(final Integer val) {
3638
}
3739

3840
@Nonnull
39-
@Override
4041
public AzureValidationInfo doValidate(Integer value) {
41-
if ((minValue != null && value < minValue) || (maxValue != null && value > maxValue)) {
42+
if (value < minValue || value > maxValue) {
4243
return AzureValidationInfo.error(String.format("Value should be in range [%d, %d]", minValue, maxValue), this);
4344
} else {
4445
return AzureValidationInfo.success(this);

PluginsAndFeatures/azure-toolkit-for-intellij/azure-intellij-plugin-lib/src/main/java/com/microsoft/azure/toolkit/intellij/common/AzureTextInput.java

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,16 @@
55

66
package com.microsoft.azure.toolkit.intellij.common;
77

8-
import com.google.common.collect.ImmutableMap;
9-
import com.intellij.icons.AllIcons;
10-
import com.intellij.ui.AnimatedIcon;
11-
import com.intellij.ui.components.fields.ExtendableTextField;
12-
import com.microsoft.azure.toolkit.lib.common.form.AzureValidationInfo;
13-
import com.microsoft.azure.toolkit.lib.common.utils.Debouncer;
14-
import com.microsoft.azure.toolkit.lib.common.utils.TailingDebouncer;
15-
168
import javax.annotation.Nullable;
179
import javax.swing.*;
18-
import java.util.Map;
19-
import java.util.Objects;
20-
import java.util.Optional;
21-
import java.util.function.Function;
22-
23-
public class AzureTextInput extends ExtendableTextField
24-
implements AzureFormInputComponent<String>, TextDocumentListenerAdapter {
25-
protected static final int DEBOUNCE_DELAY = 500;
26-
private final Debouncer debouncer;
27-
private static final Extension VALIDATING = Extension.create(AnimatedIcon.Default.INSTANCE, "Validating...", null);
28-
private static final Extension SUCCESS = Extension.create(AllIcons.General.InspectionsOK, "Validation passed.", null);
29-
private static final Map<AzureValidationInfo.Type, Function<AzureValidationInfo, Extension>> extensions = ImmutableMap.of(
30-
AzureValidationInfo.Type.PENDING, (i) -> VALIDATING,
31-
AzureValidationInfo.Type.SUCCESS, (i) -> SUCCESS,
32-
AzureValidationInfo.Type.ERROR, (i) -> Extension.create(AllIcons.General.BalloonError, i.getMessage(), null),
33-
AzureValidationInfo.Type.WARNING, (i) -> Extension.create(AllIcons.General.BalloonWarning, i.getMessage(), null)
34-
);
3510

11+
public class AzureTextInput extends AbstractAzureTextInput<String> {
3612
public AzureTextInput() {
3713
this(null);
3814
}
3915

4016
public AzureTextInput(@Nullable JTextField comp) {
41-
super();
42-
this.debouncer = new TailingDebouncer(() -> this.fireValueChangedEvent(this.getValue()), DEBOUNCE_DELAY);
43-
Optional.ofNullable(comp).or(() -> Optional.of(this.getInputComponent()))
44-
.ifPresent(t -> t.getDocument().addDocumentListener(this));
45-
this.trackValidation();
17+
super(comp);
4618
}
4719

4820
@Override
@@ -54,27 +26,4 @@ public String getValue() {
5426
public void setValue(final String val) {
5527
this.setText(val);
5628
}
57-
58-
public void setValidationInfo(@Nullable AzureValidationInfo info) {
59-
AzureFormInputComponent.super.setValidationInfo(info);
60-
final Extension ex = Objects.isNull(info) ? null : extensions.get(info.getType()).apply(info);
61-
this.setExtensions(ex);
62-
}
63-
64-
public void onDocumentChanged() {
65-
if (this.needValidation()) {
66-
this.setExtensions(VALIDATING);
67-
}
68-
this.debouncer.debounce();
69-
}
70-
71-
@Override
72-
public JTextField getInputComponent() {
73-
return this;
74-
}
75-
76-
@Override
77-
public String toString() {
78-
return String.format("[%s]%s", this.getClass().getSimpleName(), this.getLabel());
79-
}
8029
}

PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/azure/toolkit/intellij/appservice/AppServiceMonitorPanel.form

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@
195195
<properties/>
196196
<border type="none"/>
197197
<children>
198-
<component id="45563" class="com.microsoft.azure.toolkit.intellij.common.IntegerTextField" binding="txtQuota" custom-create="true">
198+
<component id="45563" class="com.microsoft.azure.toolkit.intellij.common.AzureIntegerInput" binding="txtQuota" custom-create="true">
199199
<constraints>
200200
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
201201
</constraints>
@@ -211,7 +211,7 @@
211211
<properties/>
212212
<border type="none"/>
213213
<children>
214-
<component id="d3331" class="com.microsoft.azure.toolkit.intellij.common.IntegerTextField" binding="txtRetention" custom-create="true">
214+
<component id="d3331" class="com.microsoft.azure.toolkit.intellij.common.AzureIntegerInput" binding="txtRetention" custom-create="true">
215215
<constraints>
216216
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
217217
</constraints>

PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/azure/toolkit/intellij/appservice/AppServiceMonitorPanel.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import com.intellij.ui.TitledSeparator;
1010
import com.microsoft.azure.toolkit.intellij.appservice.insights.ApplicationInsightsComboBox;
1111
import com.microsoft.azure.toolkit.intellij.common.AzureFormPanel;
12-
import com.microsoft.azure.toolkit.intellij.common.IntegerTextField;
12+
import com.microsoft.azure.toolkit.intellij.common.AzureIntegerInput;
1313
import com.microsoft.azure.toolkit.lib.appservice.ApplicationInsightsConfig;
1414
import com.microsoft.azure.toolkit.lib.appservice.MonitorConfig;
1515
import com.microsoft.azure.toolkit.lib.appservice.model.DiagnosticConfig;
@@ -43,8 +43,8 @@ public class AppServiceMonitorPanel extends JPanel implements AzureFormPanel<Mon
4343
private JPanel pnlApplicationLog;
4444
private TitledSeparator titleApplicationInsights;
4545
private TitledSeparator titleAppServiceLog;
46-
private IntegerTextField txtQuota;
47-
private IntegerTextField txtRetention;
46+
private AzureIntegerInput txtQuota;
47+
private AzureIntegerInput txtRetention;
4848
private LogLevelComboBox cbLogLevel;
4949
private JLabel lblApplicationLog;
5050

@@ -146,11 +146,11 @@ private void createUIComponents() {
146146
cbLogLevel = new LogLevelComboBox();
147147
applicationInsightsComboBox = new ApplicationInsightsComboBox();
148148

149-
txtQuota = new IntegerTextField();
149+
txtQuota = new AzureIntegerInput();
150150
txtQuota.setMinValue(25);
151151
txtQuota.setMaxValue(100);
152152

153-
txtRetention = new IntegerTextField();
153+
txtRetention = new AzureIntegerInput();
154154
txtRetention.setMinValue(0);
155155
txtRetention.setMaxValue(99999);
156156
txtRetention.setRequired(false);

PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/azure/toolkit/intellij/appservice/jfr/DurationPanel.form

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<toolTipText value=""/>
2525
</properties>
2626
</component>
27-
<component id="3da98" class="com.microsoft.azure.toolkit.intellij.common.IntegerTextField" binding="textField1" custom-create="true">
27+
<component id="3da98" class="com.microsoft.azure.toolkit.intellij.common.AzureIntegerInput" binding="textField1" custom-create="true">
2828
<constraints>
2929
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false">
3030
<preferred-size width="70" height="-1"/>

PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/azure/toolkit/intellij/appservice/jfr/DurationPanel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
package com.microsoft.azure.toolkit.intellij.appservice.jfr;
77

88
import com.microsoft.azure.toolkit.intellij.common.AzureFormPanel;
9-
import com.microsoft.azure.toolkit.intellij.common.IntegerTextField;
9+
import com.microsoft.azure.toolkit.intellij.common.AzureIntegerInput;
1010
import com.microsoft.azure.toolkit.lib.common.form.AzureFormInput;
1111

1212
import javax.swing.*;
@@ -18,7 +18,7 @@
1818

1919
public class DurationPanel extends JPanel implements AzureFormPanel<Integer> {
2020
private JSlider slider1;
21-
private IntegerTextField textField1;
21+
private AzureIntegerInput textField1;
2222
private JPanel contentPanel;
2323

2424
public DurationPanel(int minValue, int maxValue, int defaultValue) {
@@ -74,6 +74,6 @@ public List<AzureFormInput<?>> getInputs() {
7474
}
7575

7676
private void createUIComponents() {
77-
this.textField1 = new IntegerTextField();
77+
this.textField1 = new AzureIntegerInput();
7878
}
7979
}

0 commit comments

Comments
 (0)