1 /*------------------------------------------------------------------------------
  2 Name:      TestRecordParsing.java
  3 Project:   org.xmlBlasterProject:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 package org.xmlBlaster.test.contrib.dbwriter;
  7 
  8 import org.custommonkey.xmlunit.XMLTestCase;
  9 import org.custommonkey.xmlunit.XMLUnit;
 10 import org.xmlBlaster.contrib.I_Info;
 11 import org.xmlBlaster.contrib.dbwatcher.Info;
 12 import org.xmlBlaster.contrib.dbwriter.SqlInfoParser;
 13 import org.xmlBlaster.contrib.dbwriter.info.SqlColumn;
 14 import org.xmlBlaster.contrib.dbwriter.info.SqlDescription;
 15 import org.xmlBlaster.contrib.dbwriter.info.SqlInfo;
 16 import org.xmlBlaster.contrib.dbwriter.info.SqlRow;
 17 
 18 import java.io.ByteArrayInputStream;
 19 import java.io.FileInputStream;
 20 import java.io.InputStream;
 21 import java.util.logging.Logger;
 22 import java.util.prefs.Preferences;
 23 import java.util.List;
 24 
 25 /**
 26  * Test basic functionality. 
 27  * <p> 
 28  * To run most of the tests you need to have a databse (for example Oracle)
 29  * and XmlBlaster up and running.
 30  * </p>
 31  * <p>
 32  * The connection configuration (url, password etc.) is configured
 33  * as JVM property or in {@link #createTest(I_Info, Map)} and
 34  * {@link #setUpDbPool(I_Info)}
 35  * </p> 
 36  *
 37  * @see DbWatcher
 38  * @author Marcel Ruff
 39  */
 40 public class TestRecordParsing extends XMLTestCase {
 41     private static Logger log = Logger.getLogger(TestRecordParsing.class.getName());
 42     private I_Info info;
 43     
 44     /**
 45      * Start the test. 
 46      * <pre>
 47      * java -Ddb.password=secret junit.swingui.TestRunner -noloading org.xmlBlaster.test.contrib.dbwriter.TestRecordParsing
 48      * </pre>
 49      * @param args Command line settings
 50      */
 51     public static void main(String[] args) {
 52         // junit.swingui.TestRunner.run(TestRecordParsing.class);
 53 
 54        TestRecordParsing test = new TestRecordParsing();
 55        try {
 56           test.setUp();
 57           test.testUnprotectedClientProperties();
 58           test.tearDown();
 59 
 60           test.setUp();
 61           test.testParsing();
 62           test.tearDown();
 63 
 64           test.setUp();
 65           test.testParsingStream();
 66           test.tearDown();
 67        }
 68        catch (Exception ex) {
 69           ex.printStackTrace();
 70        }
 71     }
 72 
 73     /**
 74      * Default ctor. 
 75      */
 76     public TestRecordParsing() {
 77        super();
 78        Preferences prefs = Preferences.userRoot();
 79        this.info = new Info(prefs);
 80        XMLUnit.setIgnoreWhitespace(true);
 81     }
 82 
 83    /**
 84     * Constructor for TestRecordParsing.
 85     * @param arg0
 86     */
 87     public TestRecordParsing(String arg0) {
 88        super(arg0);
 89        XMLUnit.setIgnoreWhitespace(true);
 90     }
 91 
 92     /**
 93      * Configure database access. 
 94      * @see TestCase#setUp()
 95      */
 96    protected void setUp() throws Exception {
 97       super.setUp();
 98    }
 99    
100    /*
101     * @see TestCase#tearDown()
102     */
103    protected void tearDown() throws Exception {
104       super.tearDown();
105    }
106 
107    /**
108     * If the table does not exist we expect a null ResultSet
109     * @throws Exception Any type is possible
110     */
111    public final void testParsing() throws Exception {
112       log.info("Start testParsing()");
113       /** Comments are not allowed otherwise the xml are not considered the same */
114       String xml = "" + 
115       "<?xml version='1.0' encoding='UTF-8' ?>\n" +
116       "<sql>\n" +
117       " <desc>\n" +
118       "  <command>INSERT</command>\n" +
119       "  <ident>EDDI</ident>\n" +
120       "  <colname type='DATE' nullable='0'>DATUM</colname>\n" +
121       "  <colname type='NUMBER' precision='11' signed='false' nullable='1'>CPU</colname>\n" +
122       "  <colname type='VARCHAR2' precision='20' nullable='0' readOnly='true'>COL1</colname>\n" +
123       "  <colname table='OMZ' nullable='0' schema='AA' catalog='CAT' type='VARCHAR2'\n" +
124       "              precision='10' pk='true' fkCatalog='dummy' fkSchema='dummy1' fkTable='fkTab'" +
125       " fkCol='colName' fkSeq='1' fkUpdRule='none' fkDelRule='some' fkDef='somedef'>ICAO_ID</colname>\n" +
126       "  <attr name='TEST3'>SOMEATTR3</attr>\n" +
127       "  <attr name='TEST1'>SOMEATTR1</attr>\n" +
128       " </desc>\n" +
129       " <row num='0'>\n" +
130       "  <col name='DATUM'>2005-01-05 15:52:06.0</col>\n" +
131       "  <col name='CPU'>238333</col>\n" +
132       "  <col name='COL1'>Bla</col>\n" +
133       "  <col name='ICAO_ID'>EDDI</col>\n" +
134       "  <attr name='LR'>SRANIL</attr>\n" +
135       "  <attr name='SUBNET_ID'>TCP</attr>\n" +
136       " </row>\n" +
137       " <row num='1'>\n" +
138       "  <col name='DATUM'>2005-01-05 15:52:07.0</col>\n" +
139       "  <col name='CPU'>238340</col>\n" +
140       "  <col name='COL1' encoding='base64'>QmxdXT5CbA==</col>\n" +
141       "  <col name='ICAO_ID'>EDDI</col>\n" +
142       " </row>\n" +
143       " <row num='2'>\n" +
144       "  <col name='DATUM'>2005-01-05 15:52:08.0</col>\n" +
145       "  <col name='CPU'>238343</col>\n" +
146       "  <col name='COL1'>BOO</col>\n" +
147       "  <col name='ICAO_ID'>EDDI</col>\n" +
148       "  <attr name='SUBNET_ID'>X25</attr>\n" +
149       " </row>\n" +
150       "</sql>\n";
151 
152       
153       SqlInfoParser parser = new SqlInfoParser();
154       parser.init(this.info);
155       SqlInfo record = parser.parse(xml);
156 
157       SqlDescription description = record.getDescription();
158       assertNotNull("the description shall not be null", description);
159       assertNotNull("the identity shall not be null", description.getIdentity());
160       assertNotNull("the command shall not be null", description.getCommand());
161 
162       assertEquals("the identity content is wrong", "EDDI", description.getIdentity());
163       assertEquals("the command content is wrong", "INSERT", description.getCommand());
164 
165       // test the column descriptions 
166       SqlColumn[] colDescriptions = description.getColumns();
167       assertEquals("the number of column descriptions is wrong", 4, colDescriptions.length);
168       String[] names = new String[] { "DATUM", "CPU", "COL1", "ICAO_ID"};
169       for (int i=0; i < colDescriptions.length; i++) {
170          log.info("test column description #" + i + " names: '" + names[i] + "' and '" + colDescriptions[i].getColName() + "'");
171          assertEquals("the name of the column description #" + i + " is wrong", names[i], colDescriptions[i].getColName());
172       }
173       
174       List rows = record.getRows();
175       assertEquals("the number of rows is wrong", 3, rows.size());
176       int[] attr = new int[] { 2, 0, 1 };
177       for (int i=0; i < 3; i++) {
178          SqlRow row = (SqlRow)rows.get(i);
179          assertEquals("wrong number of columns for row '" + i+ "'", 4, row.getColumnNames().length);
180          assertEquals("wrong number of attributes for row '" + i+ "'", attr[i], row.getAttributeNames().length);
181       }
182       
183       System.out.println("\n\nshould be:\n" + xml);
184       System.out.println("\nis:\n" + record.toXml(""));
185       assertXMLEqual("output xml is not the same as input xml", xml, record.toXml(""));
186       //assertXpathNotExists("/myRootTag/row[@num='0']", xml);
187       //assertXpathEvaluatesTo("CREATE", "/myRootTag/desc/command/text()", xml);
188       log.info("SUCCESS");
189    }
190    
191    /**
192     * If the table does not exist we expect a null ResultSet
193     * @throws Exception Any type is possible
194     */
195    public final void testParsingStream() throws Exception {
196       log.info("Start testParsingStream()");
197       /** Comments are not allowed otherwise the xml are not considered the same */
198       String xml = "" + 
199       "<?xml version='1.0' encoding='UTF-8' ?>\n" +
200       "<sql>\n" +
201       " <desc>\n" +
202       "  <command>INSERT</command>\n" +
203       "  <ident>EDDI</ident>\n" +
204       "  <colname type='DATE' nullable='0'>DATUM</colname>\n" +
205       "  <colname type='NUMBER' precision='11' signed='false' nullable='1'>CPU</colname>\n" +
206       "  <colname type='VARCHAR2' precision='20' nullable='0' readOnly='true'>COL1</colname>\n" +
207       "  <colname table='OMZ' nullable='0' schema='AA' catalog='CAT' type='VARCHAR2'\n" +
208       "              precision='10' pk='true' fkCatalog='dummy' fkSchema='dummy1' fkTable='fkTab'" +
209       " fkCol='colName' fkSeq='1' fkUpdRule='none' fkDelRule='some' fkDef='somedef'>ICAO_ID</colname>\n" +
210       "  <attr name='TEST3'>SOMEATTR3</attr>\n" +
211       "  <attr name='TEST1'>SOMEATTR1</attr>\n" +
212       " </desc>\n" +
213       " <row num='0'>\n" +
214       "  <col name='DATUM'>2005-01-05 15:52:06.0</col>\n" +
215       "  <col name='CPU'>238333</col>\n" +
216       "  <col name='COL1'>Bla</col>\n" +
217       "  <col name='ICAO_ID'>EDDI</col>\n" +
218       "  <attr name='LR'>SRANIL</attr>\n" +
219       "  <attr name='SUBNET_ID'>TCP</attr>\n" +
220       " </row>\n" +
221       " <row num='1'>\n" +
222       "  <col name='DATUM'>2005-01-05 15:52:07.0</col>\n" +
223       "  <col name='CPU'>238340</col>\n" +
224       "  <col name='COL1' encoding='base64'>QmxdXT5CbA==</col>\n" +
225       "  <col name='ICAO_ID'>EDDI</col>\n" +
226       " </row>\n" +
227       " <row num='2'>\n" +
228       "  <col name='DATUM'>2005-01-05 15:52:08.0</col>\n" +
229       "  <col name='CPU'>238343</col>\n" +
230       "  <col name='COL1'>BOO</col>\n" +
231       "  <col name='ICAO_ID'>EDDI</col>\n" +
232       "  <attr name='SUBNET_ID'>X25</attr>\n" +
233       " </row>\n" +
234       "</sql>\n";
235 
236       
237       SqlInfoParser parser = new SqlInfoParser();
238       parser.init(this.info);
239       
240       String xmlFileName = System.getProperty("xmlFileName",null);
241       InputStream is = null;
242       if (xmlFileName == null) {
243          is = new ByteArrayInputStream(xml.getBytes());
244       }
245       else {
246          // String isChar = System.getProperty("isChar", "false");
247          // if ("true".equals(isChar))
248          //    is = new InputSource(new FileReader(xmlFileName));
249          // else
250          // is = new InputSource(new FileInputStream(xmlFileName));
251          is = new FileInputStream(xmlFileName);
252          
253       }
254       String charSet = System.getProperty("charSet", null);
255       SqlInfo record = parser.parse(is, charSet);
256       SqlDescription description = record.getDescription();
257       assertNotNull("the description shall not be null", description);
258       assertNotNull("the identity shall not be null", description.getIdentity());
259       assertNotNull("the command shall not be null", description.getCommand());
260 
261       assertEquals("the identity content is wrong", "EDDI", description.getIdentity());
262       assertEquals("the command content is wrong", "INSERT", description.getCommand());
263 
264       // test the column descriptions 
265       SqlColumn[] colDescriptions = description.getColumns();
266       assertEquals("the number of column descriptions is wrong", 4, colDescriptions.length);
267       String[] names = new String[] { "DATUM", "CPU", "COL1", "ICAO_ID"};
268       for (int i=0; i < colDescriptions.length; i++) {
269          log.info("test column description #" + i + " names: '" + names[i] + "' and '" + colDescriptions[i].getColName() + "'");
270          assertEquals("the name of the column description #" + i + " is wrong", names[i], colDescriptions[i].getColName());
271       }
272       
273       List rows = record.getRows();
274       assertEquals("the number of rows is wrong", 3, rows.size());
275       int[] attr = new int[] { 2, 0, 1 };
276       for (int i=0; i < 3; i++) {
277          SqlRow row = (SqlRow)rows.get(i);
278          assertEquals("wrong number of columns for row '" + i+ "'", 4, row.getColumnNames().length);
279          assertEquals("wrong number of attributes for row '" + i+ "'", attr[i], row.getAttributeNames().length);
280       }
281       
282       System.out.println("\n\nshould be:\n" + xml);
283       System.out.println("\nis:\n" + record.toXml(""));
284       assertXMLEqual("output xml is not the same as input xml", xml, record.toXml(""));
285       //assertXpathNotExists("/myRootTag/row[@num='0']", xml);
286       //assertXpathEvaluatesTo("CREATE", "/myRootTag/desc/command/text()", xml);
287       log.info("SUCCESS");
288    }
289    
290    /**
291     * If the table does not exist we expect a null ResultSet
292     * @throws Exception Any type is possible
293     */
294    public final void testUnprotectedClientProperties() throws Exception {
295       log.info("Start testUnprotectedClientProperties()");
296       /** Comments are not allowed otherwise the xml are not considered the same */
297             String xml = "" +
298                    "<?xml version='1.0' encoding='UTF-8' ?>\n" +
299                    "<sql>\n" +
300                    "  <desc>\n" +
301                    "    <command>INSERT</command>\n" +
302                    "    <ident>EDDI</ident>\n" +
303                  "  </desc>\n" + 
304                    "  <row num='0'>\n" + 
305                    "    <col name='WP_KEY'>270232</col>\n" + 
306                    "    <col name='WP_ID'>6400E</col>\n" + 
307                    "    <col name='WP_FLOC_KEY'>7053</col>\n" + 
308                    "    <col name='WP_NAME'>64N000E</col>\n" + 
309                    "    <col name='WP_LAT_LON'>N640000E0000000</col>\n" + 
310                    "    <col name='WP_USAGE'>BOTH</col>\n" + 
311                    "    <col name='WP_RNAV'>N</col>\n" + 
312                    "    <col name='WP_LAST_UPDATE'>2003-10-02 00:00:00</col>\n" + 
313                    "    <col name='WP_OPERATOR'>JP</col>\n" + 
314                    "    <col name='WP_SUBSTITUTE'>Y</col>\n" + 
315                    "    <attr name='oldContent' encoding='forcePlain'><col name='WP_KEY'>270232</col></attr>\n" + 
316                    "    <attr name='replKey'>2795</attr>\n" + 
317                    "    <attr name='action'>UPDATE</attr>\n" + 
318                    "    <attr name='transaction'>9.7.4118</attr>\n" + 
319                    "    <attr name='WP_SUBSTITUTE'>Y</attr>\n" + 
320                    "    <attr name='guid'>AAAXlWAAFAAAFFgAAA</attr>\n" + 
321                    "    <attr name='tableName'>R_WAYPOINTS</attr>\n" + 
322                    "    <attr name='schema'>AIS</attr>\n" + 
323                    "    <attr name='dbId'>NULL</attr>\n" + 
324                    "    <attr name='version'>0.5</attr>\n" + 
325                    "  </row>\n" +
326                  "</sql>\n";
327       
328       SqlInfoParser parser = new SqlInfoParser();
329       parser.init(this.info);
330       SqlInfo record = parser.parse(xml);
331       log.info(record.toXml(""));
332       
333       xml = "" +
334       "<?xml version='1.0' encoding='UTF-8' ?>\n" +
335       "<sql>\n" +
336       "  <row num='0'>\n" + 
337       "    <col name='TEST'>100</col>\n" + 
338       "    <attr name='oldContent' encoding='forcePlain'><col name='TEST' encoding='base64'>Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj5MVEQgICBSQUZJUzw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8</col></attr>" +
339       "    <attr name='test1' encoding='forcePlain'><col name='test2'><![CDATA[>>>>>>>>>>>>>>>>>>>>>>>>>>LTD RAFIS<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<]]></col></attr>" +
340       "  </row>\n" +
341       "</sql>\n";
342 
343       record = parser.parse(xml);
344       log.info(record.toXml(""));
345       
346       
347       log.info("SUCCESS");
348    }
349    
350 
351 }


syntax highlighted by Code2HTML, v. 0.9.1