Changed meta package and added junit tests

This commit is contained in:
Willem Cazander 2013-04-15 18:11:55 +02:00
parent 897fe80357
commit 226c1f0425
35 changed files with 822 additions and 383 deletions

View file

@ -50,10 +50,9 @@ public abstract class X4ODriver<T> {
public final static String DEFAULT_LANGUAGE_VERSION = "1.0";
/**
* Force public constructor and register the driver.
* Public constructor.
*/
public X4ODriver() {
X4ODriverManager.registerX4ODriver(this);
}
/**
@ -67,7 +66,6 @@ public abstract class X4ODriver<T> {
abstract public String[] getLanguageVersions();
protected X4OLanguage buildLanguage(String version) {
return X4ODriverManager.getDefaultBuildLanguage(this, version);
}
@ -81,7 +79,6 @@ public abstract class X4ODriver<T> {
}
public X4OSchemaWriter createSchemaWriter() {
return createSchemaWriter(getLanguageVersionDefault());
}

View file

@ -184,28 +184,8 @@ public final class X4ODriverManager {
if (instance.drivers.containsKey(language)) {
return instance.drivers.get(language);
}
try {
instance.lazyInit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
X4ODriver<?> result = null;
try {
result = instance.createX4ODriver(language);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
instance.lazyInit();
X4ODriver<?> result = instance.createX4ODriver(language);
if (result==null) {
throw new IllegalArgumentException("Can't find driver for language: "+language);
}
@ -213,15 +193,7 @@ public final class X4ODriverManager {
}
static public List<String> getX4OLanguages() {
try {
instance.lazyInit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
instance.lazyInit();
List<String> result = new ArrayList<String>(10);
result.addAll(instance.classdrivers.keySet());
result.addAll(instance.defaultDrivers.keySet());
@ -229,7 +201,7 @@ public final class X4ODriverManager {
return result;
}
private void lazyInit() throws IOException, SAXException {
private void lazyInit() {
if (reloadDrivers==false) {
return;
}
@ -237,38 +209,44 @@ public final class X4ODriverManager {
reloadDrivers = false;
}
private X4ODriver<?> createX4ODriver(String language) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
private X4ODriver<?> createX4ODriver(String language) {
String driverClassName = null;
if (classdrivers.containsKey(language)) {
String driverClassName = classdrivers.get(language);
driverClassName = classdrivers.get(language);
} else if (defaultDrivers.containsKey(language)) {
driverClassName = defaultDrivers.get(language);
}
if (driverClassName==null) {
return null;
}
try {
Class<?> driverClass = X4OLanguageClassLoader.loadClass(driverClassName);
X4ODriver<?> driver = (X4ODriver<?>)driverClass.newInstance();
registerX4ODriver(driver);
return driver;
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(),e);
}
if (defaultDrivers.containsKey(language)) {
String driverClassName = defaultDrivers.get(language);
Class<?> driverClass = X4OLanguageClassLoader.loadClass(driverClassName);
X4ODriver<?> driver = (X4ODriver<?>)driverClass.newInstance();
return driver;
}
return null;
}
/**
* Loads all defined language drivers in classpath.
*/
private void loadLanguageDrivers() throws IOException, SAXException {
private void loadLanguageDrivers() {
logger.finer("loading x4o drivers from: "+X4O_DRIVERS_RESOURCE);
Enumeration<URL> e = Thread.currentThread().getContextClassLoader().getResources(X4O_DRIVERS_RESOURCE);
while(e.hasMoreElements()) {
URL u = e.nextElement();
loadDriversXml(u.openStream());
}
e = Thread.currentThread().getContextClassLoader().getResources("/"+X4O_DRIVERS_RESOURCE);
while(e.hasMoreElements()) {
URL u = e.nextElement();
loadDriversXml(u.openStream());
try {
Enumeration<URL> e = Thread.currentThread().getContextClassLoader().getResources(X4O_DRIVERS_RESOURCE);
while(e.hasMoreElements()) {
URL u = e.nextElement();
loadDriversXml(u.openStream());
}
e = Thread.currentThread().getContextClassLoader().getResources("/"+X4O_DRIVERS_RESOURCE);
while(e.hasMoreElements()) {
URL u = e.nextElement();
loadDriversXml(u.openStream());
}
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(),e);
}
}

View file

@ -147,10 +147,8 @@ public class ElementRefectionBindingHandler extends AbstractElementBindingHandle
if (childClass.isAssignableFrom(childObject.getClass())==false) {
return;
}
if (skipChilderenClassRegex!=null) {
if (childObject.getClass().getName().matches(skipChilderenClassRegex)) {
return; // skip
}
if (skipChilderenClassRegex!=null && childObject.getClass().getName().matches(skipChilderenClassRegex)) {
return; // skip
}
createChild(parentElement,childObject);
}

View file

@ -23,9 +23,7 @@
package org.x4o.xml.io;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@ -44,6 +42,7 @@ import javax.xml.parsers.ParserConfigurationException;
* @author Willem Cazander
* @version 1.0 Aug 11, 2005
*/
@SuppressWarnings("unchecked")
abstract public class AbstractX4OReader<T> extends AbstractX4OReaderContext<T> implements X4OReader<T> {
public AbstractX4OReader(X4OLanguageContext elementLanguage) {
@ -56,11 +55,9 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderContext<T> i
public String[] getPropertyKeySet() {
return X4OLanguagePropertyKeys.DEFAULT_X4O_READER_KEYS;
}
@SuppressWarnings("unchecked")
public T read(InputStream input, String systemId, URL basePath) throws ParserConfigurationException, SAXException, IOException {
X4OLanguageContext context = readContext(input, systemId, basePath);
return (T)context.getRootElement().getElementObject();
return (T)readContext(input, systemId, basePath).getRootElement().getElementObject();
}
/**
@ -75,10 +72,7 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderContext<T> i
* @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL)
*/
public T readFile(String fileName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException {
if (fileName==null) {
throw new NullPointerException("Can't convert null fileName to file object.");
}
return readFile(new File(fileName));
return (T)readFileContext(fileName).getRootElement().getElementObject();
}
/**
@ -93,24 +87,7 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderContext<T> i
* @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL)
*/
public T readFile(File file) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException {
if (file==null) {
throw new NullPointerException("Can't read null file.");
}
if (file.exists()==false) {
throw new FileNotFoundException("File does not exists; "+file);
}
if (file.canRead()==false) {
throw new IOException("File exists but can't read file: "+file);
}
URL basePath = new File(file.getAbsolutePath()).toURI().toURL();
InputStream inputStream = new FileInputStream(file);
try {
return read(inputStream,file.getAbsolutePath(),basePath);
} finally {
if(inputStream!=null) {
inputStream.close();
}
}
return (T)readFileContext(file).getRootElement().getElementObject();
}
/**
@ -125,29 +102,7 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderContext<T> i
* @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL)
*/
public T readResource(String resourceName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException {
if (resourceName==null) {
throw new NullPointerException("Can't read null resourceName from classpath.");
}
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) cl = getClass().getClassLoader(); // fallback
URL url = cl.getResource(resourceName);
if (url==null) {
throw new NullPointerException("Could not find resource on classpath: "+resourceName);
}
String baseUrl = url.toExternalForm();
int lastSlash = baseUrl.lastIndexOf('/');
if (lastSlash > 0 && (lastSlash+1) < baseUrl.length()) {
baseUrl = baseUrl.substring(0,lastSlash+1);
}
URL basePath = new URL(baseUrl);
InputStream inputStream = cl.getResourceAsStream(resourceName);
try {
return read(inputStream,url.toExternalForm(),basePath);
} finally {
if(inputStream!=null) {
inputStream.close();
}
}
return (T)readResourceContext(resourceName).getRootElement().getElementObject();
}
/**
@ -160,11 +115,7 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderContext<T> i
* @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL)
*/
public T readString(String xmlString) throws ParserConfigurationException,SAXException,IOException,NullPointerException {
if (xmlString==null) {
throw new NullPointerException("Can't read null xml string.");
}
URL basePath = new File(System.getProperty("user.dir")).toURI().toURL();
return read(new ByteArrayInputStream(xmlString.getBytes()),"inline-xml",basePath);
return (T)readStringContext(xmlString).getRootElement().getElementObject();
}
/**
@ -177,10 +128,6 @@ abstract public class AbstractX4OReader<T> extends AbstractX4OReaderContext<T> i
* @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL)
*/
public T readUrl(URL url) throws ParserConfigurationException,SAXException,IOException,NullPointerException {
if (url==null) {
throw new NullPointerException("Can't read null url.");
}
URL basePath = new URL(url.toExternalForm().substring(0,url.toExternalForm().length()-url.getFile().length()));
return read(url.openStream(),url.toExternalForm(),basePath);
return (T)readUrlContext(url).getRootElement().getElementObject();
}
}

View file

@ -24,7 +24,6 @@
package org.x4o.xml.io;
import org.x4o.xml.X4ODriver;
import org.x4o.xml.X4ODriverManager;
/**
* DefaultX4ODriver can be used to create language without code and type safty.
@ -44,7 +43,6 @@ public class DefaultX4ODriver<T> extends X4ODriver<T> {
public DefaultX4ODriver(String languageName,String languageVersion) {
this.languageName=languageName;
this.languageVersion=languageVersion;
X4ODriverManager.registerX4ODriver(this);
}
@Override

View file

@ -365,14 +365,5 @@ public class AttributeMap<K,V> implements Map<K,V> {
}
return false;
}
/**
* @see java.lang.Object#hashCode()
* @return The hashCode.
*/
@Override
public int hashCode() {
return super.hashCode();
}
}
}

View file

@ -67,9 +67,9 @@ public class DefaultX4OLanguageLoader implements X4OLanguageLoader {
* @param message The message to log to the debug output.
*/
private void logMessage(X4OLanguage language,String message) {
logger.finest(message);
logger.finest(message+" from: "+language.getLanguageName());
/*
if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) {
try {
language.getLanguageConfiguration().getX4ODebugWriter().debugPhaseMessage(message, this.getClass());
} catch (ElementException e) {

View file

@ -37,6 +37,24 @@ import junit.framework.TestCase;
*/
public class X4ODriverManagerTest extends TestCase {
public void testDefaultLanguageVersionSelect() throws Exception {
String version = X4ODriverManager.getDefaultLanguageVersion(new String[]{"1.0","2.0","3.0"});
assertNotNull(version);
assertEquals("3.0",version);
}
public void testDefaultLanguageVersionEmpty() throws Exception {
String version = X4ODriverManager.getDefaultLanguageVersion(new String[]{});
assertNotNull(version);
assertEquals(X4ODriver.DEFAULT_LANGUAGE_VERSION,version);
}
public void testDefaultLanguageVersionNull() throws Exception {
String version = X4ODriverManager.getDefaultLanguageVersion(null);
assertNotNull(version);
assertEquals(X4ODriver.DEFAULT_LANGUAGE_VERSION,version);
}
public void testLanguageNull() throws Exception {
String language = null;
Exception e = null;

View file

@ -0,0 +1,217 @@
/*
* Copyright (c) 2004-2012, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.x4o.xml.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import org.x4o.xml.X4ODriver;
import org.x4o.xml.io.X4OReader;
import org.x4o.xml.lang.X4OLanguageContext;
import org.x4o.xml.test.TestDriver;
import org.x4o.xml.test.models.TestBean;
import org.x4o.xml.test.models.TestObjectRoot;
import junit.framework.TestCase;
/**
* X4OAbstractReaderContextTest.
*
* @author Willem Cazander
* @version 1.0 Apr 15, 2013
*/
public class X4OAbstractReaderContextTest extends TestCase {
private File copyResourceToTempFile() throws IOException {
File tempFile = File.createTempFile("test-resource", ".xml");
tempFile.deleteOnExit();
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
X4OWriter<TestObjectRoot> writer = driver.createWriter();
try {
writer.writeFile(reader.readResource("tests/attributes/test-bean.xml"), tempFile);
} catch (Exception e) {
throw new IllegalStateException(e);
}
return tempFile;
}
public void testReadFileName() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
File xmlFile = copyResourceToTempFile();
X4OLanguageContext context = reader.readFileContext(xmlFile.getAbsolutePath());
TestObjectRoot root = (TestObjectRoot)context.getRootElement().getElementObject();
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
}
public void testReadFileNameNull() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
Exception e = null;
try {
String nullFileName = null;
reader.readFileContext(nullFileName);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("fileName"));
}
public void testReadFile() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
File xmlFile = copyResourceToTempFile();
X4OLanguageContext context = reader.readFileContext(xmlFile);
TestObjectRoot root = (TestObjectRoot)context.getRootElement().getElementObject();
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
}
public void testReadFileNull() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
Exception e = null;
try {
File nullFile = null;
reader.readFileContext(nullFile);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("file"));
}
public void testReadFileNotExists() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
Exception e = null;
try {
File tempFile = File.createTempFile("test-file", ".xml");
tempFile.delete();
reader.readFileContext(tempFile);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",FileNotFoundException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("exists"));
assertTrue("Wrong exception message",e.getMessage().contains("File"));
}
public void testReadResource() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
X4OLanguageContext context = reader.readResourceContext("tests/attributes/test-bean.xml");
TestObjectRoot root = (TestObjectRoot)context.getRootElement().getElementObject();
assertNotNull(root);
}
public void testReadResourceNull() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
Exception e = null;
try {
reader.readResourceContext(null);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("resourceName"));
}
public void testReadString() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
X4OLanguageContext context = reader.readStringContext(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<root:root xmlns:root=\"http://test.x4o.org/xml/ns/test-root\" xmlns=\"http://test.x4o.org/xml/ns/test-lang\">"+
"<testBean privateIntegerTypeField=\"987654321\"/>"+
"</root:root>"
);
TestObjectRoot root = (TestObjectRoot)context.getRootElement().getElementObject();
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
assertEquals("987654321", ""+bean.getPrivateIntegerTypeField());
}
public void testReadStringNull() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
Exception e = null;
try {
reader.readStringContext(null);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("string"));
}
public void testReadUrl() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
URL xmlUrl = Thread.currentThread().getContextClassLoader().getResource("tests/attributes/test-bean.xml");
X4OLanguageContext context = reader.readUrlContext(xmlUrl);
TestObjectRoot root = (TestObjectRoot)context.getRootElement().getElementObject();
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
}
public void testReadUrlNull() throws Exception {
TestDriver driver = TestDriver.getInstance();
X4OReaderContext<TestObjectRoot> reader = driver.createReaderContext();
Exception e = null;
try {
reader.readUrlContext(null);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("url"));
}
}

View file

@ -0,0 +1,193 @@
/*
* Copyright (c) 2004-2012, Willem Cazander
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.x4o.xml.io;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.x4o.xml.X4ODriver;
import org.x4o.xml.io.X4OReader;
import org.x4o.xml.test.TestDriver;
import org.x4o.xml.test.models.TestBean;
import org.x4o.xml.test.models.TestObjectRoot;
import junit.framework.TestCase;
/**
* X4OAbstractReaderTest.
*
* @author Willem Cazander
* @version 1.0 Apr 15, 2013
*/
public class X4OAbstractReaderTest extends TestCase {
private File copyResourceToTempFile() throws IOException {
File tempFile = File.createTempFile("test-resource", ".xml");
tempFile.deleteOnExit();
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
X4OWriter<TestObjectRoot> writer = driver.createWriter();
try {
writer.writeFile(reader.readResource("tests/attributes/test-bean.xml"), tempFile);
} catch (Exception e) {
throw new IllegalStateException(e);
}
return tempFile;
}
public void testReadResource() throws Exception {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
TestObjectRoot root = reader.readResource("tests/attributes/test-bean.xml");
assertNotNull(root);
}
public void testReadResourceNull() throws Exception {
Exception e = null;
try {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
reader.readResource(null);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("resourceName"));
}
public void testReadString() throws Exception {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
TestObjectRoot root = reader.readString(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<root:root xmlns:root=\"http://test.x4o.org/xml/ns/test-root\" xmlns=\"http://test.x4o.org/xml/ns/test-lang\">"+
"<testBean privateIntegerTypeField=\"987654321\"/>"+
"</root:root>"
);
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
assertEquals("987654321", ""+bean.getPrivateIntegerTypeField());
}
public void testReadStringNull() throws Exception {
Exception e = null;
try {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
reader.readString(null);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("string"));
}
public void testReadUrl() throws Exception {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
URL xmlUrl = Thread.currentThread().getContextClassLoader().getResource("tests/attributes/test-bean.xml");
TestObjectRoot root = reader.readUrl(xmlUrl);
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
}
public void testReadUrlNull() throws Exception {
Exception e = null;
try {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
reader.readUrl(null);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("url"));
}
public void testReadFileName() throws Exception {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
File xmlFile = copyResourceToTempFile();
TestObjectRoot root = reader.readFile(xmlFile.getAbsolutePath());
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
}
public void testReadFileNameNull() throws Exception {
Exception e = null;
try {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
String nullFileName = null;
reader.readFile(nullFileName);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("fileName"));
}
public void testReadFile() throws Exception {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
File xmlFile = copyResourceToTempFile();
TestObjectRoot root = reader.readFile(xmlFile);
assertNotNull(root);
assertTrue(root.getTestBeans().size()>0);
TestBean bean = root.getTestBeans().get(0);
assertNotNull(bean);
}
public void testReadFileNull() throws Exception {
Exception e = null;
try {
X4ODriver<TestObjectRoot> driver = TestDriver.getInstance();
X4OReader<TestObjectRoot> reader = driver.createReader();
File nullFile = null;
reader.readFile(nullFile);
} catch (Exception catchE) {
e = catchE;
}
assertNotNull("No exception",e);
assertEquals("Wrong exception class",NullPointerException.class, e.getClass());
assertTrue("Wrong exception message",e.getMessage().contains("null"));
assertTrue("Wrong exception message",e.getMessage().contains("file"));
}
}

View file

@ -57,7 +57,7 @@ public class TestBean {
public Character publicCharObjectField = new Character(' ');
public String publicStringObjectField = " ";
public Date publicDateObjectField = new Date(0);
//public Date publicDateObjectField = new Date(0); // TODO add date converters
// private
@ -84,7 +84,7 @@ public class TestBean {
private Character privateCharObjectField = new Character(' ');
private String privateStringObjectField = " ";
private Date privateDateObjectField = new Date(0);
//private Date privateDateObjectField = new Date(0);
// auto gen , get/set-ers
@ -269,18 +269,18 @@ public class TestBean {
public void setPublicStringObjectField(String publicStringObjectField) {
this.publicStringObjectField = publicStringObjectField;
}
/**
/*
* @return the publicDateObjectField
*/
public Date getPublicDateObjectField() {
return publicDateObjectField;
}
/**
}*/
/*
* @param publicDateObjectField the publicDateObjectField to set
*/
public void setPublicDateObjectField(Date publicDateObjectField) {
this.publicDateObjectField = publicDateObjectField;
}
}*/
/**
* @return the privateIntegerTypeField
*/
@ -461,16 +461,16 @@ public class TestBean {
public void setPrivateStringObjectField(String privateStringObjectField) {
this.privateStringObjectField = privateStringObjectField;
}
/**
/*
* @return the privateDateObjectField
*/
public Date getPrivateDateObjectField() {
return privateDateObjectField;
}
/**
}*/
/*
* @param privateDateObjectField the privateDateObjectField to set
*/
public void setPrivateDateObjectField(Date privateDateObjectField) {
this.privateDateObjectField = privateDateObjectField;
}
}*/
}

View file

@ -0,0 +1,152 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2004-2012, Willem Cazander
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided
that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<root:module
xmlns:root="http://eld.x4o.org/xml/ns/eld-root"
xmlns:eld="http://eld.x4o.org/xml/ns/eld-lang"
xmlns:conv="http://eld.x4o.org/xml/ns/eld-conv"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://eld.x4o.org/xml/ns/eld-root http://eld.x4o.org/xml/ns/eld-root-1.0.xsd"
providerName="test.x4o.org"
name="Test Language"
id="test-module"
>
<eld:classBindingHandler id="Parent-Child" parentClass="org.x4o.xml.test.models.TestObjectParent" childClass="org.x4o.xml.test.models.TestObjectChild" addMethod="addTestObjectChild" getMethod="getTestObjectChilds">
<eld:description>Binds the TestObjectChild to the TestObjectParent</eld:description>
</eld:classBindingHandler>
<eld:classBindingHandler id="Root-Child" parentClass="org.x4o.xml.test.models.TestObjectRoot" childClass="org.x4o.xml.test.models.TestObjectChild" addMethod="addChild" getMethod="getTestObjectChilds">
<eld:description>Binds the TestBean to the TestObjectChild</eld:description>
</eld:classBindingHandler>
<eld:classBindingHandler id="Root-Parent" parentClass="org.x4o.xml.test.models.TestObjectRoot" childClass="org.x4o.xml.test.models.TestObjectParent" addMethod="addParent" getMethod="getTestObjectParents">
<eld:description>Binds the TestObjectParent to the TestObjectRoot</eld:description>
</eld:classBindingHandler>
<eld:classBindingHandler id="Root-Bean" parentClass="org.x4o.xml.test.models.TestObjectRoot" childClass="org.x4o.xml.test.models.TestBean" addMethod="addTestBean" getMethod="getTestBeans">
<eld:description>Binds the TestBean to the TestObjectRoot</eld:description>
</eld:classBindingHandler>
<eld:classBindingHandler id="JComponent-JComponent" parentClass="javax.swing.JComponent" childClass="javax.swing.JComponent" addMethod="addComponent" getMethod="">
<eld:description>Binds j components.</eld:description>
</eld:classBindingHandler>
<eld:classBindingHandler id="JFrame-JPanel" parentClass="javax.swing.JFrame" childClass="javax.swing.JPanel" addMethod="add" getMethod="getComponents">
<eld:description>Binds panel to frame components as unit check.</eld:description>
</eld:classBindingHandler>
<eld:elementInterface id="Component" interfaceClass="java.awt.Component">
<eld:description>Configs the Component based objects.</eld:description>
<eld:attribute name="bounds">
<conv:stringSplitConverter classTo="java.awt.Rectangle" split="," splitSize="4" singleToMethod="setRect" useNativeType="true">
<conv:stringSplitConverterStep fromMethod="getX" fromOrder="1" toOrder="1"><conv:doubleConverter/></conv:stringSplitConverterStep>
<conv:stringSplitConverterStep fromMethod="getY" fromOrder="2" toOrder="2"><conv:doubleConverter/></conv:stringSplitConverterStep>
<conv:stringSplitConverterStep fromMethod="getWidth" fromOrder="3" toOrder="3"><conv:doubleConverter/></conv:stringSplitConverterStep>
<conv:stringSplitConverterStep fromMethod="getHeight" fromOrder="4" toOrder="4"><conv:doubleConverter/></conv:stringSplitConverterStep>
</conv:stringSplitConverter>
</eld:attribute>
</eld:elementInterface>
<eld:elementInterface id="JComponent" interfaceClass="javax.swing.JComponent">
<eld:description>Configs the JComponent based objects.</eld:description>
<eld:classBindingHandler id="JComponent-JComponent" parentClass="javax.swing.JComponent" childClass="javax.swing.JComponent" addMethod="add" getMethod="getComponents">
<eld:description>Binds the JComponent to the JComponent.</eld:description>
</eld:classBindingHandler>
</eld:elementInterface>
<eld:configuratorGlobal bean.class="org.x4o.xml.test.element.TestElementConfigurator" id="testConfigGlobal">
<eld:description>Test the global element configurator.</eld:description>
</eld:configuratorGlobal>
<eld:attributeHandler attributeName="tt.attr1" bean.class="org.x4o.xml.test.element.TestElementAttributeHandler" id="attrTest1">
<eld:description>Test the global element attribute1 handler.</eld:description>
</eld:attributeHandler>
<eld:attributeHandler attributeName="tt.attr2" bean.class="org.x4o.xml.test.element.TestElementAttributeHandler" id="attrTest2">
<eld:description>Test the global element attribute2 handler.</eld:description>
<eld:attributeHandlerNextAttribute attributeName="tt.attr1"/>
</eld:attributeHandler>
<eld:namespace
uri="http://test.x4o.org/xml/ns/test-root"
schemaUri="http://test.x4o.org/xml/ns/test-root-1.0.xsd"
schemaResource="test-root-1.0.xsd"
schemaPrefix="root"
description="Root namespace to have nice namespaceing."
name="Test Root Namespace"
languageRoot="true"
id="test-root"
>
<!-- Root Element for nice namespace'ing -->
<eld:element tag="root" objectClass="org.x4o.xml.test.models.TestObjectRoot">
<eld:description>The test root element.</eld:description>
</eld:element>
</eld:namespace>
<eld:namespace
uri="http://test.x4o.org/xml/ns/test-lang"
schemaUri="http://test.x4o.org/xml/ns/test-lang-1.0.xsd"
schemaResource="test-lang-1.0.xsd"
schemaPrefix="lang"
description="Test language namespace to test some/most features"
name="Test Language Namespace"
id="test-lang"
>
<eld:element tag="testNoNSRoot" objectClass="org.x4o.xml.test.models.TestObjectRoot"/>
<eld:element tag="testBean" objectClass="org.x4o.xml.test.models.TestBean">
<eld:description>The test the bean object.</eld:description>
</eld:element>
<eld:element tag="configBean" objectClass="org.x4o.xml.test.models.TestBean">
<eld:configurator bean.class="org.x4o.xml.test.element.TestElementConfigurator" id="testConfig1">
<eld:description>The test element config.</eld:description>
</eld:configurator>
<eld:configurator bean.class="org.x4o.xml.test.element.TestElementConfigurator" id="testConfig2"/>
<eld:elementSkipPhase name="runAttributesPhase"/>
<eld:elementSkipPhase name="transformPhase"/>
</eld:element>
<eld:element tag="parent" objectClass="org.x4o.xml.test.models.TestObjectParent"/>
<eld:element tag="child" objectClass="org.x4o.xml.test.models.TestObjectChild"/>
<eld:element tag="testObjectParent" objectClass="org.x4o.xml.test.models.TestObjectParent"/>
<eld:element tag="testObjectChild" objectClass="org.x4o.xml.test.models.TestObjectChild"/>
<eld:element tag="testBeanObjectChild" objectClass="org.x4o.xml.test.models.TestObjectChild">
<!--
<eld:element tag="testBeanObjectChild" objectClass="org.x4o.xml.models.TestObjectChild" parentNamespace="http://test.x4o.org/eld/iets.eld" elementTag="superObject"/>
<eld:elementClass tag="JTextArea" objectClassName="javax.swing.JTextArea"/>
<eld:elementClassParentElementClass namespace="http://test.x4o.org/eld/iets.eld" elementTag="superObject"/>
<eld:elementClassParentElementClass namespace="http://test.x4o.org/eld/iets.eld" elementTag="superObject2"/>
-->
</eld:element>
<eld:element tag="JFrame" objectClass="javax.swing.JFrame">
<eld:elementParent tag="root" uri="http://test.x4o.org/xml/ns/test-root"/>
</eld:element>
<eld:element tag="JFrameContentPane" elementClass="org.x4o.xml.test.element.ContentPaneElement"/>
<eld:element tag="JLabel" objectClass="javax.swing.JLabel"/>
<eld:element tag="JPanel" objectClass="javax.swing.JPanel"/>
<eld:element tag="JTextField" objectClass="javax.swing.JTextField"/>
<eld:element tag="JTextArea" objectClass="javax.swing.JTextArea"/>
<eld:element tag="JScrollPane" objectClass="javax.swing.JScrollPane"/>
</eld:namespace>
</root:module>

View file

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2004-2012, Willem Cazander
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided
that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<modules version="1.0"
xmlns="http://language.x4o.org/xml/ns/modules"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://language.x4o.org/xml/ns/modules http://language.x4o.org/xml/ns/modules-1.0.xsd"
>
<language version="1.0">
<eld-resource>defd-lang.eld</eld-resource>
</language>
</modules>

View file

@ -30,4 +30,5 @@
>
<driver language="test" className="org.x4o.xml.test.TestDriver"/>
<driver language="swixml" className="org.x4o.xml.test.swixml.SwiXmlDriver"/>
<defaultDriver language="junit-defp"/>
</drivers>