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 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/main/java/br/gov/frameworkdemoiselle/util/Strings.java
... ... @@ -60,7 +60,7 @@ public class Strings {
60 60  
61 61 public static String insertZeros(String string, int howMuchZeros) {
62 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 65 for (int j = 0; j < difference; j++) {
66 66 result.insert(0, '0');
... ...
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 }
... ...
impl/core/src/test/java/br/gov/frameworkdemoiselle/util/StringsTest.java
... ... @@ -36,31 +36,23 @@
36 36 */
37 37 package br.gov.frameworkdemoiselle.util;
38 38  
39   -import static org.easymock.EasyMock.expect;
40 39 import static org.junit.Assert.assertEquals;
41 40 import static org.junit.Assert.assertFalse;
42 41 import static org.junit.Assert.assertNull;
43 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 43 import static org.powermock.api.easymock.PowerMock.verifyAll;
47 44  
48   -import java.lang.reflect.Field;
49   -
50   -import org.easymock.EasyMock;
51 45 import org.junit.Test;
52 46 import org.junit.runner.RunWith;
53   -import org.powermock.core.classloader.annotations.PrepareForTest;
54 47 import org.powermock.modules.junit4.PowerMockRunner;
55 48  
56 49 import br.gov.frameworkdemoiselle.annotation.Ignore;
57 50  
58 51 @RunWith(PowerMockRunner.class)
59   -@PrepareForTest({ Reflections.class })
60 52 public class StringsTest {
61 53  
62 54 @Test
63   - public void testGetString() {
  55 + public void getString() {
64 56 testEqualsGetString("teste", "teste");
65 57 testEqualsGetString("", "");
66 58 testEqualsGetString(null, null);
... ... @@ -77,7 +69,7 @@ public class StringsTest {
77 69 }
78 70  
79 71 @Test
80   - public void testIsEmpty() {
  72 + public void isEmpty() {
81 73 assertTrue(Strings.isEmpty(null));
82 74 assertTrue(Strings.isEmpty(""));
83 75 assertTrue(Strings.isEmpty(" "));
... ... @@ -89,7 +81,7 @@ public class StringsTest {
89 81 }
90 82  
91 83 @Test
92   - public void testIsResourceBundleKeyFormat() {
  84 + public void isResourceBundleKeyFormat() {
93 85 assertTrue(Strings.isResourceBundleKeyFormat("{x}"));
94 86 assertTrue(Strings.isResourceBundleKeyFormat("{.}"));
95 87 assertTrue(Strings.isResourceBundleKeyFormat("{*}"));
... ... @@ -107,7 +99,7 @@ public class StringsTest {
107 99 }
108 100  
109 101 @Test
110   - public void testCamelCaseToSymbolSeparated() {
  102 + public void camelCaseToSymbolSeparated() {
111 103 assertEquals(null, Strings.camelCaseToSymbolSeparated(null, null));
112 104 assertEquals(null, Strings.camelCaseToSymbolSeparated(null, "."));
113 105 assertEquals("myvar", Strings.camelCaseToSymbolSeparated("myVar", null));
... ... @@ -119,7 +111,7 @@ public class StringsTest {
119 111 }
120 112  
121 113 @Test
122   - public void testFirstToUpper() {
  114 + public void firstToUpper() {
123 115 assertNull(Strings.firstToUpper(null));
124 116 assertEquals("", Strings.firstToUpper(""));
125 117 assertEquals("A", Strings.firstToUpper("a"));
... ... @@ -129,15 +121,14 @@ public class StringsTest {
129 121 assertEquals("Ab", Strings.firstToUpper("ab"));
130 122 assertEquals("AB", Strings.firstToUpper("aB"));
131 123 }
132   -
  124 +
133 125 @Test
134   - public void testToStringWhenObjectIsNull() {
  126 + public void toStringWhenObjectIsNull() {
135 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 132 @SuppressWarnings("unused")
142 133 class Test {
143 134  
... ... @@ -149,24 +140,12 @@ public class StringsTest {
149 140  
150 141 @Ignore
151 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 150 verifyAll();
172 151 }
... ... @@ -175,9 +154,9 @@ public class StringsTest {
175 154 String out = Strings.getString(in, params);
176 155 assertEquals(expected, out);
177 156 }
178   -
  157 +
179 158 @Test
180   - public void testRemoveBraces() {
  159 + public void removeBraces() {
181 160 assertNull(Strings.removeBraces(null));
182 161 assertEquals("", Strings.removeBraces(""));
183 162 assertEquals(" ", Strings.removeBraces(" "));
... ... @@ -191,9 +170,9 @@ public class StringsTest {
191 170 assertEquals("?", Strings.removeBraces("{?}"));
192 171 assertEquals("*", Strings.removeBraces("{*}"));
193 172 }
194   -
  173 +
195 174 @Test
196   - public void testInsertBraces() {
  175 + public void insertBraces() {
197 176 assertNull(Strings.insertBraces(null));
198 177 assertEquals("", Strings.insertBraces(""));
199 178 assertEquals(" ", Strings.insertBraces(" "));
... ... @@ -203,18 +182,28 @@ public class StringsTest {
203 182 assertEquals("{*}", Strings.insertBraces("*"));
204 183 assertEquals("{?}", Strings.insertBraces("?"));
205 184 }
206   -
  185 +
207 186 @Test
208   - public void testRemoveCharsWhenStringIsNull() {
  187 + public void removeCharsWhenStringIsNull() {
209 188 assertEquals(null, Strings.removeChars(null, 'a'));
210 189 }
211   -
  190 +
212 191 @Test
213   - public void testRemoveCharsWhenStringIsNotNull() {
  192 + public void removeCharsWhenStringIsNotNull() {
214 193 String string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus lobortis.";
215 194 string = Strings.removeChars(string, 'L', 'l');
216 195 assertEquals(-1, string.indexOf('L'));
217 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 }
... ...