r624 - in trunk/src: main/java/org/nuiton/io/xpp3 test/java/org/nuiton/io/xpp3 test/resources/org/nuiton/io/xpp3
Author: tchemit Date: 2009-11-01 23:47:27 +0100 (Sun, 01 Nov 2009) New Revision: 624 Added: trunk/src/test/resources/org/nuiton/io/xpp3/identities2.xml Modified: trunk/src/main/java/org/nuiton/io/xpp3/Xpp3Helper.java trunk/src/test/java/org/nuiton/io/xpp3/IdentityXpp3Reader.java trunk/src/test/java/org/nuiton/io/xpp3/Xpp3HelperTest.java trunk/src/test/java/org/nuiton/io/xpp3/Xpp3ReaderTest.java Log: continue tests Modified: trunk/src/main/java/org/nuiton/io/xpp3/Xpp3Helper.java =================================================================== --- trunk/src/main/java/org/nuiton/io/xpp3/Xpp3Helper.java 2009-11-01 20:05:46 UTC (rev 623) +++ trunk/src/main/java/org/nuiton/io/xpp3/Xpp3Helper.java 2009-11-01 22:47:27 UTC (rev 624) @@ -24,6 +24,7 @@ import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; + import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; @@ -37,6 +38,7 @@ import java.util.Iterator; import java.util.Map; import java.util.ServiceLoader; + import org.apache.commons.lang.StringUtils; import org.codehaus.plexus.util.xml.pull.XmlPullParser; import org.nuiton.io.xpp3.PropertyMapper.AttributeValueToProperty; @@ -44,34 +46,48 @@ /** * A Helper to read some data stored in xml with a {@link Xpp3Reader}. - * + * <p/> * In this class, there is some methods to obtain a discovered {@link Xpp3Reader} * registred by a {@link ServiceLoader} mecanism. - * + * * @author chemit + * @see Xpp3Reader * @since 1.0.3 - * @see Xpp3Reader */ public class Xpp3Helper { /** - * les readers enregistres via un {@link ServiceLoader} + * les readers enregistres via un {@link ServiceLoader} * sur le contrat {@link Xpp3Reader}. */ protected static Map<Class<?>, Xpp3Reader<?>> readers; /** * Read a single object from a xml stream. - * - * @param <O> the type of object to read - * @param klass the type of object to read + * + * @param <O> the type of object to read + * @param klass the type of object to read * @param reader the reader where to parse the xml * @return the loaded object - * @throws IOException if any io pb + * @throws IOException if any io pb * @throws XmlPullParserException if any parsing pb */ public static <O> O readObject(Class<O> klass, Reader reader) throws IOException, XmlPullParserException { + if (klass == null) { + throw new NullPointerException("klass parameter can not be null"); + } + + if (reader == null) { + throw new NullPointerException("reader parameter can not be null"); + } + + Xpp3Reader<O> modelReader = getReader(klass); + + if (modelReader == null) { + throw new IllegalArgumentException("could not find xpp3Reader for type " + klass); + } + O result = null; try { @@ -84,8 +100,6 @@ StringReader sReader = new StringReader(rawInput); - Xpp3Reader<O> modelReader = getReader(klass); - result = modelReader.read(sReader); } finally { @@ -98,15 +112,29 @@ /** * Read an array of objects from a xml stream. * - * @param <O> the type of objects to return - * @param klass the type of object to read + * @param <O> the type of objects to return + * @param klass the type of object to read * @param reader the reader where to parse the xml * @return the loaded objects - * @throws IOException if any io pb + * @throws IOException if any io pb * @throws XmlPullParserException if any parsing pb */ public static <O> O[] readObjects(Class<O> klass, Reader reader) throws IOException, XmlPullParserException { + if (klass == null) { + throw new NullPointerException("klass parameter can not be null"); + } + + if (reader == null) { + throw new NullPointerException("reader parameter can not be null"); + } + + Xpp3Reader<O> modelReader = getReader(klass); + + if (modelReader == null) { + throw new IllegalArgumentException("could not find xpp3Reader for type " + klass); + } + O[] result = null; try { @@ -119,11 +147,6 @@ StringReader sReader = new StringReader(rawInput); - Xpp3Reader<O> modelReader = getReader(klass); - - if (modelReader == null) { - throw new IllegalArgumentException("could not find xpp3Reader for type " + klass); - } result = modelReader.readArray(sReader); } finally { @@ -134,7 +157,6 @@ } /** - * * @return an iterator on all registred {@link Xpp3Reader}. */ public static Iterator<Xpp3Reader<?>> getReaderItetator() { @@ -144,8 +166,8 @@ /** * Obtain the loaded reader which match his {@link Xpp3Reader#getType()} whith * the given type. - * - * @param <T> the type of the data which should be parsed by the researched parser + * + * @param <T> the type of the data which should be parsed by the researched parser * @param klass the type of the data which should be parsed by the researched parser * @return the parser for the given type */ @@ -156,9 +178,8 @@ /** * Clean all the registred readers. - * + * <p/> * To reload them, just call a {@link #getReader(java.lang.Class)}. - * */ public static void clearReaders() { if (readers != null) { @@ -176,6 +197,7 @@ * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent</li> * <li>http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent</li> * </ul> + * * @param parser the parser to configure * @throws XmlPullParserException if any pb */ @@ -490,22 +512,20 @@ } /** - * Load (if {@link #readers} is {@code null} the readers via a + * Load (if {@link #readers} is {@code null} the readers via a * {@link ServiceLoader} of contract {@link Xpp3Reader} and returns * the dictionnary of discovered associated to their type * ({@link Xpp3Reader#getType()}). * - * * @return all the {@link Xpp3Reader} registred via a {@link ServiceLoader} - * on the contract {@link Xpp3Reader} associated to their type - * ({@link Xpp3Reader#getType()}). - * + * on the contract {@link Xpp3Reader} associated to their type + * ({@link Xpp3Reader#getType()}). */ protected static Map<Class<?>, Xpp3Reader<?>> getReaders() { if (readers == null) { readers = new HashMap<Class<?>, Xpp3Reader<?>>(); - for (Xpp3Reader<?> r : ServiceLoader.load(Xpp3Reader.class,Xpp3Helper.class.getClassLoader())) { - + for (Xpp3Reader<?> r : ServiceLoader.load(Xpp3Reader.class, Xpp3Helper.class.getClassLoader())) { + readers.put(r.getType(), r); } } Modified: trunk/src/test/java/org/nuiton/io/xpp3/IdentityXpp3Reader.java =================================================================== --- trunk/src/test/java/org/nuiton/io/xpp3/IdentityXpp3Reader.java 2009-11-01 20:05:46 UTC (rev 623) +++ trunk/src/test/java/org/nuiton/io/xpp3/IdentityXpp3Reader.java 2009-11-01 22:47:27 UTC (rev 624) @@ -8,10 +8,15 @@ * Time: 20:37:56 */ public class IdentityXpp3Reader extends AbstractXpp3Reader<Identity> { + public IdentityXpp3Reader() { - super(Identity.class, "identities", "identity"); + this("identities"); } + public IdentityXpp3Reader(String root) { + super(Identity.class, root, "identity"); + } + @Override protected void initMappers() throws IntrospectionException { Modified: trunk/src/test/java/org/nuiton/io/xpp3/Xpp3HelperTest.java =================================================================== --- trunk/src/test/java/org/nuiton/io/xpp3/Xpp3HelperTest.java 2009-11-01 20:05:46 UTC (rev 623) +++ trunk/src/test/java/org/nuiton/io/xpp3/Xpp3HelperTest.java 2009-11-01 22:47:27 UTC (rev 624) @@ -1,16 +1,13 @@ package org.nuiton.io.xpp3; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; -import org.nuiton.plugin.TestHelper; import java.io.File; import java.io.FileReader; import java.io.Reader; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Iterator; -import java.util.List; /** * User: chemit @@ -19,6 +16,12 @@ */ public class Xpp3HelperTest { + + @BeforeClass + public static void beforeClass() { + Xpp3ReaderTest.beforeClass(); + } + @Test public void testGetReader() throws Exception { Xpp3Reader<Identity> r = Xpp3Helper.getReader(Identity.class); @@ -34,63 +37,93 @@ Assert.assertNotNull(reader); } + @Test(expected = NullPointerException.class) + public void testReadObjectFailed() throws Exception { + + Xpp3Helper.readObject(String.class, null); + } + + @Test(expected = NullPointerException.class) + public void testReadObjectFailed2() throws Exception { + + File file = new File(Xpp3ReaderTest.dir, "identity.xml"); + Assert.assertTrue(file.exists()); + + Reader input = new FileReader(file); + Assert.assertNotNull(input); + + Xpp3Helper.readObject(null, input); + } + + @Test(expected = IllegalArgumentException.class) + public void testReadObjectFailed3() throws Exception { + + File file = new File(Xpp3ReaderTest.dir, "identity.xml"); + Assert.assertTrue(file.exists()); + + Reader input = new FileReader(file); + Assert.assertNotNull(input); + + Xpp3Helper.readObject(String.class, input); + } + @Test public void testReadObject() throws Exception { - List<String> paths = new ArrayList<String>(); - paths.add("target"); - paths.add("test-classes"); - paths.addAll(Arrays.asList(getClass().getPackage().getName().split("\\."))); - File file = TestHelper.getFile(TestHelper.getBasedir(), paths.toArray(new String[paths.size()])); - file = new File(file, "identity.xml"); + + File file = new File(Xpp3ReaderTest.dir, "identity.xml"); + Assert.assertTrue(file.exists()); + Reader input = new FileReader(file); + Assert.assertNotNull(input); Identity result = Xpp3Helper.readObject(Identity.class, input); - Assert.assertNotNull(result); - Identity expected = new Identity(); - expected.setFirstName("first name"); - expected.setLastName("last name"); - expected.setEmail("noway@bishop.uk"); - expected.setAge(29); + Xpp3ReaderTest.assertEqualsSimpleIdentity(result); + } - Xpp3ReaderTest.assertEqualsIdentity(result, expected); + @Test(expected = NullPointerException.class) + public void testReadObjectsFailed() throws Exception { + Xpp3Helper.readObjects(String.class, null); } - @Test - public void testReadObjects() throws Exception { - List<String> paths = new ArrayList<String>(); - paths.add("target"); - paths.add("test-classes"); - paths.addAll(Arrays.asList(getClass().getPackage().getName().split("\\."))); - File file = TestHelper.getFile(TestHelper.getBasedir(), paths.toArray(new String[paths.size()])); - file = new File(file, "identities.xml"); + + @Test(expected = NullPointerException.class) + public void testReadObjectsFailed2() throws Exception { + + File file = new File(Xpp3ReaderTest.dir, "identity.xml"); + Assert.assertTrue(file.exists()); + Reader input = new FileReader(file); Assert.assertNotNull(input); - Identity[] result = Xpp3Helper.readObjects(Identity.class, input); - Assert.assertNotNull(result); - Assert.assertEquals(2, result.length); - Identity expected = new Identity(); - expected.setFirstName("first name"); - expected.setLastName("last name"); - expected.setEmail("noway@bishop.uk"); - expected.setAge(29); - Xpp3ReaderTest.assertEqualsIdentity(result[0], expected); + Xpp3Helper.readObjects(null, input); + } - expected = new Identity(); - expected.setFirstName("first name2"); - expected.setLastName("last name2"); - expected.setEmail("noway2@bishop.uk"); - expected.setAge(31); + @Test(expected = IllegalArgumentException.class) + public void testReadObjectsFailed3() throws Exception { - Xpp3ReaderTest.assertEqualsIdentity(result[1], expected); + File file = new File(Xpp3ReaderTest.dir, "identity.xml"); + Assert.assertTrue(file.exists()); + Reader input = new FileReader(file); + Assert.assertNotNull(input); + Xpp3Helper.readObjects(String.class, input); } @Test + public void testReadObjects() throws Exception { + + File file = new File(Xpp3ReaderTest.dir, "identities.xml"); + Reader input = new FileReader(file); + Assert.assertNotNull(input); + Identity[] result = Xpp3Helper.readObjects(Identity.class, input); + Xpp3ReaderTest.assertEqualsIdentities(result); + } + + @Test public void testCleanReaders() throws Exception { Xpp3Helper.clearReaders(); Assert.assertNull(Xpp3Helper.readers); Modified: trunk/src/test/java/org/nuiton/io/xpp3/Xpp3ReaderTest.java =================================================================== --- trunk/src/test/java/org/nuiton/io/xpp3/Xpp3ReaderTest.java 2009-11-01 20:05:46 UTC (rev 623) +++ trunk/src/test/java/org/nuiton/io/xpp3/Xpp3ReaderTest.java 2009-11-01 22:47:27 UTC (rev 624) @@ -1,6 +1,7 @@ package org.nuiton.io.xpp3; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; import org.nuiton.plugin.TestHelper; @@ -18,17 +19,27 @@ */ public class Xpp3ReaderTest { - protected Xpp3Reader<Identity> r = new IdentityXpp3Reader(); - @Test - public void testRead() throws Exception { + protected static File dir; + protected Xpp3Reader<Identity> r = new IdentityXpp3Reader("identities"); + + + @BeforeClass + public static void beforeClass() { + List<String> paths = new ArrayList<String>(); paths.add("target"); paths.add("test-classes"); - paths.addAll(Arrays.asList(getClass().getPackage().getName().split("\\."))); - File file = TestHelper.getFile(TestHelper.getBasedir(), paths.toArray(new String[paths.size()])); - file = new File(file, "identity.xml"); + paths.addAll(Arrays.asList(Xpp3HelperTest.class.getPackage().getName().split("\\."))); + dir = TestHelper.getFile(TestHelper.getBasedir(), paths.toArray(new String[paths.size()])); + } + + @Test + public void testRead() throws Exception { + + File file = new File(dir, "identity.xml"); + Reader input = new FileReader(file); Assert.assertNotNull(input); Identity result = r.read(input); @@ -40,23 +51,74 @@ expected.setAge(29); assertEqualsIdentity(result, expected); - } @Test public void testReadArray() throws Exception { - List<String> paths = new ArrayList<String>(); - paths.add("target"); - paths.add("test-classes"); - paths.addAll(Arrays.asList(getClass().getPackage().getName().split("\\."))); - File file = TestHelper.getFile(TestHelper.getBasedir(), paths.toArray(new String[paths.size()])); - file = new File(file, "identities.xml"); + File file = new File(dir, "identities.xml"); Reader input = new FileReader(file); Assert.assertNotNull(input); Identity[] result = r.readArray(input); + assertEqualsIdentities(result); + } + + + @Test + public void testReadArray2() throws Exception { + + r = new IdentityXpp3Reader("identity"); + + File file = new File(dir, "identities2.xml"); + + Assert.assertTrue(file.exists()); + Reader input = new FileReader(file); + Assert.assertNotNull(input); + Identity[] result = r.readArray(input); + assertEqualsIdentities(result); + + r.setAddDefaultEntities(false); + result = r.readArray(new FileReader(file)); + + assertEqualsIdentities(result); + + r.setAddDefaultEntities(true); + result = r.readArray(new FileReader(file)); + + assertEqualsIdentities(result); + + + r.setParentRootTagName("identities"); + + input = new FileReader(new File(dir, "identities.xml")); + Assert.assertNotNull(input); + result = r.readArray(input); + + assertEqualsIdentities(result); + } + + protected static void assertEqualsIdentity(Identity result, Identity expected) { + Assert.assertEquals(expected.getFirstName(), result.getFirstName()); + Assert.assertEquals(expected.getLastName(), result.getLastName()); + Assert.assertEquals(expected.getEmail(), result.getEmail()); + Assert.assertEquals(expected.getAge(), result.getAge()); + } + + + protected static void assertEqualsSimpleIdentity(Identity result) { Assert.assertNotNull(result); + Identity expected = new Identity(); + expected.setFirstName("first name"); + expected.setLastName("last name"); + expected.setEmail("noway@bishop.uk"); + expected.setAge(29); + + assertEqualsIdentity(result, expected); + } + + protected static void assertEqualsIdentities(Identity[] result) { + Assert.assertNotNull(result); Assert.assertEquals(2, result.length); Identity expected = new Identity(); expected.setFirstName("first name"); @@ -75,11 +137,4 @@ assertEqualsIdentity(result[1], expected); } - protected static void assertEqualsIdentity(Identity result, Identity expected) { - Assert.assertEquals(expected.getFirstName(), result.getFirstName()); - Assert.assertEquals(expected.getLastName(), result.getLastName()); - Assert.assertEquals(expected.getEmail(), result.getEmail()); - Assert.assertEquals(expected.getAge(), result.getAge()); - } - } Added: trunk/src/test/resources/org/nuiton/io/xpp3/identities2.xml =================================================================== --- trunk/src/test/resources/org/nuiton/io/xpp3/identities2.xml (rev 0) +++ trunk/src/test/resources/org/nuiton/io/xpp3/identities2.xml 2009-11-01 22:47:27 UTC (rev 624) @@ -0,0 +1,14 @@ +<identity> + <identity> + <firstName>first name</firstName> + <lastName>last name</lastName> + <email>noway@bishop.uk</email> + <age>29</age> + </identity> + <identity> + <firstName>first name2</firstName> + <lastName>last name2</lastName> + <email>noway2@bishop.uk</email> + <age>31</age> + </identity> +</identity> \ No newline at end of file
participants (1)
-
tchemit@users.nuiton.org