/*
 * Certain versions of software and/or documents ("Material") accessible here may contain branding from
 * Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company.  As of September 1, 2017,
 * the Material is now offered by Micro Focus, a separately owned and operated company.  Any reference to the HP
 * and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE
 * marks are the property of their respective owners.
 * __________________________________________________________________
 * MIT License
 *
 * (c) Copyright 2012-2018 Micro Focus or one of its affiliates.
 *
 * The only warranties for products and services of Micro Focus and its affiliates
 * and licensors ("Micro Focus") are set forth in the express warranty statements
 * accompanying such products and services. Nothing herein should be construed as
 * constituting an additional warranty. Micro Focus shall not be liable for technical
 * or editorial errors or omissions contained herein.
 * The information contained herein is subject to change without notice.
 * ___________________________________________________________________
 */

package com.microfocus.application.automation.tools.common;

import org.junit.*;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class OperatingSystemTest {
    private static String os;
    public static void initializeOperatingSystemOs(final String os) throws NoSuchFieldException, IllegalAccessException {
        changeStaticFinalField(os, "OS");

        if (os.toLowerCase().contains("windows")) {
            changeBooleanStaticFinalField(true, "IS_WINDOWS");
            changeBooleanStaticFinalField(false, "IS_MAC");
            changeBooleanStaticFinalField(false, "IS_LINUX");
        } else if (os.toLowerCase().contains("linux")) {
            changeBooleanStaticFinalField(false, "IS_WINDOWS");
            changeBooleanStaticFinalField(false, "IS_MAC");
            changeBooleanStaticFinalField(true, "IS_LINUX");
        } else if (os.toLowerCase().contains("mac")) {
            changeBooleanStaticFinalField(false, "IS_WINDOWS");
            changeBooleanStaticFinalField(true, "IS_MAC");
            changeBooleanStaticFinalField(false, "IS_LINUX");
        }
    }

    private static void changeStaticFinalField(String value, String declaredField)
            throws NoSuchFieldException, IllegalAccessException {
        Field field = OperatingSystem.class.getDeclaredField(declaredField);
        field.setAccessible(true);
        Field modifiers = Field.class.getDeclaredField("modifiers");
        modifiers.setAccessible(true);
        modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
        field.set(null, value.toLowerCase());
    }

    private static void changeBooleanStaticFinalField(boolean value, String declaredField)
            throws NoSuchFieldException, IllegalAccessException {
        Field field = OperatingSystem.class.getDeclaredField(declaredField);
        field.setAccessible(true);
        Field modifiers = Field.class.getDeclaredField("modifiers");
        modifiers.setAccessible(true);
        modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
        field.set(null, value);
    }

    @BeforeClass
    public static void setup() throws Exception {
        os = System.getProperty("os.name");
    }


    @AfterClass
    public static void tearDown() throws Exception {
        initializeOperatingSystemOs(os);
    }


    @Test
    public void equalsCurrentOs_windows() throws NoSuchFieldException, IllegalAccessException {
        initializeOperatingSystemOs("Windows 7");
        Assert.assertTrue(OperatingSystem.WINDOWS.equalsCurrentOs());
    }

    @Test
    public void equalsCurrentOs_linux() throws NoSuchFieldException, IllegalAccessException {
        initializeOperatingSystemOs("Linux");
        Assert.assertTrue(OperatingSystem.LINUX.equalsCurrentOs());
    }

    @Test
    public void equalsCurrentOs_mac() throws NoSuchFieldException, IllegalAccessException {
        initializeOperatingSystemOs("Mac OS X");
        Assert.assertTrue(OperatingSystem.MAC.equalsCurrentOs());
    }

    @Test
    public void equalsCurrentOs_invalidOsReturnsFalse() throws NoSuchFieldException, IllegalAccessException {
        initializeOperatingSystemOs("Invalid OS");
        Assert.assertFalse(OperatingSystem.WINDOWS.equalsCurrentOs());
    }
}
@@ -1259,17 +1259,104 @@
 g.junit.
-*
+AfterClass;%0Aimport org.junit.Assert;%0Aimport org.junit.BeforeClass;%0Aimport org.junit.Test
 ;%0A%0Aimpor
@@ -1483,16 +1483,17 @@
 ing os;%0A
+%0A
     publ
@@ -1705,38 +1705,38 @@
 ) %7B%0A            
-change
+setAll
 BooleanStaticFin
@@ -1746,89 +1746,87 @@
 ield
+s
 (true, 
-%22IS_WINDOWS%22);%0A            changeBooleanStaticFinalField(false, %22IS_MAC%22);
+false, false);%0A        %7D else if (os.toLowerCase().contains(%22linux%22)) %7B
 %0A   
@@ -1826,38 +1826,38 @@
 ) %7B%0A            
-change
+setAll
 BooleanStaticFin
@@ -1859,32 +1859,33 @@
 icFinalField
+s
 (false, 
 %22IS_LINUX%22);
@@ -1868,34 +1868,35 @@
 elds(false, 
-%22IS_LINUX%22
+false, true
 );%0A        %7D
@@ -1924,37 +1924,35 @@
 ase().contains(%22
-linux
+mac
 %22)) %7B%0A          
@@ -1945,38 +1945,38 @@
 ) %7B%0A            
-change
+setAll
 BooleanStaticFin
@@ -1978,32 +1978,33 @@
 icFinalField
+s
 (false, 
 %22IS_WINDOWS%22
@@ -1995,41 +1995,57 @@
 se, 
-%22IS_WINDOWS%22);%0A            change
+true, false);%0A        %7D else %7B%0A            setAll
 Bool
@@ -2059,32 +2059,33 @@
 icFinalField
+s
 (false, 
 %22IS_MAC%22);%0A 
@@ -2072,24 +2072,28 @@
 (false, 
-%22IS_MAC%22
+false, false
 );%0A     
@@ -2099,120 +2099,189 @@
     
+%7D%0A
     
-changeBooleanStaticFinalField(true, %22IS_LINUX%22);%0A        %7D else if (os.toLowerCase().contains(%22mac%22)) %7B%0A    
+%7D%0A%0A    private static void setAllBooleanStaticFinalFields(boolean isWindows, boolean isMac, boolean isLinux)%0A            throws NoSuchFieldException, IllegalAccessException %7B%0A
     
@@ -2310,29 +2310,33 @@
 cFinalField(
-false
+isWindows
 , %22IS_WINDOW
@@ -2336,28 +2336,24 @@
 _WINDOWS%22);%0A
-    
         chan
@@ -2378,20 +2378,21 @@
 alField(
-true
+isMac
 , %22IS_MA
@@ -2392,28 +2392,24 @@
  %22IS_MAC%22);%0A
-    
         chan
@@ -2434,21 +2434,23 @@
 alField(
-false
+isLinux
 , %22IS_LI
@@ -2456,26 +2456,16 @@
 INUX%22);%0A
-        %7D%0A
     %7D%0A%0A 
@@ -3494,33 +3494,16 @@
  setup()
- throws Exception
  %7B%0A     

Fix flake test with OperatingSystem