|
2 | 2 | (:use clojure.test) |
3 | 3 | (:require [clojure.tools.logging.impl :as impl])) |
4 | 4 |
|
| 5 | + |
| 6 | +(deftest test-disabled-logger-factory |
| 7 | + (is (= "disabled" (impl/name impl/disabled-logger-factory))) |
| 8 | + (is (identical? impl/disabled-logger (impl/get-logger impl/disabled-logger-factory nil)))) |
| 9 | + |
| 10 | +(deftest test-disabled-logger |
| 11 | + (is (= false (impl/enabled? impl/disabled-logger :fatal)))) |
| 12 | + |
| 13 | +(defn blacklist-loader [blacklist loader] |
| 14 | + (proxy [ClassLoader] [] |
| 15 | + (loadClass [name] |
| 16 | + (if (contains? (set blacklist) name) |
| 17 | + (throw (ClassNotFoundException. name)) |
| 18 | + (.loadClass loader name))))) |
| 19 | + |
| 20 | +(deftest test-blacklist-loader |
| 21 | + (let [white "org.slf4j.Logger" |
| 22 | + black-1 "java.util.logging.Logger" |
| 23 | + black-2 "org.apache.log4j.Logger" |
| 24 | + orig-loader (.. Thread currentThread getContextClassLoader) |
| 25 | + blacklist-loader (blacklist-loader #{black-1 black-2} orig-loader)] |
| 26 | + (Class/forName white true orig-loader) |
| 27 | + (Class/forName black-1 true orig-loader) |
| 28 | + (Class/forName black-2 true orig-loader) |
| 29 | + (Class/forName white true blacklist-loader) |
| 30 | + (is (thrown? ClassNotFoundException |
| 31 | + (Class/forName black-1 true blacklist-loader))) |
| 32 | + (is (thrown? ClassNotFoundException |
| 33 | + (Class/forName black-2 true blacklist-loader))))) |
| 34 | + |
| 35 | +(defmacro with-blacklist-loader [forbidden-set & body] |
| 36 | + `(let [forbidden-set# ~forbidden-set |
| 37 | + orig-loader# (.. Thread currentThread getContextClassLoader) |
| 38 | + this-loader# (blacklist-loader forbidden-set# orig-loader#)] |
| 39 | + (try |
| 40 | + (.. Thread currentThread (setContextClassLoader this-loader#)) |
| 41 | + ~@body |
| 42 | + (finally |
| 43 | + (.. Thread currentThread (setContextClassLoader orig-loader#)))))) |
| 44 | + |
| 45 | +(defn thread-loader [] |
| 46 | + (.. Thread currentThread getContextClassLoader)) |
| 47 | + |
| 48 | +(deftest test-with-blacklist-loader |
| 49 | + (with-blacklist-loader #{"org.apache.log4j.Logger"} |
| 50 | + (Class/forName "org.slf4j.Logger" true (thread-loader)) |
| 51 | + (is (thrown? ClassNotFoundException |
| 52 | + (Class/forName "org.apache.log4j.Logger" true (thread-loader))))) |
| 53 | + (Class/forName "org.apache.log4j.Logger" true (thread-loader))) |
| 54 | + |
| 55 | + |
| 56 | +(deftest test-class-found? |
| 57 | + (is (= true (impl/class-found? "org.apache.log4j.Logger"))) |
| 58 | + (is (= true (impl/class-found? "org.slf4j.Logger"))) |
| 59 | + (with-blacklist-loader #{"org.apache.log4j.Logger"} |
| 60 | + (is (= true (impl/class-found? "org.slf4j.Logger"))) |
| 61 | + (is (= false (impl/class-found? "org.apache.log4j.Logger"))))) |
| 62 | + |
| 63 | + |
| 64 | +(deftest test-find-factory |
| 65 | + (testing "has all loggers" |
| 66 | + (are [classname] (impl/class-found? classname) |
| 67 | + "org.slf4j.Logger" |
| 68 | + "org.apache.commons.logging.Log" |
| 69 | + "org.apache.logging.log4j.Logger" |
| 70 | + "org.apache.log4j.Logger" |
| 71 | + "java.util.logging.Logger")) |
| 72 | + |
| 73 | + (testing "finds slf4j" |
| 74 | + (with-blacklist-loader [] |
| 75 | + (is (= "org.slf4j" (impl/name (impl/find-factory)))))) |
| 76 | + |
| 77 | + (testing "finds cl" |
| 78 | + (with-blacklist-loader ["org.slf4j.Logger"] |
| 79 | + (is (= "org.apache.commons.logging" (impl/name (impl/find-factory)))))) |
| 80 | + |
| 81 | + (testing "finds log4j2" |
| 82 | + (with-blacklist-loader ["org.slf4j.Logger" |
| 83 | + "org.apache.commons.logging.Log"] |
| 84 | + (is (= "org.apache.logging.log4j" (impl/name (impl/find-factory)))))) |
| 85 | + |
| 86 | + (testing "finds log4j" |
| 87 | + (with-blacklist-loader ["org.slf4j.Logger" |
| 88 | + "org.apache.commons.logging.Log" |
| 89 | + "org.apache.logging.log4j.Logger"] |
| 90 | + (is (= "org.apache.log4j" (impl/name (impl/find-factory)))))) |
| 91 | + |
| 92 | + (testing "finds jul" |
| 93 | + (with-blacklist-loader ["org.slf4j.Logger" |
| 94 | + "org.apache.commons.logging.Log" |
| 95 | + "org.apache.logging.log4j.Logger" |
| 96 | + "org.apache.log4j.Logger"] |
| 97 | + (is (= "java.util.logging" (impl/name (impl/find-factory)))))) |
| 98 | + |
| 99 | + (testing "finds none" |
| 100 | + (with-blacklist-loader ["org.slf4j.Logger" |
| 101 | + "org.apache.commons.logging.Log" |
| 102 | + "org.apache.logging.log4j.Logger" |
| 103 | + "org.apache.log4j.Logger" |
| 104 | + "java.util.logging.Logger"] |
| 105 | + (is (thrown? RuntimeException (impl/find-factory)))))) |
| 106 | + |
| 107 | + |
5 | 108 | (let [[factory level] (try |
6 | 109 | [(impl/log4j2-factory) (eval 'org.apache.logging.log4j.Level/FATAL)] |
7 | 110 | (catch UnsupportedClassVersionError _ |
|
0 commit comments