Commit b06f2524df69112cb07598be9ad5e14ae9aff869

Authored by Wilson Guimarães
1 parent 26abe5fe
Exists in master

Aumento da cobertura de testes.

impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/interceptor/RequiredRoleInterceptor.java
@@ -94,7 +94,6 @@ public class RequiredRoleInterceptor implements Serializable { @@ -94,7 +94,6 @@ public class RequiredRoleInterceptor implements Serializable {
94 */ 94 */
95 @AroundInvoke 95 @AroundInvoke
96 public Object manage(final InvocationContext ic) throws Exception { 96 public Object manage(final InvocationContext ic) throws Exception {
97 -  
98 List<String> roles = getRoles(ic); 97 List<String> roles = getRoles(ic);
99 98
100 if (securityContext.get().isLoggedIn()) { 99 if (securityContext.get().isLoggedIn()) {
@@ -120,7 +119,6 @@ public class RequiredRoleInterceptor implements Serializable { @@ -120,7 +119,6 @@ public class RequiredRoleInterceptor implements Serializable {
120 logger.debug(bundle.getString("user-has-role", securityContext.get().getUser().getId(), userRoles)); 119 logger.debug(bundle.getString("user-has-role", securityContext.get().getUser().getId(), userRoles));
121 120
122 return ic.proceed(); 121 return ic.proceed();
123 -  
124 } 122 }
125 123
126 /** 124 /**
@@ -131,22 +129,17 @@ public class RequiredRoleInterceptor implements Serializable { @@ -131,22 +129,17 @@ public class RequiredRoleInterceptor implements Serializable {
131 * @return the value defined in {@code @RequiredRole} annotation 129 * @return the value defined in {@code @RequiredRole} annotation
132 */ 130 */
133 private List<String> getRoles(InvocationContext ic) { 131 private List<String> getRoles(InvocationContext ic) {
134 -  
135 String[] roles = {}; 132 String[] roles = {};
136 133
137 if (ic.getMethod().getAnnotation(RequiredRole.class) == null) { 134 if (ic.getMethod().getAnnotation(RequiredRole.class) == null) {
138 -  
139 if (ic.getTarget().getClass().getAnnotation(RequiredRole.class) != null) { 135 if (ic.getTarget().getClass().getAnnotation(RequiredRole.class) != null) {
140 roles = ic.getTarget().getClass().getAnnotation(RequiredRole.class).value(); 136 roles = ic.getTarget().getClass().getAnnotation(RequiredRole.class).value();
141 } 137 }
142 -  
143 } else { 138 } else {
144 -  
145 roles = ic.getMethod().getAnnotation(RequiredRole.class).value(); 139 roles = ic.getMethod().getAnnotation(RequiredRole.class).value();
146 -  
147 } 140 }
148 141
149 return Arrays.asList(roles); 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,7 +93,6 @@ public class DefaultMessageTest {
93 93
94 @Test 94 @Test
95 public void testDefaultSeverity() { 95 public void testDefaultSeverity() {
96 -  
97 message = new DefaultMessage(null); 96 message = new DefaultMessage(null);
98 assertEquals(SeverityType.INFO, message.getSeverity()); 97 assertEquals(SeverityType.INFO, message.getSeverity());
99 98
@@ -253,7 +252,6 @@ public class DefaultMessageTest { @@ -253,7 +252,6 @@ public class DefaultMessageTest {
253 252
254 // @Test 253 // @Test
255 public void testErrorMessagesEnum() { 254 public void testErrorMessagesEnum() {
256 -  
257 message = ErrorMessages.FIRST_ERROR_KEY; 255 message = ErrorMessages.FIRST_ERROR_KEY;
258 assertEquals(SeverityType.ERROR, message.getSeverity()); 256 assertEquals(SeverityType.ERROR, message.getSeverity());
259 assertEquals("First error message text", message.getText()); 257 assertEquals("First error message text", message.getText());
@@ -283,7 +281,6 @@ public class DefaultMessageTest { @@ -283,7 +281,6 @@ public class DefaultMessageTest {
283 281
284 // @Test 282 // @Test
285 public void testMessagesInterface() { 283 public void testMessagesInterface() {
286 -  
287 message = MessagesInterface.FIRST_KEY; 284 message = MessagesInterface.FIRST_KEY;
288 assertEquals(SeverityType.INFO, message.getSeverity()); 285 assertEquals(SeverityType.INFO, message.getSeverity());
289 assertEquals("First message text", message.getText()); 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,11 +82,13 @@ public class DelegateCrudTest {
82 expect(Beans.getReference(EasyMock.anyObject(Class.class))).andReturn(mockCrud); 82 expect(Beans.getReference(EasyMock.anyObject(Class.class))).andReturn(mockCrud);
83 83
84 mockCrud.delete(1L); 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 delegateCrud.delete(1L); 89 delegateCrud.delete(1L);
88 90
89 - verifyAll(); 91 + PowerMock.verify();
90 } 92 }
91 93
92 @Test 94 @Test
impl/core/src/test/java/br/gov/frameworkdemoiselle/util/ResourceBundleTest.java
@@ -36,57 +36,107 @@ @@ -36,57 +36,107 @@
36 */ 36 */
37 package br.gov.frameworkdemoiselle.util; 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 import org.junit.Before; 52 import org.junit.Before;
44 import org.junit.Test; 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 public class ResourceBundleTest { 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 private ResourceBundle resourceBundle; 72 private ResourceBundle resourceBundle;
53 - private transient java.util.ResourceBundle resourceBundleMocked; 73 +
  74 + private java.util.ResourceBundle mockResourceBundle;
54 75
55 @Before 76 @Before
56 public void setUp() throws Exception { 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 @Test 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 @Test 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 @Test 103 @Test
78 public void testGetLocale() { 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 @Test 108 @Test
86 public void testKeySet() { 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 }