JniClassTest.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-04-07
  6. * Changed: 2023-04-07
  7. *
  8. * */
  9. #include <gmock/gmock.h>
  10. #include <gtest/gtest.h>
  11. #include <ls-std-core-jni-test.hpp>
  12. #include <ls-std/ls-std-core-jni.hpp>
  13. #include <ls-std/ls-std-core.hpp>
  14. #include <memory>
  15. using ls::std::core::IllegalArgumentException;
  16. using ls::std::core::JniClass;
  17. using ls::std::core::JniClassParameter;
  18. using ls::std::core::NullPointerException;
  19. using std::make_shared;
  20. using std::shared_ptr;
  21. using std::string;
  22. using test::core::jni::MockJniApi;
  23. using testing::AtLeast;
  24. using testing::Return;
  25. using testing::Test;
  26. namespace
  27. {
  28. class JniClassTest : public Test
  29. {
  30. public:
  31. JniClassTest() = default;
  32. ~JniClassTest() override = default;
  33. JniClass createJniClass(const string &_classPath)
  34. {
  35. shared_ptr<JniClassParameter> parameter = make_shared<JniClassParameter>();
  36. this->jniApi = make_shared<MockJniApi>();
  37. parameter->setJniApi(this->jniApi);
  38. shared_ptr<JNIEnv> environment = make_shared<JNIEnv>();
  39. parameter->setJavaEnvironment(environment.get());
  40. return JniClass{parameter, _classPath};
  41. }
  42. shared_ptr<MockJniApi> jniApi{};
  43. };
  44. TEST_F(JniClassTest, constructor_no_parameter_reference)
  45. {
  46. EXPECT_THROW(
  47. {
  48. try
  49. {
  50. JniClass javaClass(nullptr, "java.utils.String");
  51. }
  52. catch (const IllegalArgumentException &_exception)
  53. {
  54. throw;
  55. }
  56. },
  57. IllegalArgumentException);
  58. }
  59. TEST_F(JniClassTest, constructor_empty_path)
  60. {
  61. EXPECT_THROW(
  62. {
  63. try
  64. {
  65. JniClass javaClass(make_shared<JniClassParameter>(), "");
  66. }
  67. catch (const IllegalArgumentException &_exception)
  68. {
  69. throw;
  70. }
  71. },
  72. IllegalArgumentException);
  73. }
  74. TEST_F(JniClassTest, constructor_no_java_environment_reference)
  75. {
  76. EXPECT_THROW(
  77. {
  78. try
  79. {
  80. JniClass javaClass(make_shared<JniClassParameter>(), "java.utils.String");
  81. }
  82. catch (const IllegalArgumentException &_exception)
  83. {
  84. throw;
  85. }
  86. },
  87. IllegalArgumentException);
  88. }
  89. TEST_F(JniClassTest, hasMethod)
  90. {
  91. string classPath = "java.utils.String";
  92. JniClass javaClass = this->createJniClass(classPath);
  93. ASSERT_FALSE(javaClass.hasMethod("getDay"));
  94. }
  95. TEST_F(JniClassTest, load)
  96. {
  97. string classPath = "java.utils.String";
  98. JniClass javaClass = this->createJniClass(classPath);
  99. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  100. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  101. ASSERT_TRUE(javaClass.load());
  102. }
  103. TEST_F(JniClassTest, loadMethod)
  104. {
  105. string classPath = "java.utils.String";
  106. JniClass javaClass = this->createJniClass(classPath);
  107. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  108. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  109. string methodIdentifier = "getDay";
  110. string methodSignature = "()B";
  111. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  112. jmethodID methodId = (jmethodID) make_shared<int>().get();
  113. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  114. ASSERT_TRUE(javaClass.load());
  115. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  116. }
  117. TEST_F(JniClassTest, loadMethod_repeat)
  118. {
  119. string classPath = "java.utils.String";
  120. JniClass javaClass = this->createJniClass(classPath);
  121. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  122. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  123. string methodIdentifier = "getDay";
  124. string methodSignature = "()B";
  125. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  126. jmethodID methodId = (jmethodID) make_shared<int>().get();
  127. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  128. ASSERT_TRUE(javaClass.load());
  129. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  130. ASSERT_FALSE(javaClass.loadMethod(methodIdentifier, methodSignature));
  131. }
  132. TEST_F(JniClassTest, loadMethod_without_previous_class_load)
  133. {
  134. string classPath = "java.utils.String";
  135. JniClass javaClass = this->createJniClass(classPath);
  136. string methodIdentifier = "getDay";
  137. string methodSignature = "()B";
  138. EXPECT_THROW(
  139. {
  140. try
  141. {
  142. javaClass.loadMethod(methodIdentifier, methodSignature);
  143. }
  144. catch (const NullPointerException &_exception)
  145. {
  146. throw;
  147. }
  148. },
  149. NullPointerException);
  150. }
  151. }