# part of JPype1; author Martin K. Scherer; 2014
import unittest2
import mock

from jpype._jvmfinder import *
from jpype._linux import *
from jpype._darwin import *

import sys

class JVMFinderTest(unittest2.TestCase):
    """
    test some methods to obtain a jvm.
    TODO: add windows (need to mock registry)
    """

    def test_find_libjvm(self):
        """
        test JVMFinder.find_libjvm does not return broken jvm implementation.
        """
        walk_fake = [('jre', ('lib',), ()),
                     ('jre/lib', ('amd64',), ()),
                     ('jre/lib/amd64',
                      ('cacao', 'jamvm', 'server'), ()),
                     ('jre/lib/amd64/cacao',
                      ('',), ('libjvm.so',)),
                     ('jre/lib/amd64/jamvm',
                      ('',), ('libjvm.so',)),
                     ('jre/lib/amd64/server',
                      ('',), ('libjvm.so',)),
                     ]
        with mock.patch('os.walk') as mockwalk:
            # contains broken and working jvms
            mockwalk.return_value = walk_fake

            finder = LinuxJVMFinder()
            p = finder.find_libjvm('arbitrary java home')
            self.assertEqual(
                p, 'jre/lib/amd64/server/libjvm.so', 'wrong jvm returned')

        with mock.patch('os.walk') as mockwalk:
            # contains only broken jvms, since server impl is removed
            walk_fake[-1] = ((), (), (),)
            mockwalk.return_value = walk_fake

            finder = LinuxJVMFinder()
            with self.assertRaises(JVMNotSupportedException) as context:
                finder.find_libjvm('arbitrary java home')

    @mock.patch('os.walk')
    @mock.patch('os.path.exists')
    @mock.patch('os.path.realpath')
    def test_get_from_bin(self, mock_real_path, mock_path_exists, mock_os_walk):
        """
        test _get_from_bin method (used in linux and darwin)
        '/usr/bin/java' => some jre/jdk path
        """
        java_path = '/usr/lib/jvm/java-6-openjdk-amd64/bin/java'

        mock_os_walk.return_value = [
            ('/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/server', ('',), ('libjvm.so',))]
        mock_path_exists.return_value = True
        mock_real_path.return_value = '/usr/lib/jvm/java-6-openjdk-amd64/bin/java'

        finder = LinuxJVMFinder()
        p = finder._get_from_bin()

        self.assertEqual(
            p, '/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/server/libjvm.so')

    @unittest2.skipIf(sys.version_info[:2] == (2, 6), "skip on py26")
    @mock.patch('platform.mac_ver')
    def test_javahome_binary_py27(self, mock_mac_ver):
        # this version has java_home binary
        mock_mac_ver.return_value = ('10.6.8', '', '')

        expected = '/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home\n'

        finder = DarwinJVMFinder()

        # fake check_output
        with mock.patch('subprocess.check_output') as mock_checkoutput:
            mock_checkoutput.return_value = expected
            p = finder._javahome_binary()

            self.assertEquals(p, expected.strip())

        # this version has no java_home binary
        mock_mac_ver.return_value = ('10.5', '', '')
        p = finder._javahome_binary()

        self.assertEquals(p, None)
        
    @unittest2.skipIf(sys.version_info[:2] == (2, 7), "skipped on py27")
    @mock.patch('platform.mac_ver')
    def test_javahome_binary_py27(self, mock_mac_ver):
        # this version has java_home binary
        mock_mac_ver.return_value = ('10.6.8', '', '')

        expected = '/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home\n'

        finder = DarwinJVMFinder()

        # fake check_output
        with mock.patch('subprocess.Popen') as mock_popen:
            mock_popen.return_value = expected
            p = finder._javahome_binary()

            self.assertEquals(p, expected.strip())

        # this version has no java_home binary
        mock_mac_ver.return_value = ('10.5', '', '')
        p = finder._javahome_binary()

        self.assertEquals(p, None)

@@ -58,16 +58,28 @@
 nittest2
+ as unittest
 %0Aimport 
@@ -211,17 +211,16 @@
 unittest
-2
 .TestCas
@@ -2488,33 +2488,32 @@
 )%0A%0A    @unittest
-2
 .skipIf(sys.vers
@@ -3312,17 +3312,16 @@
 unittest
-2
 .skipIf(
@@ -3553,33 +3553,32 @@
 0.6.8', '', '')%0A
-%0A
         expected
@@ -3773,16 +3773,119 @@
 _popen:%0A
+            class proc:%0A                def communicate(self):%0A                    return (expected, )%0A
         
@@ -3910,32 +3910,43 @@
 urn_value = 
-expected
+proc()%0A            
 %0A           
@@ -4000,32 +4000,40 @@
 f.assertEquals(p
+.strip()
 , expected.strip
@@ -4191,28 +4191,75 @@
  self.assertEquals(p, None)%0A
+%0Aif __name__ == '__main__':%0A    unittest.main()

fix py26 related test.