001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 2017, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.util;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertFalse;
018import static org.junit.Assert.assertTrue;
019
020import org.junit.After;
021import org.junit.Test;
022import org.mockito.Mock;
023
024/**
025 * @author Patrick Reinhart
026 */
027public class EnvUtilTest {
028    @Mock
029    private String savedVersion = System.getProperty("java.version");
030
031    @After
032    public void tearDown() {
033        System.setProperty("java.version", savedVersion);
034    }
035
036    @Test
037    public void jdkVersion() {
038        assertEquals(4, EnvUtil.getJDKVersion("1.4.xx"));
039        assertEquals(5, EnvUtil.getJDKVersion("1.5"));
040        assertEquals(5, EnvUtil.getJDKVersion("1.5.xx"));
041        assertEquals(5, EnvUtil.getJDKVersion("1.5AA"));
042        assertEquals(9, EnvUtil.getJDKVersion("9EA"));
043        assertEquals(9, EnvUtil.getJDKVersion("9.0.1"));
044        assertEquals(18, EnvUtil.getJDKVersion("18.3+xx"));
045    }
046
047    @Test
048    public void testJava1_4() {
049        System.setProperty("java.version", "1.4.xx");
050
051        assertFalse(EnvUtil.isJDK5());
052        assertFalse(EnvUtil.isJDK6OrHigher());
053        assertFalse(EnvUtil.isJDK7OrHigher());
054    }
055
056    @Test
057    public void testJava1_5() {
058        System.setProperty("java.version", "1.5");
059
060        assertTrue(EnvUtil.isJDK5());
061        assertFalse(EnvUtil.isJDK6OrHigher());
062        assertFalse(EnvUtil.isJDK7OrHigher());
063    }
064
065    @Test
066    public void testJava1_5_x() {
067        System.setProperty("java.version", "1.5.xx");
068
069        assertTrue(EnvUtil.isJDK5());
070        assertFalse(EnvUtil.isJDK6OrHigher());
071        assertFalse(EnvUtil.isJDK7OrHigher());
072    }
073
074    @Test
075    public void testJava1_6() {
076        System.setProperty("java.version", "1.6.xx");
077
078        assertTrue(EnvUtil.isJDK5());
079        assertTrue(EnvUtil.isJDK6OrHigher());
080        assertFalse(EnvUtil.isJDK7OrHigher());
081    }
082
083    @Test
084    public void testJava1_7() {
085        System.setProperty("java.version", "1.7.xx");
086
087        assertTrue(EnvUtil.isJDK5());
088        assertTrue(EnvUtil.isJDK6OrHigher());
089        assertTrue(EnvUtil.isJDK7OrHigher());
090    }
091
092    @Test
093    public void testJava1_8() {
094        System.setProperty("java.version", "1.8.xx");
095
096        assertTrue(EnvUtil.isJDK5());
097        assertTrue(EnvUtil.isJDK6OrHigher());
098        assertTrue(EnvUtil.isJDK7OrHigher());
099    }
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}