private void populateDateEnums(List<String> dataEnums) { for ( String enumFile : dataEnums ) { DataEnumLoader enumLoader = new DataEnumLoader( enumFile ); if ( enumLoader.hasErrors() ) { errors.addAll( enumLoader.getErrors() ); } else { builder.addAllDataEnumsList( enumLoader.getData() ); } } }
/** * This is the source of the asset, which is an MVEL map (minus the outer "[") of course. */ public DataEnumLoader(String mvelSource) { errors = new ArrayList<String>(); this.data = loadEnum(mvelSource); }
@Test public void testNoOp() { DataEnumLoader loader = new DataEnumLoader(" "); assertFalse(loader.hasErrors()); assertEquals(0, loader.getData().size()); loader = new DataEnumLoader(""); assertFalse(loader.hasErrors()); assertEquals(0, loader.getData().size()); }
mvelSource = mvelSource.substring(1); } else { mvelSource = "[ " + addCommasForNewLines(mvelSource) + " ]"; mvelData = MVEL.eval(mvelSource, new HashMap<String, Object>()); } catch (RuntimeException e) { addError("Unable to load enumeration data."); addError(e.getMessage()); addError("Error type: " + e.getClass().getName()); return Collections.emptyMap(); addError("The expression is not a map, it is a " + mvelData.getClass().getName()); return Collections.emptyMap(); if (!(list instanceof List<?> || list instanceof String)) { if (list == null) { addError("The item with " + key + " is null."); } else { addError("The item with " + key + " is not a list or a string, it is a " + list.getClass().getName());
@Test public void testNewLines() { String s = "yeah yeah, \nyeah \nyeah"; assertEquals("yeah yeah,\nyeah,\nyeah", DataEnumLoader.addCommasForNewLines( s )); }
@Test public void testLiteralHelperUtilityClass() { //this shows how you can load it up with a class (which should return a map of keys to List. DataEnumLoader loader = new DataEnumLoader("=(new org.drools.ide.common.modeldriven.SampleDataSource2()).loadData()"); assertFalse(loader.hasErrors()); assertEquals(1, loader.getData().size()); String[] res = (String[]) loader.getData().get("whee"); assertEquals(2, res.length); assertEquals("hey", res[0]); assertEquals("ho", res[1]); }
mvelSource = mvelSource.substring(1); } else { mvelSource = "[ " + addCommasForNewLines(mvelSource) + " ]"; mvelData = MVEL.eval(mvelSource, new HashMap<String, Object>()); } catch (RuntimeException e) { addError("Unable to load enumeration data."); addError(e.getMessage()); addError("Error type: " + e.getClass().getName()); return Collections.emptyMap(); addError("The expression is not a map, it is a " + mvelData.getClass().getName()); return Collections.emptyMap(); if (!(list instanceof List<?> || list instanceof String)) { if (list == null) { addError("The item with " + key + " is null."); } else { addError("The item with " + key + " is not a list or a string, it is a " + list.getClass().getName());
private void populateDateEnums(List<String> dataEnums) { for ( String enumFile : dataEnums ) { DataEnumLoader enumLoader = new DataEnumLoader( enumFile ); if ( enumLoader.hasErrors() ) { errors.addAll( enumLoader.getErrors() ); } else { builder.addAllDataEnumsList( enumLoader.getData() ); } } }
@Test public void testLazyString() { //in this case we are dealing with an expression which will not be resolved at load time. DataEnumLoader loader = new DataEnumLoader("'Person.type[sex]' : 'something @{sex}'"); assertFalse(loader.hasErrors()); Map data = loader.getData(); String[] sl = (String[]) data.get("Person.type[sex]"); String s = sl[0]; assertEquals("something @{sex}", s); Map context = new HashMap() {{ put("sex", "cool"); }}; Object r = TemplateRuntime.eval(s, context); assertEquals("something cool", r); loader = new DataEnumLoader("'Person.type[sex, money]' : '@{sex} @{money}'"); assertFalse(loader.hasErrors()); sl = (String[]) loader.getData().get("Person.type[sex, money]"); s = sl[0]; assertEquals("@{sex} @{money}", s); }
/** * This is the source of the asset, which is an MVEL map (minus the outer "[") of course. */ public DataEnumLoader(String mvelSource) { errors = new ArrayList<String>(); this.data = loadEnum(mvelSource); }
@Test @Ignore public void testEnumGeneration() throws Exception { Object result = MVEL.eval("[2, 3, 4, ]", new HashMap()); assertTrue(result instanceof List); List l = (List) result; assertEquals(3, l.size()); result = MVEL.eval("['Person.age' : [2, 3]\n, 'Person.name' : ['qqq', \n'ccc']]", new HashMap()); DataEnumLoader loader = new DataEnumLoader(readLines().toString()); assertFalse(loader.getErrors().toString(), loader.hasErrors()); Map enumeration = (Map) loader.getData(); assertEquals(loader.getErrors().toString(), 0, loader.getErrors().size()); assertEquals(3, enumeration.size()); String[] list = (String[]) enumeration.get("Person.age"); assertEquals(4, list.length); assertEquals("1", list[0]); assertEquals("2", list[1]); list = (String[]) enumeration.get("Person.rating"); assertEquals(2, list.length); assertEquals("High", list[0]); assertEquals("Low", list[1]); loader = new DataEnumLoader("goober poo error"); assertEquals(0, loader.getData().size()); assertFalse(loader.getErrors().size() == 0); assertTrue(loader.hasErrors()); }