From 651b2a99e4ebd341c83cc2566d8ea2425e7fe587 Mon Sep 17 00:00:00 2001 From: Willem Cazander Date: Fri, 7 Mar 2014 16:58:33 +0100 Subject: [PATCH] Added LanguageModuleLoader unit tests. --- .../xml/lang/DefaultX4OLanguageLoader.java | 129 ++++++++++++------ .../lang/DefaultX4OLanguageLoaderTest.java | 79 ++++++++--- .../tests/modules/test-modules-err-loader.xml | 35 +++++ .../modules/test-modules-err-sibling.xml | 35 +++++ .../tests/modules/test-modules-full.xml | 51 +++++++ .../tests/modules/test-modules-simple.xml | 34 +++++ 6 files changed, 301 insertions(+), 62 deletions(-) create mode 100644 x4o-driver/src/test/resources/tests/modules/test-modules-err-loader.xml create mode 100644 x4o-driver/src/test/resources/tests/modules/test-modules-err-sibling.xml create mode 100644 x4o-driver/src/test/resources/tests/modules/test-modules-full.xml create mode 100644 x4o-driver/src/test/resources/tests/modules/test-modules-simple.xml 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 828289e..d1d3152 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 @@ -51,14 +51,12 @@ import org.xml.sax.helpers.XMLReaderFactory; public class DefaultX4OLanguageLoader implements X4OLanguageLoader { private Logger logger = null; - protected List modulesAll = null; /** * Creates the DefaultX4OLanguageLoader. */ public DefaultX4OLanguageLoader() { logger = Logger.getLogger(DefaultX4OLanguageLoader.class.getName()); - modulesAll = new ArrayList(20); } /** @@ -84,31 +82,12 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) { */ public void loadLanguage(X4OLanguageLocal languageLocal, String language,String languageVersion) throws X4OLanguageLoaderException { logger.finer("Loading all modules for language: "+language); - try { - loadLanguageModules(languageLocal,language); - } catch (IOException e) { - throw new X4OLanguageLoaderException(e); - } catch (SAXException e) { - throw new X4OLanguageLoaderException(e); - } + List modulesAll = loadLanguageModules(languageLocal,language); + modulesAll = filterVersionModules(modulesAll,languageLocal,languageVersion); + validateModules(modulesAll); - X4OLanguageVersionFilter lvf; - try { - lvf = (X4OLanguageVersionFilter)X4OLanguageClassLoader.newInstance(languageLocal.getLanguageConfiguration().getDefaultLanguageVersionFilter()); - } catch (InstantiationException e) { - throw new X4OLanguageLoaderException(e); - } catch (IllegalAccessException e) { - throw new X4OLanguageLoaderException(e); - } int loaded = 0; for (VersionedResources versionedResources:modulesAll) { - List versions = new ArrayList(); - versions.add(versionedResources.version); // FIXME - String modulesVersion = lvf.filterVersion(languageVersion, versions); - if (modulesVersion==null) { - continue; - } - X4OLanguageModuleLoader loader; for (String value:versionedResources.eldResources) { String languagePrefix = languageLocal.getLanguageConfiguration().getLanguageResourcePathPrefix(); @@ -151,7 +130,51 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) { throw new X4OLanguageLoaderException("No modules defined for version: "+languageVersion); } } - + + private List filterVersionModules(List resources,X4OLanguageLocal languageLocal,String languageVersion) throws X4OLanguageLoaderException { + List result = new ArrayList(resources.size()); + X4OLanguageVersionFilter lvf; + try { + lvf = (X4OLanguageVersionFilter)X4OLanguageClassLoader.newInstance(languageLocal.getLanguageConfiguration().getDefaultLanguageVersionFilter()); + } catch (InstantiationException e) { + throw new X4OLanguageLoaderException(e); + } catch (IllegalAccessException e) { + throw new X4OLanguageLoaderException(e); + } + for (VersionedResources versionedResources:resources) { + List versions = new ArrayList(); + versions.add(versionedResources.version); // FIXME + String modulesVersion = lvf.filterVersion(languageVersion, versions); + if (modulesVersion==null) { + continue; + } + result.add(versionedResources); + } + return result; + } + + protected void validateModules(List resources) throws X4OLanguageLoaderException { + List eldResources = new ArrayList(5); + List moduleLoaders = new ArrayList(5); + List elbResources = new ArrayList(5); + List siblingLoaders = new ArrayList(5); + for (VersionedResources vr:resources) { + validateModuleList(eldResources,vr.eldResources,"eld-resource"); + validateModuleList(moduleLoaders,vr.moduleLoaders,"module-loader"); + validateModuleList(elbResources,vr.elbResources,"elb-resource"); + validateModuleList(siblingLoaders,vr.siblingLoaders,"sibling-loader"); + } + } + + private void validateModuleList(List data,List values,String xmlTag) throws X4OLanguageLoaderException { + for (String value:values) { + if (data.contains(value)) { + throw new X4OLanguageLoaderException("Duplicate "+xmlTag+" entry detected; "+value); + } + data.add(value); + } + } + private void loadModule(X4OLanguageLocal languageLocal,X4OLanguageModuleLoader loader,String resource,VersionedResources versionedResources) throws X4OLanguageLoaderException { X4OLanguageModuleLocal module; try { @@ -187,7 +210,8 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) { * @param languageLocal The ElementLanguage to load for. * @param language The language to load. */ - protected void loadLanguageModules(X4OLanguageLocal languageLocal,String language) throws IOException, SAXException { + protected List loadLanguageModules(X4OLanguageLocal languageLocal,String language) throws X4OLanguageLoaderException { + List result = new ArrayList(15); StringBuilder buf = new StringBuilder(150); buf.append(languageLocal.getLanguageConfiguration().getLanguageResourcePathPrefix()); buf.append('/'); @@ -197,18 +221,25 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) { buf.append(languageLocal.getLanguageConfiguration().getLanguageResourceModulesFileName()); logger.finer("loading X4O language: "+language); - Enumeration e = Thread.currentThread().getContextClassLoader().getResources(buf.toString()); - while(e.hasMoreElements()) { - URL u = e.nextElement(); - logMessage(languageLocal,"Loading relative modules: "+u+" for: "+language); - loadModulesXml(u.openStream(),u.toString()); - } - - e = Thread.currentThread().getContextClassLoader().getResources("/"+buf.toString()); - while(e.hasMoreElements()) { - URL u = e.nextElement(); - logMessage(languageLocal,"Loading root modules: "+u+" for: "+language); - loadModulesXml(u.openStream(),u.toString()); + try { + Enumeration e = Thread.currentThread().getContextClassLoader().getResources(buf.toString()); + while(e.hasMoreElements()) { + URL u = e.nextElement(); + logMessage(languageLocal,"Loading relative modules: "+u+" for: "+language); + result.addAll(loadLanguageModulesXml(u.openStream(),u.toString())); + } + + e = Thread.currentThread().getContextClassLoader().getResources("/"+buf.toString()); + while(e.hasMoreElements()) { + URL u = e.nextElement(); + logMessage(languageLocal,"Loading root modules: "+u+" for: "+language); + result.addAll(loadLanguageModulesXml(u.openStream(),u.toString())); + } + return result; + } catch (IOException e) { + throw new X4OLanguageLoaderException(e); + } catch (SAXException e) { + throw new X4OLanguageLoaderException(e); } } @@ -218,7 +249,7 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) { * @throws IOException * @throws SAXException */ - protected void loadModulesXml(InputStream in,String loadedFrom) throws IOException, SAXException { + protected List loadLanguageModulesXml(InputStream in,String loadedFrom) throws IOException, SAXException { if (in==null) { throw new NullPointerException("Can't parse null input stream"); } @@ -229,24 +260,32 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) { saxParser.setProperty("http://xml.org/sax/properties/declaration-handler",xth); try { saxParser.parse(new InputSource(in)); + return xth.getResult(); } finally { in.close(); } } - + private class ModulesTagHandler extends DefaultHandler2 { private StringBuffer buf = new StringBuffer(); private String loadedFrom = null; private VersionedResources versionedResources = null; + private List result = null; public ModulesTagHandler(String loadedFrom) { this.loadedFrom=loadedFrom; + this.result = new ArrayList(5); } - + + public List getResult() + { + return result; + } + @Override public void startDocument() throws SAXException { } - + @Override public void startElement(String namespaceUri, String tag, String qName,Attributes attr) throws SAXException { if ("language".equals(tag)) { @@ -257,7 +296,7 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) { logger.finest("Version attribute: "+version); } } - + @Override public void endElement(String namespaceUri, String tag,String qName) throws SAXException { @@ -270,7 +309,7 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) { return; } if ("language".equals(tag)) { - modulesAll.add(versionedResources); + result.add(versionedResources); versionedResources = null; return; } @@ -300,7 +339,7 @@ TODO: if (language.getLanguageConfiguration().hasX4ODebugWriter()) { } } - private class VersionedResources { + protected class VersionedResources { public String version; public String loadedFrom; public List eldResources = new ArrayList(5); diff --git a/x4o-driver/src/test/java/org/x4o/xml/lang/DefaultX4OLanguageLoaderTest.java b/x4o-driver/src/test/java/org/x4o/xml/lang/DefaultX4OLanguageLoaderTest.java index ae3dae4..9c3fc4b 100644 --- a/x4o-driver/src/test/java/org/x4o/xml/lang/DefaultX4OLanguageLoaderTest.java +++ b/x4o-driver/src/test/java/org/x4o/xml/lang/DefaultX4OLanguageLoaderTest.java @@ -22,10 +22,13 @@ */ package org.x4o.xml.lang; +import java.io.InputStream; +import java.util.List; + import org.x4o.xml.X4ODriver; import org.x4o.xml.lang.X4OLanguage; -import org.x4o.xml.lang.X4OLanguageLoader; import org.x4o.xml.lang.X4OLanguageLocal; +import org.x4o.xml.lang.DefaultX4OLanguageLoader.VersionedResources; import org.x4o.xml.lang.phase.DefaultX4OPhaseManager; import org.x4o.xml.lang.phase.X4OPhaseLanguageInit; import org.x4o.xml.lang.phase.X4OPhaseLanguageRead; @@ -80,27 +83,69 @@ public class DefaultX4OLanguageLoaderTest extends TestCase { loader.loadLanguage((X4OLanguageLocal)result, "test", "1.0"); } - public void testLanguag() throws Exception { -// loader.loadModulesXml(in); + public void testModulesSimple() throws Exception { + InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("tests/modules/test-modules-simple.xml"); + assertNotNull(in); + List result = loader.loadLanguageModulesXml(in, "test-modules-simple.xml"); + assertNotNull(result); + assertFalse(result.isEmpty()); + assertTrue("Simple test returned non-one result: "+result.size(),result.size()==1); } - /* - public void testLanguageURINameSpaceTest() throws Exception { - Map languageMap = loader.loadLanguageModules(parser.getElementLanguage(), "test"); - assertTrue("No uri for test.eld", languageMap.containsKey("eld.http://test.x4o.org/eld/test.eld")); - assertEquals("test.eld", languageMap.get("eld.http://test.x4o.org/eld/test.eld")); + public void testModulesFull() throws Exception { + InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("tests/modules/test-modules-full.xml"); + assertNotNull(in); + List result = loader.loadLanguageModulesXml(in, "test-modules-full.xml"); + assertNotNull(result); + assertFalse(result.isEmpty()); + VersionedResources vr = result.get(0); + assertTrue(vr.eldResources.size()>1); + assertTrue(vr.moduleLoaders.size()>1); + assertTrue(vr.elbResources.size()>1); + assertTrue(vr.siblingLoaders.size()==1); } - public void testLanguageURINameSpaceX4O() throws Exception { - Map languageMap = loader.loadLanguageModules(parser.getElementLanguage(), "x4o"); - assertTrue("No uri for x4o-lang.eld", languageMap.containsKey("eld.http://eld.x4o.org/eld/x4o-lang.eld")); - assertEquals("x4o-lang.eld", languageMap.get("eld.http://eld.x4o.org/eld/x4o-lang.eld")); + public void testModulesDuplicateLoaderNoError() throws Exception { + InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("tests/modules/test-modules-err-loader.xml"); + assertNotNull(in); + List result = loader.loadLanguageModulesXml(in, "test-modules-err-loader.xml"); + assertNotNull(result); + assertFalse(result.isEmpty()); } - public void testLanguageURINameSpaceELD() throws Exception { - Map languageMap = loader.loadLanguageModules(parser.getElementLanguage(), "eld"); - assertTrue("No uri for eld-lang.eld", languageMap.containsKey("eld.http://eld.x4o.org/eld/eld-lang.eld")); - assertEquals("eld-lang.eld", languageMap.get("eld.http://eld.x4o.org/eld/eld-lang.eld")); + public void testModulesDuplicateLoader() throws Exception { + InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("tests/modules/test-modules-err-loader.xml"); + assertNotNull(in); + List result = loader.loadLanguageModulesXml(in, "test-modules-err-loader.xml"); + assertNotNull(result); + assertFalse(result.isEmpty()); + + Exception e=null; + try { + loader.validateModules(result); + } catch (Exception ee) { + e=ee; + } + assertNotNull(e); + assertTrue("No 'Duplicate' found in message: "+e.getMessage(),e.getMessage().contains("Duplicate")); + assertTrue("No 'module-loader' found in message: "+e.getMessage(),e.getMessage().contains("module-loader")); + } + + public void testModulesDuplicateSiblingLoader() throws Exception { + InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("tests/modules/test-modules-err-sibling.xml"); + assertNotNull(in); + List result = loader.loadLanguageModulesXml(in, "test-modules-err-sibling.xml"); + assertNotNull(result); + assertFalse(result.isEmpty()); + + Exception e=null; + try { + loader.validateModules(result); + } catch (Exception ee) { + e=ee; + } + assertNotNull(e); + assertTrue("No 'Duplicate' found in message: "+e.getMessage(),e.getMessage().contains("Duplicate")); + assertTrue("No 'sibling-loader' found in message: "+e.getMessage(),e.getMessage().contains("sibling-loader")); } - */ } diff --git a/x4o-driver/src/test/resources/tests/modules/test-modules-err-loader.xml b/x4o-driver/src/test/resources/tests/modules/test-modules-err-loader.xml new file mode 100644 index 0000000..8c8e6fd --- /dev/null +++ b/x4o-driver/src/test/resources/tests/modules/test-modules-err-loader.xml @@ -0,0 +1,35 @@ + + + + + org.foo.bar.test.loader.EqualLoader + org.foo.bar.test.loader.EqualLoader + + diff --git a/x4o-driver/src/test/resources/tests/modules/test-modules-err-sibling.xml b/x4o-driver/src/test/resources/tests/modules/test-modules-err-sibling.xml new file mode 100644 index 0000000..320b0f9 --- /dev/null +++ b/x4o-driver/src/test/resources/tests/modules/test-modules-err-sibling.xml @@ -0,0 +1,35 @@ + + + + + org.foo.bar.test.loader.EqualSiblingLoader + org.foo.bar.test.loader.EqualSiblingLoader + + diff --git a/x4o-driver/src/test/resources/tests/modules/test-modules-full.xml b/x4o-driver/src/test/resources/tests/modules/test-modules-full.xml new file mode 100644 index 0000000..4adbe23 --- /dev/null +++ b/x4o-driver/src/test/resources/tests/modules/test-modules-full.xml @@ -0,0 +1,51 @@ + + + + + my-lib0.eld + my-lib1.eld + my-lib2.eld + my-lib3.eld + my-lib4.eld + my-lib5.eld + my-lib6.eld + my-lib7.eld + + my-bean0.elb + my-bean1.elb + my-bean2.elb + + org.foo.bar.test.loader.FirstLoader + org.foo.bar.test.loader.SecondLoader + org.foo.bar.test.loader.LastLoader + + org.foo.bar.test.loader.OtherLanguageSiblingLoader + + diff --git a/x4o-driver/src/test/resources/tests/modules/test-modules-simple.xml b/x4o-driver/src/test/resources/tests/modules/test-modules-simple.xml new file mode 100644 index 0000000..c23c914 --- /dev/null +++ b/x4o-driver/src/test/resources/tests/modules/test-modules-simple.xml @@ -0,0 +1,34 @@ + + + + + my-language.eld + +