JniClassTest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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, callMethod_short_return_value)
  167. {
  168. string classPath = "java.utils.String";
  169. JniClass javaClass = this->createJniClass(classPath);
  170. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  171. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  172. string methodIdentifier = "getDay";
  173. string methodSignature = "()S";
  174. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  175. jmethodID methodId = (jmethodID) make_shared<int>().get();
  176. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  177. EXPECT_CALL(*this->jniApi, callShortMethod(testing::_, methodId)).Times(AtLeast(1));
  178. ON_CALL(*this->jniApi, callShortMethod(testing::_, methodId)).WillByDefault(Return(55));
  179. ASSERT_TRUE(javaClass.load());
  180. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  181. ASSERT_EQ(55, javaClass.callMethod(methodIdentifier).getShortValue());
  182. }
  183. TEST_F(JniClassTest, hasMethod)
  184. {
  185. string classPath = "java.utils.String";
  186. JniClass javaClass = this->createJniClass(classPath);
  187. ASSERT_FALSE(javaClass.hasMethod("getDay"));
  188. }
  189. TEST_F(JniClassTest, load)
  190. {
  191. string classPath = "java.utils.String";
  192. JniClass javaClass = this->createJniClass(classPath);
  193. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  194. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  195. ASSERT_TRUE(javaClass.load());
  196. }
  197. TEST_F(JniClassTest, loadMethod)
  198. {
  199. string classPath = "java.utils.String";
  200. JniClass javaClass = this->createJniClass(classPath);
  201. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  202. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  203. string methodIdentifier = "getDay";
  204. string methodSignature = "()B";
  205. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  206. jmethodID methodId = (jmethodID) make_shared<int>().get();
  207. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  208. ASSERT_TRUE(javaClass.load());
  209. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  210. }
  211. TEST_F(JniClassTest, loadMethod_repeat)
  212. {
  213. string classPath = "java.utils.String";
  214. JniClass javaClass = this->createJniClass(classPath);
  215. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  216. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  217. string methodIdentifier = "getDay";
  218. string methodSignature = "()B";
  219. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  220. jmethodID methodId = (jmethodID) make_shared<int>().get();
  221. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  222. ASSERT_TRUE(javaClass.load());
  223. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  224. ASSERT_FALSE(javaClass.loadMethod(methodIdentifier, methodSignature));
  225. }
  226. TEST_F(JniClassTest, loadMethod_without_previous_class_load)
  227. {
  228. string classPath = "java.utils.String";
  229. JniClass javaClass = this->createJniClass(classPath);
  230. string methodIdentifier = "getDay";
  231. string methodSignature = "()B";
  232. EXPECT_THROW(
  233. {
  234. try
  235. {
  236. javaClass.loadMethod(methodIdentifier, methodSignature);
  237. }
  238. catch (const NullPointerException &_exception)
  239. {
  240. string expected = _exception.getName() + " thrown - no Java class reference available for loading class method!";
  241. string actual = _exception.what();
  242. ASSERT_STREQ(expected.c_str(), actual.c_str());
  243. throw;
  244. }
  245. },
  246. NullPointerException);
  247. }
  248. }