From: <id...@us...> - 2009-06-10 06:59:43
|
Revision: 251 http://cse-ip.svn.sourceforge.net/cse-ip/?rev=251&view=rev Author: idueppe Date: 2009-06-10 06:58:18 +0000 (Wed, 10 Jun 2009) Log Message: ----------- starting to fix the decorator issue. First Step - polishing the test code to be left sided. Modified Paths: -------------- trunk/cse-ip/sc-cdmm/src/test/java/de/campussource/cse/cdmm/TestXmlConversion.java Modified: trunk/cse-ip/sc-cdmm/src/test/java/de/campussource/cse/cdmm/TestXmlConversion.java =================================================================== --- trunk/cse-ip/sc-cdmm/src/test/java/de/campussource/cse/cdmm/TestXmlConversion.java 2009-06-09 13:02:33 UTC (rev 250) +++ trunk/cse-ip/sc-cdmm/src/test/java/de/campussource/cse/cdmm/TestXmlConversion.java 2009-06-10 06:58:18 UTC (rev 251) @@ -59,7 +59,7 @@ } @Test - public void testAccountXml() { + public void testAccountXml() throws JAXBException, IOException { System.out.println("----- entering Account XML Test -----"); // create and fetch account object EntityContainer container = generateTestStructure(); @@ -69,52 +69,41 @@ account = acc; } } - if (account == null) { - fail("no account object created"); - } + assertNotNull("No account object created", account); + // create jaxb context and marshaller Marshaller marshaller = null; JAXBContext context = null; - try { - context = JAXBContext.newInstance(AccountJAXBDecorator.class); - marshaller = context.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - } catch (JAXBException e) { - e.printStackTrace(); - fail(e.getMessage()); - } + context = JAXBContext.newInstance(AccountJAXBDecorator.class); + marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + // convert account object to xml String outputXml = ""; - try { - Writer writer = new StringWriter(); - marshaller.marshal(account, writer); - outputXml = writer.toString(); - writer.close(); - } catch (JAXBException e) { - fail(e.getMessage()); - } catch (IOException e) { - fail(e.getMessage()); - } + + Writer writer = new StringWriter(); + marshaller.marshal(account, writer); + outputXml = writer.toString(); + writer.close(); + assertTrue((outputXml != null) && (!outputXml.trim().isEmpty())); System.out.println(outputXml); // convert xml back to an object and assert equality to source object Account convertedAccount = null; - try { - Unmarshaller unmarshaller = context.createUnmarshaller(); - convertedAccount = (Account) unmarshaller.unmarshal(new StringReader(outputXml)); - System.out.println(convertedAccount.toString()); - } catch (JAXBException e) { - fail(e.getMessage()); - } + + Unmarshaller unmarshaller = context.createUnmarshaller(); + convertedAccount = (Account) unmarshaller.unmarshal(new StringReader(outputXml)); + System.out.println(convertedAccount.toString()); + assertTrue(account.equals(convertedAccount)); System.out.println("----- leaving Account XML Test -----"); } @Test - public void testCategoryXml() { + public void testCategoryXml() throws JAXBException, IOException { System.out.println("----- entering Category XML Test -----"); // create and fetch category object EntityContainer container = generateTestStructure(); @@ -123,46 +112,36 @@ // create jaxb context and marshaller Marshaller marshaller = null; + JAXBContext context = null; - try { - context = JAXBContext.newInstance(CategoryJAXBDecorator.class); - marshaller = context.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - } catch (JAXBException e) { - e.printStackTrace(); - fail(e.getMessage()); - } + context = JAXBContext.newInstance(CategoryJAXBDecorator.class); + marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // convert category object to xml String outputXml = ""; - try { - Writer writer = new StringWriter(); - marshaller.marshal(category, writer); - outputXml = writer.toString(); - writer.close(); - } catch (JAXBException e) { - fail(e.getMessage()); - } catch (IOException e) { - fail(e.getMessage()); - } + + Writer writer = new StringWriter(); + marshaller.marshal(category, writer); + outputXml = writer.toString(); + writer.close(); + assertTrue((outputXml != null) && (outputXml != "")); System.out.println(outputXml); // convert xml back to an object and assert equality to source object Category convertedCategory = null; - try { - Unmarshaller unmarshaller = context.createUnmarshaller(); - convertedCategory = (Category) unmarshaller.unmarshal(new StringReader(outputXml)); - System.out.println(convertedCategory.toString()); - } catch (JAXBException e) { - fail(e.getMessage()); - } + + Unmarshaller unmarshaller = context.createUnmarshaller(); + convertedCategory = (Category) unmarshaller.unmarshal(new StringReader(outputXml)); + System.out.println(convertedCategory.toString()); + assertTrue(category.equals(convertedCategory)); System.out.println("----- leaving Category XML Test -----"); } @Test - public void testCourseXml() { + public void testCourseXml() throws JAXBException, IOException { System.out.println("----- entering Course XML Test -----"); // create and fetch course object EntityContainer container = generateTestStructure(); @@ -172,45 +151,35 @@ // create jaxb context and marshaller Marshaller marshaller = null; JAXBContext context = null; - try { - context = JAXBContext.newInstance(CourseJAXBDecorator.class); - marshaller = context.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - } catch (JAXBException e) { - e.printStackTrace(); - fail(e.getMessage()); - } + context = JAXBContext.newInstance(CourseJAXBDecorator.class); + marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + // convert course object to xml String outputXml = ""; - try { - Writer writer = new StringWriter(); - marshaller.marshal(course, writer); - outputXml = writer.toString(); - writer.close(); - } catch (JAXBException e) { - fail(e.getMessage()); - } catch (IOException e) { - fail(e.getMessage()); - } + + Writer writer = new StringWriter(); + marshaller.marshal(course, writer); + outputXml = writer.toString(); + writer.close(); + assertTrue((outputXml != null) && (outputXml != "")); System.out.println(outputXml); // convert xml back to an object and assert equality to source object Course convertedCourse = null; - try { - Unmarshaller unmarshaller = context.createUnmarshaller(); - convertedCourse = (Course) unmarshaller.unmarshal(new StringReader(outputXml)); - System.out.println(convertedCourse.toString()); - } catch (JAXBException e) { - fail(e.getMessage()); - } + + Unmarshaller unmarshaller = context.createUnmarshaller(); + convertedCourse = (Course) unmarshaller.unmarshal(new StringReader(outputXml)); + System.out.println(convertedCourse.toString()); + assertTrue(course.equals(convertedCourse)); System.out.println("----- leaving Course XML Test -----"); } @Test - public void testRoleXml() { + public void testRoleXml() throws JAXBException, IOException { System.out.println("----- entering Role XML Test -----"); // create and fetch role object EntityContainer container = generateTestStructure(); @@ -220,45 +189,32 @@ // create jaxb context and marshaller Marshaller marshaller = null; JAXBContext context = null; - try { - context = JAXBContext.newInstance(RoleJAXBDecorator.class); - marshaller = context.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - } catch (JAXBException e) { - e.printStackTrace(); - fail(e.getMessage()); - } + context = JAXBContext.newInstance(RoleJAXBDecorator.class); + marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // convert role object to xml String outputXml = ""; - try { - Writer writer = new StringWriter(); - marshaller.marshal(role, writer); - outputXml = writer.toString(); - writer.close(); - } catch (JAXBException e) { - fail(e.getMessage()); - } catch (IOException e) { - fail(e.getMessage()); - } + Writer writer = new StringWriter(); + marshaller.marshal(role, writer); + outputXml = writer.toString(); + writer.close(); + assertTrue((outputXml != null) && (outputXml != "")); System.out.println(outputXml); // convert xml back to an object and assert equality to source object Role convertedRole = null; - try { - Unmarshaller unmarshaller = context.createUnmarshaller(); - convertedRole = (Role) unmarshaller.unmarshal(new StringReader(outputXml)); - System.out.println(convertedRole.toString()); - } catch (JAXBException e) { - fail(e.getMessage()); - } + Unmarshaller unmarshaller = context.createUnmarshaller(); + convertedRole = (Role) unmarshaller.unmarshal(new StringReader(outputXml)); + System.out.println(convertedRole.toString()); + assertTrue(role.equals(convertedRole)); System.out.println("----- leaving Role XML Test -----"); } @Test - public void testGroupXml() { + public void testGroupXml() throws JAXBException, IOException { System.out.println("----- entering Group XML Test -----"); // create and fetch group object EntityContainer container = generateTestStructure(); @@ -273,77 +229,57 @@ // create jaxb context and marshaller Marshaller marshaller = null; JAXBContext context = null; - try { - context = JAXBContext.newInstance(GroupJAXBDecorator.class); - marshaller = context.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - } catch (JAXBException e) { - e.printStackTrace(); - fail(e.getMessage()); - } + context = JAXBContext.newInstance(GroupJAXBDecorator.class); + marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + // convert group object to xml String outputXml = ""; - try { - Writer writer = new StringWriter(); - marshaller.marshal(group, writer); - outputXml = writer.toString(); - writer.close(); - } catch (JAXBException e) { - fail(e.getMessage()); - } catch (IOException e) { - fail(e.getMessage()); - } + + Writer writer = new StringWriter(); + marshaller.marshal(group, writer); + outputXml = writer.toString(); + writer.close(); + assertTrue((outputXml != null) && (outputXml != "")); System.out.println(outputXml); // convert xml back to an object and assert equality to source object Group convertedGroup = null; - try { - Unmarshaller unmarshaller = context.createUnmarshaller(); - convertedGroup = (Group) unmarshaller.unmarshal(new StringReader(outputXml)); - System.out.println(convertedGroup.toString()); - } catch (JAXBException e) { - fail(e.getMessage()); - } + Unmarshaller unmarshaller = context.createUnmarshaller(); + convertedGroup = (Group) unmarshaller.unmarshal(new StringReader(outputXml)); + System.out.println(convertedGroup.toString()); + assertTrue(group.equals(convertedGroup)); System.out.println("----- leaving Group XML Test -----"); } @Test - public void testXmlSchema() { + public void testXmlSchema() throws JAXBException, IOException { // create jaxb context and marshaller Marshaller marshaller = null; JAXBContext context = null; - try { - context = JAXBContext.newInstance(new Class[] { EntityContainer.class, AccountJAXBDecorator.class, GroupJAXBDecorator.class, - RoleJAXBDecorator.class, CourseJAXBDecorator.class, CategoryJAXBDecorator.class, InputMessage.class }); - marshaller = context.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - } catch (JAXBException e) { - e.printStackTrace(); - fail(e.getMessage()); - } + context = JAXBContext.newInstance(new Class[] { EntityContainer.class, AccountJAXBDecorator.class, + GroupJAXBDecorator.class, RoleJAXBDecorator.class, CourseJAXBDecorator.class, + CategoryJAXBDecorator.class, InputMessage.class }); + marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // create schema final List<DOMResult> results = new ArrayList<DOMResult>(); - try { - context.generateSchema(new SchemaOutputResolver() { - @Override - public Result createOutput(String ns, String file) throws IOException { - DOMResult result = new DOMResult(); - result.setSystemId(file); - results.add(result); - return result; - } - }); - for (DOMResult result : results) { - System.out.println("----- Generated XML Schema: "); - printDOMResult(result); + context.generateSchema(new SchemaOutputResolver() { + @Override + public Result createOutput(String ns, String file) throws IOException { + DOMResult result = new DOMResult(); + result.setSystemId(file); + results.add(result); + return result; } - } catch (IOException e) { - e.printStackTrace(); - fail(e.getMessage()); + }); + for (DOMResult result : results) { + System.out.println("----- Generated XML Schema: "); + printDOMResult(result); } } @@ -359,9 +295,9 @@ private EntityContainer generateTestStructure() { // create small category structure: parentCategory -> childCategory Category parentCategory = DomainFactory.category(createUniqueId()); - parentCategory = new CategoryJAXBDecorator(parentCategory); + parentCategory = new CategoryJAXBDecorator(parentCategory); Category childCategory = DomainFactory.category(createUniqueId()); - childCategory = new CategoryJAXBDecorator(childCategory); + childCategory = new CategoryJAXBDecorator(childCategory); List<Category> childCategoryList = new ArrayList<Category>(); childCategoryList.add(childCategory); @@ -431,7 +367,7 @@ } @Test - public void testAttributeXml() { + public void testAttributeXml() throws JAXBException, IOException { System.out.println("----- entering Attribute XML Test -----"); // create category object with two attributes Category category = new CategoryJAXBDecorator(DomainFactory.category(createUniqueId())); @@ -446,46 +382,32 @@ // create jaxb context and marshaller Marshaller marshaller = null; JAXBContext context = null; - try { - context = JAXBContext.newInstance(CategoryJAXBDecorator.class, AttributeJAXBDecorator.class, PersistentAttribute.class, - TransientAttribute.class); - marshaller = context.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - } catch (JAXBException e) { - e.printStackTrace(); - fail(e.getMessage()); - } + context = JAXBContext.newInstance(CategoryJAXBDecorator.class, AttributeJAXBDecorator.class, + PersistentAttribute.class, TransientAttribute.class); + marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + // convert group object to xml String outputXml = ""; - try { - Writer writer = new StringWriter(); - marshaller.marshal(category, writer); - outputXml = writer.toString(); - writer.close(); - } catch (JAXBException e) { - fail(e.getMessage()); - } catch (IOException e) { - fail(e.getMessage()); - } + Writer writer = new StringWriter(); + marshaller.marshal(category, writer); + outputXml = writer.toString(); + writer.close(); assertTrue((outputXml != null) && (outputXml != "")); System.out.println(outputXml); // convert xml back to an object and assert equality to source object Category convertedCategory = null; - try { - Unmarshaller unmarshaller = context.createUnmarshaller(); - convertedCategory = (Category) unmarshaller.unmarshal(new StringReader(outputXml)); - System.out.println(convertedCategory.toString()); - } catch (JAXBException e) { - fail(e.getMessage()); - } + Unmarshaller unmarshaller = context.createUnmarshaller(); + convertedCategory = (Category) unmarshaller.unmarshal(new StringReader(outputXml)); + System.out.println(convertedCategory.toString()); assertTrue(category.equals(convertedCategory)); System.out.println("----- leaving Attribute XML Test -----"); } @Test - public void testInputMessageXml() { + public void testInputMessageXml() throws JAXBException, IOException { System.out.println("----- entering InputMessage XML Test -----"); InputMessage inputMessage = new InputMessage(); @@ -493,51 +415,33 @@ inputMessage.setEventTime(new Date()); inputMessage.setInputEvent(InputEventType.CREATE_UPDATE); - Marshaller marshaller = null; - JAXBContext context = null; - try { - context = JAXBContext.newInstance(InputMessage.class); - marshaller = context.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - } catch (JAXBException e) { - e.printStackTrace(); - fail(e.getMessage()); - } + JAXBContext context = JAXBContext.newInstance(InputMessage.class); + Marshaller marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // convert group object to xml String outputXml = ""; - try { - Writer writer = new StringWriter(); - marshaller.marshal(inputMessage, writer); - outputXml = writer.toString(); - writer.close(); - } catch (JAXBException e) { - fail(e.getMessage()); - } catch (IOException e) { - fail(e.getMessage()); - } + Writer writer = new StringWriter(); + marshaller.marshal(inputMessage, writer); + outputXml = writer.toString(); + writer.close(); assertTrue((outputXml != null) && (outputXml != "")); System.out.println(outputXml); System.out.println("----- leaving InputMessage XML Test -----"); } - - + @Test - public void testUnmarshalling() { + public void testUnmarshalling() throws JAXBException { String courseXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<tns:course xmlns:tns=\"http://cse.campussource.de/DataTypes\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<id>1</id>" + "<categories>" + "<category>" + "<id>ID_2</id>" + "</category>" + "</categories>" + "</tns:course>"; - try { - JAXBContext context = JAXBContext.newInstance(CategoryJAXBDecorator.class, AttributeJAXBDecorator.class, PersistentAttribute.class, - TransientAttribute.class); - Unmarshaller unmarshaller = context.createUnmarshaller(); - Course convertedCourse = (Course) unmarshaller.unmarshal(new StringReader(courseXml)); - System.out.println(convertedCourse.toString()); - } catch (JAXBException e) { - fail(e.getMessage()); - } + JAXBContext context = JAXBContext.newInstance(CategoryJAXBDecorator.class, AttributeJAXBDecorator.class, + PersistentAttribute.class, TransientAttribute.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + Course convertedCourse = (Course) unmarshaller.unmarshal(new StringReader(courseXml)); + System.out.println(convertedCourse.toString()); } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |