Skip to content

Commit

Permalink
+ Mockito test framework
Browse files Browse the repository at this point in the history
+ eclipse-ee4j#5417: remove "optimization" when composite attribute default is literal instead of VE, this will fail when the final value itself is VE

Signed-off-by: pizzi80 <paolo@given2.com>
  • Loading branch information
pizzi80 committed Mar 25, 2024
2 parents 668f7d2 + 402dd2d commit d140e4e
Show file tree
Hide file tree
Showing 85 changed files with 1,375 additions and 1,825 deletions.
19 changes: 6 additions & 13 deletions impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,16 @@
</dependency>

<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymockclassextension</artifactId>
<version>3.2</version>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static jakarta.faces.component.UIComponent.ATTRS_WITH_DECLARED_DEFAULT_VALUES;
import static jakarta.faces.component.UIComponent.BEANINFO_KEY;
import static jakarta.faces.component.UIComponent.COMPOSITE_COMPONENT_TYPE_KEY;
import static java.beans.Introspector.getBeanInfo;
import static java.beans.PropertyEditorManager.findEditor;
import static java.text.MessageFormat.format;
import static java.util.Collections.unmodifiableMap;
Expand All @@ -37,7 +36,6 @@

import java.beans.BeanDescriptor;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
Expand All @@ -57,16 +55,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sun.faces.application.ApplicationAssociate;
import com.sun.faces.application.ConverterPropertyEditorFactory;
import com.sun.faces.application.ViewMemberInstanceFactoryMetadataMap;
import com.sun.faces.cdi.CdiUtils;
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.util.FacesLogger;
import com.sun.faces.util.MessageUtils;
import com.sun.faces.util.ReflectionUtils;
import com.sun.faces.util.Util;

import jakarta.el.ExpressionFactory;
import jakarta.el.ValueExpression;
import jakarta.enterprise.inject.spi.BeanManager;
Expand All @@ -83,6 +71,16 @@
import jakarta.faces.validator.Validator;
import jakarta.faces.view.ViewDeclarationLanguage;

import com.sun.faces.application.ApplicationAssociate;
import com.sun.faces.application.ConverterPropertyEditorFactory;
import com.sun.faces.application.ViewMemberInstanceFactoryMetadataMap;
import com.sun.faces.cdi.CdiUtils;
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.util.FacesLogger;
import com.sun.faces.util.MessageUtils;
import com.sun.faces.util.ReflectionUtils;
import com.sun.faces.util.Util;

public class InstanceFactory {

// Log instance for this class
Expand Down Expand Up @@ -805,21 +803,12 @@ private void pushDeclaredDefaultValuesToAttributesMap(FacesContext context, Bean
ExpressionFactory expressionFactory) {

Collection<String> attributesWithDeclaredDefaultValues = null;
PropertyDescriptor[] propertyDescriptors = null;

for (PropertyDescriptor propertyDescriptor : componentMetadata.getPropertyDescriptors()) {
Object defaultValue = propertyDescriptor.getValue("default");

if (defaultValue != null) {
String key = propertyDescriptor.getName();
boolean isLiteralText = false;

if (defaultValue instanceof ValueExpression) {
isLiteralText = ((ValueExpression) defaultValue).isLiteralText();
if (isLiteralText) {
defaultValue = ((ValueExpression) defaultValue).getValue(context.getELContext());
}
}

// Ensure this attribute is not a method-signature. method-signature
// declared default values are handled in retargetMethodExpressions.
Expand All @@ -834,24 +823,6 @@ private void pushDeclaredDefaultValuesToAttributesMap(FacesContext context, Bean
}
}
attributesWithDeclaredDefaultValues.add(key);

// Only store the attribute if it is literal text. If it
// is a ValueExpression, it will be handled explicitly in
// CompositeComponentAttributesELResolver.ExpressionEvalMap.get().
// If it is a MethodExpression, it will be dealt with in
// retargetMethodExpressions.
if (isLiteralText) {
try {
if (propertyDescriptors == null) {
propertyDescriptors = getBeanInfo(component.getClass()).getPropertyDescriptors();
}
} catch (IntrospectionException e) {
throw new FacesException(e);
}

defaultValue = convertValueToTypeIfNecessary(key, defaultValue, propertyDescriptors, expressionFactory);
attrs.put(key, defaultValue);
}
}
}
}
Expand Down
77 changes: 44 additions & 33 deletions impl/src/main/java/com/sun/faces/context/FacesContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

package com.sun.faces.context;

import static java.util.Collections.emptyIterator;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toSet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -32,7 +29,6 @@
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import com.sun.faces.cdi.CdiExtension;
import com.sun.faces.el.ELContextImpl;
Expand Down Expand Up @@ -144,8 +140,8 @@ public Application getApplication() {
if (null != application) {
return application;
}
ApplicationFactory factory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
application = factory.getApplication();
ApplicationFactory aFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
application = aFactory.getApplication();
assert null != application;
return application;
}
Expand Down Expand Up @@ -256,7 +252,7 @@ public ELContext getELContext() {
@Override
public Iterator<String> getClientIdsWithMessages() {
assertNotReleased();
return componentMessageLists == null ? emptyIterator() : componentMessageLists.keySet().iterator();
return componentMessageLists == null ? Collections.<String>emptyList().iterator() : componentMessageLists.keySet().iterator();
}

/**
Expand Down Expand Up @@ -288,7 +284,15 @@ public List<FacesMessage> getMessageList() {

assertNotReleased();

return componentMessageLists == null ? emptyList() : componentMessageLists.values().stream().flatMap(Collection::stream).toList();
if (null == componentMessageLists) {
return Collections.unmodifiableList(Collections.<FacesMessage>emptyList());
} else {
List<FacesMessage> messages = new ArrayList<>();
for (List<FacesMessage> list : componentMessageLists.values()) {
messages.addAll(list);
}
return Collections.unmodifiableList(messages);
}

}

Expand All @@ -300,12 +304,11 @@ public List<FacesMessage> getMessageList(String clientId) {

assertNotReleased();

if (componentMessageLists == null) {
return emptyList();
}
else {
if (null == componentMessageLists) {
return Collections.unmodifiableList(Collections.<FacesMessage>emptyList());
} else {
List<FacesMessage> list = componentMessageLists.get(clientId);
return list != null ? Collections.unmodifiableList(list) : emptyList();
return Collections.unmodifiableList(list != null ? list : Collections.<FacesMessage>emptyList());
}

}
Expand All @@ -316,14 +319,16 @@ public List<FacesMessage> getMessageList(String clientId) {
@Override
public Iterator<FacesMessage> getMessages() {
assertNotReleased();
if (componentMessageLists == null) {
return emptyIterator();
if (null == componentMessageLists) {
List<FacesMessage> emptyList = Collections.emptyList();
return emptyList.iterator();
}

if (!componentMessageLists.isEmpty()) {
if (componentMessageLists.size() > 0) {
return new ComponentMessagesIterator(componentMessageLists);
} else {
return emptyIterator();
List<FacesMessage> emptyList = Collections.emptyList();
return emptyList.iterator();
}
}

Expand All @@ -336,12 +341,17 @@ public Iterator<FacesMessage> getMessages(String clientId) {

// If no messages have been enqueued at all,
// return an empty List Iterator
if (componentMessageLists == null) {
return emptyIterator();
if (null == componentMessageLists) {
List<FacesMessage> emptyList = Collections.emptyList();
return emptyList.iterator();
}

List<FacesMessage> list = componentMessageLists.get(clientId);
return list == null ? emptyIterator() : list.iterator();
if (list == null) {
List<FacesMessage> emptyList = Collections.emptyList();
return emptyList.iterator();
}
return list.iterator();
}

/**
Expand Down Expand Up @@ -390,7 +400,7 @@ public ResponseStream getResponseStream() {
@Override
public void setResponseStream(ResponseStream responseStream) {
assertNotReleased();
Util.notNull("responseStream", responseStream);
Util.notNull("responseStrean", responseStream);
this.responseStream = responseStream;
}

Expand All @@ -412,13 +422,10 @@ public void setViewRoot(UIViewRoot root) {
Util.notNull("root", root);

if (viewRoot != null && !viewRoot.equals(root)) {
// if exists, retrieve the view map
Map<String, Object> viewMap = viewRoot.getViewMap(false);

// if exists, clear the view map
if (viewMap != null) viewMap.clear();

// clear the relevant request attributes
if (viewMap != null) {
viewMap.clear();
}
RequestStateManager.clearAttributesOnChangeOfView(this);
}

Expand Down Expand Up @@ -467,8 +474,12 @@ public void addMessage(String clientId, FacesMessage message) {
}

// Add this message to our internal queue
componentMessageLists.computeIfAbsent(clientId, k -> new ArrayList<>(2)).add(message); // 2 messages for each component is enough on average?

List<FacesMessage> list = componentMessageLists.get(clientId);
if (list == null) {
list = new ArrayList<>();
componentMessageLists.put(clientId, list);
}
list.add(message);
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Adding Message[sourceId=" + (clientId != null ? clientId : "<<NONE>>") + ",summary=" + message.getSummary() + ")");
}
Expand Down Expand Up @@ -593,7 +604,7 @@ public boolean getRenderResponse() {
@Override
public List<String> getResourceLibraryContracts() {
assertNotReleased();
return null == resourceLibraryContracts ? emptyList() : resourceLibraryContracts;
return null == resourceLibraryContracts ? Collections.emptyList() : resourceLibraryContracts;
}

@Override
Expand Down Expand Up @@ -648,11 +659,11 @@ private void assertNotReleased() {

private static final class ComponentMessagesIterator implements Iterator<FacesMessage> {

private final Map<String, List<FacesMessage>> messages;
private Map<String, List<FacesMessage>> messages;
private int outerIndex = -1;
private final int messagesSize;
private int messagesSize;
private Iterator<FacesMessage> inner;
private final Iterator<String> keys;
private Iterator<String> keys;

// ------------------------------------------------------- Constructors

Expand Down
4 changes: 2 additions & 2 deletions impl/src/main/java/jakarta/faces/convert/MessageFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ static FacesMessage getMessage(Locale locale, String messageId, Object... params
*
* @param context - the <code>FacesContext</code> for the current request
* @param messageId - the key of the message in the resource bundle
* @param params - substittion parameters
* @param params - substitution parameters
*
* @return a localized <code>FacesMessage</code> with the severity of FacesMessage.SEVERITY_ERROR
*/
Expand Down Expand Up @@ -218,7 +218,7 @@ static FacesMessage getMessage(FacesContext context, String messageId, Object...
static Object getLabel(FacesContext context, UIComponent component) {

Object o = component.getAttributes().get("label");
if (o == null || o instanceof String && ((String) o).length() == 0) {
if (o == null || o instanceof String && ((String) o).isEmpty()) {
o = component.getValueExpression("label");
}
// Use the "clientId" if there was no label specified.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package com.sun.faces.application;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import jakarta.faces.application.FacesMessage;

Expand Down Expand Up @@ -69,6 +69,7 @@ private void persistAndCheck(FacesMessage message) {
String mDetail, mDetail1 = null;
String severity, severity1 = null;
ByteArrayOutputStream bos = null;
ByteArrayInputStream bis = null;

mSummary = message.getSummary();
mDetail = message.getDetail();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@

package com.sun.faces.application.resource;

import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.jupiter.api.Test;

public class ResourceCacheTest {

Expand All @@ -36,7 +35,7 @@ public void noMemoryLeakWithContracts() {
// now we clear the contracts list, which was used to create the cache entry.
contracts.clear();
ResourceInfo cachedResourceInfo = cache.get("bar.gif", null, null, Arrays.asList("foo", "baz"));
assertThat(cachedResourceInfo, is(resourceInfo));
assertEquals(resourceInfo, cachedResourceInfo);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.sun.faces.config.processor;

import static com.sun.faces.util.Util.createLocalDocumentBuilderFactory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand All @@ -28,7 +28,7 @@
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
Expand Down
Loading

0 comments on commit d140e4e

Please sign in to comment.