/*
 * Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
 * This file is part of SIGAR.
 * 
 * SIGAR is free software; you can redistribute it and/or modify
 * it under the terms version 2 of the GNU General Public License as
 * published by the Free Software Foundation. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */

package org.hyperic.sigar.win32.test;

import java.util.ArrayList;

import org.hyperic.sigar.test.SigarTestCase;
import org.hyperic.sigar.win32.RegistryKey;

public class TestRegistryKey extends SigarTestCase {

    private static final boolean TEST_WRITE = false;
    
    public TestRegistryKey(String name) {
        super(name);
    }

    public void testRegistryRead() throws Exception {
        RegistryKey software =
            RegistryKey.LocalMachine.openSubKey("SOFTWARE");
        String[] keys = software.getSubKeyNames();
        assertTrue(keys.length > 0);
        software.close();
    }

    public void testHardwareValues() throws Exception {
        RegistryKey hw =
            RegistryKey.LocalMachine.openSubKey("HARDWARE\\DESCRIPTION\\System");
        try {
            ArrayList values = new ArrayList();
            hw.getMultiStringValue("SystemBiosVersion", values);
            assertGtZeroTrace("SystemBiosVersion.size()", values.size());
            traceln("SystemBiosVersion=" + values);
        } catch (Exception e) {}
        RegistryKey cpu0 = hw.openSubKey("CentralProcessor\\0");
        String cpu = cpu0.getStringValue("ProcessorNameString");
        assertLengthTrace("cpu0", cpu);
        cpu0.close();
        hw.close();
    }

    public void testSoftwareValues() throws Exception {
        RegistryKey ms =
            RegistryKey.LocalMachine.openSubKey("SOFTWARE\\Microsoft");

        RegistryKey msmq = null;
        try {
            msmq = ms.openSubKey("MSMQ\\Parameters");

        } catch (Exception e) { /*not installed - ok*/ }
        if (msmq != null) {
            traceln("MSMQ...");
            assertTrue(msmq.getSubKeyNames().length > 0);
            String build = msmq.getStringValue("CurrentBuild");
            assertLengthTrace("CurrentBuild", build);
            int id = msmq.getIntValue("SeqID");
            assertGtZeroTrace("SeqID", id);
        }
        ms.close();
    }

    //dont want to be writing to the registry
    public void testRegistryWrite() throws Exception {
        if (!TEST_WRITE) {
            return;
        }
        RegistryKey key = RegistryKey.LocalMachine.
            createSubKey("SOFTWARE\\Hyperic\\Test", "Hyperic Test");
        
        key.setStringValue("TestString", "Hello World");
        key.setIntValue("Test Int", 100);
            
        String[] astrNames = key.getValueNames();
        String strValue = key.getStringValue(astrNames[0]);
        //assertTrue(strValue.equals("Covalent Test"));

        int iValue = key.getIntValue(astrNames[1]);
        //assertTrue(iValue == 100);
        
        key = RegistryKey.LocalMachine.openSubKey("SOFTWARE\\Hyperic");
        astrNames = key.getSubKeyNames();

        // Clean up
        key.deleteSubKey("Test");
    }
}

@@ -2253,25 +2253,24 @@
 rameters%22);%0A
-%0A
         %7D ca
@@ -2594,24 +2594,24 @@
 e(%22SeqID%22);%0A
-
             
@@ -2638,24 +2638,433 @@
 eqID%22, id);%0A
+            msmq.close();%0A        %7D%0A%0A        RegistryKey sql = null;%0A        try %7B%0A            sql = ms.openSubKey(%22Microsoft SQL Server%5C%5CMSSQL.1%5C%5CSetup%22);%0A        %7D catch (Exception e) %7B /*not installed - ok*/ %7D%0A        if (sql != null) %7B%0A            traceln(%22MsSQL...%22);%0A            String edition = sql.getStringValue(%22Edition%22);%0A            assertLengthTrace(%22Edition%22, edition);%0A            sql.close();%0A
         %7D%0A  

add another test for software values