Skip to content

Commit cd6c625

Browse files
Babcock, ScottBabcock, Scott
authored andcommitted
Merge pull request #2 in MFATT/common from pr/create-readme-file to master
* commit '7a97909c35ac2ab796d2243b0c7a3c7d33539fb7': Convert tabs to spaces Finish JavaDoc and README New JavaDoc Commit current changes
2 parents 4773aa7 + 7a97909 commit cd6c625

File tree

3 files changed

+644
-313
lines changed

3 files changed

+644
-313
lines changed

README.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# NORDSTROM COMMON
2+
3+
**Nordstrom Common** is a small collection of general-purpose utility classes with wide applicability.
4+
5+
## UncheckedThrow
6+
7+
The **UncheckedThrow** class uses type erasure to enable client code to throw checked exceptions as unchecked. This allows methods to throw checked exceptions without requiring clients to handle or declare them. It should be sparingly, as this exempts client code from handling or declaring exceptions created by their own actions. The target use case for this facility is to throw exceptions that were serialized in responses from a remote system. Although the compiler won't require clients of methods using this technique to handle or declare the suppressed exception, the JavaDoc for such methods should include a `@throws` declaration for implementers who might want to handle or declare it voluntarily.
8+
9+
10+
```java
11+
...
12+
13+
String value;
14+
try {
15+
value = URLDecoder.decode(keyVal[1], "UTF-8");
16+
} catch (UnsupportedEncodingException e) {
17+
UncheckedThrow.throwUnchecked(e);
18+
}
19+
20+
...
21+
```
22+
23+
## DatabaseUtils
24+
25+
The **DatabaseUtils** class provides facilities that enable you to define collections of Oracle database queries and execute them easily. Query collections are defined as Java enumeration that implement the `QueryAPI` interface:
26+
* `getQueryStr` - Get the query string for this constant. This is the actual query that's sent to the database.
27+
* `getArgNames` - Get the names of the arguments for this query. This provides diagnostic information if the incorrect number of arguments is specified by the client.
28+
* `getArgCount` - Get the number of arguments required by this query. This enables **DatabaseUtils** to verify that the correct number of arguments has been specified by the client.
29+
* `getConnection` - Get the connection string associated with this query. This eliminates the need for the client to provide this information.
30+
* `getEnum` - Get the enumeration to which this query belongs. This enables **DatabaseUtils** to retrieve the name of the query's enumerated constant for diagnostic messages.
31+
32+
To maximize usability and configurability, we recommend the following implementation strategy for your query collections:
33+
* Define your query collection as an enumeration that implements `QueryAPI`.
34+
* Define each query constant with a property name and a name for each argument (if any).
35+
* To assist users of your queries, preface their names with a type indicator (**GET** or **UPDATE**).
36+
* Back the query collection with a configuration that implements the `Settings API`:
37+
* groupId: com.nordstrom.test-automation.tools
38+
* artifactId: settings
39+
* className: com.nordstrom.automation.settings.SettingsCore
40+
* To support execution on multiple endpoints, implement `getConnection` with sub-configurations or other dynamic data sources (e.g. - web service).
41+
42+
##### Query Collection Example
43+
44+
```java
45+
public class OpctConfig extends SettingsCore<OpctConfig.OpctValues> {
46+
47+
private static final String SETTINGS_FILE = "OpctConfig.properties";
48+
49+
private OpctConfig() throws ConfigurationException, IOException {
50+
super(OpctValues.class);
51+
}
52+
53+
public enum OpctValues implements SettingsCore.SettingsAPI, QueryAPI {
54+
/** args: [ ] */
55+
GET_RULE_HEAD_DETAILS("opct.query.getRuleHeadDetails"),
56+
/** args: [ name, zone_id, priority, rule_type ] */
57+
GET_RULE_COUNT("opct.query.getRuleCount", "name", "zone_id", "priority", "rule_type"),
58+
/** args: [ role_id, user_id ] */
59+
UPDATE_USER_ROLE("opct.query.updateRsmUserRole", "role_id", "user_id"),
60+
/** MST connection string */
61+
MST_CONNECT("opct.connect.mst"),
62+
/** RMS connection string */
63+
RMS_CONNECT("opct.connect.rms");
64+
65+
private String key;
66+
private String[] args;
67+
private String query;
68+
69+
private static OpctConfig config;
70+
private static String mstConnect;
71+
private static String rmsConnect;
72+
73+
private static EnumSet<OpctValues> rmsQueries = EnumSet.of(UPDATE_USER_ROLE);
74+
75+
static {
76+
try {
77+
config = new OpctConfig();
78+
} catch (ConfigurationException | IOException e) {
79+
throw new RuntimeException("Unable to instantiate OPCT configuration object", e);
80+
}
81+
}
82+
83+
OpctValues(String key, String... args) {
84+
this.key = key;
85+
this.args = args;
86+
}
87+
88+
@Override
89+
public String key() {
90+
return key;
91+
}
92+
93+
@Override
94+
public String getQueryStr() {
95+
if (query == null) {
96+
query = config.getString(key);
97+
}
98+
return query;
99+
}
100+
101+
@Override
102+
public String[] getArgNames() {
103+
return args;
104+
}
105+
106+
@Override
107+
public int getArgCount() {
108+
return args.length;
109+
}
110+
111+
@Override
112+
public String getConnection() {
113+
if (rmsQueries.contains(this)) {
114+
return getRmsConnect();
115+
} else {
116+
return getMstConnect();
117+
}
118+
}
119+
120+
@Override
121+
public Enum<OpctValues> getEnum() {
122+
return this;
123+
}
124+
125+
/**
126+
* Get MST connection string.
127+
*
128+
* @return MST connection string
129+
*/
130+
public static String getMstConnect() {
131+
if (mstConnect == null) {
132+
mstConnect = config.getString(OpctValues.MST_CONNECT.key());
133+
}
134+
return mstConnect;
135+
}
136+
137+
/**
138+
* Get RMS connection string.
139+
*
140+
* @return RMS connection string
141+
*/
142+
public static String getRmsConnect() {
143+
if (rmsConnect == null) {
144+
rmsConnect = config.getString(OpctValues.RMS_CONNECT.key());
145+
}
146+
return rmsConnect;
147+
}
148+
}
149+
150+
@Override
151+
public String getSettingsPath() {
152+
return SETTINGS_FILE;
153+
}
154+
155+
/**
156+
* Get OPCT configuration object.
157+
*
158+
* @return OPCT configuration object
159+
*/
160+
public static OpctConfig getConfig() {
161+
return OpctValues.config;
162+
}
163+
}
164+
```

src/main/java/com/nordstrom/common/base/UncheckedThrow.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
* a {@code @throws} declaration for implementers who might want to handle or declare it voluntarily.
1111
*/
1212
public final class UncheckedThrow {
13-
14-
private UncheckedThrow() {
15-
throw new AssertionError("UncheckedThrow is a static utility class that cannot be instantiated");
16-
}
17-
18-
/**
19-
* This method throws the specified checked exception, using generic type erasure to enable client
20-
* methods to propagate checked exceptions without being required to declare them.
21-
*
22-
* @param thrown exception to be thrown
23-
* @return <b>nothing</b> this method always throws the specified exception
24-
*/
13+
14+
private UncheckedThrow() {
15+
throw new AssertionError("UncheckedThrow is a static utility class that cannot be instantiated");
16+
}
17+
18+
/**
19+
* This method throws the specified checked exception, using generic type erasure to enable client
20+
* methods to propagate checked exceptions without being required to declare them.
21+
*
22+
* @param thrown exception to be thrown
23+
* @return <b>nothing</b> this method always throws the specified exception
24+
*/
2525
public static RuntimeException throwUnchecked(final Throwable thrown) {
2626
UncheckedThrow.<RuntimeException>propagate(thrown);
2727
// suppress complaint about missing return value
@@ -36,9 +36,9 @@ public static RuntimeException throwUnchecked(final Throwable thrown) {
3636
* @throws T dummy declaration to satisfy the compiler
3737
*/
3838
@SuppressWarnings("unchecked")
39-
private static <T extends Exception> void propagate(Throwable thrown) throws T {
39+
private static <T extends Exception> void propagate(Throwable thrown) throws T {
4040
// Due to generic type erasure, this cast only serves to satisfy the compiler
41-
// that the requirement to declare the thrown exception has been met.
41+
// that the requirement to declare the thrown exception has been met.
4242
throw (T) thrown;
4343
}
4444
}

0 commit comments

Comments
 (0)