JniClassTest.cpp 13 KB

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