Commit c077b2fb6edc042e9eaf9cef3ac1bcebb3d119f4

Authored by Cleverson Sacramento
2 parents 91ac2acb ade9dd29
Exists in master

Merge branch 'master' of git@github.com:demoiselle/framework.git

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/main/java/br/gov/frameworkdemoiselle/util/Strings.java
@@ -60,7 +60,7 @@ public class Strings { @@ -60,7 +60,7 @@ public class Strings {
60 60
61 public static String insertZeros(String string, int howMuchZeros) { 61 public static String insertZeros(String string, int howMuchZeros) {
62 StringBuffer result = new StringBuffer((string == null ? "" : string).trim()); 62 StringBuffer result = new StringBuffer((string == null ? "" : string).trim());
63 - int difference = howMuchZeros - string.length(); 63 + int difference = howMuchZeros - result.toString().length();
64 64
65 for (int j = 0; j < difference; j++) { 65 for (int j = 0; j < difference; j++) {
66 result.insert(0, '0'); 66 result.insert(0, '0');
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 }
impl/core/src/test/java/br/gov/frameworkdemoiselle/util/StringsTest.java
@@ -36,31 +36,23 @@ @@ -36,31 +36,23 @@
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.junit.Assert.assertEquals; 39 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertFalse; 40 import static org.junit.Assert.assertFalse;
42 import static org.junit.Assert.assertNull; 41 import static org.junit.Assert.assertNull;
43 import static org.junit.Assert.assertTrue; 42 import static org.junit.Assert.assertTrue;
44 -import static org.powermock.api.easymock.PowerMock.mockStatic;  
45 -import static org.powermock.api.easymock.PowerMock.replayAll;  
46 import static org.powermock.api.easymock.PowerMock.verifyAll; 43 import static org.powermock.api.easymock.PowerMock.verifyAll;
47 44
48 -import java.lang.reflect.Field;  
49 -  
50 -import org.easymock.EasyMock;  
51 import org.junit.Test; 45 import org.junit.Test;
52 import org.junit.runner.RunWith; 46 import org.junit.runner.RunWith;
53 -import org.powermock.core.classloader.annotations.PrepareForTest;  
54 import org.powermock.modules.junit4.PowerMockRunner; 47 import org.powermock.modules.junit4.PowerMockRunner;
55 48
56 import br.gov.frameworkdemoiselle.annotation.Ignore; 49 import br.gov.frameworkdemoiselle.annotation.Ignore;
57 50
58 @RunWith(PowerMockRunner.class) 51 @RunWith(PowerMockRunner.class)
59 -@PrepareForTest({ Reflections.class })  
60 public class StringsTest { 52 public class StringsTest {
61 53
62 @Test 54 @Test
63 - public void testGetString() { 55 + public void getString() {
64 testEqualsGetString("teste", "teste"); 56 testEqualsGetString("teste", "teste");
65 testEqualsGetString("", ""); 57 testEqualsGetString("", "");
66 testEqualsGetString(null, null); 58 testEqualsGetString(null, null);
@@ -77,7 +69,7 @@ public class StringsTest { @@ -77,7 +69,7 @@ public class StringsTest {
77 } 69 }
78 70
79 @Test 71 @Test
80 - public void testIsEmpty() { 72 + public void isEmpty() {
81 assertTrue(Strings.isEmpty(null)); 73 assertTrue(Strings.isEmpty(null));
82 assertTrue(Strings.isEmpty("")); 74 assertTrue(Strings.isEmpty(""));
83 assertTrue(Strings.isEmpty(" ")); 75 assertTrue(Strings.isEmpty(" "));
@@ -89,7 +81,7 @@ public class StringsTest { @@ -89,7 +81,7 @@ public class StringsTest {
89 } 81 }
90 82
91 @Test 83 @Test
92 - public void testIsResourceBundleKeyFormat() { 84 + public void isResourceBundleKeyFormat() {
93 assertTrue(Strings.isResourceBundleKeyFormat("{x}")); 85 assertTrue(Strings.isResourceBundleKeyFormat("{x}"));
94 assertTrue(Strings.isResourceBundleKeyFormat("{.}")); 86 assertTrue(Strings.isResourceBundleKeyFormat("{.}"));
95 assertTrue(Strings.isResourceBundleKeyFormat("{*}")); 87 assertTrue(Strings.isResourceBundleKeyFormat("{*}"));
@@ -107,7 +99,7 @@ public class StringsTest { @@ -107,7 +99,7 @@ public class StringsTest {
107 } 99 }
108 100
109 @Test 101 @Test
110 - public void testCamelCaseToSymbolSeparated() { 102 + public void camelCaseToSymbolSeparated() {
111 assertEquals(null, Strings.camelCaseToSymbolSeparated(null, null)); 103 assertEquals(null, Strings.camelCaseToSymbolSeparated(null, null));
112 assertEquals(null, Strings.camelCaseToSymbolSeparated(null, ".")); 104 assertEquals(null, Strings.camelCaseToSymbolSeparated(null, "."));
113 assertEquals("myvar", Strings.camelCaseToSymbolSeparated("myVar", null)); 105 assertEquals("myvar", Strings.camelCaseToSymbolSeparated("myVar", null));
@@ -119,7 +111,7 @@ public class StringsTest { @@ -119,7 +111,7 @@ public class StringsTest {
119 } 111 }
120 112
121 @Test 113 @Test
122 - public void testFirstToUpper() { 114 + public void firstToUpper() {
123 assertNull(Strings.firstToUpper(null)); 115 assertNull(Strings.firstToUpper(null));
124 assertEquals("", Strings.firstToUpper("")); 116 assertEquals("", Strings.firstToUpper(""));
125 assertEquals("A", Strings.firstToUpper("a")); 117 assertEquals("A", Strings.firstToUpper("a"));
@@ -129,15 +121,14 @@ public class StringsTest { @@ -129,15 +121,14 @@ public class StringsTest {
129 assertEquals("Ab", Strings.firstToUpper("ab")); 121 assertEquals("Ab", Strings.firstToUpper("ab"));
130 assertEquals("AB", Strings.firstToUpper("aB")); 122 assertEquals("AB", Strings.firstToUpper("aB"));
131 } 123 }
132 - 124 +
133 @Test 125 @Test
134 - public void testToStringWhenObjectIsNull() { 126 + public void toStringWhenObjectIsNull() {
135 assertEquals("", Strings.toString(null)); 127 assertEquals("", Strings.toString(null));
136 } 128 }
137 -  
138 - @Test  
139 - public void testToString() throws SecurityException, NoSuchFieldException {  
140 129
  130 + @Test
  131 + public void classToString() throws SecurityException, NoSuchFieldException {
141 @SuppressWarnings("unused") 132 @SuppressWarnings("unused")
142 class Test { 133 class Test {
143 134
@@ -149,24 +140,12 @@ public class StringsTest { @@ -149,24 +140,12 @@ public class StringsTest {
149 140
150 @Ignore 141 @Ignore
151 private String ignore = "ignoreMe"; 142 private String ignore = "ignoreMe";
152 -  
153 - }  
154 -  
155 - mockStatic(Reflections.class);  
156 - Test test = new Test();  
157 -  
158 - expect(Reflections.getNonStaticDeclaredFields(test.getClass())).andReturn(Test.class.getDeclaredFields());  
159 - expect(Reflections.getFieldValue(EasyMock.anyObject(Field.class), EasyMock.anyObject())).andReturn("myName");  
160 - expect(Reflections.getFieldValue(EasyMock.anyObject(Field.class), EasyMock.anyObject()))  
161 - .andReturn("myLastname");  
162 - expect(Reflections.getFieldValue(EasyMock.anyObject(Field.class), EasyMock.anyObject())).andReturn(null);  
163 - expect(Reflections.getFieldValue(EasyMock.anyObject(Field.class), EasyMock.anyObject())).andReturn("Object");  
164 143
165 - replayAll(Reflections.class); 144 + }
166 145
167 - // FIXME Este this$0=Object não deveria aparecer!  
168 - assertEquals("Test [name=myName, lastname=myLastname, nullField=null, this$0=Object]",  
169 - Strings.toString(new Test())); 146 + String result = Strings.toString(new Test());
  147 +
  148 + assertTrue(result.contains("Test [name=myName, lastname=myLastname, nullField=null, this"));
170 149
171 verifyAll(); 150 verifyAll();
172 } 151 }
@@ -175,9 +154,9 @@ public class StringsTest { @@ -175,9 +154,9 @@ public class StringsTest {
175 String out = Strings.getString(in, params); 154 String out = Strings.getString(in, params);
176 assertEquals(expected, out); 155 assertEquals(expected, out);
177 } 156 }
178 - 157 +
179 @Test 158 @Test
180 - public void testRemoveBraces() { 159 + public void removeBraces() {
181 assertNull(Strings.removeBraces(null)); 160 assertNull(Strings.removeBraces(null));
182 assertEquals("", Strings.removeBraces("")); 161 assertEquals("", Strings.removeBraces(""));
183 assertEquals(" ", Strings.removeBraces(" ")); 162 assertEquals(" ", Strings.removeBraces(" "));
@@ -191,9 +170,9 @@ public class StringsTest { @@ -191,9 +170,9 @@ public class StringsTest {
191 assertEquals("?", Strings.removeBraces("{?}")); 170 assertEquals("?", Strings.removeBraces("{?}"));
192 assertEquals("*", Strings.removeBraces("{*}")); 171 assertEquals("*", Strings.removeBraces("{*}"));
193 } 172 }
194 - 173 +
195 @Test 174 @Test
196 - public void testInsertBraces() { 175 + public void insertBraces() {
197 assertNull(Strings.insertBraces(null)); 176 assertNull(Strings.insertBraces(null));
198 assertEquals("", Strings.insertBraces("")); 177 assertEquals("", Strings.insertBraces(""));
199 assertEquals(" ", Strings.insertBraces(" ")); 178 assertEquals(" ", Strings.insertBraces(" "));
@@ -203,18 +182,28 @@ public class StringsTest { @@ -203,18 +182,28 @@ public class StringsTest {
203 assertEquals("{*}", Strings.insertBraces("*")); 182 assertEquals("{*}", Strings.insertBraces("*"));
204 assertEquals("{?}", Strings.insertBraces("?")); 183 assertEquals("{?}", Strings.insertBraces("?"));
205 } 184 }
206 - 185 +
207 @Test 186 @Test
208 - public void testRemoveCharsWhenStringIsNull() { 187 + public void removeCharsWhenStringIsNull() {
209 assertEquals(null, Strings.removeChars(null, 'a')); 188 assertEquals(null, Strings.removeChars(null, 'a'));
210 } 189 }
211 - 190 +
212 @Test 191 @Test
213 - public void testRemoveCharsWhenStringIsNotNull() { 192 + public void removeCharsWhenStringIsNotNull() {
214 String string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus lobortis."; 193 String string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus lobortis.";
215 string = Strings.removeChars(string, 'L', 'l'); 194 string = Strings.removeChars(string, 'L', 'l');
216 assertEquals(-1, string.indexOf('L')); 195 assertEquals(-1, string.indexOf('L'));
217 assertEquals(-1, string.indexOf('l')); 196 assertEquals(-1, string.indexOf('l'));
218 } 197 }
219 198
  199 + @Test
  200 + public void insertZeros() {
  201 + String string = "Lorem ipsum";
  202 + assertEquals("00000", Strings.insertZeros(null, 5));
  203 + assertEquals(string, Strings.insertZeros(string, string.length()-1));
  204 + assertEquals(string, Strings.insertZeros(string, string.length()));
  205 + assertEquals("0"+string, Strings.insertZeros(string, string.length()+1));
  206 + assertEquals("00"+string, Strings.insertZeros(string, string.length()+2));
  207 + }
  208 +
220 } 209 }