JniClassTest.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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-08
  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, callMethod_byte_return_value)
  90. {
  91. string classPath = "java.utils.String";
  92. JniClass javaClass = this->createJniClass(classPath);
  93. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  94. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  95. string methodIdentifier = "getDay";
  96. string methodSignature = "()B";
  97. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  98. jmethodID methodId = (jmethodID) make_shared<int>().get();
  99. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  100. EXPECT_CALL(*this->jniApi, callByteMethod(testing::_, methodId)).Times(AtLeast(1));
  101. ON_CALL(*this->jniApi, callByteMethod(testing::_, methodId)).WillByDefault(Return(22));
  102. ASSERT_TRUE(javaClass.load());
  103. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  104. ASSERT_EQ(22, javaClass.callMethod(methodIdentifier).getByteValue());
  105. }
  106. TEST_F(JniClassTest, callMethod_integer_return_value)
  107. {
  108. string classPath = "java.utils.String";
  109. JniClass javaClass = this->createJniClass(classPath);
  110. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  111. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  112. string methodIdentifier = "getYear";
  113. string methodSignature = "()I";
  114. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  115. jmethodID methodId = (jmethodID) make_shared<int>().get();
  116. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  117. EXPECT_CALL(*this->jniApi, callIntMethod(testing::_, methodId)).Times(AtLeast(1));
  118. ON_CALL(*this->jniApi, callIntMethod(testing::_, methodId)).WillByDefault(Return(1989));
  119. ASSERT_TRUE(javaClass.load());
  120. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  121. ASSERT_EQ(1989, javaClass.callMethod(methodIdentifier).getIntegerValue());
  122. }
  123. TEST_F(JniClassTest, hasMethod)
  124. {
  125. string classPath = "java.utils.String";
  126. JniClass javaClass = this->createJniClass(classPath);
  127. ASSERT_FALSE(javaClass.hasMethod("getDay"));
  128. }
  129. TEST_F(JniClassTest, load)
  130. {
  131. string classPath = "java.utils.String";
  132. JniClass javaClass = this->createJniClass(classPath);
  133. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  134. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  135. ASSERT_TRUE(javaClass.load());
  136. }
  137. TEST_F(JniClassTest, loadMethod)
  138. {
  139. string classPath = "java.utils.String";
  140. JniClass javaClass = this->createJniClass(classPath);
  141. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  142. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  143. string methodIdentifier = "getDay";
  144. string methodSignature = "()B";
  145. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  146. jmethodID methodId = (jmethodID) make_shared<int>().get();
  147. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  148. ASSERT_TRUE(javaClass.load());
  149. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  150. }
  151. TEST_F(JniClassTest, loadMethod_repeat)
  152. {
  153. string classPath = "java.utils.String";
  154. JniClass javaClass = this->createJniClass(classPath);
  155. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  156. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  157. string methodIdentifier = "getDay";
  158. string methodSignature = "()B";
  159. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  160. jmethodID methodId = (jmethodID) make_shared<int>().get();
  161. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  162. ASSERT_TRUE(javaClass.load());
  163. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  164. ASSERT_FALSE(javaClass.loadMethod(methodIdentifier, methodSignature));
  165. }
  166. TEST_F(JniClassTest, loadMethod_without_previous_class_load)
  167. {
  168. string classPath = "java.utils.String";
  169. JniClass javaClass = this->createJniClass(classPath);
  170. string methodIdentifier = "getDay";
  171. string methodSignature = "()B";
  172. EXPECT_THROW(
  173. {
  174. try
  175. {
  176. javaClass.loadMethod(methodIdentifier, methodSignature);
  177. }
  178. catch (const NullPointerException &_exception)
  179. {
  180. throw;
  181. }
  182. },
  183. NullPointerException);
  184. }
  185. }