Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing braces to if statements #382

Merged
merged 1 commit into from
May 7, 2024
Merged
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
3 changes: 2 additions & 1 deletion src/main/java/winstone/AbstractSecuredConnectorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@

if (keyStore != null) {
// load from default Keystore
if (!keyStore.exists() || !keyStore.isFile())
if (!keyStore.exists() || !keyStore.isFile()) {

Check warning on line 43 in src/main/java/winstone/AbstractSecuredConnectorFactory.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 43 is only partially covered, 2 branches are missing
throw new WinstoneException(
SSL_RESOURCES.getString("HttpsListener.KeyStoreNotFound", keyStore.getPath()));
}

this.keystorePassword = pwd;

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/winstone/HostConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@
while (mappingST.hasMoreTokens()) {
String mapping = mappingST.nextToken();
int delimPos = mapping.indexOf('=');
if (delimPos == -1) continue;
if (delimPos == -1) {

Check warning on line 96 in src/main/java/winstone/HostConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 96 is only partially covered, one branch is missing
continue;

Check warning on line 97 in src/main/java/winstone/HostConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 97 is not covered by tests
}
String extension = mapping.substring(0, delimPos);
String mimeType = mapping.substring(delimPos + 1);
this.mimeTypes.addMimeMapping(extension.toLowerCase(), mimeType);
Expand Down Expand Up @@ -222,8 +224,9 @@
Logger.log(Level.INFO, Launcher.RESOURCES, "HostConfig.BeginningWarExtraction");

// open the war file
if (!warfile.exists() || !warfile.isFile())
if (!warfile.exists() || !warfile.isFile()) {

Check warning on line 227 in src/main/java/winstone/HostConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 227 is only partially covered, 2 branches are missing
throw new WinstoneException(Launcher.RESOURCES.getString("HostConfig.WarFileInvalid", warfile));
}

// Get the webroot folder (or a temp dir if none supplied)
File unzippedDir;
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/winstone/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,15 @@
if (libFolder.exists() && libFolder.isDirectory()) {
Logger.log(Level.FINER, RESOURCES, "Launcher.UsingCommonLib", libFolder.getCanonicalPath());
File[] children = libFolder.listFiles();
if (children != null)
for (File aChildren : children)
if (children != null) {
for (File aChildren : children) {

Check warning on line 139 in src/main/java/winstone/Launcher.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 138-139 are not covered by tests
if (aChildren.getName().endsWith(".jar")
|| aChildren.getName().endsWith(".zip")) {
jars.add(aChildren.toURI().toURL());
Logger.log(Level.FINER, RESOURCES, "Launcher.AddedCommonLibJar", aChildren.getName());
}
}
}
} else {
Logger.log(Level.FINER, RESOURCES, "Launcher.NoCommonLib");
}
Expand All @@ -151,12 +153,14 @@
if (extraLibFolder != null && extraLibFolder.exists()) {
Logger.log(Level.WARNING, RESOURCES, "Launcher.ExtraLibFolder");
File[] children = extraLibFolder.listFiles();
if (children != null)
for (File aChildren : children)
if (children != null) {

Check warning on line 156 in src/main/java/winstone/Launcher.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 156 is only partially covered, one branch is missing
for (File aChildren : children) {
if (aChildren.getName().endsWith(".jar")
|| aChildren.getName().endsWith(".zip")) {
extraJars.add(aChildren.toURI().toURL());
}
}
}
}

ClassLoader commonLibCL =
Expand Down Expand Up @@ -212,7 +216,9 @@

success = true;
} finally {
if (!success) shutdown();
if (!success) {
shutdown();
}
}

try {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/winstone/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@
public static void setCurrentDebugLevel(int level) {
if (!initialised) {
init(level);
} else
} else {
synchronized (semaphore) {
LOGGER.setLevel(Level.parse(String.valueOf(level)));
}
}
}

Check warning on line 65 in src/main/java/winstone/Logger.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 65 is not covered by tests

/**
* Writes a log message to the requested stream, and immediately flushes
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/winstone/cmdline/CmdLineParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,60 +45,68 @@
int equalPos = option.indexOf('=');
String paramName = option.substring(2, equalPos == -1 ? option.length() : equalPos);
Option<?> opt = toOption(paramName);
if (opt == null)
if (opt == null) {
throw new IllegalArgumentException(
Launcher.RESOURCES.getString("CmdLineParser.UnrecognizedOption", option));
}

if (equalPos != -1) {
args.put(paramName, option.substring(equalPos + 1));
} else {
if (opt.type == Boolean.class) args.put(paramName, "true");
else
if (opt.type == Boolean.class) {
args.put(paramName, "true");
} else {
throw new IllegalArgumentException(
Launcher.RESOURCES.getString("CmdLineParser.OperandExpected", option));
}
}
if (paramName.equals(Option.CONFIG.name)) {
configFilename = args.get(paramName);
}
} else {
if (args.containsKey(nonSwitchArgName))
if (args.containsKey(nonSwitchArgName)) {
throw new IllegalArgumentException(
Launcher.RESOURCES.getString("CmdLineParser.MultipleArgs", option));
}
args.put(nonSwitchArgName, option);
}
}

// Load default props if available
File configFile = new File(configFilename);
if (configFile.exists() && configFile.isFile()) {
try (InputStream inConfig = new FileInputStream(configFile)) {
loadPropsFromStream(inConfig, args);
inConfig.close();
Launcher.initLogger(args);
Logger.log(Level.FINER, Launcher.RESOURCES, "Launcher.UsingPropertyFile", configFilename);
}
} else {
Launcher.initLogger(args);
}
return args;
}

private static void loadPropsFromStream(InputStream inConfig, Map<String, String> args) throws IOException {
Properties props = new Properties();
props.load(inConfig);
for (Object o : props.keySet()) {
String key = (String) o;
if (!args.containsKey(key.trim())) {
args.put(key.trim(), props.getProperty(key).trim());
}
}
props.clear();
}

private Option<?> toOption(String paramName) {
for (Option<?> o : options) {
if (o.isWildcard() && paramName.startsWith(o.name)) return o;
if (!o.isWildcard() && paramName.equals(o.name)) return o;
if (o.isWildcard() && paramName.startsWith(o.name)) {
return o;
}
if (!o.isWildcard() && paramName.equals(o.name)) {
return o;

Check warning on line 108 in src/main/java/winstone/cmdline/CmdLineParser.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 48-108 are not covered by tests
}
}
return null;
}
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/winstone/cmdline/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,19 @@
public <T> Class<? extends T> get(Map<String, String> args, Class<T> expectedType, ClassLoader cl)
throws ClassNotFoundException {
String v = args.get(name);
if (v == null) return defaultValue;
if (v == null) {
return defaultValue;
}

v = v.trim();
if (v.length() == 0) return defaultValue;
if (v.length() == 0) {

Check warning on line 298 in src/main/java/winstone/cmdline/Option.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 298 is only partially covered, one branch is missing
return defaultValue;

Check warning on line 299 in src/main/java/winstone/cmdline/Option.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 299 is not covered by tests
}

Class<?> c = Class.forName(v, true, cl);
if (!expectedType.isAssignableFrom(c))
if (!expectedType.isAssignableFrom(c)) {

Check warning on line 303 in src/main/java/winstone/cmdline/Option.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 303 is only partially covered, one branch is missing
throw new ClassNotFoundException("Expected a subype of " + expectedType + " but got " + c + " instead");
}

return c.asSubclass(expectedType);
}
Expand All @@ -313,8 +318,11 @@

public static boolean booleanArg(Map<String, String> args, String name, boolean defaultTrue) {
String value = args.get(name);
if (defaultTrue) return (value == null) || (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"));
else return (value != null) && (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"));
if (defaultTrue) {

Check warning on line 321 in src/main/java/winstone/cmdline/Option.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 321 is only partially covered, one branch is missing
return (value == null) || (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"));

Check warning on line 322 in src/main/java/winstone/cmdline/Option.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 322 is not covered by tests
} else {
return (value != null) && (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"));

Check warning on line 324 in src/main/java/winstone/cmdline/Option.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 324 is only partially covered, 3 branches are missing
}
}

public static String stringArg(Map<String, String> args, String name, String defaultValue) {
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/winstone/realm/FileRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,39 @@
setUserStore(userStore);
// Get the filename and parse the xml doc
File realmFile = Option.FILEREALM_CONFIGFILE.get(args);
if (realmFile == null) realmFile = new File(DEFAULT_FILE_NAME);
if (!realmFile.exists())
if (realmFile == null) {
realmFile = new File(DEFAULT_FILE_NAME);
}
if (!realmFile.exists()) {
throw new WinstoneException(REALM_RESOURCES.getString("FileRealm.FileNotFound", realmFile.getPath()));
}
try (InputStream inFile = new FileInputStream(realmFile)) {
int count = 0;
Document doc = this.parseStreamToXML(inFile);
Node rootElm = doc.getDocumentElement();
for (int n = 0; n < rootElm.getChildNodes().getLength(); n++) {
Node child = rootElm.getChildNodes().item(n);

if ((child.getNodeType() == Node.ELEMENT_NODE)
&& (child.getNodeName().equals(ELEM_USER))) {
String userName = null;
String password = null;
String roleList = null;
// Loop through for attributes
for (int j = 0; j < child.getAttributes().getLength(); j++) {
Node thisAtt = child.getAttributes().item(j);
if (thisAtt.getNodeName().equals(ATT_USERNAME)) userName = thisAtt.getNodeValue();
else if (thisAtt.getNodeName().equals(ATT_PASSWORD)) password = thisAtt.getNodeValue();
else if (thisAtt.getNodeName().equals(ATT_ROLELIST)) roleList = thisAtt.getNodeValue();
if (thisAtt.getNodeName().equals(ATT_USERNAME)) {
userName = thisAtt.getNodeValue();
} else if (thisAtt.getNodeName().equals(ATT_PASSWORD)) {
password = thisAtt.getNodeValue();
} else if (thisAtt.getNodeName().equals(ATT_ROLELIST)) {
roleList = thisAtt.getNodeValue();
}
}

if ((userName == null) || (password == null) || (roleList == null))
if ((userName == null) || (password == null) || (roleList == null)) {

Check warning on line 84 in src/main/java/winstone/realm/FileRealm.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 54-84 are not covered by tests
Logger.log(Level.FINEST, REALM_RESOURCES, "FileRealm.SkippingUser", userName);
else {
} else {
// Parse the role list into an array and sort it
StringTokenizer st = new StringTokenizer(roleList, ",");
List<String> rl = new ArrayList<>();
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/winstone/AbstractWinstoneTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public class AbstractWinstoneTest {

@After
public void tearDown() {
if (winstone != null) winstone.shutdown();
if (winstone != null) {
winstone.shutdown();
}
}

public String makeRequest(String url) throws Exception {
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/winstone/TrustManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public void checkServerTrusted(X509Certificate[] xcs, String string) throws Cert
for (X509Certificate x509Certificate : xcs) {
System.out.println(
"certificate: " + x509Certificate.getIssuerX500Principal().getName());
if (cert.getSubjectX500Principal().equals(x509Certificate.getIssuerX500Principal())) return;
if (cert.getSubjectX500Principal().equals(x509Certificate.getIssuerX500Principal())) {
return;
}
}

throw new CertificateException("Untrusted certificate?");
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/winstone/accesslog/SimpleAccessLoggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public void testSimpleConnection() throws Exception {
for (int i = 0; i < 50; ++i) {
Thread.sleep(100);
text = Files.readString(logFile, StandardCharsets.UTF_8);
if (!"".equals(text)) break;
if (!"".equals(text)) {
break;
}
}
assertEquals(String.format("127.0.0.1 - - GET /examples/CountRequestsServlet HTTP/1.1 200%n"), text);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ public void destroy() {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (this.dumpRequestParams)
if (this.dumpRequestParams) {
for (Enumeration<String> paramNames = request.getParameterNames(); paramNames.hasMoreElements(); ) {
String name = paramNames.nextElement();
this.context.log("Request parameter: " + name + "=" + request.getParameter(name));
}
}

long startTime = System.currentTimeMillis();
chain.doFilter(request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ public class UnavailableServlet extends HttpServlet {
public void init() throws ServletException {
String errorTime = getServletConfig().getInitParameter("errorTime");
this.errorAtInit = ((errorTime == null) || errorTime.equals("init"));
if (this.errorAtInit) throw new UnavailableException("Error thrown deliberately during init");
if (this.errorAtInit) {
throw new UnavailableException("Error thrown deliberately during init");
}
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (!this.errorAtInit) throw new UnavailableException("Error thrown deliberately during get");
if (!this.errorAtInit) {
throw new UnavailableException("Error thrown deliberately during get");
}

try (Writer out = response.getWriter()) {
out.write("This should not be shown, because we've thrown unavailable exceptions");
Expand Down
11 changes: 8 additions & 3 deletions src/test/java/winstone/testCase/load/LoadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,16 @@ public void test() throws InterruptedException {

// Loop through in steps
for (int n = this.startThreads; n <= this.endThreads; n += this.stepSize) {
if (this.useKeepAlives) client = HttpClient.newHttpClient();
if (this.useKeepAlives) {
client = HttpClient.newHttpClient();
}

// Spawn the threads
int noOfSeconds = (int) this.stepPeriod / 1000;
List<LoadTestThread> threads = new ArrayList<>();
for (int m = 0; m < n; m++)
for (int m = 0; m < n; m++) {
threads.add(new LoadTestThread(this.url, this, this.resources, client, noOfSeconds - 1));
}

// Sleep for step period
Thread.sleep(this.stepPeriod + gracePeriod);
Expand All @@ -105,7 +108,9 @@ public void test() throws InterruptedException {
averageSuccessTime + "");

// Close threads
for (LoadTestThread thread : threads) thread.destroy();
for (LoadTestThread thread : threads) {
thread.destroy();
}

this.successTimeTotal = 0;
this.successCount = 0;
Expand Down
15 changes: 11 additions & 4 deletions src/test/java/winstone/testCase/load/LoadTestThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,19 @@ public LoadTestThread(
this.thread.start();

// Launch the next second's getter
if (delayedThreads > 0) this.next = new LoadTestThread(url, loadTest, resources, client, delayedThreads - 1);
if (delayedThreads > 0) {
this.next = new LoadTestThread(url, loadTest, resources, client, delayedThreads - 1);
}
}

@Override
public void run() {
if (this.delayBeforeStarting > 0)
if (this.delayBeforeStarting > 0) {
try {
Thread.sleep(this.delayBeforeStarting);
} catch (InterruptedException err) {
}
}

long startTime = System.currentTimeMillis();

Expand All @@ -68,7 +71,9 @@ public void run() {
HttpRequest.newBuilder(new URI(this.url)).GET().build();
HttpResponse<String> response = this.client.send(request, HttpResponse.BodyHandlers.ofString());
int responseCode = response.statusCode();
if (responseCode >= 400) throw new IOException("Failed with status " + responseCode);
if (responseCode >= 400) {
throw new IOException("Failed with status " + responseCode);
}
if (this.interrupted) {
return;
}
Expand All @@ -82,6 +87,8 @@ public void run() {
public void destroy() {
this.interrupted = true;
this.thread.interrupt();
if (this.next != null) this.next.destroy();
if (this.next != null) {
this.next.destroy();
}
}
}
Loading