killbill-memoizeit

Details

diff --git a/catalog/src/test/java/com/ning/billing/catalog/io/TestXMLReader.java b/catalog/src/test/java/com/ning/billing/catalog/io/TestXMLReader.java
index b856849..a75cb06 100644
--- a/catalog/src/test/java/com/ning/billing/catalog/io/TestXMLReader.java
+++ b/catalog/src/test/java/com/ning/billing/catalog/io/TestXMLReader.java
@@ -27,7 +27,7 @@ import org.xml.sax.SAXException;
 
 import com.google.common.io.Resources;
 import com.ning.billing.catalog.Catalog;
-import com.ning.billing.catalog.api.InvalidConfigException;
+import com.ning.billing.catalog.api.InvalidConfigException; 
 import com.ning.billing.util.config.XMLLoader;
 
 public class TestXMLReader {

util/pom.xml 5(+5 -0)

diff --git a/util/pom.xml b/util/pom.xml
index cb5fee0..9aa0bc8 100644
--- a/util/pom.xml
+++ b/util/pom.xml
@@ -66,6 +66,11 @@
             <artifactId>commons-io</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+        </dependency>
+        
     </dependencies>
     <build>
         <plugins>
diff --git a/util/src/main/java/com/ning/billing/util/config/XMLLoader.java b/util/src/main/java/com/ning/billing/util/config/XMLLoader.java
index e465ee6..f7dee0f 100644
--- a/util/src/main/java/com/ning/billing/util/config/XMLLoader.java
+++ b/util/src/main/java/com/ning/billing/util/config/XMLLoader.java
@@ -78,7 +78,7 @@ public class XMLLoader {
         }
     }
 
-	private static <T extends ValidatingConfig<T>> T getObjectFromStream(URI uri,InputStream stream, Class<T> clazz) throws SAXException, InvalidConfigException, JAXBException, IOException, TransformerException {
+	public static <T extends ValidatingConfig<T>> T getObjectFromStream(URI uri,InputStream stream, Class<T> clazz) throws SAXException, InvalidConfigException, JAXBException, IOException, TransformerException {
         Object o = unmarshaller(clazz).unmarshal(stream);
         if (clazz.isInstance(o)) {
         	@SuppressWarnings("unchecked")
diff --git a/util/src/main/java/com/ning/billing/util/config/XMLWriter.java b/util/src/main/java/com/ning/billing/util/config/XMLWriter.java
new file mode 100644
index 0000000..3f16e90
--- /dev/null
+++ b/util/src/main/java/com/ning/billing/util/config/XMLWriter.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.util.config;
+
+import java.io.ByteArrayOutputStream;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+
+public class XMLWriter<T> {
+	final private static  int MAX_XML_SIZE_IN_BYTES = 100000;
+	
+	public static <T> String writeXML(T object, Class<T> type) throws Exception {
+   	 	JAXBContext context =JAXBContext.newInstance(type);
+        Marshaller marshaller = context.createMarshaller();
+        ByteArrayOutputStream output = new ByteArrayOutputStream(MAX_XML_SIZE_IN_BYTES);
+        
+        marshaller.marshal(object, output);
+        
+        return new String(output.toByteArray());
+   }
+}
diff --git a/util/src/test/java/com/ning/billing/util/config/TestXMLLoader.java b/util/src/test/java/com/ning/billing/util/config/TestXMLLoader.java
new file mode 100644
index 0000000..02fbcd2
--- /dev/null
+++ b/util/src/test/java/com/ning/billing/util/config/TestXMLLoader.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.util.config;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Date;
+
+import javax.xml.bind.JAXBException;
+import javax.xml.transform.TransformerException;
+
+import org.testng.annotations.Test;
+import org.xml.sax.SAXException;
+
+import com.ning.billing.catalog.api.InvalidConfigException;
+
+
+public class TestXMLLoader {
+	public static final String TEST_XML = 
+			"<xmlTestClass>" +
+			"	<foo>foo</foo>" +
+			"	<bar>1.0</bar>" +
+			"	<lala>42</lala>" +
+			"</xmlTestClass>";
+	
+	@Test
+	public void test() throws SAXException, InvalidConfigException, JAXBException, IOException, TransformerException, URISyntaxException {
+		InputStream is = new ByteArrayInputStream(TEST_XML.getBytes());
+		XmlTestClass test = XMLLoader.getObjectFromStream(new URI("internal:/"), is, XmlTestClass.class);
+		assertEquals(test.getFoo(), "foo");
+		assertEquals(test.getBar(),1.0);
+		assertEquals(test.getLala(), 42);
+	}
+	
+	
+	
+	
+	
+}
diff --git a/util/src/test/java/com/ning/billing/util/config/TestXMLSchemaGenerator.java b/util/src/test/java/com/ning/billing/util/config/TestXMLSchemaGenerator.java
new file mode 100644
index 0000000..f863784
--- /dev/null
+++ b/util/src/test/java/com/ning/billing/util/config/TestXMLSchemaGenerator.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.util.config;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+
+import javax.xml.bind.JAXBException;
+import javax.xml.transform.TransformerException;
+
+import org.apache.commons.io.IOUtils;
+import org.testng.annotations.Test;
+
+public class TestXMLSchemaGenerator {
+	
+	@Test
+	public void test() throws IOException, TransformerException, JAXBException {
+		InputStream stream = XMLSchemaGenerator.xmlSchema(XmlTestClass.class);
+		StringWriter writer = new StringWriter();
+		IOUtils.copy(stream, writer);
+		String result = writer.toString();
+		
+		System.out.println(result);
+	}
+}
diff --git a/util/src/test/java/com/ning/billing/util/config/TestXMLWriter.java b/util/src/test/java/com/ning/billing/util/config/TestXMLWriter.java
new file mode 100644
index 0000000..f9115a0
--- /dev/null
+++ b/util/src/test/java/com/ning/billing/util/config/TestXMLWriter.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.util.config;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.net.URI;
+
+import org.testng.annotations.Test;
+
+public class TestXMLWriter {
+	public static final String TEST_XML = 
+			"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
+		    "<xmlTestClass>" +
+			"<foo>foo</foo>" +
+			"<bar>1.0</bar>" +
+			"<lala>42</lala>" +
+			"</xmlTestClass>";
+	
+	@Test
+	public void test() throws Exception {
+		InputStream is = new ByteArrayInputStream(TEST_XML.getBytes());
+		XmlTestClass test = XMLLoader.getObjectFromStream(new URI("internal:/"), is, XmlTestClass.class);
+		assertEquals(test.getFoo(), "foo");
+		assertEquals(test.getBar(), 1.0);
+		assertEquals(test.getLala(), 42);
+		
+		String output = XMLWriter.writeXML(test, XmlTestClass.class);
+		
+		System.out.println(output);
+		assertEquals(output, TEST_XML);
+		 
+	}
+
+	
+}
diff --git a/util/src/test/java/com/ning/billing/util/config/XmlTestClass.java b/util/src/test/java/com/ning/billing/util/config/XmlTestClass.java
new file mode 100644
index 0000000..baa8f3c
--- /dev/null
+++ b/util/src/test/java/com/ning/billing/util/config/XmlTestClass.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.util.config;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement
+@XmlAccessorType(XmlAccessType.FIELD)
+public class XmlTestClass extends ValidatingConfig<XmlTestClass>{
+	private String foo;
+	private Double bar;
+	private int lala;
+	
+	public String getFoo() {
+		return foo;
+	}
+	
+	public Double getBar() {
+		return bar;
+	}
+	
+	public int getLala() {
+		return lala;
+	}
+	
+	@Override
+	public ValidationErrors validate(XmlTestClass root, ValidationErrors errors) {
+		return errors;
+	}
+}
\ No newline at end of file