Niedzielski has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/224940

Change subject: Update checkstyle to match app
......................................................................

Update checkstyle to match app

- Copy the checkstyle configuration from the Wikipedia Android app.
- Fix violations.

Change-Id: If36772b98cd8868ccb0b71d2e916dc458af5c417
---
R config/checkstyle.xml
A config/quality.gradle
A config/suppressions.xml
M lib/build.gradle
M lib/src/main/java/org/mediawiki/api/json/Api.java
M lib/src/main/java/org/mediawiki/api/json/RequestBuilder.java
M lib/src/test/java/org/mediawiki/json/ApiConstructionTest.java
M lib/src/test/java/org/mediawiki/json/ApiTest.java
8 files changed, 91 insertions(+), 28 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/java-mwapi 
refs/changes/40/224940/1

diff --git a/checkstyle.xml b/config/checkstyle.xml
similarity index 60%
rename from checkstyle.xml
rename to config/checkstyle.xml
index d7245c3..ce64b35 100644
--- a/checkstyle.xml
+++ b/config/checkstyle.xml
@@ -31,11 +31,8 @@
 
     <!-- Checks that a package-info.java file exists for each package.     -->
     <!-- See http://checkstyle.sf.net/config_javadoc.html#JavadocPackage -->
-    <module name="JavadocPackage"/>
+    <!--<module name="JavadocPackage"/>-->
 
-    <!-- Checks that property files contain the same keys.         -->
-    <!-- See http://checkstyle.sf.net/config_misc.html#Translation -->
-    <module name="Translation"/>
 
     <!-- Checks for whitespace                               -->
     <!-- See http://checkstyle.sf.net/config_whitespace.html -->
@@ -50,14 +47,21 @@
        <property name="message" value="Line has trailing spaces."/>
     </module>
 
+    <module name="SuppressionFilter">
+        <property name="file" value="config/suppressions.xml"/>
+    </module>
+
+    <!-- Filter out Checkstyle warnings that have been suppressed with the 
@SuppressWarnings annotation -->
+    <module name="SuppressWarningsFilter" />
+
     <module name="TreeWalker">
 
         <!-- Checks for Javadoc comments.                     -->
         <!-- See http://checkstyle.sf.net/config_javadoc.html -->
-        <module name="JavadocMethod"/>
-        <module name="JavadocType"/>
-        <module name="JavadocVariable"/>
-        <module name="JavadocStyle"/>
+        <!--<module name="JavadocMethod"/>-->
+        <!--<module name="JavadocType"/>-->
+        <!--<module name="JavadocVariable"/>-->
+        <!--<module name="JavadocStyle"/>-->
 
 
         <!-- Checks for Naming Conventions.                  -->
@@ -69,7 +73,9 @@
         <module name="MethodName"/>
         <module name="PackageName"/>
         <module name="ParameterName"/>
-        <module name="StaticVariableName"/>
+        <module name="StaticVariableName">
+            <property name="format" value="^[A-Z0-9_]*$" />
+        </module>
         <module name="TypeName"/>
 
         <!-- Checks for imports                              -->
@@ -95,8 +101,13 @@
         <module name="OperatorWrap"/>
         <module name="ParenPad"/>
         <module name="TypecastParenPad"/>
-        <module name="WhitespaceAfter"/>
-        <module name="WhitespaceAround"/>
+        <module name="WhitespaceAfter">
+            <property name="tokens" value="COMMA, SEMI" /> <!-- No whitespace 
after typecasts -->
+        </module>
+        <module name="WhitespaceAround">
+            <property name="tokens"
+                value="ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, 
BSR_ASSIGN, BXOR, BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN, EQUAL, GE, GT, LAND, LE, 
LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_FOR, 
LITERAL_IF, LITERAL_RETURN, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE, 
LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN, NOT_EQUAL, PLUS, PLUS_ASSIGN, 
QUESTION, SL, SLIST, SL_ASSIGN, SR, SR_ASSIGN, STAR, STAR_ASSIGN, 
TYPE_EXTENSION_AND "/>
+        </module>
 
 
         <!-- Modifier Checks                                    -->
@@ -108,7 +119,9 @@
         <!-- Checks for blocks. You know, those {}'s         -->
         <!-- See http://checkstyle.sf.net/config_blocks.html -->
         <module name="AvoidNestedBlocks"/>
-        <module name="EmptyBlock"/>
+        <module name="EmptyBlock">
+            <property name="option" value="text"/>
+        </module>
         <module name="LeftCurly"/>
         <module name="NeedBraces"/>
         <module name="RightCurly"/>
@@ -116,15 +129,36 @@
 
         <!-- Checks for common coding problems               -->
         <!-- See http://checkstyle.sf.net/config_coding.html -->
-        <module name="AvoidInlineConditionals"/>
+        <!--<module name="AvoidInlineConditionals"/> Inline conditionals are 
fine!-->
         <module name="EmptyStatement"/>
         <module name="EqualsHashCode"/>
-        <module name="HiddenField"/>
+        <module name="HiddenField">
+            <property name="tokens" value="VARIABLE_DEF"/>
+            <property name="ignoreSetter" value="true"/>
+            <property name="ignoreConstructorParameter" value="true"/>
+        </module>
         <module name="IllegalInstantiation"/>
         <module name="InnerAssignment"/>
-        <module name="MagicNumber"/>
+        <module name="MagicNumber">
+            <!-- 0 is commonly used in length checks. 2 is used to make half.
+                 Typing ZERO and TWO sounds stupid -->
+            <property name="ignoreNumbers" value="-1, 0, 1, 2"/>
+            <property name="ignoreHashCodeMethod" value="true"/>
+            <property name="ignoreAnnotation" value="true"/>
+        </module>
         <module name="MissingSwitchDefault"/>
-        <module name="RedundantThrows"/>
+
+        <!-- For the Gradle checkstyle task. I kept getting something like 
this:-->
+        <!--<file 
name=".../android-wikipedia/wikipedia/src/main/java/org/wikipedia/Utils.java">-->
+            <!--<error line="515" column="71" severity="error" message="Unable 
to get class information for JSONException." 
source="com.puppycrawl.tools.checkstyle.checks.coding.RedundantThrowsCheck"/>-->
+        <!--</file>-->
+        <!-- Ideally, we would get the Exception classes included in the 
checkstyle specific classpath. -->
+        <module name="RedundantThrows">
+            <property name="allowUnchecked" value="true"/>
+            <property name="allowSubclasses" value="true"/>
+            <property name="suppressLoadErrors" value="true"/>
+        </module>
+
         <module name="SimplifyBooleanExpression"/>
         <module name="SimplifyBooleanReturn"/>
 
@@ -139,9 +173,12 @@
         <!-- Miscellaneous other checks.                   -->
         <!-- See http://checkstyle.sf.net/config_misc.html -->
         <module name="ArrayTypeStyle"/>
-        <module name="FinalParameters"/>
+        <!--<module name="FinalParameters"/>-->
         <module name="UpperEll"/>
 
+        <!-- Make the @SuppressWarnings annotations available to Checkstyle -->
+        <module name="SuppressWarningsHolder" />
+
     </module>
 
 </module>
diff --git a/config/quality.gradle b/config/quality.gradle
new file mode 100644
index 0000000..7ce5c84
--- /dev/null
+++ b/config/quality.gradle
@@ -0,0 +1,16 @@
+// see also 
http://stackoverflow.com/questions/17050654/get-android-gradle-plugin-checkstyle-working-together-command-line-usage
+
+apply plugin: 'checkstyle'
+
+check.dependsOn 'checkstyle'
+
+task checkstyle(type: Checkstyle) {
+    configFile file("${project.rootDir}/config/checkstyle.xml")
+    source 'src/main/java'
+    source 'src/androidTest/java'
+    source 'src/test/java'
+    include '**/*.java'
+    exclude '**/gen/**'
+
+    classpath = configurations.compile
+}
\ No newline at end of file
diff --git a/config/suppressions.xml b/config/suppressions.xml
new file mode 100644
index 0000000..5dd7715
--- /dev/null
+++ b/config/suppressions.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0"?>
+<!DOCTYPE suppressions PUBLIC
+    "-//Puppy Crawl//DTD Suppressions 1.1//EN"
+    "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd";>
+<suppressions>
+</suppressions>
diff --git a/lib/build.gradle b/lib/build.gradle
index 1fb10f9..a7ae426 100644
--- a/lib/build.gradle
+++ b/lib/build.gradle
@@ -1,5 +1,6 @@
 apply plugin: 'java'
-apply from: "../deploy.gradle"
+apply from: '../deploy.gradle'
+apply from: '../config/quality.gradle'
 
 group = 'org.mediawiki.api'
 archivesBaseName = 'json'
diff --git a/lib/src/main/java/org/mediawiki/api/json/Api.java 
b/lib/src/main/java/org/mediawiki/api/json/Api.java
index 6b10305..0dcff38 100644
--- a/lib/src/main/java/org/mediawiki/api/json/Api.java
+++ b/lib/src/main/java/org/mediawiki/api/json/Api.java
@@ -34,7 +34,7 @@
     /**
      * Any custom headers, if specified by the appropriate constructor
      */
-    private HashMap<String,String> customHeaders;
+    private HashMap<String, String> customHeaders;
 
     /**
      * Default API endpoint
@@ -78,7 +78,7 @@
      * @param userAgent Custom User-Agent to simplify identification of 
consuming application
      * @param customHeaders Any extra headers to send with each request, e.g. 
User-Agent.
      */
-    public Api(final String domain, final String userAgent, 
HashMap<String,String> customHeaders) {
+    public Api(final String domain, final String userAgent, HashMap<String, 
String> customHeaders) {
         this(domain, true, DEFAULT_ENDPOINT, customHeaders);
         this.customHeaders.put("User-Agent", userAgent);
     }
@@ -92,7 +92,7 @@
      * @param domain Domain name of the MediaWiki API to connect to
      * @param customHeaders Any extra headers to send with each request, e.g. 
User-Agent.
      */
-    public Api(final String domain, HashMap<String,String> customHeaders) {
+    public Api(final String domain, HashMap<String, String> customHeaders) {
         this(domain, true, DEFAULT_ENDPOINT, customHeaders);
     }
 
@@ -130,7 +130,7 @@
      * @param endpointPath Path to the api.php file. Require preceding slash.
      * @param customHeaders Any extra headers to send with each request, e.g. 
User-Agent.
      */
-    public Api(final String domain, final boolean useSecure, final String 
endpointPath, HashMap<String,String> customHeaders) {
+    public Api(final String domain, final boolean useSecure, final String 
endpointPath, HashMap<String, String> customHeaders) {
         String protocol;
         if (useSecure) {
             protocol = "https";
diff --git a/lib/src/main/java/org/mediawiki/api/json/RequestBuilder.java 
b/lib/src/main/java/org/mediawiki/api/json/RequestBuilder.java
index 26026dc..c64aea7 100644
--- a/lib/src/main/java/org/mediawiki/api/json/RequestBuilder.java
+++ b/lib/src/main/java/org/mediawiki/api/json/RequestBuilder.java
@@ -7,6 +7,9 @@
  * Fluent interface to easily build up an API request from params.
  */
 public class RequestBuilder {
+    private static final int INITIAL_CAPACITY = 14;
+    private static final float LOAD_FACTOR = .8f;
+
     /**
      * LinkedHashMap used to hold the parameters with which to make the API 
call.
      */
@@ -25,7 +28,7 @@
      */
     RequestBuilder(final Api apiToUse, final String action) {
         this.api = apiToUse;
-        params = new LinkedHashMap<String, String>(14, .8f, false);
+        params = new LinkedHashMap<String, String>(INITIAL_CAPACITY, 
LOAD_FACTOR, false);
         params.put("action", action); // put action first to match robots.txt 
whitelist of action=mobileview for app search indexing
         params.put("format", "json"); // Force everything to be JSON
     }
diff --git a/lib/src/test/java/org/mediawiki/json/ApiConstructionTest.java 
b/lib/src/test/java/org/mediawiki/json/ApiConstructionTest.java
index 3f5c442..5b9adba 100644
--- a/lib/src/test/java/org/mediawiki/json/ApiConstructionTest.java
+++ b/lib/src/test/java/org/mediawiki/json/ApiConstructionTest.java
@@ -31,7 +31,7 @@
                 "https://test.wikipedia.org/w/api.php";,
                 new Api("test.wikipedia.org", 
"java-mwapi-UA").getApiUrl().toString()
         );
-        HashMap<String,String> additionalHeaders = new 
java.util.HashMap<String,String>();
+        HashMap<String, String> additionalHeaders = new java.util.HashMap<>();
         additionalHeaders.put("X-Java-Mwapi-UnitTest", "java-mwapi-UA");
         assertEquals(
                 "https://test.wikipedia.org/w/api.php";,
diff --git a/lib/src/test/java/org/mediawiki/json/ApiTest.java 
b/lib/src/test/java/org/mediawiki/json/ApiTest.java
index 5f4c241..37f715e 100644
--- a/lib/src/test/java/org/mediawiki/json/ApiTest.java
+++ b/lib/src/test/java/org/mediawiki/json/ApiTest.java
@@ -2,6 +2,7 @@
 
 import static org.junit.Assert.*;
 
+import java.net.HttpURLConnection;
 import java.util.Map;
 import java.util.HashMap;
 import java.util.List;
@@ -15,7 +16,6 @@
  * Tests that actually hit the API to return something.
  */
 public class ApiTest {
-
     @Test
     public void testBasicPost() throws Exception {
         Api api = getApi();
@@ -80,7 +80,7 @@
     @Test(expected = IllegalArgumentException.class)
     public void testInvalidMethod() throws Exception {
         Api api = getApi();
-        api.setupRequest(404, null);
+        api.setupRequest(HttpURLConnection.HTTP_NOT_FOUND, null);
     }
 
     /**
@@ -149,7 +149,7 @@
     /**
      * Test to verify that accessing headers before asObject throws.
      */
-    @Test(expected=NullPointerException.class)
+    @Test(expected = NullPointerException.class)
     public void testGetHeadersOutOfOrder() throws Exception {
         Api api = getApi();
         String inputText = "Test String";
@@ -167,7 +167,7 @@
      * @return API with test-friendly construction
      */
     public Api getApi() {
-        HashMap<String,String> getApi = new HashMap<String,String>();
+        HashMap<String, String> getApi = new HashMap<>();
         getApi.put("X-Java-Mwapi-UnitTest", "java-mwapi-UA");
         return new Api("test.wikipedia.org", "java-mwapi-UA", getApi);
     }

-- 
To view, visit https://gerrit.wikimedia.org/r/224940
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If36772b98cd8868ccb0b71d2e916dc458af5c417
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/java-mwapi
Gerrit-Branch: master
Gerrit-Owner: Niedzielski <sniedziel...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to