Skip to content

Commit

Permalink
Fix incorrect/missing annotations, improve instanceof annotations (go…
Browse files Browse the repository at this point in the history
  • Loading branch information
geri-m committed Nov 6, 2018
1 parent d59ab30 commit 388a9ae
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.util.List;
Expand Down Expand Up @@ -61,7 +57,6 @@ public class AtomTest {
/**
* Test for checking the Slug Header
*/
@SuppressWarnings("unchecked")
@Test
public void testSetSlugHeader() {
HttpHeaders headers = new HttpHeaders();
Expand Down Expand Up @@ -92,7 +87,6 @@ public void subtestSetSlugHeader(HttpHeaders headers, String expectedValue, Stri
* AtomFeedParser#parseNextEntry} and see if the mapping of the XML element to the entity classes
* is done correctly.
*/
@SuppressWarnings("unchecked")
@Test
public void testAtomFeedUsingCustomizedParser() throws Exception {
XmlPullParser parser = Xml.createParser();
Expand Down Expand Up @@ -138,7 +132,6 @@ public void testAtomFeedUsingCustomizedParser() throws Exception {
* The purpose of this test is to assert, if the parsed elements are correctly parsed using a
* {@link AtomFeedParser}.
*/
@SuppressWarnings("unchecked")
@Test
public void testAtomFeedUsingStandardParser() throws Exception {
Feed feed = new Feed();
Expand Down Expand Up @@ -179,7 +172,6 @@ public void testAtomFeedUsingStandardParser() throws Exception {
* (HTML in this case), that are not part of the {@link FeedEntry} and to see if there is an issue
* if we parse some more entries.
*/
@SuppressWarnings("unchecked")
@Test
public void testSampleFeedParser() throws Exception {
XmlPullParser parser = Xml.createParser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class GenericXmlListTest {
/**
* The purpose of this test is to map an XML with an Array of {@link XmlTest.AnyType} objects.
*/
@SuppressWarnings("unchecked")
@Test
public void testParseArrayTypeWithClassType() throws Exception {
ArrayWithClassTypeGeneric xml = new ArrayWithClassTypeGeneric();
Expand All @@ -68,7 +69,7 @@ public void testParseArrayTypeWithClassType() throws Exception {
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertTrue(xml.rep instanceof XmlTest.AnyType[]);
assertNotNull(xml.rep);
XmlTest.AnyType[] rep = xml.rep;
assertNotNull(rep);
assertEquals(3, rep.length);
Expand Down Expand Up @@ -105,7 +106,7 @@ public void testParseCollectionWithClassType() throws Exception {
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertTrue(xml.rep instanceof Collection);
assertNotNull(xml.rep);
Collection<XmlTest.AnyType> rep = xml.rep;
assertNotNull(rep);
assertEquals(3, rep.size());
Expand All @@ -120,6 +121,7 @@ public void testParseCollectionWithClassType() throws Exception {
/**
* The purpose of this test is to map an XML with an Array of {@link XmlTest.AnyType} objects.
*/
@SuppressWarnings("unchecked")
@Test
public void testParseMultiGenericWithClassType() throws Exception {
MultiGenericWithClassType xml = new MultiGenericWithClassType();
Expand Down Expand Up @@ -154,6 +156,7 @@ public void testParseMultiGenericWithClassType() throws Exception {
/**
* The purpose of this test is to map an XML with an Array of {@link XmlTest.AnyType} objects.
*/
@SuppressWarnings("unchecked")
@Test
public void testParseMultiGenericWithClassTypeGeneric() throws Exception {
MultiGenericWithClassTypeGeneric xml = new MultiGenericWithClassTypeGeneric();
Expand All @@ -162,7 +165,6 @@ public void testParseMultiGenericWithClassTypeGeneric() throws Exception {
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type

GenericXml[] rep = xml.rep;
assertNotNull(rep);
assertEquals(3, rep.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
import com.google.api.client.util.ArrayMap;
import com.google.api.client.util.Key;
Expand Down Expand Up @@ -72,7 +74,6 @@ public GenericXmlTest() {
* The purpose of this test is to parse the given XML into a {@link GenericXml} Object that has no
* fixed structure.
*/

@SuppressWarnings("unchecked")
@Test
public void testParseToGenericXml() throws Exception {
Expand Down Expand Up @@ -105,7 +106,7 @@ public void testParseToGenericXml() throws Exception {
/**
* The purpose of this test is map a generic XML to an element inside a dedicated element.
*/
@SuppressWarnings("cast")
@SuppressWarnings("unchecked")
@Test
public void testParseAnyGenericType() throws Exception {
AnyGenericType xml = new AnyGenericType();
Expand Down Expand Up @@ -197,29 +198,15 @@ public void testParseSimpleTypeAsValueInteger() throws Exception {
* The purpose of this test is to map a {@link GenericXml} to the String element in the
* object.
*/
@SuppressWarnings("cast")
@Test
public void testParseToAnyType() throws Exception {
AnyTypeGeneric xml = new AnyTypeGeneric();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(ANY_TYPE_XML));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
assertNotNull(xml);
assertEquals(4, xml.values().size());
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(ANY_TYPE_XML, out.toString());
processAnyTypeGeneric(ANY_TYPE_XML);
}

/**
* The purpose of this test is to map a {@link GenericXml} to the String element in the
* object.
*/
@SuppressWarnings("cast")
@Test
public void testParseToAnyTypeMissingField() throws Exception {
AnyTypeMissingFieldGeneric xml = new AnyTypeMissingFieldGeneric();
Expand All @@ -243,7 +230,6 @@ public void testParseToAnyTypeMissingField() throws Exception {
* The purpose of this test isto map a {@link GenericXml} to the String element in the
* object.
*/
@SuppressWarnings("cast")
@Test
public void testParseToAnyTypeAdditionalField() throws Exception {
AnyTypeAdditionalFieldGeneric xml = new AnyTypeAdditionalFieldGeneric();
Expand Down Expand Up @@ -329,22 +315,26 @@ public void testParseIncorrectMapping() throws Exception {
* The purpose of this test is to map a {@link GenericXml} to the String element in the
* object.
*/
@SuppressWarnings("cast")
@Test
public void testParseAnyTypeWithNestedElementArrayMap() throws Exception {
processAnyTypeGeneric(ANY_TYPE_XML_NESTED_ARRAY);
}

private void processAnyTypeGeneric(final String anyTypeXmlNestedArray) throws XmlPullParserException, IOException {
AnyTypeGeneric xml = new AnyTypeGeneric();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(ANY_TYPE_XML_NESTED_ARRAY));
parser.setInput(new StringReader(anyTypeXmlNestedArray));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
assertNotNull(xml);
assertEquals(4, xml.values().size());
assertEquals(4, xml.values()
.size());
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(ANY_TYPE_XML_NESTED_ARRAY, out.toString());
assertEquals(anyTypeXmlNestedArray, out.toString());
}

private static class AnyGenericType {
Expand Down Expand Up @@ -408,7 +398,7 @@ private static class AnyTypePrimitiveIntGeneric extends GenericXml {
@Key("@attr")
public int attr;
@Key
public int intArray[];
public int[] intArray;
}

private static class AnyTypePrimitiveStringGeneric extends GenericXml {
Expand All @@ -417,7 +407,7 @@ private static class AnyTypePrimitiveStringGeneric extends GenericXml {
@Key("@attr")
public String attr;
@Key
public String strArray[];
public String[] strArray;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ private String testStandardXml(final String xmlString) throws Exception {
parser.setInput(new StringReader(xmlString));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
assertTrue(xml.elementEnum instanceof XmlEnumTest.AnyEnum);
assertTrue(xml.elementEnum.equals(AnyEnum.ENUM_2));
assertNotNull(xml.elementEnum);
assertEquals(xml.elementEnum, AnyEnum.ENUM_2);
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand All @@ -124,8 +124,8 @@ public void testParse_enumAttributeType() throws Exception {
parser.setInput(new StringReader(XML_ENUM_ATTRIBUTE_ONLY));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
assertTrue(xml.attributeEnum instanceof XmlEnumTest.AnyEnum);
assertTrue(xml.attributeEnum.equals(AnyEnum.ENUM_1));
assertNotNull(xml.attributeEnum);
assertEquals(xml.attributeEnum, AnyEnum.ENUM_1);
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class XmlListTest {
/**
* The purpose of this test is to map an XML with an Array of {@link XmlTest.AnyType} objects.
*/
@SuppressWarnings("unchecked")
@Test
public void testParseArrayTypeWithClassType() throws Exception {
ArrayWithClassType xml = new ArrayWithClassType();
Expand All @@ -63,7 +64,7 @@ public void testParseArrayTypeWithClassType() throws Exception {
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertTrue(xml.rep instanceof XmlTest.AnyType[]);
assertNotNull(xml.rep);
XmlTest.AnyType[] rep = xml.rep;
assertNotNull(rep);
assertEquals(3, rep.length);
Expand Down Expand Up @@ -100,12 +101,11 @@ public void testParseCollectionWithClassType() throws Exception {
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
// check type
assertTrue(xml.rep instanceof Collection);
assertNotNull(xml.rep);
Collection<XmlTest.AnyType> rep = xml.rep;
assertNotNull(rep);
assertEquals(3, rep.size());


// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
Expand Down Expand Up @@ -221,7 +222,6 @@ public void testFailMappingOfDataType() throws Exception {
* The purpose of this tests it to test the {@link Key} Annotation for mapping of elements and
* attributes. All elements/attributes are matched.
*/
@SuppressWarnings("cast")
@Test
public void testParseToAnyType() throws Exception {
AnyType xml = new AnyType();
Expand All @@ -232,7 +232,7 @@ public void testParseToAnyType() throws Exception {
assertTrue(xml.attr instanceof String);
assertTrue(xml.elem.toString(), xml.elem instanceof ArrayList<?>);
assertTrue(xml.rep.toString(), xml.rep instanceof ArrayList<?>);
assertTrue(xml.value instanceof ValueType);
assertNotNull(xml.value);
assertTrue(xml.value.content instanceof String);
// serialize
XmlSerializer serializer = Xml.createSerializer();
Expand All @@ -247,7 +247,6 @@ public void testParseToAnyType() throws Exception {
* attributes. The matched object misses some field that are present int the XML ('elem' is
* missing and therefore ignored).
*/
@SuppressWarnings("cast")
@Test
public void testParseToAnyTypeMissingField() throws Exception {
AnyTypeMissingField xml = new AnyTypeMissingField();
Expand All @@ -257,7 +256,7 @@ public void testParseToAnyTypeMissingField() throws Exception {
Xml.parseElement(parser, xml, namespaceDictionary, null);
assertTrue(xml.attr instanceof String);
assertTrue(xml.elem.toString(), xml.elem instanceof ArrayList<?>);
assertTrue(xml.value instanceof ValueType);
assertNotNull(xml.value);
assertTrue(xml.value.content instanceof String);
// serialize
XmlSerializer serializer = Xml.createSerializer();
Expand All @@ -272,7 +271,6 @@ public void testParseToAnyTypeMissingField() throws Exception {
* attributes. The matched object has an additional field, that will not be used and stays {@code
* null}.
*/
@SuppressWarnings("cast")
@Test
public void testParseToAnyTypeAdditionalField() throws Exception {
AnyTypeAdditionalField xml = new AnyTypeAdditionalField();
Expand All @@ -282,7 +280,7 @@ public void testParseToAnyTypeAdditionalField() throws Exception {
Xml.parseElement(parser, xml, namespaceDictionary, null);
assertTrue(xml.attr instanceof String);
assertTrue(xml.elem.toString(), xml.elem instanceof ArrayList<?>);
assertTrue(xml.value instanceof ValueType);
assertNotNull(xml.value);
assertNull(xml.additionalField);
assertTrue(xml.rep.toString(), xml.rep instanceof ArrayList<?>);
assertTrue(xml.value.content instanceof String);
Expand Down Expand Up @@ -321,7 +319,7 @@ public void testParseAnyTypeWithCustomParser() throws Exception {
assertTrue(xml.attr instanceof String);
assertTrue(xml.elem.toString(), xml.elem instanceof ArrayList<?>);
assertTrue(xml.rep.toString(), xml.rep instanceof ArrayList<?>);
assertTrue(xml.value instanceof ValueType);
assertNotNull(xml.value);
assertTrue(xml.value.content instanceof String);
// serialize
XmlSerializer serializer = Xml.createSerializer();
Expand Down Expand Up @@ -489,7 +487,6 @@ public void testParseIncorrectMapping() throws Exception {
assertNull(xml.value);
assertNull(xml.rep);
assertNull(xml.rep);

// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand All @@ -502,7 +499,6 @@ public void testParseIncorrectMapping() throws Exception {
* The purpose of this test is to map the sub elements of an {@link ArrayMap} again to an {@link
* ArrayMap}.
*/
@SuppressWarnings("cast")
@Test
public void testParseAnyTypeWithNestedElementArrayMap() throws Exception {
AnyType xml = new AnyType();
Expand All @@ -513,14 +509,12 @@ public void testParseAnyTypeWithNestedElementArrayMap() throws Exception {
assertTrue(xml.attr instanceof String);
assertTrue(xml.elem.toString(), xml.elem instanceof ArrayList<?>);
assertTrue(xml.rep.toString(), xml.rep instanceof ArrayList<?>);
assertTrue(xml.value instanceof ValueType);
assertNotNull(xml.value);
assertTrue(xml.value.content instanceof String);
assertEquals(1, ((ArrayList<?>) xml.elem).size());
assertEquals(2, ((ArrayList<?>) xml.rep).size());
assertEquals(1, ((ArrayList<?>) xml.rep).toArray(new ArrayMap[]{})[0].size());
assertEquals(1, ((ArrayList<?>) xml.rep).toArray(new ArrayMap[]{})[1].size());


assertEquals(1, ((Collection<?>) xml.elem).size());
assertEquals(2, ((Collection<?>) xml.rep).size());
assertEquals(1, ((Collection<?>) xml.rep).toArray(new ArrayMap[]{})[0].size());
assertEquals(1, ((Collection<?>) xml.rep).toArray(new ArrayMap[]{})[1].size());
assertEquals("rep1",
((ArrayList<?>) ((ArrayList<?>) xml.rep).toArray(new ArrayMap[]{})[0].get("p")).toArray(new ArrayMap[]{})[0].getValue(0));
assertEquals("rep2",
Expand Down Expand Up @@ -592,7 +586,7 @@ public static class AnyTypePrimitiveInt {
@Key("@attr")
public int attr;
@Key
public int intArray[];
public int[] intArray;
}

public static class AnyTypePrimitiveString {
Expand All @@ -601,7 +595,7 @@ public static class AnyTypePrimitiveString {
@Key("@attr")
public String attr;
@Key
public String strArray[];
public String[] strArray;
}

private static class AnyTypeInf {
Expand Down Expand Up @@ -629,6 +623,5 @@ private static class AllType {
@Key
public List<Integer> integerCollection;
}

}

0 comments on commit 388a9ae

Please sign in to comment.