You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2018/03/16 16:01:06 UTC

[isis] 01/03: ISIS-1925: adds a null guard in CommonDtoUtils for blob and clob's

This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git

commit ca24c1f9d15da86143666c88fa3cf4b5178274b0
Author: Dan Haywood <da...@haywood-associates.co.uk>
AuthorDate: Fri Mar 16 15:44:48 2018 +0000

    ISIS-1925: adds a null guard in CommonDtoUtils for blob and clob's
---
 .gitignore                                         |  2 +
 .../apache/isis/schema/utils/CommonDtoUtils.java   | 24 +++++---
 .../utils/CommonDtoUtils_setValueOn_Test.java      | 71 ++++++++++++++++++++++
 3 files changed, 87 insertions(+), 10 deletions(-)

diff --git a/.gitignore b/.gitignore
index 83896b3..4d7509d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -45,3 +45,5 @@ core/integtestsupport/fixture-data
 
 rebel.xml
 **/.DS_Store
+
+.flattened-pom.xml
diff --git a/core/applib/src/main/java/org/apache/isis/schema/utils/CommonDtoUtils.java b/core/applib/src/main/java/org/apache/isis/schema/utils/CommonDtoUtils.java
index 6c3ea91..605c886 100644
--- a/core/applib/src/main/java/org/apache/isis/schema/utils/CommonDtoUtils.java
+++ b/core/applib/src/main/java/org/apache/isis/schema/utils/CommonDtoUtils.java
@@ -263,20 +263,24 @@ public final class CommonDtoUtils {
         }
         case BLOB: {
             final Blob blob = (Blob) val;
-            final BlobDto blobDto = new BlobDto();
-            blobDto.setName(blob.getName());
-            blobDto.setBytes(blob.getBytes());
-            blobDto.setMimeType(blob.getMimeType().toString());
-            valueDto.setBlob(blobDto);
+            if(blob != null) {
+                final BlobDto blobDto = new BlobDto();
+                blobDto.setName(blob.getName());
+                blobDto.setBytes(blob.getBytes());
+                blobDto.setMimeType(blob.getMimeType().toString());
+                valueDto.setBlob(blobDto);
+            }
             return valueDto;
         }
         case CLOB: {
             final Clob clob = (Clob) val;
-            final ClobDto clobDto = new ClobDto();
-            clobDto.setName(clob.getName());
-            clobDto.setChars(clob.getChars().toString());
-            clobDto.setMimeType(clob.getMimeType().toString());
-            valueDto.setClob(clobDto);
+            if(clob != null) {
+                final ClobDto clobDto = new ClobDto();
+                clobDto.setName(clob.getName());
+                clobDto.setChars(clob.getChars().toString());
+                clobDto.setMimeType(clob.getMimeType().toString());
+                valueDto.setClob(clobDto);
+            }
             return valueDto;
         }
         case VOID: {
diff --git a/core/applib/src/test/java/org/apache/isis/schema/utils/CommonDtoUtils_setValueOn_Test.java b/core/applib/src/test/java/org/apache/isis/schema/utils/CommonDtoUtils_setValueOn_Test.java
new file mode 100644
index 0000000..ee4f275
--- /dev/null
+++ b/core/applib/src/test/java/org/apache/isis/schema/utils/CommonDtoUtils_setValueOn_Test.java
@@ -0,0 +1,71 @@
+package org.apache.isis.schema.utils;
+
+import org.jmock.auto.Mock;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import org.apache.isis.applib.services.bookmark.BookmarkService;
+import org.apache.isis.applib.value.Blob;
+import org.apache.isis.applib.value.Clob;
+import org.apache.isis.core.unittestsupport.jmocking.JUnitRuleMockery2;
+import org.apache.isis.schema.common.v1.BlobDto;
+import org.apache.isis.schema.common.v1.ClobDto;
+import org.apache.isis.schema.common.v1.ValueDto;
+import org.apache.isis.schema.common.v1.ValueType;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.nullValue;
+
+public class CommonDtoUtils_setValueOn_Test {
+
+    @Rule
+    public JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(JUnitRuleMockery2.Mode.INTERFACES_AND_CLASSES);
+
+    @Mock
+    private BookmarkService mockBookmarkService;
+
+    ValueDto valueDto;
+    @Before
+    public void setUp() throws Exception {
+        valueDto = new ValueDto();
+    }
+
+    @Test
+    public void when_blob_is_null() {
+        CommonDtoUtils.setValueOn(valueDto, ValueType.BLOB, null, mockBookmarkService);
+        final BlobDto blobDto = valueDto.getBlob();
+        Assert.assertThat(blobDto, is(nullValue()));
+    }
+
+    @Test
+    public void when_blob_is_not_null() {
+        final Blob val = new Blob("image.png", "image/png", new byte[]{1,2,3,4,5});
+        CommonDtoUtils.setValueOn(valueDto, ValueType.BLOB, val, mockBookmarkService);
+        final BlobDto blobDto = valueDto.getBlob();
+        Assert.assertThat(blobDto, is(notNullValue()));
+        Assert.assertThat(blobDto.getBytes(), is(val.getBytes()));
+        Assert.assertThat(blobDto.getName(), is(val.getName()));
+        Assert.assertThat(blobDto.getMimeType(), is(val.getMimeType().toString()));
+    }
+
+    @Test
+    public void when_clob_is_null() {
+        CommonDtoUtils.setValueOn(valueDto, ValueType.CLOB, null, mockBookmarkService);
+        final ClobDto clobDto = valueDto.getClob();
+        Assert.assertThat(clobDto, is(nullValue()));
+    }
+
+    @Test
+    public void when_clob_is_not_null() {
+        final Clob val = new Clob("image.png", "image/png", new char[]{1,2,3,4,5});
+        CommonDtoUtils.setValueOn(valueDto, ValueType.CLOB, val, mockBookmarkService);
+        final ClobDto clobDto = valueDto.getClob();
+        Assert.assertThat(clobDto, is(notNullValue()));
+        Assert.assertThat(clobDto.getChars(), is(val.getChars()));
+        Assert.assertThat(clobDto.getName(), is(val.getName()));
+        Assert.assertThat(clobDto.getMimeType(), is(val.getMimeType().toString()));
+    }
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
danhaywood@apache.org.