/dspace-api/src/test/java/org/dspace/core/ContextTest.java

https://gitlab.com/marcio.rga/educacao-livre · Java · 591 lines · 290 code · 95 blank · 206 comment · 0 complexity · 9a7537714ed004c971095bc7a29703ea MD5 · raw file

  1. /**
  2. * The contents of this file are subject to the license and copyright
  3. * detailed in the LICENSE and NOTICE files at the root of the source
  4. * tree and available online at
  5. *
  6. * http://www.dspace.org/license/
  7. */
  8. package org.dspace.core;
  9. import java.sql.Connection;
  10. import java.sql.SQLException;
  11. import java.util.Locale;
  12. import mockit.NonStrictExpectations;
  13. import org.dspace.AbstractUnitTest;
  14. import org.dspace.authorize.AuthorizeException;
  15. import org.dspace.authorize.AuthorizeManager;
  16. import org.dspace.eperson.EPerson;
  17. import org.dspace.eperson.Group;
  18. import org.junit.*;
  19. import static org.junit.Assert.* ;
  20. import static org.hamcrest.CoreMatchers.*;
  21. /**
  22. * Perform some basic unit tests for Context Class
  23. * @author tdonohue
  24. */
  25. public class ContextTest extends AbstractUnitTest
  26. {
  27. /**
  28. * Test of getDBConnection method, of class Context.
  29. */
  30. @Test
  31. public void testGetDBConnection() throws SQLException
  32. {
  33. Connection connection = context.getDBConnection();
  34. assertThat("testGetDBConnection 0", connection, notNullValue());
  35. assertThat("testGetDBConnection 1", connection.isClosed(), equalTo(false));
  36. }
  37. /**
  38. * Test of setCurrentUser method, of class Context.
  39. */
  40. @Test
  41. public void testSetCurrentUser() throws SQLException, AuthorizeException
  42. {
  43. new NonStrictExpectations(AuthorizeManager.class)
  44. {{
  45. // Allow Admin permissions - needed to create a new EPerson
  46. AuthorizeManager.isAdmin((Context) any); result = true;
  47. }};
  48. EPerson oldUser = context.getCurrentUser();
  49. // Create a dummy EPerson to set as current user
  50. EPerson newUser = EPerson.create(context);
  51. newUser.setFirstName("Jane");
  52. newUser.setLastName("Doe");
  53. newUser.setEmail("jane@email.com");
  54. newUser.setCanLogIn(true);
  55. newUser.setLanguage(I18nUtil.getDefaultLocale().getLanguage());
  56. context.setCurrentUser(newUser);
  57. assertThat("testSetCurrentUser 0", context.getCurrentUser(), notNullValue());
  58. assertThat("testSetCurrentUser 1", context.getCurrentUser(), equalTo(newUser));
  59. // Restore the previous current user
  60. context.setCurrentUser(oldUser);
  61. }
  62. /**
  63. * Test of getCurrentUser method, of class Context.
  64. */
  65. @Test
  66. public void testGetCurrentUser()
  67. {
  68. //NOTE: 'eperson' is initialized by AbstractUnitTest & set as the "currentUser" there
  69. assertThat("testGetCurrentUser 0", context.getCurrentUser(), notNullValue());
  70. assertThat("testGetCurrentUser 1", context.getCurrentUser(), equalTo(eperson));
  71. }
  72. /**
  73. * Test of getCurrentLocale method, of class Context.
  74. */
  75. @Test
  76. public void testGetCurrentLocale()
  77. {
  78. //NOTE: CurrentLocale is not initialized in AbstractUnitTest. So it should be DEFAULTLOCALE
  79. assertThat("testGetCurrentLocale 0", context.getCurrentLocale(), notNullValue());
  80. assertThat("testGetCurrentLocale 1", context.getCurrentLocale(), equalTo(I18nUtil.DEFAULTLOCALE));
  81. }
  82. /**
  83. * Test of setCurrentLocale method, of class Context.
  84. */
  85. @Test
  86. public void testSetCurrentLocale() {
  87. //Get previous value
  88. Locale oldLocale = context.getCurrentLocale();
  89. //Set a new, non-English value
  90. Locale newLocale = Locale.FRENCH;
  91. context.setCurrentLocale(newLocale);
  92. assertThat("testSetCurrentLocale 0", context.getCurrentLocale(), notNullValue());
  93. assertThat("testSetCurrentLocale 1", context.getCurrentLocale(), equalTo(newLocale));
  94. // Restore previous value
  95. context.setCurrentLocale(oldLocale);
  96. }
  97. /**
  98. * Test of ignoreAuthorization method, of class Context.
  99. */
  100. @Test
  101. public void testIgnoreAuthorization()
  102. {
  103. // Turn off authorization
  104. context.turnOffAuthorisationSystem();
  105. assertThat("testIgnoreAuthorization 0", context.ignoreAuthorization(), equalTo(true));
  106. // Turn it back on
  107. context.restoreAuthSystemState();
  108. assertThat("testIgnoreAuthorization 1", context.ignoreAuthorization(), equalTo(false));
  109. }
  110. /**
  111. * Test of turnOffAuthorisationSystem method, of class Context.
  112. */
  113. /*@Test
  114. public void testTurnOffAuthorisationSystem() {
  115. // Already tested in testIgnoreAuthorization()
  116. }*/
  117. /**
  118. * Test of restoreAuthSystemState method, of class Context.
  119. */
  120. /*@Test
  121. public void testRestoreAuthSystemState() {
  122. // Already tested in testIgnoreAuthorization()
  123. }*/
  124. /**
  125. * Test of setIgnoreAuthorization method, of class Context.
  126. */
  127. /*@Test
  128. public void testSetIgnoreAuthorization() {
  129. // Deprecated method
  130. }*/
  131. /**
  132. * Test of setExtraLogInfo method, of class Context.
  133. */
  134. @Test
  135. public void testSetExtraLogInfo()
  136. {
  137. // Get the previous value
  138. String oldValue = context.getExtraLogInfo();
  139. // Set a new value
  140. String newValue = "This is some extra log info";
  141. context.setExtraLogInfo(newValue);
  142. assertThat("testSetExtraLogInfo 0", context.getExtraLogInfo(), notNullValue());
  143. assertThat("testSetExtraLogInfo 1", context.getExtraLogInfo(), equalTo(newValue));
  144. }
  145. /**
  146. * Test of getExtraLogInfo method, of class Context.
  147. */
  148. @Test
  149. public void testGetExtraLogInfo()
  150. {
  151. // Extra log info has a default value of "", and AbstractUnitTest doesn't change it
  152. String defaultValue = "";
  153. assertThat("testGetExtraLogInfo 0", context.getExtraLogInfo(), notNullValue());
  154. assertThat("testGetExtraLogInfo 1", context.getExtraLogInfo(), equalTo(defaultValue));
  155. }
  156. /**
  157. * Test of complete method, of class Context.
  158. */
  159. @Test
  160. public void testComplete() throws SQLException
  161. {
  162. // To test complete() we need a new Context object
  163. Context instance = new Context();
  164. // By default, we should have a new DB connection, so let's make sure it is there
  165. assertThat("testComplete 0", instance.getDBConnection(), notNullValue());
  166. assertThat("testComplete 1", instance.getDBConnection().isClosed(), equalTo(false));
  167. assertThat("testComplete 2", instance.isValid(), equalTo(true));
  168. // Now, call complete(). This should set DB connection to null & invalidate context
  169. instance.complete();
  170. assertThat("testComplete 3", instance.getDBConnection(), nullValue());
  171. assertThat("testComplete 4", instance.isValid(), equalTo(false));
  172. // Cleanup our new context
  173. cleanupContext(instance);
  174. // TODO: May also want to test that complete() is calling commit()?
  175. }
  176. /**
  177. * Test of complete method, of class Context.
  178. */
  179. @Test
  180. public void testComplete2() throws SQLException
  181. {
  182. // To test complete() we need a new Context object
  183. Context instance = new Context();
  184. // Call complete twice. The second call should NOT throw an error
  185. // and effectively does nothing
  186. instance.complete();
  187. instance.complete();
  188. // Cleanup our new context
  189. cleanupContext(instance);
  190. }
  191. /**
  192. * Test of commit method, of class Context.
  193. */
  194. @Test
  195. public void testCommit() throws Exception
  196. {
  197. new NonStrictExpectations(AuthorizeManager.class)
  198. {{
  199. // Allow Admin permissions - needed to create a new EPerson
  200. AuthorizeManager.isAdmin((Context) any); result = true;
  201. }};
  202. // Create a new EPerson & commit it
  203. String createdEmail = "jimmy@email.com";
  204. EPerson newUser = EPerson.create(context);
  205. newUser.setFirstName("Jimmy");
  206. newUser.setLastName("Doe");
  207. newUser.setEmail(createdEmail);
  208. newUser.setCanLogIn(true);
  209. newUser.setLanguage(I18nUtil.getDefaultLocale().getLanguage());
  210. // Ensure EPerson is committed
  211. newUser.update();
  212. context.commit();
  213. //Now, open a new context, and see if this eperson can be found!
  214. Context newInstance = new Context();
  215. EPerson found = EPerson.findByEmail(newInstance, createdEmail);
  216. assertThat("testCommit 0", found, notNullValue());
  217. // Cleanup our new context
  218. cleanupContext(newInstance);
  219. }
  220. /**
  221. * Test of commit method, of class Context.
  222. */
  223. @Test(expected=IllegalStateException.class)
  224. public void testCommitReadOnlyContext() throws Exception
  225. {
  226. // Create a read-only Context
  227. Context instance = new Context(Context.READ_ONLY);
  228. try
  229. {
  230. // Attempt to commit to it - should throw an exception
  231. instance.commit();
  232. }
  233. finally
  234. {
  235. // Cleanup our context
  236. cleanupContext(instance);
  237. }
  238. }
  239. /**
  240. * Test of commit method, of class Context.
  241. */
  242. @Test(expected=IllegalStateException.class)
  243. public void testCommitInvalidContext() throws Exception
  244. {
  245. // Create a new Context
  246. Context instance = new Context();
  247. // Close context (invalidating it)
  248. instance.abort();
  249. try
  250. {
  251. // Attempt to commit to it - should throw an exception
  252. instance.commit();
  253. }
  254. finally
  255. {
  256. // Cleanup our context
  257. cleanupContext(instance);
  258. }
  259. }
  260. /**
  261. * Test of abort method, of class Context.
  262. */
  263. @Test
  264. public void testAbort() throws SQLException, AuthorizeException
  265. {
  266. new NonStrictExpectations(AuthorizeManager.class)
  267. {{
  268. // Allow Admin permissions - needed to create a new EPerson
  269. AuthorizeManager.isAdmin((Context) any); result = true;
  270. }};
  271. // To test abort() we need a new Context object
  272. Context instance = new Context();
  273. // Create a new EPerson (DO NOT COMMIT IT)
  274. String createdEmail = "susie@email.com";
  275. EPerson newUser = EPerson.create(instance);
  276. newUser.setFirstName("Susan");
  277. newUser.setLastName("Doe");
  278. newUser.setEmail(createdEmail);
  279. newUser.setCanLogIn(true);
  280. newUser.setLanguage(I18nUtil.getDefaultLocale().getLanguage());
  281. // Abort our context
  282. instance.abort();
  283. // Ensure the context is no longer valid
  284. assertThat("testAbort 0", instance.isValid(), equalTo(false));
  285. // Open a new context, let's make sure that EPerson isn't there
  286. Context newInstance = new Context();
  287. EPerson found = EPerson.findByEmail(newInstance, createdEmail);
  288. assertThat("testAbort 1", found, nullValue());
  289. // Cleanup our contexts
  290. cleanupContext(instance);
  291. cleanupContext(newInstance);
  292. }
  293. /**
  294. * Test of abort method, of class Context.
  295. */
  296. @Test
  297. public void testAbort2() throws SQLException
  298. {
  299. // To test abort() we need a new Context object
  300. Context instance = new Context();
  301. // Call abort twice. The second call should NOT throw an error
  302. // and effectively does nothing
  303. instance.abort();
  304. instance.abort();
  305. // Cleanup our context
  306. cleanupContext(instance);
  307. }
  308. /**
  309. * Test of isValid method, of class Context.
  310. */
  311. /*@Test
  312. public void testIsValid() {
  313. // This is already tested by testComplete() and testAbort()
  314. }*/
  315. /**
  316. * Test of isReadOnly method, of class Context.
  317. */
  318. @Test
  319. public void testIsReadOnly() throws SQLException
  320. {
  321. // Our default context should NOT be read only
  322. assertThat("testIsReadOnly 0", context.isReadOnly(), equalTo(false));
  323. // Create a new read-only context
  324. Context instance = new Context(Context.READ_ONLY);
  325. assertThat("testIsReadOnly 1", instance.isReadOnly(), equalTo(true));
  326. // Cleanup our context
  327. cleanupContext(instance);
  328. }
  329. /**
  330. * Test of fromCache method, of class Context.
  331. */
  332. @Test
  333. public void testFromCache() throws SQLException, AuthorizeException
  334. {
  335. new NonStrictExpectations(AuthorizeManager.class)
  336. {{
  337. // Allow Admin permissions - needed to create a new EPerson
  338. AuthorizeManager.isAdmin((Context) any); result = true;
  339. }};
  340. // To test caching we need a new Context object
  341. Context instance = new Context();
  342. // Create a new Eperson object
  343. EPerson newEperson = EPerson.create(instance);
  344. newEperson.setFirstName("Sam");
  345. newEperson.setLastName("Smith");
  346. newEperson.setEmail("sammy@smith.com");
  347. newEperson.setCanLogIn(true);
  348. newEperson.setLanguage(I18nUtil.getDefaultLocale().getLanguage());
  349. // Cache the object
  350. instance.cache(newEperson, newEperson.getID());
  351. // Now, pull the object out of the cache
  352. EPerson fromCache = (EPerson) instance.fromCache(EPerson.class, newEperson.getID());
  353. assertThat("testFromCache 0", fromCache, notNullValue());
  354. assertThat("testFromCache 1", fromCache, equalTo(newEperson));
  355. // Cleanup our context
  356. cleanupContext(instance);
  357. }
  358. /**
  359. * Test of cache method, of class Context.
  360. */
  361. @Test
  362. public void testCache() throws SQLException
  363. {
  364. // To test caching we need a new Context object
  365. Context instance = new Context();
  366. // Create a simple object to cache
  367. String cacheMe = "Look for me in your local cache!";
  368. int cacheMeID = 9999999;
  369. // Cache the object
  370. instance.cache(cacheMe, cacheMeID);
  371. // Now, can we get it back?
  372. String fromCache = (String) instance.fromCache(String.class, cacheMeID);
  373. assertThat("testCache 0", fromCache, notNullValue());
  374. assertThat("testCache 1", fromCache, equalTo(cacheMe));
  375. // Cleanup our context
  376. cleanupContext(instance);
  377. }
  378. /**
  379. * Test of removeCached method, of class Context.
  380. */
  381. @Test
  382. public void testRemoveCached() throws SQLException
  383. {
  384. // To test caching we need a new Context object
  385. Context instance = new Context();
  386. // Create a simple object to cache
  387. String cacheMe = "Look for me in your local cache!";
  388. int cacheMeID = 9999999;
  389. // Cache the object
  390. instance.cache(cacheMe, cacheMeID);
  391. // Can we get it back?
  392. String fromCache = (String) instance.fromCache(String.class, cacheMeID);
  393. assertThat("testRemoveCache 0", fromCache, notNullValue());
  394. assertThat("testRemoveCache 1", fromCache, equalTo(cacheMe));
  395. // Now, can we remove it?
  396. instance.removeCached(cacheMe, cacheMeID);
  397. assertThat("testRemoveCache 3", instance.fromCache(String.class, cacheMeID), nullValue());
  398. // Cleanup our context
  399. cleanupContext(instance);
  400. }
  401. /**
  402. * Test of clearCache method, of class Context.
  403. */
  404. @Test
  405. public void testClearCache() throws SQLException
  406. {
  407. // To test caching we need a new Context object
  408. Context instance = new Context();
  409. // Create a simple object to cache
  410. String cacheMe = "Look for me in your local cache!";
  411. int cacheMeID = 9999999;
  412. // Cache the object
  413. instance.cache(cacheMe, cacheMeID);
  414. // Ensure cache is non-empty
  415. assertThat("testClearCache 0", instance.getCacheSize(), equalTo(1));
  416. // Clear our cache
  417. instance.clearCache();
  418. // Ensure cache is empty
  419. assertThat("testClearCache 1", instance.getCacheSize(), equalTo(0));
  420. // Cleanup our context
  421. cleanupContext(instance);
  422. }
  423. /**
  424. * Test of getCacheSize method, of class Context.
  425. */
  426. /*@Test
  427. public void testGetCacheSize() {
  428. // Tested in testClearCache()
  429. }*/
  430. /**
  431. * Test of setSpecialGroup method, of class Context.
  432. */
  433. @Test
  434. public void testSetSpecialGroup() throws SQLException
  435. {
  436. // To test special groups we need a new Context object
  437. Context instance = new Context();
  438. // Pass in random integers (need not be valid group IDs)
  439. instance.setSpecialGroup(10000);
  440. instance.setSpecialGroup(10001);
  441. assertThat("testSetSpecialGroup 0", instance.inSpecialGroup(10000), equalTo(true));
  442. assertThat("testSetSpecialGroup 1", instance.inSpecialGroup(10001), equalTo(true));
  443. assertThat("testSetSpecialGroup 2", instance.inSpecialGroup(20000), equalTo(false));
  444. // Cleanup our context
  445. cleanupContext(instance);
  446. }
  447. /**
  448. * Test of inSpecialGroup method, of class Context.
  449. */
  450. /*@Test
  451. public void testInSpecialGroup() {
  452. // tested in testSetSpecialGroup
  453. }*/
  454. /**
  455. * Test of getSpecialGroups method, of class Context.
  456. */
  457. @Test
  458. public void testGetSpecialGroups() throws SQLException, AuthorizeException
  459. {
  460. new NonStrictExpectations(AuthorizeManager.class)
  461. {{
  462. // Allow Admin permissions - needed to create a new Group
  463. AuthorizeManager.isAdmin((Context) any); result = true;
  464. }};
  465. // To test special groups we need a new Context object
  466. Context instance = new Context();
  467. // Create a new group & add it as a special group
  468. Group group = Group.create(instance);
  469. int groupID = group.getID();
  470. instance.setSpecialGroup(groupID);
  471. // Also add Administrator group as a special group
  472. Group adminGroup = Group.find(instance, Group.ADMIN_ID);
  473. int adminGroupID = adminGroup.getID();
  474. instance.setSpecialGroup(adminGroupID);
  475. // Now get our special groups
  476. Group[] specialGroups = instance.getSpecialGroups();
  477. assertThat("testGetSpecialGroup 0", specialGroups.length, equalTo(2));
  478. assertThat("testGetSpecialGroup 1", specialGroups[0], equalTo(group));
  479. assertThat("testGetSpecialGroup 1", specialGroups[1], equalTo(adminGroup));
  480. // Cleanup our context
  481. cleanupContext(instance);
  482. }
  483. /**
  484. * Test of finalize method, of class Context.
  485. */
  486. @Test
  487. public void testFinalize() throws Throwable {
  488. // We need a new Context object
  489. Context instance = new Context();
  490. instance.finalize();
  491. // Finalize is like abort()...should invalidate our context
  492. assertThat("testSetSpecialGroup 0", instance.isValid(), equalTo(false));
  493. // Cleanup our context
  494. cleanupContext(instance);
  495. }
  496. }