View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 2017, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.util;
15  
16  import org.junit.jupiter.api.AfterEach;
17  import org.junit.jupiter.api.Assertions;
18  import org.junit.jupiter.api.Test;
19  import org.mockito.Mock;
20  
21  /**
22   * @author Patrick Reinhart
23   */
24  public class EnvUtilTest {
25      @Mock
26      private String savedVersion = System.getProperty("java.version");
27  
28      @AfterEach
29      public void tearDown() {
30          System.setProperty("java.version", savedVersion);
31      }
32  
33      @Test
34      public void jdkVersion() {
35          Assertions.assertEquals(4, EnvUtil.getJDKVersion("1.4.xx"));
36          Assertions.assertEquals(5, EnvUtil.getJDKVersion("1.5"));
37          Assertions.assertEquals(5, EnvUtil.getJDKVersion("1.5.xx"));
38          Assertions.assertEquals(5, EnvUtil.getJDKVersion("1.5AA"));
39          Assertions.assertEquals(9, EnvUtil.getJDKVersion("9EA"));
40          Assertions.assertEquals(9, EnvUtil.getJDKVersion("9.0.1"));
41          Assertions.assertEquals(18, EnvUtil.getJDKVersion("18.3+xx"));
42          Assertions.assertEquals(21, EnvUtil.getJDKVersion("21.0.1"));
43      }
44  
45  
46  
47      @Test
48      public void testJava1_4() {
49          System.setProperty("java.version", "1.4.xx");
50  
51          Assertions.assertFalse(EnvUtil.isJDK5());
52          Assertions.assertFalse(EnvUtil.isJDK6OrHigher());
53          Assertions.assertFalse(EnvUtil.isJDK7OrHigher());
54      }
55  
56      @Test
57      public void testJava1_5() {
58          System.setProperty("java.version", "1.5");
59  
60          Assertions.assertTrue(EnvUtil.isJDK5());
61          Assertions.assertFalse(EnvUtil.isJDK6OrHigher());
62          Assertions.assertFalse(EnvUtil.isJDK7OrHigher());
63      }
64  
65      @Test
66      public void testJava1_5_x() {
67          System.setProperty("java.version", "1.5.xx");
68  
69          Assertions.assertTrue(EnvUtil.isJDK5());
70          Assertions.assertFalse(EnvUtil.isJDK6OrHigher());
71          Assertions.assertFalse(EnvUtil.isJDK7OrHigher());
72      }
73  
74      @Test
75      public void testJava1_6() {
76          System.setProperty("java.version", "1.6.xx");
77  
78          Assertions.assertTrue(EnvUtil.isJDK5());
79          Assertions.assertTrue(EnvUtil.isJDK6OrHigher());
80          Assertions.assertFalse(EnvUtil.isJDK7OrHigher());
81      }
82  
83      @Test
84      public void testJava1_7() {
85          System.setProperty("java.version", "1.7.xx");
86  
87          Assertions.assertTrue(EnvUtil.isJDK5());
88          Assertions.assertTrue(EnvUtil.isJDK6OrHigher());
89          Assertions.assertTrue(EnvUtil.isJDK7OrHigher());
90      }
91  
92      @Test
93      public void testJava1_8() {
94          System.setProperty("java.version", "1.8.xx");
95  
96          Assertions.assertTrue(EnvUtil.isJDK5());
97          Assertions.assertTrue(EnvUtil.isJDK6OrHigher());
98          Assertions.assertTrue(EnvUtil.isJDK7OrHigher());
99      }
100 
101     @Test
102     public void testJava9() {
103         System.setProperty("java.version", "9");
104 
105         Assertions.assertTrue(EnvUtil.isJDK5());
106         Assertions.assertTrue(EnvUtil.isJDK6OrHigher());
107         Assertions.assertTrue(EnvUtil.isJDK7OrHigher());
108     }
109 
110     @Test
111     public void testJava9_1() {
112         System.setProperty("java.version", "9.xx");
113 
114         Assertions.assertTrue(EnvUtil.isJDK5());
115         Assertions.assertTrue(EnvUtil.isJDK6OrHigher());
116         Assertions.assertTrue(EnvUtil.isJDK7OrHigher());
117     }
118 
119     @Test
120     public void testJava18_3() {
121         System.setProperty("java.version", "18.3+xx");
122         Assertions.assertEquals(18, EnvUtil.getJDKVersion("18.3+xx"));
123         Assertions.assertTrue(EnvUtil.isJDK5());
124         Assertions.assertTrue(EnvUtil.isJDK6OrHigher());
125         Assertions.assertTrue(EnvUtil.isJDK7OrHigher());
126     }
127 }