001    package nl.cwi.sen1.visbase.rstorecontainer;
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.RStore;
008    import nl.cwi.sen1.relationstores.types.RTuple;
009    import nl.cwi.sen1.relationstores.types.RTupleRtuples;
010    import nl.cwi.sen1.relationstores.types.RType;
011    import nl.cwi.sen1.relationstores.types.RTypeColumnTypes;
012    import nl.cwi.sen1.relationstores.types.relem.Tuple;
013    import nl.cwi.sen1.visbase.rstorecontainer.datatypes.FactInfoList;
014    import aterm.pure.PureFactory;
015    
016    /**
017     * Tests the code of the RStore Tracker
018     * 
019     * @Date 2007-03-13
020     * @author Ricardo Lindooren
021     */
022    public class RStoreTrackerTest extends TestCase {
023    
024        // private RStoreTracker rStoreTrackerUnderTest;
025    
026        public RStoreTrackerTest(String arg0) {
027            super(arg0);
028        }
029    
030        @Override
031        protected void setUp() throws Exception {
032            super.setUp();
033        }
034    
035        @Override
036        protected void tearDown() throws Exception {
037            super.tearDown();
038        }
039    
040        /**
041         * Tests the code of the constructor
042         * 
043         * @Date 2007-03-13
044         * @author Ricardo Lindooren
045         */
046        public void testRStoreTracker() {
047    
048            // First a destructive test, null-input
049            RuntimeException rex = null;
050            try {
051                new RStoreTracker(null);
052            } catch (RuntimeException ex) {
053                rex = ex;
054            }
055            assertNotNull(
056                    "Using the constructor with null-input should throw a RuntimeException",
057                    rex);
058    
059            RStoreTracker rStoreTracker = new RStoreTracker(createSimpleRStore());
060            assertNotNull("Newly created RStoreTracker should not be null",
061                    rStoreTracker);
062        }
063    
064        /**
065         * <b>TODO</b>
066         * 
067         * @Date 2007-03-13
068         * @author Ricardo Lindooren
069         */
070        public void testUpdate() {
071    
072            RStoreTracker rStoreTracker = new RStoreTracker(createSimpleRStore());
073    
074            rStoreTracker.update(createUpdatedRStore());
075    
076            // TODO
077        }
078    
079        /**
080         * Simple getter test
081         * 
082         * @Date 2007-03-13
083         * @author Ricardo Lindooren
084         */
085        public void testGetRStore() {
086            RStoreTracker rStoreTracker = new RStoreTracker(createSimpleRStore());
087    
088            assertNotNull("RStore object should not be null", rStoreTracker
089                    .getRStore());
090        }
091    
092        /**
093         * Simple getter test
094         * 
095         * @Date 2007-03-13
096         * @author Ricardo Lindooren
097         */
098        public void testGetRTuple() {
099            RStoreTracker rStoreTracker = new RStoreTracker(createSimpleRStore());
100    
101            assertNotNull("There should be a RTuple associated with id 1",
102                    rStoreTracker.getRTuple(1));
103    
104            assertNull("There shouldn't be a RTuple associated with id 2",
105                    rStoreTracker.getRTuple(2));
106        }
107    
108        /**
109         * Tests the code used for retrieving a FactInfoList based on the RStore in
110         * the Tracker
111         * 
112         * @Date 2007-03-13
113         * @author Ricardo Lindooren
114         */
115        public void testGetFactInfoFromRStore() {
116            RStoreTracker rStoreTracker = new RStoreTracker(createUpdatedRStore());
117    
118            FactInfoList fil = rStoreTracker.getFactInfoFromRStore();
119    
120            assertNotNull("The FactInfoList should not be null", fil);
121    
122            assertEquals(
123                    "The FactInfoList should contain two FactInfo objects when created based on the updated RStore",
124                    2, fil.getFactInfos().size());
125        }
126    
127        /**
128         * Tests the code used to determine if two RTuples differ from eachother
129         * 
130         * @Date 2007-03-13
131         * @author Ricardo Lindooren
132         */
133        public void testDiffRtuples() {
134            RStore simpleRStoreA = createSimpleRStore();
135    
136            RTuple rTupleA1 = simpleRStoreA.getRtuples().getRTupleAt(0);
137    
138            // First destructive tests (null-input)
139            RuntimeException rex = null;
140            try {
141                RStoreTracker.diffRtuples(null, rTupleA1);
142            } catch (RuntimeException ex) {
143                rex = ex;
144            }
145            assertNotNull(
146                    "Using diffRtuples with null-input should throw a RuntimeException",
147                    rex);
148            
149            rex = null;
150            try {
151                RStoreTracker.diffRtuples(rTupleA1, null);
152            } catch (RuntimeException ex) {
153                rex = ex;
154            }
155            assertNotNull(
156                    "Using diffRtuples with null-input should throw a RuntimeException",
157                    rex);
158    
159            // Normal usage tests
160            RStore simpleRStoreB = createSimpleRStore();
161    
162            RTuple rTupleB1 = simpleRStoreB.getRtuples().getRTupleAt(0);
163    
164            boolean areDifferent = RStoreTracker.diffRtuples(rTupleA1, rTupleB1);
165    
166            assertFalse("RTuples should be equal", areDifferent);
167    
168            RStore updatedRStoreC = createUpdatedRStore();
169    
170            RTuple rTupleC1 = updatedRStoreC.getRtuples().getRTupleAt(0);
171    
172            areDifferent = RStoreTracker.diffRtuples(rTupleA1, rTupleC1);
173    
174            assertTrue("RTuples should not be equal", areDifferent);
175        }
176    
177         /*
178         * Test method for
179         *
180         'nl.cwi.sen1.visbase.rstorecontainer.RStoreTracker.registerNewRTuple(RTuple)'
181         */
182         public void testRegisterNewRTuple() {
183    
184             RStoreTracker rStoreTracker = new RStoreTracker(createSimpleRStore());
185             
186             // First a destructive test, null-input
187             RuntimeException rex = null;
188             try {
189                 rStoreTracker.registerNewRTuple(null);
190             } catch (RuntimeException ex) {
191                 rex = ex;
192             }
193             assertNotNull(
194                     "Using registerNewRTuple with null-input should throw a RuntimeException",
195                     rex);
196         }
197    
198        /**
199         * Tests the code that is used to generate an textual identifier for a
200         * RTuple
201         * 
202         * @author Ricardo Lindooren
203         * @Date 2007-03-13
204         */
205        public void testCreateIdentifierForRTuple() {
206            // First a destructive test (null-input)
207            RuntimeException rex = null;
208            try {
209                RStoreTracker.createIdentifierForRTuple(null);
210            } catch (RuntimeException ex) {
211                rex = ex;
212            }
213            assertNotNull(
214                    "Using createIdentifierForRTupler with null-input should throw a RuntimeException",
215                    rex);
216    
217            // Normal usage test
218            RStore simpleRStoreA = createSimpleRStore();
219    
220            RTuple rTupleA1 = simpleRStoreA.getRtuples().getRTupleAt(0);
221    
222            final String identifier = RStoreTracker
223                    .createIdentifierForRTuple(rTupleA1);
224    
225            assertEquals("CYCLIC_GRAPH-relation([str,str])", identifier);
226        }
227    
228        /**
229         * Creates a simple RStore.
230         * 
231         * Using coding example from the WIKI
232         * (http://www.renarj.nl/metatrac/wiki/RStores)
233         * 
234         * @author Ricardo Lindooren
235         * @Date 2007-03-13
236         * 
237         * @return the generated simple RStore
238         */
239        private RStore createSimpleRStore() {
240            PureFactory pureFactory = RStoreContainer.getPureFactory();
241            Factory factory = Factory.getInstance(pureFactory);
242    
243            IdCon idCon = factory.makeIdCon_IdCon("CYCLIC_GRAPH");
244            RElem a = factory.makeRElem_Str("a");
245            RElem b = factory.makeRElem_Str("b");
246            RElem c = factory.makeRElem_Str("c");
247            RElem d = factory.makeRElem_Str("d");
248    
249            RType stringType = factory.makeRType_Str();
250            RTypeColumnTypes rTypeColumnTypes = factory.makeRTypeColumnTypes(
251                    stringType, stringType);
252            RType rType = factory.makeRType_Relation(rTypeColumnTypes);
253            Tuple ab = factory.makeRElem_Tuple(factory.makeRElemElements(a, b));
254            Tuple bc = factory.makeRElem_Tuple(factory.makeRElemElements(b, c));
255            Tuple cd = factory.makeRElem_Tuple(factory.makeRElemElements(c, d));
256            Tuple da = factory.makeRElem_Tuple(factory.makeRElemElements(d, a));
257            RElem rElem = factory.makeRElem_Set(factory.makeRElemElements(ab, bc,
258                    cd, da));
259            RTuple rTuple = factory.makeRTuple_Rtuple(idCon, rType, rElem);
260    
261            RTupleRtuples rTupleRtuples = factory.makeRTupleRtuples(rTuple);
262            RStore rstore = factory.makeRStore_Rstore(rTupleRtuples);
263    
264            return rstore;
265        }
266    
267        /**
268         * Creates an updated version of the simple RStore.
269         * 
270         * @see #createSimpleRStore()
271         * 
272         * Using coding example from the WIKI
273         * (http://www.renarj.nl/metatrac/wiki/RStores)
274         * 
275         * @author Ricardo Lindooren
276         * @Date 2007-03-13
277         * 
278         * @return the generated updated RStore
279         */
280        private RStore createUpdatedRStore() {
281            PureFactory pureFactory = RStoreContainer.getPureFactory();
282            Factory factory = Factory.getInstance(pureFactory);
283    
284            IdCon idCon = factory.makeIdCon_IdCon("CYCLIC_GRAPH");
285            RElem a = factory.makeRElem_Str("a");
286            RElem b = factory.makeRElem_Str("b");
287            RElem c = factory.makeRElem_Str("c");
288            RElem d = factory.makeRElem_Str("d");
289            RElem e = factory.makeRElem_Str("e"); //
290    
291            RType stringType = factory.makeRType_Str();
292            RTypeColumnTypes rTypeColumnTypes = factory.makeRTypeColumnTypes(
293                    stringType, stringType);
294            RType rType = factory.makeRType_Relation(rTypeColumnTypes);
295            Tuple ab = factory.makeRElem_Tuple(factory.makeRElemElements(a, b));
296            Tuple bc = factory.makeRElem_Tuple(factory.makeRElemElements(b, c));
297            Tuple cd = factory.makeRElem_Tuple(factory.makeRElemElements(c, d));
298            Tuple da = factory.makeRElem_Tuple(factory.makeRElemElements(d, a));
299    
300            Tuple de = factory.makeRElem_Tuple(factory.makeRElemElements(d, e));
301            // RElem rElem = factory.makeRElem_Set(factory.makeRElemElements(ab, bc,
302            // cd, da));
303            RElem rElem = factory.makeRElem_Set(factory.makeRElemElements(ab, bc,
304                    cd, da, de));
305            RTuple rTuple = factory.makeRTuple_Rtuple(idCon, rType, rElem);
306    
307            // ---
308    
309            IdCon idCon2 = factory.makeIdCon_IdCon("CYCLIC_GRAPH2");
310            RElem x = factory.makeRElem_Str("x");
311            RElem y = factory.makeRElem_Str("y");
312            RElem z = factory.makeRElem_Str("z");
313    
314            RTypeColumnTypes rTypeColumnTypes2 = factory.makeRTypeColumnTypes(
315                    stringType, stringType);
316            RType rType2 = factory.makeRType_Relation(rTypeColumnTypes2);
317    
318            Tuple xy = factory.makeRElem_Tuple(factory.makeRElemElements(x, y));
319            Tuple xz = factory.makeRElem_Tuple(factory.makeRElemElements(x, z));
320            Tuple zy = factory.makeRElem_Tuple(factory.makeRElemElements(z, y));
321    
322            RElem rElem2 = factory.makeRElem_Set(factory.makeRElemElements(xy, xz,
323                    zy));
324            RTuple rTuple2 = factory.makeRTuple_Rtuple(idCon2, rType2, rElem2);
325    
326            // ---
327    
328            RTupleRtuples rTupleRtuples = factory
329                    .makeRTupleRtuples(rTuple, rTuple2);
330            RStore rstore = factory.makeRStore_Rstore(rTupleRtuples);
331    
332            return rstore;
333        }
334    }