From 226c1f04255fa32eb3d17c0646ba4ffbc3ef66bc Mon Sep 17 00:00:00 2001 From: Willem Cazander Date: Mon, 15 Apr 2013 18:11:55 +0200 Subject: [PATCH] Changed meta package and added junit tests --- todo.txt | 6 +- versions.txt | 10 +- .../src/main/java/org/x4o/xml/X4ODriver.java | 5 +- .../java/org/x4o/xml/X4ODriverManager.java | 84 +++---- .../lang/ElementRefectionBindingHandler.java | 6 +- .../org/x4o/xml/io/AbstractX4OReader.java | 69 +----- .../java/org/x4o/xml/io/DefaultX4ODriver.java | 2 - .../java/org/x4o/xml/io/sax/AttributeMap.java | 9 - .../xml/lang/DefaultX4OLanguageLoader.java | 4 +- .../org/x4o/xml/X4ODriverManagerTest.java | 18 ++ .../xml/io/X4OAbstractReaderContextTest.java | 217 ++++++++++++++++++ .../org/x4o/xml/io/X4OAbstractReaderTest.java | 193 ++++++++++++++++ .../org/x4o/xml/test/models/TestBean.java | 28 +-- .../META-INF/junit-defd/defd-lang.eld | 152 ++++++++++++ .../META-INF/junit-defd/defd-modules.xml | 34 +++ .../test/resources/META-INF/x4o-drivers.xml | 1 + .../meta}/ELIDAttributeHandler.java | 2 +- .../meta}/ELReferenceElement.java | 2 +- .../meta/MetaLanguageSiblingLoader.java | 2 +- .../lang => lang/meta}/MethodElement.java | 2 +- .../meta}/ParentObjectElement.java | 2 +- .../lang => lang/meta}/PropertyElement.java | 2 +- .../x4o/xml/{ => lang}/meta/package-info.java | 4 +- .../xml/meta/lang/BeanPropertyComparator.java | 175 -------------- .../org/x4o/xml/meta/lang/package-info.java | 31 --- .../resources/META-INF/meta/meta-lang.eld | 10 +- .../{meta/test => lang/meta}/MTestDriver.java | 4 +- .../x4o/xml/lang/meta/ParentObjectTest.java | 84 +++++++ .../{ => lang}/meta/ReferenceStoreTest.java | 9 +- .../META-INF/mtest/mtest-modules.xml | 2 +- .../template/test-meta-template-bean.xml} | 0 .../template/test-meta-template-parent.xml} | 0 .../test-meta-generic.xml} | 0 .../junit/test-meta-parent-element.xml | 36 +++ .../test-meta-reference.xml} | 0 35 files changed, 822 insertions(+), 383 deletions(-) create mode 100644 x4o-driver/src/test/java/org/x4o/xml/io/X4OAbstractReaderContextTest.java create mode 100644 x4o-driver/src/test/java/org/x4o/xml/io/X4OAbstractReaderTest.java create mode 100644 x4o-driver/src/test/resources/META-INF/junit-defd/defd-lang.eld create mode 100644 x4o-driver/src/test/resources/META-INF/junit-defd/defd-modules.xml rename x4o-meta/src/main/java/org/x4o/xml/{meta/lang => lang/meta}/ELIDAttributeHandler.java (98%) rename x4o-meta/src/main/java/org/x4o/xml/{meta/lang => lang/meta}/ELReferenceElement.java (98%) rename x4o-meta/src/main/java/org/x4o/xml/{ => lang}/meta/MetaLanguageSiblingLoader.java (99%) rename x4o-meta/src/main/java/org/x4o/xml/{meta/lang => lang/meta}/MethodElement.java (98%) rename x4o-meta/src/main/java/org/x4o/xml/{meta/lang => lang/meta}/ParentObjectElement.java (98%) rename x4o-meta/src/main/java/org/x4o/xml/{meta/lang => lang/meta}/PropertyElement.java (99%) rename x4o-meta/src/main/java/org/x4o/xml/{ => lang}/meta/package-info.java (95%) delete mode 100644 x4o-meta/src/main/java/org/x4o/xml/meta/lang/BeanPropertyComparator.java delete mode 100644 x4o-meta/src/main/java/org/x4o/xml/meta/lang/package-info.java rename x4o-meta/src/test/java/org/x4o/xml/{meta/test => lang/meta}/MTestDriver.java (96%) create mode 100644 x4o-meta/src/test/java/org/x4o/xml/lang/meta/ParentObjectTest.java rename x4o-meta/src/test/java/org/x4o/xml/{ => lang}/meta/ReferenceStoreTest.java (92%) rename x4o-meta/src/test/resources/{tests/template/bean-template.xml => junit/template/test-meta-template-bean.xml} (100%) rename x4o-meta/src/test/resources/{tests/template/test-templating.xml => junit/template/test-meta-template-parent.xml} (100%) rename x4o-meta/src/test/resources/{tests/meta-generic.xml => junit/test-meta-generic.xml} (100%) create mode 100644 x4o-meta/src/test/resources/junit/test-meta-parent-element.xml rename x4o-meta/src/test/resources/{tests/reference/store-ref.xml => junit/test-meta-reference.xml} (100%) diff --git a/todo.txt b/todo.txt index 99bc34f..d67bb23 100644 --- a/todo.txt +++ b/todo.txt @@ -8,11 +8,10 @@ - meta add more ns and jndi ref/etc' - redo eld/default converters config - tag aliases -- maven plugin - lang-el-beans.xml loading - Add XML schema tags to ELD - Auto generate schemas from eld -- Basic write support (for editor) +- Basic write support - Basic editor support - Make converter config/loading better - Single xsd output @@ -28,7 +27,6 @@ - event listeners on all DOM tree element operations - custom and merga'ble parse phases for speed - make element tree jdom api compatible -- (DONE) change phase enum to generic (text) phases. - Test if possible to use threadpool for executing phases -- TODO for version 1.0 -- @@ -36,8 +34,6 @@ ## NON-CODE - Add tutorial - doc eld and x4o lang files -- Create xslt eld to html converter -- split into multiple maven projects ##CODE - Add (super) tag for extending tags of other namespace diff --git a/versions.txt b/versions.txt index a71c942..929f4e5 100644 --- a/versions.txt +++ b/versions.txt @@ -2,14 +2,20 @@ === X4O versions === +Version 0.8.6: +- Changed to X4ODriver interface. +- Added write support +- Added ant and maven plugins + Version 0.8.5: - Made module loading system. -- Added XSD generator. +- Added eld to schema generator. +- Added eld to html generator. - Cleaned xml uri nameing. - Cleaned language properties. - refactored ELD tag names. - Made elddoc ~working. - +- Changed phase enum to text phases. Version 0.8.2: - Changed packages to org.x4o diff --git a/x4o-driver/src/main/java/org/x4o/xml/X4ODriver.java b/x4o-driver/src/main/java/org/x4o/xml/X4ODriver.java index 320c629..f55337a 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/X4ODriver.java +++ b/x4o-driver/src/main/java/org/x4o/xml/X4ODriver.java @@ -50,10 +50,9 @@ public abstract class X4ODriver { 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 { abstract public String[] getLanguageVersions(); - protected X4OLanguage buildLanguage(String version) { return X4ODriverManager.getDefaultBuildLanguage(this, version); } @@ -81,7 +79,6 @@ public abstract class X4ODriver { } - public X4OSchemaWriter createSchemaWriter() { return createSchemaWriter(getLanguageVersionDefault()); } diff --git a/x4o-driver/src/main/java/org/x4o/xml/X4ODriverManager.java b/x4o-driver/src/main/java/org/x4o/xml/X4ODriverManager.java index f43395a..4b60acb 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/X4ODriverManager.java +++ b/x4o-driver/src/main/java/org/x4o/xml/X4ODriverManager.java @@ -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 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 result = new ArrayList(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 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 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); } } diff --git a/x4o-driver/src/main/java/org/x4o/xml/eld/lang/ElementRefectionBindingHandler.java b/x4o-driver/src/main/java/org/x4o/xml/eld/lang/ElementRefectionBindingHandler.java index 6cbaa16..f9b3777 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/eld/lang/ElementRefectionBindingHandler.java +++ b/x4o-driver/src/main/java/org/x4o/xml/eld/lang/ElementRefectionBindingHandler.java @@ -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); } diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OReader.java b/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OReader.java index d87f2cd..2f87542 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OReader.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OReader.java @@ -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 extends AbstractX4OReaderContext implements X4OReader { public AbstractX4OReader(X4OLanguageContext elementLanguage) { @@ -56,11 +55,9 @@ abstract public class AbstractX4OReader extends AbstractX4OReaderContext 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 extends AbstractX4OReaderContext 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 extends AbstractX4OReaderContext 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 extends AbstractX4OReaderContext 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 extends AbstractX4OReaderContext 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 extends AbstractX4OReaderContext 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(); } } diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4ODriver.java b/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4ODriver.java index 7d016c3..efbaba3 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4ODriver.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4ODriver.java @@ -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 extends X4ODriver { public DefaultX4ODriver(String languageName,String languageVersion) { this.languageName=languageName; this.languageVersion=languageVersion; - X4ODriverManager.registerX4ODriver(this); } @Override diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/sax/AttributeMap.java b/x4o-driver/src/main/java/org/x4o/xml/io/sax/AttributeMap.java index ff5e1b4..d9bbdb3 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/sax/AttributeMap.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/sax/AttributeMap.java @@ -365,14 +365,5 @@ public class AttributeMap implements Map { } return false; } - - /** - * @see java.lang.Object#hashCode() - * @return The hashCode. - */ - @Override - public int hashCode() { - return super.hashCode(); - } } } diff --git a/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageLoader.java b/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageLoader.java index a8d6ff4..7839fa5 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageLoader.java +++ b/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageLoader.java @@ -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) { diff --git a/x4o-driver/src/test/java/org/x4o/xml/X4ODriverManagerTest.java b/x4o-driver/src/test/java/org/x4o/xml/X4ODriverManagerTest.java index 981954e..35eddee 100644 --- a/x4o-driver/src/test/java/org/x4o/xml/X4ODriverManagerTest.java +++ b/x4o-driver/src/test/java/org/x4o/xml/X4ODriverManagerTest.java @@ -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; diff --git a/x4o-driver/src/test/java/org/x4o/xml/io/X4OAbstractReaderContextTest.java b/x4o-driver/src/test/java/org/x4o/xml/io/X4OAbstractReaderContextTest.java new file mode 100644 index 0000000..ceadb8e --- /dev/null +++ b/x4o-driver/src/test/java/org/x4o/xml/io/X4OAbstractReaderContextTest.java @@ -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 driver = TestDriver.getInstance(); + X4OReader reader = driver.createReader(); + X4OWriter 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 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 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 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 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 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 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 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 reader = driver.createReaderContext(); + X4OLanguageContext context = reader.readStringContext( + ""+ + ""+ + ""+ + "" + ); + 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 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 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 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")); + } +} diff --git a/x4o-driver/src/test/java/org/x4o/xml/io/X4OAbstractReaderTest.java b/x4o-driver/src/test/java/org/x4o/xml/io/X4OAbstractReaderTest.java new file mode 100644 index 0000000..efa0731 --- /dev/null +++ b/x4o-driver/src/test/java/org/x4o/xml/io/X4OAbstractReaderTest.java @@ -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 driver = TestDriver.getInstance(); + X4OReader reader = driver.createReader(); + X4OWriter 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 driver = TestDriver.getInstance(); + X4OReader reader = driver.createReader(); + TestObjectRoot root = reader.readResource("tests/attributes/test-bean.xml"); + assertNotNull(root); + } + + public void testReadResourceNull() throws Exception { + Exception e = null; + try { + X4ODriver driver = TestDriver.getInstance(); + X4OReader 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 driver = TestDriver.getInstance(); + X4OReader reader = driver.createReader(); + TestObjectRoot root = reader.readString( + ""+ + ""+ + ""+ + "" + ); + 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 driver = TestDriver.getInstance(); + X4OReader 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 driver = TestDriver.getInstance(); + X4OReader 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 driver = TestDriver.getInstance(); + X4OReader 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 driver = TestDriver.getInstance(); + X4OReader 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 driver = TestDriver.getInstance(); + X4OReader 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 driver = TestDriver.getInstance(); + X4OReader 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 driver = TestDriver.getInstance(); + X4OReader 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")); + } +} diff --git a/x4o-driver/src/test/java/org/x4o/xml/test/models/TestBean.java b/x4o-driver/src/test/java/org/x4o/xml/test/models/TestBean.java index 2760f06..9ac516a 100644 --- a/x4o-driver/src/test/java/org/x4o/xml/test/models/TestBean.java +++ b/x4o-driver/src/test/java/org/x4o/xml/test/models/TestBean.java @@ -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; - } + }*/ } diff --git a/x4o-driver/src/test/resources/META-INF/junit-defd/defd-lang.eld b/x4o-driver/src/test/resources/META-INF/junit-defd/defd-lang.eld new file mode 100644 index 0000000..9b2da10 --- /dev/null +++ b/x4o-driver/src/test/resources/META-INF/junit-defd/defd-lang.eld @@ -0,0 +1,152 @@ + + + + + + Binds the TestObjectChild to the TestObjectParent + + + Binds the TestBean to the TestObjectChild + + + Binds the TestObjectParent to the TestObjectRoot + + + Binds the TestBean to the TestObjectRoot + + + Binds j components. + + + Binds panel to frame components as unit check. + + + + Configs the Component based objects. + + + + + + + + + + + + Configs the JComponent based objects. + + Binds the JComponent to the JComponent. + + + + + Test the global element configurator. + + + + Test the global element attribute1 handler. + + + Test the global element attribute2 handler. + + + + + + + The test root element. + + + + + + + + The test the bean object. + + + + + The test element config. + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/x4o-driver/src/test/resources/META-INF/junit-defd/defd-modules.xml b/x4o-driver/src/test/resources/META-INF/junit-defd/defd-modules.xml new file mode 100644 index 0000000..feed91d --- /dev/null +++ b/x4o-driver/src/test/resources/META-INF/junit-defd/defd-modules.xml @@ -0,0 +1,34 @@ + + + + + defd-lang.eld + + diff --git a/x4o-driver/src/test/resources/META-INF/x4o-drivers.xml b/x4o-driver/src/test/resources/META-INF/x4o-drivers.xml index 2168bf1..242a911 100644 --- a/x4o-driver/src/test/resources/META-INF/x4o-drivers.xml +++ b/x4o-driver/src/test/resources/META-INF/x4o-drivers.xml @@ -30,4 +30,5 @@ > + diff --git a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/ELIDAttributeHandler.java b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/ELIDAttributeHandler.java similarity index 98% rename from x4o-meta/src/main/java/org/x4o/xml/meta/lang/ELIDAttributeHandler.java rename to x4o-meta/src/main/java/org/x4o/xml/lang/meta/ELIDAttributeHandler.java index aa58139..1096367 100644 --- a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/ELIDAttributeHandler.java +++ b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/ELIDAttributeHandler.java @@ -21,7 +21,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.x4o.xml.meta.lang; +package org.x4o.xml.lang.meta; import java.util.logging.Logger; diff --git a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/ELReferenceElement.java b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/ELReferenceElement.java similarity index 98% rename from x4o-meta/src/main/java/org/x4o/xml/meta/lang/ELReferenceElement.java rename to x4o-meta/src/main/java/org/x4o/xml/lang/meta/ELReferenceElement.java index 6a2bc19..37876c2 100644 --- a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/ELReferenceElement.java +++ b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/ELReferenceElement.java @@ -21,7 +21,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.x4o.xml.meta.lang; +package org.x4o.xml.lang.meta; import java.util.logging.Logger; diff --git a/x4o-meta/src/main/java/org/x4o/xml/meta/MetaLanguageSiblingLoader.java b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/MetaLanguageSiblingLoader.java similarity index 99% rename from x4o-meta/src/main/java/org/x4o/xml/meta/MetaLanguageSiblingLoader.java rename to x4o-meta/src/main/java/org/x4o/xml/lang/meta/MetaLanguageSiblingLoader.java index 4d72e53..bca64f1 100644 --- a/x4o-meta/src/main/java/org/x4o/xml/meta/MetaLanguageSiblingLoader.java +++ b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/MetaLanguageSiblingLoader.java @@ -21,7 +21,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.x4o.xml.meta; +package org.x4o.xml.lang.meta; import org.x4o.xml.lang.X4OLanguageModule; import org.x4o.xml.lang.X4OLanguageModuleLoaderException; diff --git a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/MethodElement.java b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/MethodElement.java similarity index 98% rename from x4o-meta/src/main/java/org/x4o/xml/meta/lang/MethodElement.java rename to x4o-meta/src/main/java/org/x4o/xml/lang/meta/MethodElement.java index ba99e4c..7a8da21 100644 --- a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/MethodElement.java +++ b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/MethodElement.java @@ -21,7 +21,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.x4o.xml.meta.lang; +package org.x4o.xml.lang.meta; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; diff --git a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/ParentObjectElement.java b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/ParentObjectElement.java similarity index 98% rename from x4o-meta/src/main/java/org/x4o/xml/meta/lang/ParentObjectElement.java rename to x4o-meta/src/main/java/org/x4o/xml/lang/meta/ParentObjectElement.java index 02892f0..d33c348 100644 --- a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/ParentObjectElement.java +++ b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/ParentObjectElement.java @@ -21,7 +21,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.x4o.xml.meta.lang; +package org.x4o.xml.lang.meta; import org.x4o.xml.element.AbstractElement; diff --git a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/PropertyElement.java b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/PropertyElement.java similarity index 99% rename from x4o-meta/src/main/java/org/x4o/xml/meta/lang/PropertyElement.java rename to x4o-meta/src/main/java/org/x4o/xml/lang/meta/PropertyElement.java index 0a18d7c..d9c32bf 100644 --- a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/PropertyElement.java +++ b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/PropertyElement.java @@ -21,7 +21,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.x4o.xml.meta.lang; +package org.x4o.xml.lang.meta; import java.lang.reflect.Method; import java.util.Map; diff --git a/x4o-meta/src/main/java/org/x4o/xml/meta/package-info.java b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/package-info.java similarity index 95% rename from x4o-meta/src/main/java/org/x4o/xml/meta/package-info.java rename to x4o-meta/src/main/java/org/x4o/xml/lang/meta/package-info.java index 0e6ffb1..2d190fd 100644 --- a/x4o-meta/src/main/java/org/x4o/xml/meta/package-info.java +++ b/x4o-meta/src/main/java/org/x4o/xml/lang/meta/package-info.java @@ -22,10 +22,10 @@ */ /** - * The X4O XML Meta Language Loader. + * The X4O XML Meta Language. * * * @since 1.0 */ -package org.x4o.xml.meta; +package org.x4o.xml.lang.meta; diff --git a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/BeanPropertyComparator.java b/x4o-meta/src/main/java/org/x4o/xml/meta/lang/BeanPropertyComparator.java deleted file mode 100644 index ea80e1a..0000000 --- a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/BeanPropertyComparator.java +++ /dev/null @@ -1,175 +0,0 @@ -/* - * 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.meta.lang; - -import java.util.Comparator; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.x4o.xml.element.DefaultElementObjectPropertyValue; - - -/** - * Compares a property of a java bean. - * The property should be Comparable. - * - * @author Willem Cazander - * @version 1.0 Jan 11, 2006 - */ -public class BeanPropertyComparator implements Comparator { - - /** The propery of the bean to compare. */ - private String property = null; - /** The logger to log to. */ - private Logger logger = null; - /** The ascending */ - private boolean ascending = true; - - private DefaultElementObjectPropertyValue helper = null; - - /** - * The constructor inits the logger. - */ - public BeanPropertyComparator() { - logger = Logger.getLogger(BeanPropertyComparator.class.getName()); - helper = new DefaultElementObjectPropertyValue(); - } - - /** - * Creates an BeanPropertyComparator with an property - * @param property The property to compare to. - */ - public BeanPropertyComparator(String property) { - this(); - setProperty(property); - } - - /** - * Creates an BeanPropertyComparator with an property - * @param property - */ - public BeanPropertyComparator(String property,boolean ascending) { - this(); - setProperty(property); - setAscending(ascending); - } - - /** - * Compares 2 objects by there Comparable property. - * - * @param o1 Object 1 - * @param o2 Object 2 - * @return the differce between the objects. - */ - public int compare(Object o1,Object o2) throws ClassCastException { - - Comparable c1 = getComparableProperty(o1); - Comparable c2 = getComparableProperty(o2); - - if(c1==null && c2==null) { - return 0; - } - if(c1==null) { - if(ascending) { - return 1; - } else { - return -1; - } - } - if(c2==null) { - if(ascending) { - return 1; - } else { - return -1; - } - } - - if(ascending) { - return c1.compareTo(c2); - } else { - return c2.compareTo(c1); - } - } - - /** - * Returns the Comparable property of the object. - * @param object The object to get the property field of. - * @return Returns the Comparable casted object of the property field of the object. - * @throws ClassCastException - */ - @SuppressWarnings("unchecked") - private Comparable getComparableProperty(Object object) throws ClassCastException { - - if(property==null) { - throw new IllegalStateException("property is not set."); - } - Object result = null; - try { - result = helper.getProperty(object,property); - } catch (Exception e) { - logger.log(Level.WARNING,"property:"+property+" is not an property of object: "+object.getClass().getName(),e); - } - try { - Comparable c = (Comparable)result; - return c; - } catch (ClassCastException e) { - logger.log(Level.WARNING,"property:"+property+" is not Comparable",e); - throw e; - } - } - - /** - * @return Returns the property. - */ - public String getProperty() { - return property; - } - - /** - * @param property The property to set. - */ - public void setProperty(String property) { - if(property==null) { - throw new NullPointerException("property may not be null"); - } - this.property = property; - logger.finest("property="+property); - } - - /** - * @return Returns the ascending. - */ - public boolean isAscending() { - return ascending; - } - - /** - * @param ascending The ascending to set. - */ - public void setAscending(boolean ascending) { - this.ascending = ascending; - logger.finest("ascending="+ascending); - } -} - diff --git a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/package-info.java b/x4o-meta/src/main/java/org/x4o/xml/meta/lang/package-info.java deleted file mode 100644 index a946101..0000000 --- a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/package-info.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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. - */ - -/** - * The X4O XML Meta Language Classes. - * - * - * @since 1.0 - */ - -package org.x4o.xml.meta.lang; diff --git a/x4o-meta/src/main/resources/META-INF/meta/meta-lang.eld b/x4o-meta/src/main/resources/META-INF/meta/meta-lang.eld index bfadad6..ac3cce3 100644 --- a/x4o-meta/src/main/resources/META-INF/meta/meta-lang.eld +++ b/x4o-meta/src/main/resources/META-INF/meta/meta-lang.eld @@ -34,7 +34,7 @@ > The x4o meta language lets you do special xml tricks. - + Lets you bind object into the expression language context. - + Used to get a reference of an Object into the Element tree. Mostly used in combination with the el.id tag. - + Hack in ElementTree, fills this Element with the ElementObject of his parent Element. - + Set an property value on the object of the parent Element. @@ -75,7 +75,7 @@ - + Executes an Method of an Object. diff --git a/x4o-meta/src/test/java/org/x4o/xml/meta/test/MTestDriver.java b/x4o-meta/src/test/java/org/x4o/xml/lang/meta/MTestDriver.java similarity index 96% rename from x4o-meta/src/test/java/org/x4o/xml/meta/test/MTestDriver.java rename to x4o-meta/src/test/java/org/x4o/xml/lang/meta/MTestDriver.java index 0de8532..0d0a31d 100644 --- a/x4o-meta/src/test/java/org/x4o/xml/meta/test/MTestDriver.java +++ b/x4o-meta/src/test/java/org/x4o/xml/lang/meta/MTestDriver.java @@ -21,14 +21,14 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.x4o.xml.meta.test; +package org.x4o.xml.lang.meta; import org.x4o.xml.X4ODriver; import org.x4o.xml.X4ODriverManager; import org.x4o.xml.io.X4OReaderContext; import org.x4o.xml.io.X4OWriterContext; -public class MTestDriver extends X4ODriver { +public class MTestDriver extends X4ODriver { public static final String LANGUAGE_NAME = "mtest"; public static final String[] LANGUAGE_VERSIONS = new String[]{X4ODriver.DEFAULT_LANGUAGE_VERSION}; diff --git a/x4o-meta/src/test/java/org/x4o/xml/lang/meta/ParentObjectTest.java b/x4o-meta/src/test/java/org/x4o/xml/lang/meta/ParentObjectTest.java new file mode 100644 index 0000000..05cd004 --- /dev/null +++ b/x4o-meta/src/test/java/org/x4o/xml/lang/meta/ParentObjectTest.java @@ -0,0 +1,84 @@ +/* + * 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.lang.meta; + +import javax.swing.JLabel; + +import org.x4o.xml.element.DefaultElement; +import org.x4o.xml.element.Element; +import org.x4o.xml.io.X4OReaderContext; +import org.x4o.xml.lang.X4OLanguageContext; +import org.x4o.xml.lang.X4OLanguagePropertyKeys; + +import junit.framework.TestCase; + +/** + * Tests the parent object meta element. + * + * @author Willem Cazander + * @version 1.0 Apr 15, 2012 + */ +public class ParentObjectTest extends TestCase { + + public void testParentElement() throws Exception { + X4OLanguageContext context = null; + MTestDriver driver = new MTestDriver(); + X4OReaderContext reader = driver.createReaderContext(); + reader.setProperty(X4OLanguagePropertyKeys.PHASE_SKIP_RELEASE, true); + try { + context = reader.readResourceContext("junit/test-meta-parent-element.xml"); + assertEquals(1,context.getRootElement().getChilderen().size()); + Element childElement = context.getRootElement().getChilderen().get(0); + JLabel test = (JLabel)childElement.getElementObject(); + assertEquals("parentTest",test.getText()); + } finally { + reader.releaseContext(context); + } + } + + public void testParentObjectElement() throws Exception { + DefaultElement ep = new DefaultElement(); + ParentObjectElement e = new ParentObjectElement(); + Object o; + + // test non parent + o = e.getElementObject(); + assertNull(o); + e.setElementObject("test"); + o = e.getElementObject(); + assertNull(o); + + // test parent + e.setParent(ep); + o = e.getElementObject(); + assertNull(o); + e.setElementObject("test"); + o = e.getElementObject(); + assertEquals("test",o); + o = ep.getElementObject(); + assertEquals("test",o); + assertEquals(e.getElementObject(),ep.getElementObject()); + } + +} diff --git a/x4o-meta/src/test/java/org/x4o/xml/meta/ReferenceStoreTest.java b/x4o-meta/src/test/java/org/x4o/xml/lang/meta/ReferenceStoreTest.java similarity index 92% rename from x4o-meta/src/test/java/org/x4o/xml/meta/ReferenceStoreTest.java rename to x4o-meta/src/test/java/org/x4o/xml/lang/meta/ReferenceStoreTest.java index 79f31d3..9ae74a1 100644 --- a/x4o-meta/src/test/java/org/x4o/xml/meta/ReferenceStoreTest.java +++ b/x4o-meta/src/test/java/org/x4o/xml/lang/meta/ReferenceStoreTest.java @@ -21,14 +21,13 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.x4o.xml.meta; +package org.x4o.xml.lang.meta; import java.util.Date; import org.x4o.xml.io.X4OReaderContext; import org.x4o.xml.lang.X4OLanguageContext; import org.x4o.xml.lang.X4OLanguagePropertyKeys; -import org.x4o.xml.meta.test.MTestDriver; import junit.framework.TestCase; @@ -46,7 +45,7 @@ public class ReferenceStoreTest extends TestCase { X4OReaderContext reader = driver.createReaderContext(); reader.setProperty(X4OLanguagePropertyKeys.PHASE_SKIP_RELEASE, true); try { - context = reader.readResourceContext("tests/meta-generic.xml"); + context = reader.readResourceContext("junit/test-meta-generic.xml"); assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(0).getElementObject().getClass().getName()); } finally { reader.releaseContext(context); @@ -59,7 +58,7 @@ public class ReferenceStoreTest extends TestCase { X4OReaderContext reader = driver.createReaderContext(); reader.setProperty(X4OLanguagePropertyKeys.PHASE_SKIP_RELEASE, true); try { - context = reader.readResourceContext("tests/reference/store-ref.xml"); + context = reader.readResourceContext("junit/test-meta-reference.xml"); assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(0).getElementObject().getClass().getName()); } finally { reader.releaseContext(context); @@ -72,7 +71,7 @@ public class ReferenceStoreTest extends TestCase { X4OReaderContext reader = driver.createReaderContext(); reader.setProperty(X4OLanguagePropertyKeys.PHASE_SKIP_RELEASE, true); try { - context = reader.readResourceContext("tests/reference/store-ref.xml"); + context = reader.readResourceContext("junit/test-meta-reference.xml"); assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(0).getElementObject().getClass().getName()); assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(1).getElementObject().getClass().getName()); assertEquals(Date.class.getName(),context.getRootElement().getChilderen().get(2).getElementObject().getClass().getName()); diff --git a/x4o-meta/src/test/resources/META-INF/mtest/mtest-modules.xml b/x4o-meta/src/test/resources/META-INF/mtest/mtest-modules.xml index dfdd50b..797c7ac 100644 --- a/x4o-meta/src/test/resources/META-INF/mtest/mtest-modules.xml +++ b/x4o-meta/src/test/resources/META-INF/mtest/mtest-modules.xml @@ -30,6 +30,6 @@ > mtest-lang.eld - org.x4o.xml.meta.MetaLanguageSiblingLoader + org.x4o.xml.lang.meta.MetaLanguageSiblingLoader diff --git a/x4o-meta/src/test/resources/tests/template/bean-template.xml b/x4o-meta/src/test/resources/junit/template/test-meta-template-bean.xml similarity index 100% rename from x4o-meta/src/test/resources/tests/template/bean-template.xml rename to x4o-meta/src/test/resources/junit/template/test-meta-template-bean.xml diff --git a/x4o-meta/src/test/resources/tests/template/test-templating.xml b/x4o-meta/src/test/resources/junit/template/test-meta-template-parent.xml similarity index 100% rename from x4o-meta/src/test/resources/tests/template/test-templating.xml rename to x4o-meta/src/test/resources/junit/template/test-meta-template-parent.xml diff --git a/x4o-meta/src/test/resources/tests/meta-generic.xml b/x4o-meta/src/test/resources/junit/test-meta-generic.xml similarity index 100% rename from x4o-meta/src/test/resources/tests/meta-generic.xml rename to x4o-meta/src/test/resources/junit/test-meta-generic.xml diff --git a/x4o-meta/src/test/resources/junit/test-meta-parent-element.xml b/x4o-meta/src/test/resources/junit/test-meta-parent-element.xml new file mode 100644 index 0000000..44985ad --- /dev/null +++ b/x4o-meta/src/test/resources/junit/test-meta-parent-element.xml @@ -0,0 +1,36 @@ + + + + + + + + + \ No newline at end of file diff --git a/x4o-meta/src/test/resources/tests/reference/store-ref.xml b/x4o-meta/src/test/resources/junit/test-meta-reference.xml similarity index 100% rename from x4o-meta/src/test/resources/tests/reference/store-ref.xml rename to x4o-meta/src/test/resources/junit/test-meta-reference.xml