Author: dklco
Date: Mon May 20 21:26:55 2013
New Revision: 1484603
URL: http://svn.apache.org/r1484603
Log:
Cleaned up the class checking code, added the ability to set a default integer
and added some tests around the default handling
Added:
sling/whiteboard/dklco/sling-proxy/src/test/java/org/apache/sling/commons/proxy/TestSlingDefaultProxy.java
sling/whiteboard/dklco/sling-proxy/src/test/java/org/apache/sling/commons/proxy/samples/SlingDefaultsProxy.java
Modified:
sling/whiteboard/dklco/sling-proxy/ (props changed)
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/annotations/SlingProperty.java
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedPropertyTO.java
Propchange: sling/whiteboard/dklco/sling-proxy/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Mon May 20 21:26:55 2013
@@ -1 +1,4 @@
target
+.settings
+.classpath
+.project
Modified:
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/annotations/SlingProperty.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/annotations/SlingProperty.java?rev=1484603&r1=1484602&r2=1484603&view=diff
==============================================================================
---
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/annotations/SlingProperty.java
(original)
+++
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/annotations/SlingProperty.java
Mon May 20 21:26:55 2013
@@ -27,9 +27,9 @@ import java.lang.annotation.Target;
/**
* Annotation used to mark a Method as being a JCR backed property.
*
- * Paths starting with '/' are absolute references, and do not have to
- * be contained beneath the current Resource. Paths not starting with
- * '/' are assumed to be relative to the current Resource.
+ * Paths starting with '/' are absolute references, and do not have to be
+ * contained beneath the current Resource. Paths not starting with '/' are
+ * assumed to be relative to the current Resource.
*
* Here are 3 examples, one of each style:
*
@@ -79,6 +79,13 @@ public @interface SlingProperty {
double defaultDouble() default 0.0;
/**
+ * Sets the default integer value, used to set the value for integers.
+ *
+ * @return the default integer
+ */
+ int defaultInt() default 0;
+
+ /**
* The default long, used to set the default values for long.
*
* @return the default long
Modified:
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java?rev=1484603&r1=1484602&r2=1484603&view=diff
==============================================================================
---
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java
(original)
+++
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/SlingInvocationHandler.java
Mon May 20 21:26:55 2013
@@ -176,31 +176,31 @@ public class SlingInvocationHandler impl
}
if (to.isUseDefault()) {
- if
(to.getMethod().getReturnType().equals(Boolean.class)) {
+ Class<?> rt = to.getMethod().getReturnType();
+ if (Boolean.class.equals(rt) ||
boolean.class.equals(rt)) {
objReturn = vm.get(to.getName(),
to.getDefaultBoolean());
- } else if
(to.getMethod().getReturnType().equals(Byte[].class)) {
+ } else if (Byte[].class.equals(rt) ||
byte[].class.equals(rt)) {
objReturn = vm.get(to.getName(),
to.getDefaultBytes());
- } else if (to.getMethod().getReturnType()
- .equals(InputStream.class)) {
+ } else if (InputStream.class.equals(rt)) {
final InputStream defaultIs = new
ByteArrayInputStream(
to.getDefaultBytes());
objReturn = vm.get(to.getName(),
defaultIs);
- } else if (to.getMethod().getReturnType()
- .equals(Calendar.class)) {
+ } else if (Calendar.class.equals(rt)) {
final Calendar c =
Calendar.getInstance();
c.setTimeInMillis(to.getDefaultLong());
objReturn = vm.get(to.getName(), c);
- } else if
(to.getMethod().getReturnType().equals(Date.class)) {
+ } else if (Date.class.equals(rt)) {
objReturn = vm.get(to.getName(),
new
Date(to.getDefaultDate()));
- } else if
(to.getMethod().getReturnType().equals(Double.class)) {
+ } else if (Integer.class.equals(rt) ||
int.class.equals(rt)) {
+ objReturn = vm.get(to.getName(),
to.getDefaultInt());
+ } else if (Double.class.equals(rt) ||
double.class.equals(rt)) {
objReturn = vm.get(to.getName(),
to.getDefaultDouble());
- } else if
(to.getMethod().getReturnType().equals(Long.class)) {
+ } else if (Long.class.equals(rt) ||
long.class.equals(rt)) {
objReturn = vm.get(to.getName(),
to.getDefaultLong());
- } else if
(to.getMethod().getReturnType().equals(String.class)) {
+ } else if (String.class.equals(rt)) {
objReturn = vm.get(to.getName(),
to.getDefaultString());
- } else if (to.getMethod().getReturnType()
- .equals(String[].class)) {
+ } else if (String[].class.equals(rt)) {
objReturn = vm.get(to.getName(),
to.getDefaultStrings());
} else {
log.warn(
Modified:
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedPropertyTO.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedPropertyTO.java?rev=1484603&r1=1484602&r2=1484603&view=diff
==============================================================================
---
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedPropertyTO.java
(original)
+++
sling/whiteboard/dklco/sling-proxy/src/main/java/org/apache/sling/commons/proxy/impl/to/InvokedPropertyTO.java
Mon May 20 21:26:55 2013
@@ -39,6 +39,9 @@ public final class InvokedPropertyTO ext
/** The default double. */
private final double defaultDouble;
+ /** The default int. */
+ private final int defaultInt;
+
/** The default long. */
private final long defaultLong;
@@ -87,6 +90,7 @@ public final class InvokedPropertyTO ext
this.defaultBytes = sp.defaultBytes();
this.defaultDate = sp.defaultDate();
this.defaultDouble = sp.defaultDouble();
+ this.defaultInt = sp.defaultInt();
this.defaultLong = sp.defaultLong();
this.defaultString = sp.defaultString();
this.defaultStrings = sp.defaultStrings();
@@ -126,6 +130,15 @@ public final class InvokedPropertyTO ext
}
/**
+ * Gets the default integer.
+ *
+ * @return the default integer
+ */
+ public Integer getDefaultInt() {
+ return this.defaultInt;
+ }
+
+ /**
* Gets the default long.
*
* @return the default long
Added:
sling/whiteboard/dklco/sling-proxy/src/test/java/org/apache/sling/commons/proxy/TestSlingDefaultProxy.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/test/java/org/apache/sling/commons/proxy/TestSlingDefaultProxy.java?rev=1484603&view=auto
==============================================================================
---
sling/whiteboard/dklco/sling-proxy/src/test/java/org/apache/sling/commons/proxy/TestSlingDefaultProxy.java
(added)
+++
sling/whiteboard/dklco/sling-proxy/src/test/java/org/apache/sling/commons/proxy/TestSlingDefaultProxy.java
Mon May 20 21:26:55 2013
@@ -0,0 +1,80 @@
+package org.apache.sling.commons.proxy;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.Date;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.commons.proxy.samples.SlingDefaultsProxy;
+import org.apache.tika.io.IOUtils;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Tests the default value capability in the Sling Proxy.
+ */
+public class TestSlingDefaultProxy extends BaseSlingProxyTest {
+
+ private static final Logger log = LoggerFactory
+ .getLogger(TestSlingDefaultProxy.class);
+
+ /**
+ * This test checks all of the default value options, ensuring the
correct
+ * default value is returned.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testDefaults() throws IOException {
+
+ log.info("Testing defaults");
+
+ Resource resource =
resolver.getResource("/content/test/jcr:content");
+
+ SlingDefaultsProxy sdp =
this.slingProxyService.getProxy(resource,
+ SlingDefaultsProxy.class);
+
+ assertFalse(sdp.getBoolean());
+ assertArrayEquals(new byte[0], sdp.getBytes());
+ Calendar cal = Calendar.getInstance();
+ cal.setTimeInMillis(0);
+ assertEquals(cal, sdp.getCalendarDate());
+ assertEquals(new Date(0), sdp.getDate());
+ assertEquals(1.0, sdp.getDouble(), 1.0);
+ byte[] data = IOUtils.toByteArray(sdp.getInputStream());
+ assertArrayEquals(new byte[0], data);
+
+ assertEquals(10, sdp.getInt());
+ assertEquals(10, sdp.getLong());
+ assertEquals("bob", sdp.getString());
+ assertArrayEquals(new String[] { "bob", "jones" },
sdp.getStrings());
+
+ log.info("Tests successful");
+ }
+
+ /**
+ * This test checks to ensure the proxy returns the original value if a
+ * value is set instead of the default value.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testDefaultWithValue() throws IOException {
+ log.info("Testing default with a value");
+
+ Resource resource = resolver.getResource("/content/test");
+
+ SlingDefaultsProxy sdp =
this.slingProxyService.getProxy(resource,
+ SlingDefaultsProxy.class);
+
+ assertEquals(BaseSlingProxyTest.TITLE, sdp.getBackupTitle());
+
+ log.info("Test successful");
+ }
+
+}
Added:
sling/whiteboard/dklco/sling-proxy/src/test/java/org/apache/sling/commons/proxy/samples/SlingDefaultsProxy.java
URL:
http://svn.apache.org/viewvc/sling/whiteboard/dklco/sling-proxy/src/test/java/org/apache/sling/commons/proxy/samples/SlingDefaultsProxy.java?rev=1484603&view=auto
==============================================================================
---
sling/whiteboard/dklco/sling-proxy/src/test/java/org/apache/sling/commons/proxy/samples/SlingDefaultsProxy.java
(added)
+++
sling/whiteboard/dklco/sling-proxy/src/test/java/org/apache/sling/commons/proxy/samples/SlingDefaultsProxy.java
Mon May 20 21:26:55 2013
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.commons.proxy.samples;
+
+import java.io.InputStream;
+import java.util.Calendar;
+import java.util.Date;
+
+import org.apache.sling.commons.proxy.annotations.SlingProperty;
+
+/**
+ * A proxy for testing the defaults functionality
+ */
+public interface SlingDefaultsProxy extends SlingPropertyProxy {
+
+ @SlingProperty(name = "boolean", useDefault=true, defaultBoolean=false)
+ public boolean getBoolean();
+
+ @SlingProperty(name="bytes", useDefault=true)
+ public byte[] getBytes();
+
+ @SlingProperty(name="input", useDefault=true)
+ public InputStream getInputStream();
+
+ @SlingProperty(name="date", useDefault=true, defaultDate=0)
+ public Date getDate();
+
+ @SlingProperty(name="calendar", useDefault=true, defaultDate=0)
+ public Calendar getCalendarDate();
+
+ @SlingProperty(name="double", useDefault=true, defaultDouble=1.0)
+ public double getDouble();
+
+ @SlingProperty(name = "int", useDefault=true, defaultInt=10)
+ public int getInt();
+
+ @SlingProperty(name = "long", useDefault=true, defaultLong=10)
+ public long getLong();
+
+ @SlingProperty(name = "string", useDefault=true, defaultString="bob")
+ public String getString();
+
+ @SlingProperty(name = "strings", useDefault=true,
defaultStrings={"bob","jones"})
+ public String[] getStrings();
+
+
+ @SlingProperty(name = "jcr:title", path = "jcr:content",
useDefault=true, defaultString="Default Title")
+ public String getBackupTitle();
+}