01: package org.drools.brms.modeldriven;
02:
03: import java.io.BufferedReader;
04: import java.io.IOException;
05: import java.io.InputStreamReader;
06: import java.util.HashMap;
07: import java.util.List;
08: import java.util.Map;
09: import java.util.StringTokenizer;
10:
11: import junit.framework.TestCase;
12:
13: import org.drools.brms.server.util.DataEnumLoader;
14: import org.mvel.MVEL;
15:
16: public class DataEnumLoaderTest extends TestCase {
17:
18: public void testEnumGeneration() throws Exception {
19:
20: Object result = MVEL.eval("[2, 3, 4, ]", new HashMap());
21: assertTrue(result instanceof List);
22: List l = (List) result;
23: assertEquals(3, l.size());
24:
25: result = MVEL
26: .eval(
27: "['Person.age' : [2, 3]\n 'Person.name' : ['qqq', \n'ccc']]",
28: new HashMap());
29:
30: DataEnumLoader loader = new DataEnumLoader(readLines()
31: .toString());
32:
33: assertFalse(loader.getErrors().toString(), loader.hasErrors());
34:
35: Map enumeration = (Map) loader.getData();
36: assertEquals(loader.getErrors().toString(), 0, loader
37: .getErrors().size());
38: assertEquals(3, enumeration.size());
39:
40: String[] list = (String[]) enumeration.get("Person.age");
41: assertEquals(4, list.length);
42: assertEquals("1", list[0]);
43: assertEquals("2", list[1]);
44:
45: list = (String[]) enumeration.get("Person.rating");
46: assertEquals(2, list.length);
47: assertEquals("High", list[0]);
48: assertEquals("Low", list[1]);
49:
50: loader = new DataEnumLoader("goober poo error");
51: assertEquals(0, loader.getData().size());
52: assertFalse(loader.getErrors().size() == 0);
53: assertTrue(loader.hasErrors());
54:
55: }
56:
57: public void testNoOp() {
58: DataEnumLoader loader = new DataEnumLoader(" ");
59: assertFalse(loader.hasErrors());
60: assertEquals(0, loader.getData().size());
61:
62: loader = new DataEnumLoader("");
63: assertFalse(loader.hasErrors());
64: assertEquals(0, loader.getData().size());
65:
66: }
67:
68: public void testNewLines() {
69: String s = "yeah yeah, \nyeah \nyeah";
70: assertEquals("yeah yeah,\nyeah,\nyeah", DataEnumLoader
71: .addCommasForNewLines(s));
72: }
73:
74: private StringBuffer readLines() throws IOException {
75: BufferedReader r = new BufferedReader(
76: new InputStreamReader(this .getClass()
77: .getResourceAsStream("Some.enumeration")));
78: String line = "";
79: StringBuffer buf = new StringBuffer();
80: while ((line = r.readLine()) != null) {
81: buf.append(line);
82: buf.append('\n');
83: }
84: return buf;
85: }
86:
87: }
|