Skip to content

Commit f8fdc0c

Browse files
committed
Remove saving from Configuration for now. It's never used and should be explicit if it is ever added back
1 parent cd89799 commit f8fdc0c

File tree

12 files changed

+486
-75
lines changed

12 files changed

+486
-75
lines changed

SpellChecker/src/main/java/org/fife/com/swabunga/spell/engine/Configuration.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
*/
3535
public abstract class Configuration {
3636

37+
/**
38+
* System property that, if defined, should be a fully qualified class name of a configuration.
39+
*/
40+
public static final String PROPERTY_CONFIG_OVERRIDE = "jazzy.config";
41+
3742
/** used by EditDistance: the cost of having to remove a character <br/>(integer greater than 0). */
3843
public static final String COST_REMOVE_CHAR = "EDIT_DEL1";
3944

@@ -102,18 +107,18 @@ public abstract class Configuration {
102107
public abstract boolean getBoolean(String key);
103108

104109
/**
105-
* Sets one of the integer constants.
110+
* Sets one of the integer constants. Does not persist the change.
106111
*
107112
* @param key one of the integer constants defined in this class
108-
* @param value new integer value of the constant
113+
* @param value new integer value of the constant.
109114
*/
110115
public abstract void setInteger(String key, int value);
111116

112117
/**
113-
* Sets one of the boolean constants.
118+
* Sets one of the boolean constants. Does not persist the change.
114119
*
115120
* @param key one of the boolean constants defined in this class
116-
* @param value new boolean value of this setting
121+
* @param value new boolean value of this setting.
117122
*/
118123
public abstract void setBoolean(String key, boolean value);
119124

@@ -122,8 +127,8 @@ public abstract class Configuration {
122127
*
123128
* @return Configuration
124129
*/
125-
public static final Configuration getConfiguration() {
126-
String config = System.getProperty("jazzy.config"); // added by bd
130+
public static Configuration getConfiguration() {
131+
String config = System.getProperty(PROPERTY_CONFIG_OVERRIDE); // added by bd
127132
if (config != null && !config.isEmpty())
128133
return getConfiguration(config);
129134
return getConfiguration(null);
@@ -133,9 +138,9 @@ public static final Configuration getConfiguration() {
133138
* Returns a new instance of a Configuration class.
134139
*
135140
* @param className - the class to return, must be based on Configuration
136-
* @return Configuration
141+
* @return The configuration.
137142
*/
138-
public static final Configuration getConfiguration(String className) {
143+
public static Configuration getConfiguration(String className) {
139144

140145
Configuration result;
141146

SpellChecker/src/main/java/org/fife/com/swabunga/spell/engine/DoubleMeta.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,13 @@ private static boolean stringAt(String string, int start, int length, String[] l
196196
*/
197197
@Override
198198
public final String transform(String word) {
199+
if (word == null || word.isEmpty()) {
200+
return "";
201+
}
199202
StringBuilder primary = new StringBuilder(word.length() + 5);
200203
String in = word.toUpperCase() + " ";
201204
int current = 0;
202205
int length = in.length();
203-
if (length < 1)
204-
return "";
205206
int last = length - 1;
206207
boolean isSlavoGermaic = slavoGermanic(in);
207208
if (stringAt(in, 0, 2, MY_LIST))

SpellChecker/src/main/java/org/fife/com/swabunga/spell/engine/PropertyConfiguration.java

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
package org.fife.com.swabunga.spell.engine;
2121

2222
import java.io.*;
23-
import java.net.URL;
23+
import java.nio.charset.Charset;
24+
import java.nio.charset.StandardCharsets;
2425
import java.util.Properties;
2526

2627

@@ -37,72 +38,46 @@ public class PropertyConfiguration extends Configuration {
3738
*/
3839
private Properties prop;
3940

40-
/**
41-
* The name of the file containing spell engine properties.
42-
*/
43-
private URL filename;
41+
private static final String DEFAULT_PROPERTIES = "org/fife/com/swabunga/spell/engine/configuration.properties";
42+
43+
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
4444

4545
/**
4646
* Constructs and loads spell engine properties configuration.
4747
*/
4848
public PropertyConfiguration() {
49+
this(DEFAULT_PROPERTIES);
50+
}
51+
52+
public PropertyConfiguration(String resource) {
4953
prop = new Properties();
50-
try {
51-
filename = getClass().getClassLoader().getResource("org/fife/com/swabunga/spell/engine/configuration.properties");
52-
InputStream in = filename.openStream();
53-
prop.load(in);
54-
} catch (IOException ioe) {
55-
ioe.printStackTrace();
54+
InputStream in = getClass().getClassLoader().getResourceAsStream(resource);
55+
if (in != null) {
56+
try (BufferedReader r = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET))) {
57+
prop.load(r);
58+
} catch (IOException e) {
59+
e.printStackTrace();
60+
}
5661
}
5762
}
5863

59-
/**
60-
* @see org.fife.com.swabunga.spell.engine.Configuration#getBoolean(String)
61-
*/
6264
@Override
6365
public boolean getBoolean(String key) {
64-
// robert: Avoid Boolean allocations
6566
return Boolean.parseBoolean(prop.getProperty(key));
6667
}
6768

68-
/**
69-
* @see org.fife.com.swabunga.spell.engine.Configuration#getInteger(String)
70-
*/
7169
@Override
7270
public int getInteger(String key) {
73-
// robert: Avoid Integer allocations
7471
return Integer.parseInt(prop.getProperty(key), 10);
7572
}
7673

77-
/**
78-
* @see org.fife.com.swabunga.spell.engine.Configuration#setBoolean(String, boolean)
79-
*/
8074
@Override
8175
public void setBoolean(String key, boolean value) {
8276
prop.setProperty(key, String.valueOf(value));
83-
save();
8477
}
8578

86-
/**
87-
* @see org.fife.com.swabunga.spell.engine.Configuration#setInteger(String, int)
88-
*/
8979
@Override
9080
public void setInteger(String key, int value) {
9181
prop.setProperty(key, Integer.toString(value));
92-
save();
9382
}
94-
95-
/**
96-
* Writes the property list (key and element pairs) in the
97-
* PropertyConfiguration file.
98-
*/
99-
public void save() {
100-
File file = new File(filename.getFile());
101-
try (FileOutputStream fout = new FileOutputStream(file)) {
102-
prop.store(fout, "HEADER");
103-
} catch (IOException ioe) {
104-
ioe.printStackTrace();
105-
}
106-
}
107-
10883
}

SpellChecker/src/main/java/org/fife/com/swabunga/spell/event/AbstractWordTokenizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public abstract class AbstractWordTokenizer implements WordTokenizer {
3838
/**
3939
* The word being analyzed.
4040
*/
41-
protected Word currentWord;
41+
private Word currentWord;
4242
/**
4343
* The word finder used to filter out words which are non pertinent to spell checking.
4444
*/

SpellChecker/src/main/java/org/fife/com/swabunga/spell/event/SpellChecker.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,14 @@ public static boolean beginsAsINETWord(String word) {
232232
* @return true if this word looks like an Internet address.
233233
* @see #beginsAsINETWord(String)
234234
*/
235-
public static final boolean isINETWord(String word) {
235+
public static boolean isINETWord(String word) {
236236
return beginsAsINETWord(word) || word.indexOf('@') > 0;
237237
}
238238

239239

240240
/**
241241
* Verifies if the word that is being spell checked contains all
242-
* upper-cases characters.
242+
* upper-cased characters.
243243
*
244244
* @param word The word to analyze for upper-cases characters
245245
* @return true if this word contains all upper case characters
@@ -554,5 +554,4 @@ private boolean isSupposedToBeCapitalized(String word, WordTokenizer wordTokeniz
554554
return configCapitalize && wordTokenizer.isNewSentence() && Character.isLowerCase(word.charAt(0));
555555
}
556556

557-
558557
}

SpellChecker/src/test/java/org/fife/com/swabunga/spell/engine/ConfigurationTest.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,65 @@
11
package org.fife.com.swabunga.spell.engine;
22

3+
import org.junit.jupiter.api.AfterEach;
34
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.BeforeEach;
46
import org.junit.jupiter.api.Test;
57

68
/**
79
* Unit tests for {@link Configuration}.
810
*/
911
class ConfigurationTest {
1012

13+
private String origJazzyConfig;
14+
15+
@BeforeEach
16+
void setUp() {
17+
origJazzyConfig = System.getProperty(Configuration.PROPERTY_CONFIG_OVERRIDE);
18+
}
19+
20+
@AfterEach
21+
void tearDown() {
22+
if (origJazzyConfig != null) {
23+
System.setProperty(Configuration.PROPERTY_CONFIG_OVERRIDE, origJazzyConfig);
24+
} else {
25+
System.clearProperty(Configuration.PROPERTY_CONFIG_OVERRIDE);
26+
}
27+
}
28+
29+
@Test
30+
void testGetConfiguration_zeroArg_null_defaultValue() {
31+
Configuration config = Configuration.getConfiguration();
32+
Assertions.assertNotNull(config);
33+
}
34+
35+
@Test
36+
void testGetConfiguration_zeroArg_fromSystemProperty_defined() {
37+
System.setProperty(Configuration.PROPERTY_CONFIG_OVERRIDE,
38+
"org.fife.com.swabunga.spell.engine.PropertyConfiguration");
39+
Configuration config = Configuration.getConfiguration();
40+
Assertions.assertNotNull(config);
41+
}
42+
43+
@Test
44+
void testGetConfiguration_zeroArg_fromSystemProperty_emptyString_triggersDefaults() {
45+
System.setProperty(Configuration.PROPERTY_CONFIG_OVERRIDE, "no.such.ClassConfiguration");
46+
Configuration config = Configuration.getConfiguration();
47+
Assertions.assertNotNull(config);
48+
}
49+
1150
@Test
12-
void testGetConfiguration_zeroArg_defaultValue() {
51+
void testGetConfiguration_zeroArg_fromSystemProperty_notDefined_triggersDefaults() {
52+
System.setProperty(Configuration.PROPERTY_CONFIG_OVERRIDE, "");
1353
Configuration config = Configuration.getConfiguration();
1454
Assertions.assertNotNull(config);
1555
}
1656

57+
@Test
58+
void testGetConfiguration_oneArg_emptyString_defaultValue() {
59+
Configuration config = Configuration.getConfiguration("");
60+
Assertions.assertNotNull(config);
61+
}
62+
1763
@Test
1864
void testGetConfiguration_oneArg_errorTriggersDefaults() {
1965
Configuration config = Configuration.getConfiguration("not/there");

0 commit comments

Comments
 (0)