JniClassTest.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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_double_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 = "getPi";
  156. string methodSignature = "()D";
  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, callDoubleMethod(testing::_, methodId)).Times(AtLeast(1));
  161. ON_CALL(*this->jniApi, callDoubleMethod(testing::_, methodId)).WillByDefault(Return(3.14159265l));
  162. ASSERT_TRUE(javaClass.load());
  163. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  164. ASSERT_DOUBLE_EQ(3.14159265l, javaClass.callMethod(methodIdentifier).getDoubleValue());
  165. }
  166. TEST_F(JniClassTest, callMethod_float_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 = "getPi";
  173. string methodSignature = "()F";
  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, callFloatMethod(testing::_, methodId)).Times(AtLeast(1));
  178. ON_CALL(*this->jniApi, callFloatMethod(testing::_, methodId)).WillByDefault(Return(3.14159f));
  179. ASSERT_TRUE(javaClass.load());
  180. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  181. ASSERT_FLOAT_EQ(3.14159f, javaClass.callMethod(methodIdentifier).getFloatValue());
  182. }
  183. TEST_F(JniClassTest, callMethod_integer_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 = "getYear";
  190. string methodSignature = "()I";
  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, callIntMethod(testing::_, methodId)).Times(AtLeast(1));
  195. ON_CALL(*this->jniApi, callIntMethod(testing::_, methodId)).WillByDefault(Return(1989));
  196. ASSERT_TRUE(javaClass.load());
  197. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  198. ASSERT_EQ(1989, javaClass.callMethod(methodIdentifier).getIntegerValue());
  199. }
  200. TEST_F(JniClassTest, callMethod_long_return_value)
  201. {
  202. string classPath = "java.utils.String";
  203. JniClass javaClass = this->createJniClass(classPath);
  204. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  205. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  206. string methodIdentifier = "getAmountOfMoney";
  207. string methodSignature = "()J";
  208. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  209. jmethodID methodId = (jmethodID) make_shared<int>().get();
  210. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  211. EXPECT_CALL(*this->jniApi, callLongMethod(testing::_, methodId)).Times(AtLeast(1));
  212. ON_CALL(*this->jniApi, callLongMethod(testing::_, methodId)).WillByDefault(Return(18837828981));
  213. ASSERT_TRUE(javaClass.load());
  214. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  215. ASSERT_EQ(18837828981, javaClass.callMethod(methodIdentifier).getLongValue());
  216. }
  217. TEST_F(JniClassTest, callMethod_short_return_value)
  218. {
  219. string classPath = "java.utils.String";
  220. JniClass javaClass = this->createJniClass(classPath);
  221. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  222. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  223. string methodIdentifier = "getDay";
  224. string methodSignature = "()S";
  225. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  226. jmethodID methodId = (jmethodID) make_shared<int>().get();
  227. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  228. EXPECT_CALL(*this->jniApi, callShortMethod(testing::_, methodId)).Times(AtLeast(1));
  229. ON_CALL(*this->jniApi, callShortMethod(testing::_, methodId)).WillByDefault(Return(55));
  230. ASSERT_TRUE(javaClass.load());
  231. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  232. ASSERT_EQ(55, javaClass.callMethod(methodIdentifier).getShortValue());
  233. }
  234. TEST_F(JniClassTest, hasMethod)
  235. {
  236. string classPath = "java.utils.String";
  237. JniClass javaClass = this->createJniClass(classPath);
  238. ASSERT_FALSE(javaClass.hasMethod("getDay"));
  239. }
  240. TEST_F(JniClassTest, load)
  241. {
  242. string classPath = "java.utils.String";
  243. JniClass javaClass = this->createJniClass(classPath);
  244. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  245. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  246. ASSERT_TRUE(javaClass.load());
  247. }
  248. TEST_F(JniClassTest, loadMethod)
  249. {
  250. string classPath = "java.utils.String";
  251. JniClass javaClass = this->createJniClass(classPath);
  252. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  253. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  254. string methodIdentifier = "getDay";
  255. string methodSignature = "()B";
  256. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  257. jmethodID methodId = (jmethodID) make_shared<int>().get();
  258. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  259. ASSERT_TRUE(javaClass.load());
  260. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  261. }
  262. TEST_F(JniClassTest, loadMethod_repeat)
  263. {
  264. string classPath = "java.utils.String";
  265. JniClass javaClass = this->createJniClass(classPath);
  266. EXPECT_CALL(*this->jniApi, findClass(classPath)).Times(AtLeast(1));
  267. ON_CALL(*this->jniApi, findClass(classPath)).WillByDefault(Return(make_shared<_jclass>().get()));
  268. string methodIdentifier = "getDay";
  269. string methodSignature = "()B";
  270. EXPECT_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).Times(AtLeast(1));
  271. jmethodID methodId = (jmethodID) make_shared<int>().get();
  272. ON_CALL(*this->jniApi, getMethodId(testing::_, methodIdentifier.c_str(), methodSignature.c_str())).WillByDefault(Return(methodId));
  273. ASSERT_TRUE(javaClass.load());
  274. ASSERT_TRUE(javaClass.loadMethod(methodIdentifier, methodSignature));
  275. ASSERT_FALSE(javaClass.loadMethod(methodIdentifier, methodSignature));
  276. }
  277. TEST_F(JniClassTest, loadMethod_without_previous_class_load)
  278. {
  279. string classPath = "java.utils.String";
  280. JniClass javaClass = this->createJniClass(classPath);
  281. string methodIdentifier = "getDay";
  282. string methodSignature = "()B";
  283. EXPECT_THROW(
  284. {
  285. try
  286. {
  287. javaClass.loadMethod(methodIdentifier, methodSignature);
  288. }
  289. catch (const NullPointerException &_exception)
  290. {
  291. string expected = _exception.getName() + " thrown - no Java class reference available for loading class method!";
  292. string actual = _exception.what();
  293. ASSERT_STREQ(expected.c_str(), actual.c_str());
  294. throw;
  295. }
  296. },
  297. NullPointerException);
  298. }
  299. }