Author: cziegeler
Date: Tue Dec 30 23:42:34 2008
New Revision: 730337
URL: http://svn.apache.org/viewvc?rev=730337&view=rev
Log:
SLING-797 - Fix class check and add junit tests to cover this.
Modified:
incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/EventUtil.java
incubator/sling/trunk/extensions/event/src/test/java/org/apache/sling/event/EventUtilTest.java
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
Modified:
incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/EventUtil.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/EventUtil.java?rev=730337&r1=730336&r2=730337&view=diff
==============================================================================
---
incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/EventUtil.java
(original)
+++
incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/EventUtil.java
Tue Dec 30 23:42:34 2008
@@ -500,13 +500,13 @@
*/
public static Value getNodePropertyValue(final ValueFactory valueFactory,
final Object eventValue) {
final Value val;
- if (eventValue.getClass().isAssignableFrom(Calendar.class)) {
+ if (eventValue instanceof Calendar) {
val = valueFactory.createValue((Calendar)eventValue);
- } else if (eventValue.getClass().isAssignableFrom(Long.class)) {
+ } else if (eventValue instanceof Long) {
val = valueFactory.createValue((Long)eventValue);
- } else if (eventValue.getClass().isAssignableFrom(Double.class)) {
+ } else if (eventValue instanceof Double) {
val = valueFactory.createValue(((Double)eventValue).doubleValue());
- } else if (eventValue.getClass().isAssignableFrom(Boolean.class)) {
+ } else if (eventValue instanceof Boolean) {
val = valueFactory.createValue((Boolean) eventValue);
} else if (eventValue instanceof String) {
val = valueFactory.createValue((String)eventValue);
Modified:
incubator/sling/trunk/extensions/event/src/test/java/org/apache/sling/event/EventUtilTest.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/event/src/test/java/org/apache/sling/event/EventUtilTest.java?rev=730337&r1=730336&r2=730337&view=diff
==============================================================================
---
incubator/sling/trunk/extensions/event/src/test/java/org/apache/sling/event/EventUtilTest.java
(original)
+++
incubator/sling/trunk/extensions/event/src/test/java/org/apache/sling/event/EventUtilTest.java
Tue Dec 30 23:42:34 2008
@@ -18,18 +18,40 @@
*/
package org.apache.sling.event;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import java.io.InputStream;
+import java.util.Calendar;
import java.util.Properties;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.jcr.ValueFactory;
+import javax.jcr.ValueFormatException;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.osgi.service.event.Event;
/**
* Tests for the EventUtil utility methods.
*/
+...@runwith(JMock.class)
public class EventUtilTest {
+ protected Mockery context;
+
+ public EventUtilTest() {
+ this.context = new JUnit4Mockery();
+ }
+
@Test public void testDistributeFlag() {
final Event distributableEvent =
EventUtil.createDistributableEvent("some/topic", null);
assertTrue(EventUtil.shouldDistribute(distributableEvent));
@@ -45,4 +67,76 @@
final Event remoteEvent = new Event("remote/event", props);
assertFalse(EventUtil.isLocal(remoteEvent));
}
+
+ @Test public void testGetNodePropertyValue() {
+ final ValueFactory factory = this.context.mock(ValueFactory.class);
+ this.context.checking(new Expectations() {{
+ allowing(factory).createValue(true);
+ will(returnValue(new ValueImpl(PropertyType.BOOLEAN)));
+ allowing(factory).createValue(false);
+ will(returnValue(new ValueImpl(PropertyType.BOOLEAN)));
+ allowing(factory).createValue(with(any(Long.class)));
+ will(returnValue(new ValueImpl(PropertyType.LONG)));
+ allowing(factory).createValue(with(any(String.class)));
+ will(returnValue(new ValueImpl(PropertyType.STRING)));
+ allowing(factory).createValue(with(any(Calendar.class)));
+ will(returnValue(new ValueImpl(PropertyType.DATE)));
+ }});
+ // boolean
+ assertEquals(PropertyType.BOOLEAN,
EventUtil.getNodePropertyValue(factory, true).getType());
+ assertEquals(PropertyType.BOOLEAN,
EventUtil.getNodePropertyValue(factory, false).getType());
+ assertEquals(PropertyType.BOOLEAN,
EventUtil.getNodePropertyValue(factory, Boolean.TRUE).getType());
+ assertEquals(PropertyType.BOOLEAN,
EventUtil.getNodePropertyValue(factory, Boolean.FALSE).getType());
+ // long
+ assertEquals(PropertyType.LONG,
EventUtil.getNodePropertyValue(factory, (long)5).getType());
+ // int = not possible
+ assertEquals(null, EventUtil.getNodePropertyValue(factory, 5));
+ // string
+ assertEquals(PropertyType.STRING,
EventUtil.getNodePropertyValue(factory, "something").getType());
+ // calendar
+ assertEquals(PropertyType.DATE,
EventUtil.getNodePropertyValue(factory, Calendar.getInstance()).getType());
+ }
+
+ private final static class ValueImpl implements Value {
+
+ private final int type;
+
+ public ValueImpl(int type) {
+ this.type = type;
+ }
+
+ public boolean getBoolean() throws ValueFormatException,
+ IllegalStateException, RepositoryException {
+ return false;
+ }
+
+ public Calendar getDate() throws ValueFormatException,
+ IllegalStateException, RepositoryException {
+ return null;
+ }
+
+ public double getDouble() throws ValueFormatException,
+ IllegalStateException, RepositoryException {
+ return 0;
+ }
+
+ public long getLong() throws ValueFormatException,
+ IllegalStateException, RepositoryException {
+ return 0;
+ }
+
+ public InputStream getStream() throws IllegalStateException,
+ RepositoryException {
+ return null;
+ }
+
+ public String getString() throws ValueFormatException,
+ IllegalStateException, RepositoryException {
+ return null;
+ }
+
+ public int getType() {
+ return this.type;
+ }
+ }
}
Modified:
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java?rev=730337&r1=730336&r2=730337&view=diff
==============================================================================
---
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
(original)
+++
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
Tue Dec 30 23:42:34 2008
@@ -113,17 +113,17 @@
throws RepositoryException {
Value val;
ValueFactory fac = session.getValueFactory();
- if(value.getClass().isAssignableFrom(Calendar.class)) {
+ if(value instanceof Calendar) {
val = fac.createValue((Calendar)value);
- } else if (value.getClass().isAssignableFrom(InputStream.class)) {
+ } else if (value instanceof InputStream) {
val = fac.createValue((InputStream)value);
- } else if (value.getClass().isAssignableFrom(Node.class)) {
+ } else if (value instanceof Node) {
val = fac.createValue((Node)value);
- } else if (value.getClass().isAssignableFrom(Long.class)) {
+ } else if (value instanceof Long) {
val = fac.createValue((Long)value);
- } else if (value.getClass().isAssignableFrom(Number.class)) {
+ } else if (value instanceof Number) {
val = fac.createValue(((Number)value).doubleValue());
- } else if (value.getClass().isAssignableFrom(Boolean.class)) {
+ } else if (value instanceof Boolean) {
val = fac.createValue((Boolean) value);
} else {
val = fac.createValue((String)value);