JniClassTest.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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-10
  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::NullPointerException;
  17. using ls::std::core::experimental::JniClass;
  18. using ls::std::core::experimental::JniClassParameter;
  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. string expected = _exception.getName() + " thrown - no provided reference to JNI class parameter!";
  55. string actual = _exception.what();
  56. ASSERT_STREQ(expected.c_str(), actual.c_str());
  57. throw;
  58. }
  59. },
  60. IllegalArgumentException);
  61. }
  62. TEST_F(JniClassTest, constructor_empty_path)
  63. {
  64. EXPECT_THROW(
  65. {
  66. try
  67. {
  68. JniClass javaClass(make_shared<JniClassParameter>(), "");
  69. }
  70. catch (const IllegalArgumentException &_exception)
  71. {
  72. string expected = _exception.getName() + " thrown - path to associated Java class is empty!";
  73. string actual = _exception.what();
  74. ASSERT_STREQ(expected.c_str(), actual.c_str());
  75. throw;
  76. }
  77. },
  78. IllegalArgumentException);
  79. }
  80. TEST_F(JniClassTest, constructor_no_java_environment_reference)
  81. {
  82. EXPECT_THROW(
  83. {
  84. try
  85. {
  86. JniClass javaClass(make_shared<JniClassParameter>(), "java.utils.String");
  87. }
  88. catch (const IllegalArgumentException &_exception)
  89. {
  90. string expected = _exception.getName() + " thrown - Java environment is not being provided!";
  91. string actual = _exception.what();
  92. ASSERT_STREQ(expected.c_str(), actual.c_str());
  93. throw;
  94. }
  95. },
  96. IllegalArgumentException);
  97. }
  98. TEST_F(JniClassTest, callMethod_boolean_return_value)
  99. {
  100. string classPath = "java.utils.String";
  101. JniClass javaClass = this->createJniClass(classPath);
  102. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  103. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  104. string methodIdentifier = "isBlue";
  105. string methodSignature = "()Z";
  106. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  107. jmethodID methodId = (jmethodID) make_shared<int>().get();
  108. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  109. EXPECT_CALL(*this->jniApi, callBooleanMethod(testing::_, methodId)).Times(AtLeast(1));
  110. ON_CALL(*this->jniApi, callBooleanMethod(testing::_, methodId)).WillByDefault(Return(true));
  111. ASSERT_TRUE(javaClass.load());
  112. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  113. ASSERT_TRUE(javaClass.callMethod(methodIdentifier).getBooleanValue());
  114. }
  115. TEST_F(JniClassTest, callMethod_byte_return_value)
  116. {
  117. string classPath = "java.utils.String";
  118. JniClass javaClass = this->createJniClass(classPath);
  119. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  120. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  121. string methodIdentifier = "getDay";
  122. string methodSignature = "()B";
  123. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  124. jmethodID methodId = (jmethodID) make_shared<int>().get();
  125. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  126. EXPECT_CALL(*this->jniApi, callByteMethod(testing::_, methodId)).Times(AtLeast(1));
  127. ON_CALL(*this->jniApi, callByteMethod(testing::_, methodId)).WillByDefault(Return(22));
  128. ASSERT_TRUE(javaClass.load());
  129. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  130. ASSERT_EQ(22, javaClass.callMethod(methodIdentifier).getByteValue());
  131. }
  132. TEST_F(JniClassTest, callMethod_integer_return_value)
  133. {
  134. string classPath = "java.utils.String";
  135. JniClass javaClass = this->createJniClass(classPath);
  136. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  137. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  138. string methodIdentifier = "getYear";
  139. string methodSignature = "()I";
  140. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  141. jmethodID methodId = (jmethodID) make_shared<int>().get();
  142. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  143. EXPECT_CALL(*this->jniApi, callIntMethod(testing::_, methodId)).Times(AtLeast(1));
  144. ON_CALL(*this->jniApi, callIntMethod(testing::_, methodId)).WillByDefault(Return(1989));
  145. ASSERT_TRUE(javaClass.load());
  146. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  147. ASSERT_EQ(1989, javaClass.callMethod(methodIdentifier).getIntegerValue());
  148. }
  149. TEST_F(JniClassTest, hasMethod)
  150. {
  151. string classPath = "java.utils.String";
  152. JniClass javaClass = this->createJniClass(classPath);
  153. ASSERT_FALSE(javaClass.hasMethod("getDay"));
  154. }
  155. TEST_F(JniClassTest, load)
  156. {
  157. string classPath = "java.utils.String";
  158. JniClass javaClass = this->createJniClass(classPath);
  159. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  160. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  161. ASSERT_TRUE(javaClass.load());
  162. }
  163. TEST_F(JniClassTest, loadMethod)
  164. {
  165. string classPath = "java.utils.String";
  166. JniClass javaClass = this->createJniClass(classPath);
  167. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  168. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  169. string methodIdentifier = "getDay";
  170. string methodSignature = "()B";
  171. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  172. jmethodID methodId = (jmethodID) make_shared<int>().get();
  173. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  174. ASSERT_TRUE(javaClass.load());
  175. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  176. }
  177. TEST_F(JniClassTest, loadMethod_repeat)
  178. {
  179. string classPath = "java.utils.String";
  180. JniClass javaClass = this->createJniClass(classPath);
  181. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  182. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  183. string methodIdentifier = "getDay";
  184. string methodSignature = "()B";
  185. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  186. jmethodID methodId = (jmethodID) make_shared<int>().get();
  187. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  188. ASSERT_TRUE(javaClass.load());
  189. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  190. ASSERT_FALSE(javaClass.loadMethod(methodIdentifier, methodSignature));
  191. }
  192. TEST_F(JniClassTest, loadMethod_without_previous_class_load)
  193. {
  194. string classPath = "java.utils.String";
  195. JniClass javaClass = this->createJniClass(classPath);
  196. string methodIdentifier = "getDay";
  197. string methodSignature = "()B";
  198. EXPECT_THROW(
  199. {
  200. try
  201. {
  202. javaClass.loadMethod(methodIdentifier, methodSignature);
  203. }
  204. catch (const NullPointerException &_exception)
  205. {
  206. string expected = _exception.getName() + " thrown - no Java class reference available for loading class method!";
  207. string actual = _exception.what();
  208. ASSERT_STREQ(expected.c_str(), actual.c_str());
  209. throw;
  210. }
  211. },
  212. NullPointerException);
  213. }
  214. }