001 package nl.cwi.sen1.visbase.rstorecontainer.datatypes;
002
003 import junit.framework.TestCase;
004 import nl.cwi.sen1.relationstores.Factory;
005 import nl.cwi.sen1.relationstores.types.IdCon;
006 import nl.cwi.sen1.relationstores.types.RElem;
007 import nl.cwi.sen1.relationstores.types.RTuple;
008 import nl.cwi.sen1.relationstores.types.RType;
009 import nl.cwi.sen1.relationstores.types.RTypeColumnTypes;
010 import nl.cwi.sen1.relationstores.types.relem.Tuple;
011 import nl.cwi.sen1.visbase.rstorecontainer.RStoreContainer;
012
013 import org.apache.commons.logging.Log;
014 import org.apache.commons.logging.LogFactory;
015
016 import aterm.ATermAppl;
017 import aterm.ATermInt;
018 import aterm.ATermList;
019
020 /**
021 * Tests the FactInfo class.
022 *
023 * @author Ricardo Lindooren
024 * @author Arend van Beelen (reviewer)
025 * @date 2007-02-14
026 */
027 public class FactInfoTest extends TestCase {
028
029 private FactInfo m_factInfo;
030
031 private final static Log m_log = LogFactory.getLog(FactInfoTest.class);
032
033 private final static int RSTORE_ID = 1;
034 private final static String RSTORE_NAME = "RicoTestFact";
035 private final static String RSTORE_TYPE = "relation([str,str])";
036
037 @Override
038 protected void setUp() throws Exception {
039 super.setUp();
040
041 m_factInfo = new FactInfo(RSTORE_ID, getTestRTuple(RSTORE_NAME));
042 }
043
044 @Override
045 protected void tearDown() throws Exception {
046 super.tearDown();
047
048 m_factInfo = null;
049 }
050
051 /**
052 * Simple getter test.
053 *
054 * @author Ricardo Lindooren
055 * @author Arend van Beelen (reviewer)
056 * @date 2007-02-14
057 */
058 public void testGetId() {
059 assertEquals(RSTORE_ID, m_factInfo.getId());
060 }
061
062 /**
063 * Simple getter test.
064 *
065 * @author Ricardo Lindooren
066 * @author Arend van Beelen (reviewer)
067 * @date 2007-02-14
068 */
069 public void testGetName() {
070 assertEquals(RSTORE_NAME, m_factInfo.getName());
071 }
072
073 /**
074 * Simple getter test.
075 *
076 * @author Ricardo Lindooren
077 * @author Arend van Beelen (reviewer)
078 * @date 2007-02-14
079 */
080 public void testGetType() {
081 assertEquals(RSTORE_TYPE, m_factInfo.getType());
082 }
083
084 /**
085 * Simple getter test.
086 *
087 * @author Ricardo Lindooren
088 * @author Arend van Beelen (reviewer)
089 * @date 2007-02-21
090 */
091 public void testGetRType() {
092 assertNotNull("The RType instance of the factinfo under test should not be null",
093 m_factInfo.getRType());
094 }
095
096 /**
097 * Tests the code that creates an AtermList.
098 *
099 * @author Ricardo Lindooren
100 * @author Arend van Beelen (reviewer)
101 * @date 2007-02-14
102 */
103 public void testToAtermList() {
104 ATermList list = m_factInfo.toAtermList();
105
106 assertNotNull("AtermList should not be null", list);
107
108 if (m_log.isDebugEnabled()) {
109 m_log.debug("AtermList created from RStoreFactData during test: \n\n\t"
110 + list.toString() + "\n");
111 }
112
113 ATermInt factId = (ATermInt) list.elementAt(0);
114 assertEquals("Id of fact does not equal expected value",
115 1, factId.getInt());
116
117 ATermAppl factName = (ATermAppl) list.elementAt(1);
118 assertEquals("Name of fact does not equal expected value",
119 RSTORE_NAME, factName.getName());
120
121 ATermAppl factType = (ATermAppl) list.elementAt(2);
122
123 Factory factory = Factory.getInstance(RStoreContainer.getPureFactory());
124 RType rType = factory.RTypeFromTerm(factType);
125
126 final String typeName = rType.toString();
127
128 assertEquals("Name of fact does not equal expected value",
129 RSTORE_TYPE, typeName);
130 }
131
132 /**
133 * Tests the Constructor used to parse ATermList data.
134 *
135 * @author Ricardo Lindooren
136 * @author Arend van Beelen (reviewer)
137 * @date 2007-02-14
138 */
139 public void testConstructFromAtermList() throws Exception {
140
141 ATermList list = m_factInfo.toAtermList();
142
143 // first some destructive tests
144 RuntimeException runtimeException = null;
145 try {
146 new FactInfo(null);
147 } catch (RuntimeException exception) {
148 runtimeException = exception;
149 }
150 assertNotNull("Creating a new instance of FactInfo with a null-ATermList should throw a RuntimeException",
151 runtimeException);
152
153 ATermList illegalList = RStoreContainer.getPureFactory().makeList();
154
155 ATermParseException termParseException = null;
156 try {
157 new FactInfo(illegalList);
158 } catch (ATermParseException exception) {
159 termParseException = exception;
160 }
161 assertNotNull("Creating a new instance of FactInfo with a not correctly constructed ATermList should throw a ATermParseException",
162 termParseException);
163
164 FactInfo factInfo = new FactInfo(list);
165
166 assertEquals("Id of constructed FactData from ATermList not correct",
167 factInfo.getId(), m_factInfo.getId());
168 assertEquals("Name of constructed FactData from ATermList not correct",
169 factInfo.getName(), m_factInfo.getName());
170 assertNotNull("The RType instance should not be null after constructing from ATermList",
171 m_factInfo.getRType());
172 assertEquals("Type of constructed FactData from ATermList not correct",
173 factInfo.getType(), m_factInfo.getType());
174 }
175
176 /**
177 * Tests the Constructor used to parse RTuple data (this code is actually
178 * also indirectly tested from the RStoreContainerTest).
179 *
180 * @see nl.cwi.sen1.visbase.rstorecontainer.RStoreContainerTest
181 *
182 * @author Ricardo Lindooren
183 * @author Arend van Beelen (reviewer)
184 * @date 2007-02-14
185 */
186 public void testConstructFromRTuple() {
187 RuntimeException runtimeException = null;
188 try {
189 new FactInfo(1, null);
190 } catch (RuntimeException exception) {
191 runtimeException = exception;
192 }
193
194 assertNotNull("Creating a new instance of FactInfo with a null-RTuple should throw a RuntimeException",
195 runtimeException);
196 }
197
198 /**
199 * Creates a dummy RTuple.
200 *
201 * Format is like:
202 * <code>rtuple("##name##",relation([str,str]),set([tuple([str("a"),str("b")]),tuple([str("b"),str("c")]),tuple([str("c"),str("d")]),tuple([str("d"),str("a")])]))</code><br />
203 *
204 * This method is shared with the FactInfoListTest.
205 *
206 * @return The dummy RTuple
207 *
208 * @author Ricardo Lindooren
209 * @author Arend van Beelen (reviewer)
210 * @date 2007-02-21
211 */
212 public static RTuple getTestRTuple(final String name) {
213 Factory factory = Factory.getInstance(RStoreContainer.getPureFactory());
214
215 IdCon idCon = factory.makeIdCon_IdCon(name);
216 RElem a = factory.makeRElem_Str("a");
217 RElem b = factory.makeRElem_Str("b");
218 RElem c = factory.makeRElem_Str("c");
219 RElem d = factory.makeRElem_Str("d");
220
221 RType stringType = factory.makeRType_Str();
222 RTypeColumnTypes rTypeColumnTypes = factory.makeRTypeColumnTypes(
223 stringType, stringType);
224 RType rType = factory.makeRType_Relation(rTypeColumnTypes);
225 Tuple ab = factory.makeRElem_Tuple(factory.makeRElemElements(a, b));
226 Tuple bc = factory.makeRElem_Tuple(factory.makeRElemElements(b, c));
227 Tuple cd = factory.makeRElem_Tuple(factory.makeRElemElements(c, d));
228 Tuple da = factory.makeRElem_Tuple(factory.makeRElemElements(d, a));
229 RElem rElem = factory.makeRElem_Set(factory.makeRElemElements(ab, bc,
230 cd, da));
231 RTuple rTuple = factory.makeRTuple_Rtuple(idCon, rType, rElem);
232
233 return rTuple;
234 }
235 }