Commit b06f2524df69112cb07598be9ad5e14ae9aff869
1 parent
26abe5fe
Exists in
master
Aumento da cobertura de testes.
Showing
4 changed files
with
87 additions
and
45 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/RequiredRoleInterceptor.java
| ... | ... | @@ -94,7 +94,6 @@ public class RequiredRoleInterceptor implements Serializable { |
| 94 | 94 | */ |
| 95 | 95 | @AroundInvoke |
| 96 | 96 | public Object manage(final InvocationContext ic) throws Exception { |
| 97 | - | |
| 98 | 97 | List<String> roles = getRoles(ic); |
| 99 | 98 | |
| 100 | 99 | if (securityContext.get().isLoggedIn()) { |
| ... | ... | @@ -120,7 +119,6 @@ public class RequiredRoleInterceptor implements Serializable { |
| 120 | 119 | logger.debug(bundle.getString("user-has-role", securityContext.get().getUser().getId(), userRoles)); |
| 121 | 120 | |
| 122 | 121 | return ic.proceed(); |
| 123 | - | |
| 124 | 122 | } |
| 125 | 123 | |
| 126 | 124 | /** |
| ... | ... | @@ -131,22 +129,17 @@ public class RequiredRoleInterceptor implements Serializable { |
| 131 | 129 | * @return the value defined in {@code @RequiredRole} annotation |
| 132 | 130 | */ |
| 133 | 131 | private List<String> getRoles(InvocationContext ic) { |
| 134 | - | |
| 135 | 132 | String[] roles = {}; |
| 136 | 133 | |
| 137 | 134 | if (ic.getMethod().getAnnotation(RequiredRole.class) == null) { |
| 138 | - | |
| 139 | 135 | if (ic.getTarget().getClass().getAnnotation(RequiredRole.class) != null) { |
| 140 | 136 | roles = ic.getTarget().getClass().getAnnotation(RequiredRole.class).value(); |
| 141 | 137 | } |
| 142 | - | |
| 143 | 138 | } else { |
| 144 | - | |
| 145 | 139 | roles = ic.getMethod().getAnnotation(RequiredRole.class).value(); |
| 146 | - | |
| 147 | 140 | } |
| 148 | 141 | |
| 149 | 142 | return Arrays.asList(roles); |
| 150 | - | |
| 151 | 143 | } |
| 144 | + | |
| 152 | 145 | } | ... | ... |
impl/core/src/test/java/br/gov/frameworkdemoiselle/message/DefaultMessageTest.java
| ... | ... | @@ -93,7 +93,6 @@ public class DefaultMessageTest { |
| 93 | 93 | |
| 94 | 94 | @Test |
| 95 | 95 | public void testDefaultSeverity() { |
| 96 | - | |
| 97 | 96 | message = new DefaultMessage(null); |
| 98 | 97 | assertEquals(SeverityType.INFO, message.getSeverity()); |
| 99 | 98 | |
| ... | ... | @@ -253,7 +252,6 @@ public class DefaultMessageTest { |
| 253 | 252 | |
| 254 | 253 | // @Test |
| 255 | 254 | public void testErrorMessagesEnum() { |
| 256 | - | |
| 257 | 255 | message = ErrorMessages.FIRST_ERROR_KEY; |
| 258 | 256 | assertEquals(SeverityType.ERROR, message.getSeverity()); |
| 259 | 257 | assertEquals("First error message text", message.getText()); |
| ... | ... | @@ -283,7 +281,6 @@ public class DefaultMessageTest { |
| 283 | 281 | |
| 284 | 282 | // @Test |
| 285 | 283 | public void testMessagesInterface() { |
| 286 | - | |
| 287 | 284 | message = MessagesInterface.FIRST_KEY; |
| 288 | 285 | assertEquals(SeverityType.INFO, message.getSeverity()); |
| 289 | 286 | assertEquals("First message text", message.getText()); | ... | ... |
impl/core/src/test/java/br/gov/frameworkdemoiselle/template/DelegateCrudTest.java
| ... | ... | @@ -82,11 +82,13 @@ public class DelegateCrudTest { |
| 82 | 82 | expect(Beans.getReference(EasyMock.anyObject(Class.class))).andReturn(mockCrud); |
| 83 | 83 | |
| 84 | 84 | mockCrud.delete(1L); |
| 85 | - replayAll(Reflections.class, Beans.class, mockCrud); | |
| 85 | + PowerMock.expectLastCall(); | |
| 86 | + | |
| 87 | + PowerMock.replay(Reflections.class, Beans.class, mockCrud); | |
| 86 | 88 | |
| 87 | 89 | delegateCrud.delete(1L); |
| 88 | 90 | |
| 89 | - verifyAll(); | |
| 91 | + PowerMock.verify(); | |
| 90 | 92 | } |
| 91 | 93 | |
| 92 | 94 | @Test | ... | ... |
impl/core/src/test/java/br/gov/frameworkdemoiselle/util/ResourceBundleTest.java
| ... | ... | @@ -36,57 +36,107 @@ |
| 36 | 36 | */ |
| 37 | 37 | package br.gov.frameworkdemoiselle.util; |
| 38 | 38 | |
| 39 | -import static org.easymock.EasyMock.expect; | |
| 40 | -import static org.powermock.api.easymock.PowerMock.replay; | |
| 41 | -import static org.powermock.api.easymock.PowerMock.verify; | |
| 39 | +import static org.easymock.EasyMock.createMock; | |
| 40 | +import static org.easymock.EasyMock.replay; | |
| 41 | +import static org.easymock.EasyMock.verify; | |
| 42 | +import static org.junit.Assert.assertEquals; | |
| 43 | +import static org.junit.Assert.assertFalse; | |
| 44 | +import static org.junit.Assert.assertNull; | |
| 45 | +import static org.junit.Assert.assertTrue; | |
| 46 | + | |
| 47 | +import java.util.Enumeration; | |
| 48 | +import java.util.ListResourceBundle; | |
| 49 | + | |
| 50 | +import junit.framework.Assert; | |
| 42 | 51 | |
| 43 | 52 | import org.junit.Before; |
| 44 | 53 | import org.junit.Test; |
| 45 | -import org.junit.runner.RunWith; | |
| 46 | -import org.powermock.api.easymock.PowerMock; | |
| 47 | -import org.powermock.modules.junit4.PowerMockRunner; | |
| 48 | 54 | |
| 49 | -@RunWith(PowerMockRunner.class) | |
| 50 | 55 | public class ResourceBundleTest { |
| 51 | 56 | |
| 57 | + /** | |
| 58 | + * This is a workaround to mock java.util.ResourceBundle. Since getString(key) method is defined as final, there is | |
| 59 | + * no way to extend and override it. For that reason, setting expectations (i.e. expect(...)) won't work. | |
| 60 | + */ | |
| 61 | + class MockResourceBundle extends ListResourceBundle { | |
| 62 | + | |
| 63 | + private Object[][] contents = new Object[][] { { "msgWithoutParams", "no params" }, | |
| 64 | + { "msgWithParams", "params: {0}, {1}" } }; | |
| 65 | + | |
| 66 | + protected Object[][] getContents() { | |
| 67 | + return contents; | |
| 68 | + } | |
| 69 | + | |
| 70 | + }; | |
| 71 | + | |
| 52 | 72 | private ResourceBundle resourceBundle; |
| 53 | - private transient java.util.ResourceBundle resourceBundleMocked; | |
| 73 | + | |
| 74 | + private java.util.ResourceBundle mockResourceBundle; | |
| 54 | 75 | |
| 55 | 76 | @Before |
| 56 | 77 | public void setUp() throws Exception { |
| 57 | - this.resourceBundleMocked = PowerMock.createMock(java.util.ResourceBundle.class); | |
| 58 | - this.resourceBundle = new ResourceBundle(resourceBundleMocked); | |
| 78 | + mockResourceBundle = new MockResourceBundle(); | |
| 79 | + resourceBundle = new ResourceBundle(mockResourceBundle); | |
| 59 | 80 | } |
| 60 | - | |
| 81 | + | |
| 61 | 82 | @Test |
| 62 | - public void testContainsKey() { | |
| 63 | - expect(this.resourceBundleMocked.containsKey("")).andReturn(true); | |
| 64 | - replay(this.resourceBundleMocked); | |
| 65 | - this.resourceBundle.containsKey(""); | |
| 66 | - verify(this.resourceBundleMocked); | |
| 83 | + public void containsKey() { | |
| 84 | + assertTrue(resourceBundle.containsKey("msgWithoutParams")); | |
| 85 | + | |
| 86 | + assertFalse(resourceBundle.containsKey("inexistentKey")); | |
| 67 | 87 | } |
| 68 | - | |
| 88 | + | |
| 69 | 89 | @Test |
| 70 | - public void testGetKeys() { | |
| 71 | - expect(this.resourceBundleMocked.getKeys()).andReturn(null); | |
| 72 | - replay(this.resourceBundleMocked); | |
| 73 | - this.resourceBundle.getKeys(); | |
| 74 | - verify(this.resourceBundleMocked); | |
| 90 | + public void getKeys() { | |
| 91 | + int keyCount = 0; | |
| 92 | + | |
| 93 | + Enumeration<String> e = resourceBundle.getKeys(); | |
| 94 | + | |
| 95 | + while (e.hasMoreElements()) { | |
| 96 | + keyCount++; | |
| 97 | + e.nextElement(); | |
| 98 | + } | |
| 99 | + | |
| 100 | + assertEquals(resourceBundle.keySet().size(), keyCount); | |
| 75 | 101 | } |
| 76 | - | |
| 102 | + | |
| 77 | 103 | @Test |
| 78 | 104 | public void testGetLocale() { |
| 79 | - expect(this.resourceBundleMocked.getLocale()).andReturn(null); | |
| 80 | - replay(this.resourceBundleMocked); | |
| 81 | - this.resourceBundle.getLocale(); | |
| 82 | - verify(this.resourceBundleMocked); | |
| 105 | + assertNull(resourceBundle.getLocale()); | |
| 83 | 106 | } |
| 84 | - | |
| 107 | + | |
| 85 | 108 | @Test |
| 86 | 109 | public void testKeySet() { |
| 87 | - expect(this.resourceBundleMocked.keySet()).andReturn(null); | |
| 88 | - replay(this.resourceBundleMocked); | |
| 89 | - this.resourceBundle.keySet(); | |
| 90 | - verify(this.resourceBundleMocked); | |
| 110 | + assertEquals(2, resourceBundle.keySet().size()); | |
| 91 | 111 | } |
| 112 | + | |
| 113 | + @Test | |
| 114 | + public void getString() { | |
| 115 | + assertEquals("no params", resourceBundle.getString("msgWithoutParams")); | |
| 116 | + | |
| 117 | + assertEquals("params: a, b", resourceBundle.getString("msgWithParams", "a", "b")); | |
| 118 | + | |
| 119 | + assertEquals("params: {0}, {1}", resourceBundle.getString("msgWithParams")); | |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 123 | + * For this test, java.util.ResourceBundle is mocked to force an exception. Since the getString method is called | |
| 124 | + * from the actual ResourceBundle, not from the mock, it tries to find a handleGetObject method that doesn't exist. | |
| 125 | + * | |
| 126 | + * @throws Exception | |
| 127 | + */ | |
| 128 | + @Test(expected = RuntimeException.class) | |
| 129 | + public void getStringWhenHandleGetObjectThrowsException() { | |
| 130 | + mockResourceBundle = createMock(java.util.ResourceBundle.class); | |
| 131 | + resourceBundle = new ResourceBundle(mockResourceBundle); | |
| 132 | + | |
| 133 | + replay(mockResourceBundle); | |
| 134 | + | |
| 135 | + resourceBundle.getString("msgWithParams"); | |
| 136 | + | |
| 137 | + verify(mockResourceBundle); | |
| 138 | + | |
| 139 | + Assert.fail(); | |
| 140 | + } | |
| 141 | + | |
| 92 | 142 | } | ... | ... |