From 44ac64c99816b4dcca03f73419aa1d6d8ad79c3e Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 30 Aug 2017 23:20:28 +0100 Subject: [PATCH] Added ability to specify context-params in the project file. --- src/leiningen/ring/war.clj | 45 ++++++++++++++++++++++++++---------- test/leiningen/test/ring.clj | 2 +- test/leiningen/test/war.clj | 21 +++++++++++++++++ 3 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 test/leiningen/test/war.clj diff --git a/src/leiningen/ring/war.clj b/src/leiningen/ring/war.clj index 7568bba..f4c45e9 100644 --- a/src/leiningen/ring/war.clj +++ b/src/leiningen/ring/war.clj @@ -100,24 +100,45 @@ (def default-servlet-version "2.5") +(defn make-context-params + "Take this `params-map` and construct from it a sequence of + s-expression representations of context-param elements." + [params-map] + (if + (map? params-map) + (map + #(vector + :context-param + [:param-name (name %)] + [:param-value (params-map %)]) + (keys params-map)))) + +(defn make-web-sexpr + "Take this `project` and construct from it an s-expression + representation of the complete web.xml file" + [project] + (let [ring-options (:ring project)] + [:web-app + (get web-app-attrs + (get-in project [:ring :servlet-version] default-servlet-version) + {}) + (make-context-params (:context-params ring-options)) + [:listener + [:listener-class (listener-class project)]] + [:servlet + [:servlet-name (servlet-name project)] + [:servlet-class (servlet-class project)]] + [:servlet-mapping + [:servlet-name (servlet-name project)] + [:url-pattern (url-pattern project)]]])) + (defn make-web-xml [project] (let [ring-options (:ring project)] (if (contains? ring-options :web-xml) (slurp (:web-xml ring-options)) (indent-str (sexp-as-element - [:web-app - (get web-app-attrs - (get-in project [:ring :servlet-version] default-servlet-version) - {}) - [:listener - [:listener-class (listener-class project)]] - [:servlet - [:servlet-name (servlet-name project)] - [:servlet-class (servlet-class project)]] - [:servlet-mapping - [:servlet-name (servlet-name project)] - [:url-pattern (url-pattern project)]]]))))) + (make-web-sexpr project)))))) (defn generate-handler [project handler-sym] (if (get-in project [:ring :servlet-path-info?] true) diff --git a/test/leiningen/test/ring.clj b/test/leiningen/test/ring.clj index f2c12b8..ccd01fe 100644 --- a/test/leiningen/test/ring.clj +++ b/test/leiningen/test/ring.clj @@ -3,4 +3,4 @@ (:use clojure.test)) (deftest replace-me ;; FIXME: write - (is false "No tests have been written.")) + (is false "No tests have been written.")) diff --git a/test/leiningen/test/war.clj b/test/leiningen/test/war.clj new file mode 100644 index 0000000..afea116 --- /dev/null +++ b/test/leiningen/test/war.clj @@ -0,0 +1,21 @@ +(ns leiningen.test.war + (:require [clojure.test :refer :all] + [leiningen.ring.war :refer :all])) + +(deftest test-generate-war + (testing "Generation of context params elements" + (is (empty? (make-context-params {})) + "If no context params are specified, none should be generated") + (is (empty? (make-context-params "froboz")) + "If the argument to make-context-params is not a map, no context params should be generated") + (is (= 1 (count (make-context-params {:foo "bar"}))) + "If one context param is specified, one should be generated") + (is (= '([:context-param [:param-name "foo"][:param-value "bar"]]) + (make-context-params {:foo "bar"}))) + (is (= '([:context-param [:param-name "foo"][:param-value 123]]) + (make-context-params {:foo 123}))) + (is (= 2 (count (make-context-params {:foo "bar" :froboz "baz"}))) + "If two context params are specified, two should be generated")) + (testing "Generation of war file" + (is (= "" (make-web-xml {:ring {:web-xml "/dev/null"}})) + "If a file path is specified as :web-xml, the content of that file should be returned")))