ONOS-4572 Adding checkstyle for Java tests in Buck

Change-Id: I3099046ba9db4ff59a9c4d0aa6bd0a73edc367d2
diff --git a/bucklets/onos.bucklet b/bucklets/onos.bucklet
index 6af6a32..e412fd4 100644
--- a/bucklets/onos.bucklet
+++ b/bucklets/onos.bucklet
@@ -18,6 +18,39 @@
     base_path = get_base_path()
     return ONOS_ARTIFACT_BASE + base_path.replace('/', '-') #TODO Unix-separator
 
+def checkstyle(
+    name,
+    srcs = None,
+    jar_target = None,
+    ):
+
+    if srcs and jar_target:
+        base = get_base_path()
+        files = '%s\n%s\n' % (name, base) + '\n'.join(['%s/%s' % (base, s) for s in srcs])
+
+        genrule(
+                name = name + '-checkstyle-files',
+                bash = "echo '%s' > $OUT" % files,
+                srcs = srcs,
+                out = 'checkstyle-files.txt',
+        )
+
+        sh_test(
+                name = name + '-checkstyle',
+                test = '//tools/build/conf:start-checkstyle',
+                deps = [ jar_target ],
+                args = [
+                    '$(location //tools/build/conf:checkstyle-jar)',
+                    '$(location :' + name + '-checkstyle-files)',
+                    '$(location //tools/build/conf:checkstyle-xml)',
+                    '$(location //tools/build/conf:suppressions-xml)',
+                    ],
+                test_rule_timeout_ms = 20000,
+                labels = [ 'checkstyle' ],
+        )
+    else:
+        print 'Not generating checkstyle rule for %s because there are no sources.' % name
+
 def osgi_jar(
     name = None,
     srcs = None,
@@ -109,34 +142,11 @@
   )
 
   ### Checkstyle
-  if srcs:
-    base = get_base_path()
-    files = '%s\n%s\n' % (name, base) + '\n'.join(['%s/%s' % (base, s) for s in srcs])
-
-    genrule(
+  checkstyle(
       name = name + '-checkstyle-files',
-      bash = "echo '%s' > $OUT" % files,
       srcs = srcs,
-      out = 'checkstyle-files.txt',
-    )
-
-    sh_test(
-      name = name + '-checkstyle',
-      test = '//tools/build/conf:start-checkstyle',
-      deps = [
-               ':'+ bare_jar_name,
-             ],
-      args = [
-                '$(location //tools/build/conf:checkstyle-jar)',
-                '$(location :' + name + '-checkstyle-files)',
-                '$(location //tools/build/conf:checkstyle-xml)',
-                '$(location //tools/build/conf:suppressions-xml)',
-             ],
-      test_rule_timeout_ms = 20000,
-      labels = [ 'checkstyle' ],
-    )
-  else:
-    print 'Not generating checkstyle rule for %s because there are no sources.' % name
+      jar_target = ':'+ bare_jar_name,
+  )
 
   # TODO add project config for intellij
   # project_config(
@@ -211,4 +221,10 @@
     visibility = visibility
   )
 
+  checkstyle(
+      name = name + '-tests',
+      srcs = test_srcs,
+      jar_target = ':' + name + '-tests',
+  )
+
   #FIXME need to run checkstyle on test sources