JniClassTest.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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-11
  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_char_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 = "getLetter";
  139. string methodSignature = "()C";
  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, callCharMethod(testing::_, methodId)).Times(AtLeast(1));
  144. ON_CALL(*this->jniApi, callCharMethod(testing::_, methodId)).WillByDefault(Return('P'));
  145. ASSERT_TRUE(javaClass.load());
  146. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  147. ASSERT_EQ('P', javaClass.callMethod(methodIdentifier).getCharValue());
  148. }
  149. TEST_F(JniClassTest, callMethod_integer_return_value)
  150. {
  151. string classPath = "java.utils.String";
  152. JniClass javaClass = this->createJniClass(classPath);
  153. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  154. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  155. string methodIdentifier = "getYear";
  156. string methodSignature = "()I";
  157. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  158. jmethodID methodId = (jmethodID) make_shared<int>().get();
  159. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  160. EXPECT_CALL(*this->jniApi, callIntMethod(testing::_, methodId)).Times(AtLeast(1));
  161. ON_CALL(*this->jniApi, callIntMethod(testing::_, methodId)).WillByDefault(Return(1989));
  162. ASSERT_TRUE(javaClass.load());
  163. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  164. ASSERT_EQ(1989, javaClass.callMethod(methodIdentifier).getIntegerValue());
  165. }
  166. TEST_F(JniClassTest, hasMethod)
  167. {
  168. string classPath = "java.utils.String";
  169. JniClass javaClass = this->createJniClass(classPath);
  170. ASSERT_FALSE(javaClass.hasMethod("getDay"));
  171. }
  172. TEST_F(JniClassTest, load)
  173. {
  174. string classPath = "java.utils.String";
  175. JniClass javaClass = this->createJniClass(classPath);
  176. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  177. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  178. ASSERT_TRUE(javaClass.load());
  179. }
  180. TEST_F(JniClassTest, loadMethod)
  181. {
  182. string classPath = "java.utils.String";
  183. JniClass javaClass = this->createJniClass(classPath);
  184. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  185. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  186. string methodIdentifier = "getDay";
  187. string methodSignature = "()B";
  188. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  189. jmethodID methodId = (jmethodID) make_shared<int>().get();
  190. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  191. ASSERT_TRUE(javaClass.load());
  192. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  193. }
  194. TEST_F(JniClassTest, loadMethod_repeat)
  195. {
  196. string classPath = "java.utils.String";
  197. JniClass javaClass = this->createJniClass(classPath);
  198. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  199. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  200. string methodIdentifier = "getDay";
  201. string methodSignature = "()B";
  202. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  203. jmethodID methodId = (jmethodID) make_shared<int>().get();
  204. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  205. ASSERT_TRUE(javaClass.load());
  206. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  207. ASSERT_FALSE(javaClass.loadMethod(methodIdentifier, methodSignature));
  208. }
  209. TEST_F(JniClassTest, loadMethod_without_previous_class_load)
  210. {
  211. string classPath = "java.utils.String";
  212. JniClass javaClass = this->createJniClass(classPath);
  213. string methodIdentifier = "getDay";
  214. string methodSignature = "()B";
  215. EXPECT_THROW(
  216. {
  217. try
  218. {
  219. javaClass.loadMethod(methodIdentifier, methodSignature);
  220. }
  221. catch (const NullPointerException &_exception)
  222. {
  223. string expected = _exception.getName() + " thrown - no Java class reference available for loading class method!";
  224. string actual = _exception.what();
  225. ASSERT_STREQ(expected.c_str(), actual.c_str());
  226. throw;
  227. }
  228. },
  229. NullPointerException);
  230. }
  231. }