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 static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertFalse;
18  import static org.junit.Assert.assertTrue;
19  
20  import org.junit.After;
21  import org.junit.Test;
22  import org.mockito.Mock;
23  
24  /**
25   * @author Patrick Reinhart
26   */
27  public class EnvUtilTest {
28      @Mock
29      private String savedVersion = System.getProperty("java.version");
30  
31      @After
32      public void tearDown() {
33          System.setProperty("java.version", savedVersion);
34      }
35  
36      @Test
37      public void jdkVersion() {
38          assertEquals(4, EnvUtil.getJDKVersion("1.4.xx"));
39          assertEquals(5, EnvUtil.getJDKVersion("1.5"));
40          assertEquals(5, EnvUtil.getJDKVersion("1.5.xx"));
41          assertEquals(5, EnvUtil.getJDKVersion("1.5AA"));
42          assertEquals(9, EnvUtil.getJDKVersion("9EA"));
43          assertEquals(9, EnvUtil.getJDKVersion("9.0.1"));
44          assertEquals(18, EnvUtil.getJDKVersion("18.3+xx"));
45      }
46  
47      @Test
48      public void testJava1_4() {
49          System.setProperty("java.version", "1.4.xx");
50  
51          assertFalse(EnvUtil.isJDK5());
52          assertFalse(EnvUtil.isJDK6OrHigher());
53          assertFalse(EnvUtil.isJDK7OrHigher());
54      }
55  
56      @Test
57      public void testJava1_5() {
58          System.setProperty("java.version", "1.5");
59  
60          assertTrue(EnvUtil.isJDK5());
61          assertFalse(EnvUtil.isJDK6OrHigher());
62          assertFalse(EnvUtil.isJDK7OrHigher());
63      }
64  
65      @Test
66      public void testJava1_5_x() {
67          System.setProperty("java.version", "1.5.xx");
68  
69          assertTrue(EnvUtil.isJDK5());
70          assertFalse(EnvUtil.isJDK6OrHigher());
71          assertFalse(EnvUtil.isJDK7OrHigher());
72      }
73  
74      @Test
75      public void testJava1_6() {
76          System.setProperty("java.version", "1.6.xx");
77  
78          assertTrue(EnvUtil.isJDK5());
79          assertTrue(EnvUtil.isJDK6OrHigher());
80          assertFalse(EnvUtil.isJDK7OrHigher());
81      }
82  
83      @Test
84      public void testJava1_7() {
85          System.setProperty("java.version", "1.7.xx");
86  
87          assertTrue(EnvUtil.isJDK5());
88          assertTrue(EnvUtil.isJDK6OrHigher());
89          assertTrue(EnvUtil.isJDK7OrHigher());
90      }
91  
92      @Test
93      public void testJava1_8() {
94          System.setProperty("java.version", "1.8.xx");
95  
96          assertTrue(EnvUtil.isJDK5());
97          assertTrue(EnvUtil.isJDK6OrHigher());
98          assertTrue(EnvUtil.isJDK7OrHigher());
99      }
100 
101     @Test
102     public void testJava9() {
103         System.setProperty("java.version", "9");
104 
105         assertTrue(EnvUtil.isJDK5());
106         assertTrue(EnvUtil.isJDK6OrHigher());
107         assertTrue(EnvUtil.isJDK7OrHigher());
108     }
109 
110     @Test
111     public void testJava9_1() {
112         System.setProperty("java.version", "9.xx");
113 
114         assertTrue(EnvUtil.isJDK5());
115         assertTrue(EnvUtil.isJDK6OrHigher());
116         assertTrue(EnvUtil.isJDK7OrHigher());
117     }
118 
119     @Test
120     public void testJava18_3() {
121         System.setProperty("java.version", "18.3+xx");
122         assertEquals(18, EnvUtil.getJDKVersion("18.3+xx"));
123         assertTrue(EnvUtil.isJDK5());
124         assertTrue(EnvUtil.isJDK6OrHigher());
125         assertTrue(EnvUtil.isJDK7OrHigher());
126     }
127 }