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

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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<T> implements Comparator<T> {
/** 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<Object> c1 = getComparableProperty(o1);
Comparable<Object> 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<Object> 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<Object> c = (Comparable<Object>)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);
}
}

View file

@ -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;

View file

@ -34,7 +34,7 @@
>
<eld:description>The x4o meta language lets you do special xml tricks.</eld:description>
<eld:attributeHandler id="global-el-id" attributeName="el.id" bean.class="org.x4o.xml.meta.lang.ELIDAttributeHandler">
<eld:attributeHandler id="global-el-id" attributeName="el.id" bean.class="org.x4o.xml.lang.meta.ELIDAttributeHandler">
<eld:description>Lets you bind object into the expression language context.</eld:description>
</eld:attributeHandler>
<eld:namespace
@ -54,20 +54,20 @@
</eld:description>
</eld:element>
<eld:element tag="elReference" elementClass="org.x4o.xml.meta.lang.ELReferenceElement">
<eld:element tag="elReference" elementClass="org.x4o.xml.lang.meta.ELReferenceElement">
<eld:description>
Used to get a reference of an Object into the Element tree.
Mostly used in combination with the el.id tag.
</eld:description>
</eld:element>
<eld:element tag="parentObject" elementClass="org.x4o.xml.meta.lang.ParentObjectElement">
<eld:element tag="parentObject" elementClass="org.x4o.xml.lang.meta.ParentObjectElement">
<eld:description>
Hack in ElementTree, fills this Element with the ElementObject of his parent Element.
</eld:description>
</eld:element>
<eld:element tag="property" elementClass="org.x4o.xml.meta.lang.PropertyElement">
<eld:element tag="property" elementClass="org.x4o.xml.lang.meta.PropertyElement">
<eld:description>
Set an property value on the object of the parent Element.
@ -75,7 +75,7 @@
</eld:description>
</eld:element>
<eld:element tag="method" elementClass="org.x4o.xml.meta.lang.MethodElement">
<eld:element tag="method" elementClass="org.x4o.xml.lang.meta.MethodElement">
<eld:description>
Executes an Method of an Object.
</eld:description>

View file

@ -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<Object> {
public static final String LANGUAGE_NAME = "mtest";
public static final String[] LANGUAGE_VERSIONS = new String[]{X4ODriver.DEFAULT_LANGUAGE_VERSION};

View file

@ -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());
}
}

View file

@ -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());

View file

@ -30,6 +30,6 @@
>
<language version="1.0">
<eld-resource>mtest-lang.eld</eld-resource>
<sibling-loader>org.x4o.xml.meta.MetaLanguageSiblingLoader</sibling-loader>
<sibling-loader>org.x4o.xml.lang.meta.MetaLanguageSiblingLoader</sibling-loader>
</language>
</modules>

View file

@ -0,0 +1,36 @@
<?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.
-->
<tree:root
xmlns:tree="http://mtest.x4o.org/xml/ns/mtest-root"
xmlns:mt="http://mtest.x4o.org/xml/ns/mtest-lang"
xmlns:x4o="http://meta.x4o.org/xml/ns/meta-lang"
>
<mt:JLabel>
<x4o:parentObject>
<x4o:property name="text" value="parentTest"/>
</x4o:parentObject>
</mt:JLabel>
</tree:root>