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

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


The following commit(s) were added to refs/heads/master by this push:
     new 4d6c30d  JOHNZON-217 toUpper on a Class name seems a bad idea...
4d6c30d is described below

commit 4d6c30df35b6edbfaf8465ed73aceab836667601
Author: Mark Struberg <[email protected]>
AuthorDate: Wed Jun 26 11:05:17 2019 +0200

    JOHNZON-217 toUpper on a Class name seems a bad idea...
---
 .../apache/johnzon/core/AbstractJsonFactory.java   |  3 +-
 .../apache/johnzon/core/BufferStrategyFactory.java |  3 +-
 .../johnzon/core/BufferStrategyFactoryTest.java    | 39 ++++++++++++++++++++--
 3 files changed, 40 insertions(+), 5 deletions(-)

diff --git 
a/johnzon-core/src/main/java/org/apache/johnzon/core/AbstractJsonFactory.java 
b/johnzon-core/src/main/java/org/apache/johnzon/core/AbstractJsonFactory.java
index 4f09d38..40194bd 100644
--- 
a/johnzon-core/src/main/java/org/apache/johnzon/core/AbstractJsonFactory.java
+++ 
b/johnzon-core/src/main/java/org/apache/johnzon/core/AbstractJsonFactory.java
@@ -22,7 +22,6 @@ import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.Locale;
 import java.util.Map;
 import java.util.logging.Logger;
 
@@ -54,7 +53,7 @@ public abstract class AbstractJsonFactory implements 
Serializable {
     protected BufferStrategy getBufferProvider() {
         final Object name = internalConfig.get(BUFFER_STRATEGY);
         if (name != null) {
-            return 
BufferStrategyFactory.valueOf(name.toString().toUpperCase(Locale.ENGLISH));
+            return BufferStrategyFactory.valueOf(name.toString());
         }
         return DEFAULT_BUFFER_STRATEGY;
     }
diff --git 
a/johnzon-core/src/main/java/org/apache/johnzon/core/BufferStrategyFactory.java 
b/johnzon-core/src/main/java/org/apache/johnzon/core/BufferStrategyFactory.java
index a52d379..03ebf4f 100644
--- 
a/johnzon-core/src/main/java/org/apache/johnzon/core/BufferStrategyFactory.java
+++ 
b/johnzon-core/src/main/java/org/apache/johnzon/core/BufferStrategyFactory.java
@@ -17,6 +17,7 @@
 package org.apache.johnzon.core;
 
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.Map;
 import java.util.concurrent.ConcurrentLinkedQueue;
 
@@ -95,7 +96,7 @@ public class BufferStrategyFactory {
      * @throws IllegalArgumentException if the given strategyName does not 
resolve to a BufferStrategy.
      */
     public static BufferStrategy valueOf(String strategyName) {
-        BufferStrategy bufferStrategy = DEFAULT_STRATEGIES.get(strategyName);
+        BufferStrategy bufferStrategy = 
DEFAULT_STRATEGIES.get(strategyName.toUpperCase(Locale.ENGLISH));
         if (bufferStrategy == null) {
             // try to load the BufferStrategy via reflection
             Class<?> bsClass = ClassUtil.loadClassOptional(strategyName, 
false);
diff --git 
a/johnzon-core/src/test/java/org/apache/johnzon/core/BufferStrategyFactoryTest.java
 
b/johnzon-core/src/test/java/org/apache/johnzon/core/BufferStrategyFactoryTest.java
index 06efdcf..70024c9 100644
--- 
a/johnzon-core/src/test/java/org/apache/johnzon/core/BufferStrategyFactoryTest.java
+++ 
b/johnzon-core/src/test/java/org/apache/johnzon/core/BufferStrategyFactoryTest.java
@@ -16,8 +16,16 @@
  */
 package org.apache.johnzon.core;
 
+import java.io.StringWriter;
+import java.util.HashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import javax.json.spi.JsonProvider;
+import javax.json.stream.JsonGenerator;
+
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
@@ -41,20 +49,47 @@ public class BufferStrategyFactoryTest {
         
verify(BufferStrategyFactory.valueOf(DummyBufferStrategy.class.getName()));
     }
 
+    @Test
+    public void testJsonGeneratorStrategyFromClass() {
+        DummyBufferStrategy.counter.set(0);
+
+        StringWriter sw = new StringWriter();
+        JsonGenerator generator = 
JsonProvider.provider().createGeneratorFactory(new HashMap<String, Object>() {
+            {
+                put(AbstractJsonFactory.BUFFER_STRATEGY, 
DummyBufferStrategy.class.getName());
+            }
+        }).createGenerator(sw);
+
+
+        generator.writeStartObject()
+                .write("age", 27)
+                .write("name", "karl")
+                .writeEnd()
+                .flush();
+
+        assertEquals(1, DummyBufferStrategy.counter.get());
+        assertEquals("{\"age\":27,\"name\":\"karl\"}", sw.toString());
+    }
+
     private void verify(Object bufferStrategy) {
         assertNotNull(bufferStrategy);
         assertTrue(bufferStrategy instanceof BufferStrategy);
     }
 
     public static final class DummyBufferStrategy implements BufferStrategy {
+        static AtomicInteger counter = new AtomicInteger(0);
+        private BufferStrategy delegate = 
BufferStrategyFactory.valueOf("BY_INSTANCE");
+
         @Override
         public BufferProvider<char[]> newCharProvider(int size) {
-            return null;
+            counter.incrementAndGet();
+            return delegate.newCharProvider(size);
         }
 
         @Override
         public BufferProvider<StringBuilder> newStringBuilderProvider(int 
size) {
-            return null;
+            counter.incrementAndGet();
+            return delegate.newStringBuilderProvider(size);
         }
     }
 }

Reply via email to