KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > standalone > sql > filters > MacrosHandlerTest


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Marc Wick.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.standalone.sql.filters;
26
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.Date JavaDoc;
29
30 import org.objectweb.cjdbc.common.sql.filters.MacrosHandler;
31 import org.objectweb.cjdbc.scenario.templates.NoTemplate;
32
33 /**
34  * This class defines a MacrosHandlerTest
35  *
36  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
37  * @version 1.0
38  */

39 public class MacrosHandlerTest extends NoTemplate
40 {
41   /**
42    * @see org.objectweb.cjdbc.scenario.standalone.sql.filters.MacrosHandlerTest
43    */

44   public void testNowMacro()
45   {
46     MacrosHandler handler = new MacrosHandler(MacrosHandler.RAND_FLOAT, 120000,
47         MacrosHandler.DATE_TIMESTAMP, MacrosHandler.DATE_DATE,
48         MacrosHandler.DATE_TIMESTAMP, MacrosHandler.DATE_TIMESTAMP,
49         MacrosHandler.DATE_TIMESTAMP);
50
51     String JavaDoc select = " select With nothing to be Replaced";
52     String JavaDoc replaced = handler.processMacros(select);
53     assertEquals("", " select With nothing to be Replaced", replaced);
54
55     // get rid of milliseconds, to compensate for time differences (hopefully)
56
SimpleDateFormat JavaDoc fmt = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss.0");
57
58     String JavaDoc expectedResult = "";
59     // we repeat the test twice if it failed, to compensate of time differences
60
String JavaDoc update = "update TABLE set column = now() where name = 'AAA'";
61     replaced = handler.processMacros(update);
62     Date JavaDoc date = new Date JavaDoc();
63     long td = date.getTime() - date.getTime() % 120000;
64     String JavaDoc time = fmt.format(new Date JavaDoc(td));
65     expectedResult = "update TABLE set column = {ts '" + time
66         + "'} where name = 'AAA'";
67     assertEquals("test replacement of now() " + replaced, replaced,
68         expectedResult);
69
70   }
71
72   /**
73    * Test CURRENT_TIMESTAMP macro
74    */

75   public void testTimeStampMacro()
76   {
77     MacrosHandler handler = new MacrosHandler(MacrosHandler.RAND_FLOAT, 120000,
78         MacrosHandler.DATE_TIMESTAMP, MacrosHandler.DATE_DATE,
79         MacrosHandler.DATE_TIMESTAMP, MacrosHandler.DATE_TIMESTAMP,
80         MacrosHandler.DATE_TIMESTAMP);
81
82     // get rid of milliseconds, to compensate for time differences (hopefully)
83
SimpleDateFormat JavaDoc fmt = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss.0");
84
85     Date JavaDoc date = new Date JavaDoc();
86     long td = date.getTime() - date.getTime() % 120000;
87     String JavaDoc time = fmt.format(new Date JavaDoc(td));
88
89     // get rid of milliseconds, to compensate for time differences (hopefully)
90
fmt = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss.0");
91
92     String JavaDoc update = "update table set column = CURRENT_TIMESTAMP";
93     String JavaDoc replaced = handler.processMacros(update);
94     date = new Date JavaDoc();
95     td = date.getTime() - date.getTime() % 120000;
96     time = fmt.format(new Date JavaDoc(td));
97     String JavaDoc expectedResult = "update table set column = {ts '" + time + "'}";
98     assertEquals("test replacement of CURRENT_TIMESTAMP " + replaced, replaced,
99         expectedResult);
100   }
101
102   /**
103    * @see org.objectweb.cjdbc.scenario.standalone.sql.filters.MacrosHandlerTest
104    */

105   public void testRandMacro() throws Exception JavaDoc
106   {
107     MacrosHandler handler = new MacrosHandler(MacrosHandler.RAND_FLOAT, 1000,
108         MacrosHandler.DATE_DATE, MacrosHandler.DATE_DATE,
109         MacrosHandler.DATE_TIMESTAMP, MacrosHandler.DATE_TIMESTAMP,
110         MacrosHandler.DATE_TIMESTAMP);
111     String JavaDoc query = " select With nothing to be Replaced";
112     String JavaDoc replaced = handler.macroRand(query, null);
113     assertEquals("", " select With nothing to be Replaced", replaced);
114   }
115
116   /**
117    * @see org.objectweb.cjdbc.scenario.standalone.sql.filters.MacrosHandlerTest
118    */

119   public void testReplaceMacro()
120   {
121     MacrosHandler handler = new MacrosHandler(MacrosHandler.RAND_FLOAT, 1000,
122         MacrosHandler.DATE_DATE, MacrosHandler.DATE_DATE,
123         MacrosHandler.DATE_TIMESTAMP, MacrosHandler.DATE_TIMESTAMP,
124         MacrosHandler.DATE_TIMESTAMP);
125
126     String JavaDoc query = "update table set stringvalue= 'macros in string should not be replaced , like rand(), now() and current_timestamp'";
127     String JavaDoc replaced = handler.processMacros(query);
128     assertEquals("Query was replaced while it should not have", query, replaced);
129   }
130 }
Popular Tags