Skip to content

Commit

Permalink
Merge pull request #23 from rac2030/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
aalmiray committed Aug 24, 2014
2 parents af9a421 + 9e3d93f commit 8a9bca6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -70,7 +70,7 @@ dependencies {
compileExcludingCommonsLogging('org.kordamp.ezmorph:ezmorph:2.0.0')

testCompileExcludingCommonsLogging('xmlunit:xmlunit:1.5')
testCompile("org.slf4j:slf4j-simple:$slf4jVersion")
testCompile("uk.org.lidalia:slf4j-test:1.1.0")
}

task sourceJar(type: Jar) {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
@@ -1,4 +1,4 @@
#Tue Aug 19 17:07:57 CEST 2014
#Thu Aug 21 19:13:38 CEST 2014
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/org/kordamp/json/xml/XMLSerializer.java
Expand Up @@ -1049,6 +1049,20 @@ private JSON processArrayElement(Element element, String defaultType) {
result.put(arrayName, jsonArray);
return result;
}
} else if ((forcedArrayElements != null)?forcedArrayElements.contains(element.getQualifiedName()):false) {
// array not named, check if forced array and give warning if elements not same type
String arrayName = null;
for (int i = 0; i < element.getChildElements().size(); i++) {
final String arrayElement = element.getChildElements().get(i).getQualifiedName();
if (arrayName == null) {
arrayName = arrayElement;
} else if (!arrayName.equals(arrayElement)) {
log.warn("Child elements [" + arrayName + "," + arrayElement + "] of " +
"forced array element [" + element.getQualifiedName() + "] " +
"are not from the same type");
}
}

}
return jsonArray;
}
Expand Down Expand Up @@ -1651,4 +1665,4 @@ private void writeTagBeginning(CustomElement element) throws IOException {
writeNamespaceDeclarations(element);
}
}
}
}
20 changes: 18 additions & 2 deletions src/test/java/org/kordamp/json/xml/TestForcedArrayElementFlag.java
Expand Up @@ -15,8 +15,13 @@
*/
package org.kordamp.json.xml;

import com.google.common.collect.ImmutableList;
import junit.framework.TestCase;
import org.kordamp.json.JSONObject;
import uk.org.lidalia.slf4jext.Level;
import uk.org.lidalia.slf4jtest.LoggingEvent;
import uk.org.lidalia.slf4jtest.TestLogger;
import uk.org.lidalia.slf4jtest.TestLoggerFactory;

import static java.util.Arrays.asList;

Expand All @@ -29,6 +34,9 @@ public class TestForcedArrayElementFlag extends TestCase {
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "trace");
}

// Test logger which we can use to check if warnings get logged correctly
TestLogger logger = TestLoggerFactory.getTestLogger(XMLSerializer.class);

/**
* Should get an array without the list in this case.
* With the list it needs to have the same behaviour.
Expand All @@ -47,13 +55,15 @@ public void test_same_elements_should_be_forced_array() {
"</Document>\n");

String expectedJsonString = "{@DOMVersion:\"8.0\", @Self:\"d\", TinDocumentDataObject:{" +
"Properties:[\"/////wAAAAAAAAAA\",\"/////wBBBBBBBBBB\"]}}";
"Properties:[\"/////wAAAAAAAAAA\",\"/////wBBBBBBBBBB\"]}}";
final JSONObject expected = JSONObject.fromObject(expectedJsonString);

assertEquals(expected, actual);
}

public void test_different_elements_should_be_forced_array_with_log_warning() {
TestLoggerFactory.clear();
logger.setEnabledLevels(Level.WARN);
final XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setKeepCData(true);
xmlSerializer.setForcedArrayElements(asList("Properties"));
Expand All @@ -73,7 +83,13 @@ public void test_different_elements_should_be_forced_array_with_log_warning() {
assertEquals(expected, actual);

// Check if warning appears in log
//TODO Check log, find how to do it?
String expectedWarningMessage = "Child elements [GaijiRefMaps,ForcedArrayElement] of forced array element [Properties] are not from the same type";
boolean expectedWarningFound = false;
ImmutableList<LoggingEvent> loggingEvents = logger.getLoggingEvents();
for (LoggingEvent le : loggingEvents)
if (le.getLevel() == Level.WARN)
expectedWarningFound = le.getMessage().equals(expectedWarningMessage);
assertTrue("Expected warning message has been found to notify the caller that child the elements are not from the same type", expectedWarningFound);
}

public void test_single_element_should_be_forced_array() {
Expand Down

0 comments on commit 8a9bca6

Please sign in to comment.