Bug 505883 - Manual synchronization of code github -> eclipse repo
On behalf of Ehsan Zaery Moghaddam

Author: Ehsan Zaery Moghaddam <zaerymoghaddam@gmail.com>
Signed-off-by: David Kral <david.k.kral@oracle.com>
Reviewed-by: Roman Grigoriadi <roman.grigoriadi@oracle.com>
diff --git a/jsonb/build.gradle b/jsonb/build.gradle
index 9d68941..1fee1a9 100644
--- a/jsonb/build.gradle
+++ b/jsonb/build.gradle
@@ -2,7 +2,7 @@
 apply plugin: "maven"
 apply plugin: 'signing'
 apply plugin: 'idea'
-
+apply plugin: 'findbugs'
 
 group = "org.eclipse.persistence"
 version = "1.0-SNAPSHOT"
@@ -145,3 +145,20 @@
         }
     }
 }
+
+/**
+ * Configure Findbugs plugin and make it disable on normal build process. The Findbug plugin introduces two tasks to run
+ * the inspections for the main sources or tests. To call them, we can just run following tasks directly:
+ *
+ * findbugsMain: runs the inspections for the main sources (e.g. ./gradlew findbugsMain)
+ * findbugsTest: runs the inspections for the test sources (e.g. ./gradlew findbugsTest)
+ */
+findbugs {
+    ignoreFailures = true
+    effort = "max"
+    reportLevel = "low"
+    toolVersion = "3.0.1"
+    //  Empty sourceSets prevents the findbugs to be run as part of default build life cycle, but it still works by
+    //  calling its corresponding tasks (findbugsMain and findbugsTest) directly
+    sourceSets = []
+}
\ No newline at end of file
diff --git a/jsonb/src/main/java/org/eclipse/persistence/json/bind/internal/serializer/BooleanTypeDeserializer.java b/jsonb/src/main/java/org/eclipse/persistence/json/bind/internal/serializer/BooleanTypeDeserializer.java
index 6146b76..013937c 100644
--- a/jsonb/src/main/java/org/eclipse/persistence/json/bind/internal/serializer/BooleanTypeDeserializer.java
+++ b/jsonb/src/main/java/org/eclipse/persistence/json/bind/internal/serializer/BooleanTypeDeserializer.java
@@ -13,9 +13,15 @@
 
 package org.eclipse.persistence.json.bind.internal.serializer;
 
+import org.eclipse.persistence.json.bind.internal.JsonbParser;
 import org.eclipse.persistence.json.bind.internal.Unmarshaller;
+import org.eclipse.persistence.json.bind.internal.properties.MessageKeys;
+import org.eclipse.persistence.json.bind.internal.properties.Messages;
 import org.eclipse.persistence.json.bind.model.JsonBindingModel;
 
+import javax.json.bind.JsonbException;
+import javax.json.bind.serializer.DeserializationContext;
+import javax.json.stream.JsonParser;
 import java.lang.reflect.Type;
 
 /**
@@ -28,7 +34,25 @@
     }
 
     @Override
+    public Boolean deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
+        final JsonParser.Event event = ((JsonbParser) parser).moveToValue();
+        switch (event) {
+            case VALUE_TRUE:
+                return Boolean.TRUE;
+            case VALUE_FALSE:
+                return Boolean.FALSE;
+            case VALUE_STRING:
+                return Boolean.parseBoolean(parser.getString());
+            default:
+                throw new JsonbException(Messages.getMessage(MessageKeys.INTERNAL_ERROR, "Unknown JSON value: " + event));
+        }
+    }
+
+    @Override
     public Boolean deserialize(String jsonValue, Unmarshaller unmarshaller, Type rtType) {
-        return Boolean.parseBoolean(jsonValue);
+        // TODO: Fix API.
+        //       Unfortunately, the JSON API doesn't have a getBooleanValue method, so we need to override
+        //       the other deserialize method and parse the value manually
+        throw new UnsupportedOperationException();
     }
 }
diff --git a/jsonb/src/main/java/org/eclipse/persistence/json/bind/internal/serializer/DefaultSerializers.java b/jsonb/src/main/java/org/eclipse/persistence/json/bind/internal/serializer/DefaultSerializers.java
index 061693b..ad196d2 100644
--- a/jsonb/src/main/java/org/eclipse/persistence/json/bind/internal/serializer/DefaultSerializers.java
+++ b/jsonb/src/main/java/org/eclipse/persistence/json/bind/internal/serializer/DefaultSerializers.java
@@ -64,6 +64,7 @@
     private Map<Class<?>, SerializerProvider> initSerializers() {
         final Map<Class<?>, SerializerProvider> serializers = new HashMap<>();
         serializers.put(Boolean.class, new SerializerProvider(BooleanTypeSerializer.class, BooleanTypeDeserializer.class));
+        serializers.put(Boolean.TYPE, new SerializerProvider(BooleanTypeSerializer.class, BooleanTypeDeserializer.class));
         serializers.put(Byte.class, new SerializerProvider(ByteTypeSerializer.class, ByteTypeDeserializer.class));
         serializers.put(Byte.TYPE, new SerializerProvider(ByteTypeSerializer.class, ByteTypeDeserializer.class));
         serializers.put(Calendar.class, new SerializerProvider(CalendarTypeSerializer.class, CalendarTypeDeserializer.class));
diff --git a/jsonb/src/test/java/org/eclipse/persistence/json/bind/defaultmapping/basic/BooleanTest.java b/jsonb/src/test/java/org/eclipse/persistence/json/bind/defaultmapping/basic/BooleanTest.java
new file mode 100644
index 0000000..f2c6307
--- /dev/null
+++ b/jsonb/src/test/java/org/eclipse/persistence/json/bind/defaultmapping/basic/BooleanTest.java
@@ -0,0 +1,42 @@
+package org.eclipse.persistence.json.bind.defaultmapping.basic;
+
+import org.eclipse.persistence.json.bind.defaultmapping.basic.model.BooleanModel;
+import org.eclipse.persistence.json.bind.defaultmapping.generics.model.GenericTestClass;
+import org.junit.Test;
+
+import javax.json.bind.Jsonb;
+import javax.json.bind.JsonbBuilder;
+import java.lang.reflect.Type;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Tests serialization and deserialization of boolean values
+ *
+ * Created by Ehsan Zaery Moghaddam (zaerymoghaddam@gmail.com) on 9/17/16.
+ */
+public class BooleanTest {
+    private Jsonb jsonb = JsonbBuilder.create();
+
+    @Test
+    public void testBooleanSerialization() throws Exception {
+        BooleanModel booleanModel = new BooleanModel(true, false);
+
+        String expected = "{\"field1\":\"true\",\"field2\":\"false\"}";
+        assertEquals(expected, jsonb.toJson(booleanModel));
+    }
+
+    @Test
+    public void testBooleanDeserializationFromBooleanAsStringValue() throws Exception {
+        BooleanModel booleanModel = jsonb.fromJson("{\"field1\":\"true\",\"field2\":\"true\"}", BooleanModel.class);
+        assertEquals(booleanModel.field1, true);
+        assertEquals(booleanModel.field2, true);
+    }
+
+    @Test
+    public void testBooleanDeserializationFromBooleanRawValue() throws Exception {
+        BooleanModel booleanModel = jsonb.fromJson("{\"field1\":false,\"field2\":false}", BooleanModel.class);
+        assertEquals(booleanModel.field1, false);
+        assertEquals(booleanModel.field2, false);
+    }
+}
diff --git a/jsonb/src/test/java/org/eclipse/persistence/json/bind/defaultmapping/basic/model/BooleanModel.java b/jsonb/src/test/java/org/eclipse/persistence/json/bind/defaultmapping/basic/model/BooleanModel.java
new file mode 100644
index 0000000..66d63db
--- /dev/null
+++ b/jsonb/src/test/java/org/eclipse/persistence/json/bind/defaultmapping/basic/model/BooleanModel.java
@@ -0,0 +1,20 @@
+package org.eclipse.persistence.json.bind.defaultmapping.basic.model;
+
+/**
+ * Encapsulates different types of boolean values as a field so that the boolean value's serialization and
+ * deserialization could be tested
+ *
+ * Created by Ehsan Zaery Moghaddam (zaerymoghaddam@gmail.com) on 9/15/16.
+ */
+public class BooleanModel {
+    public Boolean field1;
+    public boolean field2;
+
+    public BooleanModel() {
+    }
+
+    public BooleanModel(boolean field1, Boolean field2) {
+        this.field2 = field2;
+        this.field1 = field1;
+    }
+}
diff --git a/jsonb/src/test/java/org/eclipse/persistence/json/bind/defaultmapping/dates/DatesTest.java b/jsonb/src/test/java/org/eclipse/persistence/json/bind/defaultmapping/dates/DatesTest.java
index 440bb7c..3943954 100644
--- a/jsonb/src/test/java/org/eclipse/persistence/json/bind/defaultmapping/dates/DatesTest.java
+++ b/jsonb/src/test/java/org/eclipse/persistence/json/bind/defaultmapping/dates/DatesTest.java
@@ -86,7 +86,9 @@
 
     @Test
     public void testCalendar() {
-        final Calendar timeCalendar = new Calendar.Builder().setDate(2015, Calendar.APRIL, 3).setTimeOfDay(11, 11, 10).build();
+        final Calendar timeCalendar = new Calendar.Builder().setDate(2015, Calendar.APRIL, 3).setTimeOfDay(11, 11, 10)
+                .setTimeZone(TimeZone.getTimeZone("Europe/Prague"))
+                .build();
 
         CalendarPojo calendarPojo = new CalendarPojo();
         calendarPojo.customCalendar = timeCalendar;
@@ -158,7 +160,9 @@
         assertEquals("{\"value\":\"2015-04-03\"}", jsonb.toJson(new ScalarValueWrapper<>(dateGregorianCalendar)));
 
         // marshal to ISO_DATE_TIME
-        final Calendar dateTimeGregorianCalendar = new Calendar.Builder().setDate(2015, 3, 3).build();
+        final Calendar dateTimeGregorianCalendar = new Calendar.Builder().setDate(2015, 3, 3)
+                .setTimeZone(TimeZone.getTimeZone("Europe/Prague"))
+                .build();
         assertEquals("{\"value\":\"2015-04-03T00:00:00+02:00[Europe/Prague]\"}", jsonb.toJson(new ScalarValueWrapper<>(dateTimeGregorianCalendar)));
     }