<commit_before>(ns codewars.runner.java-test
  (:require [clojure.test :refer :all]
            [codewars.core :refer [-main]]
            [cheshire.core :as json]
            [codewars.runner :refer [run]]))

(deftest java-basic
  (testing "-main can handle a very basic java solution and fixture"
    (with-in-str
      (json/generate-string
       {:language "java"
        :solution "class Foo {}"
        :fixture "class Bar {}"})
      (is (= org.junit.runner.Result (class (-main)))))))

(deftest java-solution-only
  (testing "-main can handle a java solution without a fixture"
    (with-in-str
      (json/generate-string
       {:language "java"
        :solution "public class FooFighters {public static int main() {return 1;}}"})
      (is (= 1 (-main))))))<commit_msg>Redirecting System.out to a string for testing purposes<commit_after>(ns codewars.runner.java-test
  (:require [clojure.test :refer :all]
            [codewars.core :refer [-main]]
            [cheshire.core :as json]
            [codewars.runner :refer [run]])
  (:import [org.apache.commons.io.output WriterOutputStream]
           [java.io PrintStream ByteArrayOutputStream]))

(defmacro with-java-out-str [& body]
  `(let [original-out# (PrintStream. (. System out))
         byte-out# (ByteArrayOutputStream.)]
     (-> byte-out# (PrintStream.) System/setOut)
     ~@body
     (System/setOut original-out#)
     (.toString byte-out#)))

(deftest java-basic
  (testing "-main can handle a very basic java solution and fixture"
    (with-in-str
      (json/generate-string
       {:language "java"
        :solution "class Foo {}"
        :fixture "class Bar {}"})
      (is (= org.junit.runner.Result (class (-main)))))))

(deftest java-solution-only
  (testing "-main can handle a java solution without a fixture"
    (with-in-str
      (json/generate-string
       {:language "java"
        :solution "public class FooFighters {public static int main() {return 1;}}"})
      (is (= 1 (-main))))))


(deftest java-solution-print
  (testing "-main can handle a java solution that prints to standard out"
    (with-in-str
      (json/generate-string
       {:language "java"
        :solution "public class Hello {public static void main() {System.out.print(\"Hellooo!\");}}"})
      (is (= "Hellooo!" (with-java-out-str (-main)))))))
{'fraction_non_alphanumeric': 0.15178956446744285, 'fraction_numerical': 0.0017248814144027599, 'mean_word_length': 2.7908496732026142, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4220443', 'n_tokens_mistral': 744, 'n_tokens_neox': 713, 'n_words': 203}