package Cases; import java.io.*; import java.io.File; import neustore.base.DBIndex;//because we create a object of index.java import neustore.base.LRUBuffer;//because we create an object of it //This Testcase has only one file and all the pages of all types are managed in the same file public class TestCaseAll { public static void main(String[] args)throws IOException { //all the pages will be stored under this file name String filename1="TESTFILE"; File file= new File(filename1); if(file.delete()) { System.out.println("The old file with that name has been deleted"); } else { System.out.println("The file was not found"); } //'buf' is an object of XMLBuffer class. Passews 2 arguments 5=Buffer size and 2048=page size in bytes XMLBuffer buf= new XMLBuffer(5,2048); //Creating an object of ElementIndex class and passing the 'buf'(buffer) , filename and boolean value 'true' (indicating a new file has to be created with the passed filename) ElementIndex EI = new ElementIndex(buf,filename1,1); //inserting an element named "root" and displaying the element id on console int id1=EI.insertElementName("root"); //asuuming all id's are given numbers starting from 0 and negative numbers means something has gone wrong if(id1>=0) { System.out.println("The 'root' element has been assigned an id:"+id1); } else { System.out.println("The 'root' element has not been inserted"); } //The follwing code will return the id of the recently inserted element "root" int id2= EI.getElementId("root"); System.out.println("The 'root' element's id is "+id2); //this code will check if the id1 and id2 are equal, if they are then insertion and retreival are running good if(id1==id2) { System.out.println("Both the Ids are same. The Insertion and retreival works"); } else { System.out.println("There is some problem with the insertion or retrival"); } //When both the ids are equal and everything is running fine, we go ahead to test the next API //The follwing code will try to get an element name given an id String ename=EI.getElementName(id1); //this statement should return "root" System.out.println("The element which has an id="+id1+" is "+ename); if(ename!=null &&ename.equals("root") ) { System.out.println("getElementName method works fine as of now"); } else { System.out.println("The value returned by getElementName is not what we expected."); } //The following code will test the "getAddress(string name)" method DiskAddress da=EI.getAddress("root"); if(da!=null) { //make sure that the PageID and recno(if using dense index) are correct. //Normally , All the elements are stored in 1st page and since 'root' is the first element, its bucket shall start from pageid=2. However it depends on what scheme //you are following in assigning the pageIds. System.out.println("The PageId from which the records of element named 'root' starts is:"+da.PageId); //This statement should be considered by those who used dense index instead of buckets System.out.println("The Record number from which the records of element named 'root' starts in the pageID shown above is:"+da.RecordNumber); } //Following code Will insert a record in the pages(buckets) that hold the records of all the element appearances in the XML file (STATE 1) DiskAddress da2=new DiskAddress(3,1); //should insert a record in page2 , the tuple would be (1,3,1) as the first record in the bucket(or page 2) int i=EI.insertElementEntry("root",da2); System.out.println("An (root)element entry with Pageid=3 and recno=1 has been done and the returned exit status is "+i); //Follwing code will test the insertion of an element entry given an ID (STATE 2) //practically we cant find two 'root' elements. This is just for demo DiskAddress da3=new DiskAddress(3,2); //should insert a record in page2(if the space is still available) , the tuple would be (1,3,2) as the second record in the bucket(or page 2) i=EI.insertElementEntryById(id2,da3);//passing the id of 'root' and the disk adress //Follwing method would tell us if the insertion of element entries went as planned i=EI.getNumberEntries(id2); if(i==2)//because we inserted only 2 record in the elements page { System.out.println("The element entries have been inserted. No of entries for Element with id="+id2+ "are "+i); } else { System.out.println("No of entries is "+id2+ " and we are supposed to get '2'"); } //The follwing call would try to get the adress of the ith entry of an element DiskAddress da4=EI.getElementEntry(id2,2);//this should return the address of the 2nd record in the element entries System.out.println("The PageId of the 2nd record entry for element with id="+id2+" is "+da4.PageId); System.out.println("The record number of the 2nd record entry for element with id="+id2+" is "+da4.RecordNumber); //Next we try to delete one of the entry by name int k=EI.deleteElementEntryById(1,da2);//This is delete the "root" elements first int id6=EI.insertElementName("database"); // The following code will test the "getAddress(string name)" method DiskAddress da10=EI.getAddress("database"); id6=EI.deleteElementEntry("database",da10); System.out.println("__________________________________________________________________________________________"); //creating an object of StructureIndex class to test its methods StructureIndex SI = new StructureIndex(buf,filename1,0); //creating a record of StructureIndexrecord class DiskAddress ptr=new DiskAddress(4,0);//this means the content for this element starts from 0th byte in page with id '4' StructureIndexRecord record=new StructureIndexRecord(1,1,0,20,0,0,-1,1,ptr); //lets try to insert the above created record in the structure page (STATE 5) DiskAddress ret= SI.insertEntry(record); if(ret!=null) { //we should get pageid=3 and recno=1 (STATE 4) System.out.println("The adress of the recently inserted record in structure page is : PageId="+ret.PageId+" and Recno:"+ret.RecordNumber); } else //assuming that 'null' is returned when something goes wrong { System.out.println("The Structure Index record has not been inserted"); } //This code tries to get the address of the record given an id, since we inserted a record with Nodeid=1, try lets try to get that DiskAddress add= SI.getAddress(1); if(add!=null) { //we should get pageid=3 and recno=1 (STATE 4) System.out.println("The record with nodeid=1 is in page="+add.PageId+" and recno="+add.RecordNumber); } else { System.out.println("could not retrieve the address given a node id"); } //The following code will try to update the end position of the record having Nodeid=1 . //In the previous steps we put the end position as 20..Now lest change it to 30 i=SI.updateEndPosition(1,30); if(i>=0)//asumming the exit codes are negatives when something goes wrong { System.out.println("The end position has been updated to 30 for nodeid=1"); } else { System.out.println("There was some error in updating the End position"); } //The following method will attempt to update the pointer to the content of the XML node. //Last time we used 'ptr' variable(4,0) to store the pointer. This time we will update the pointer by changing the starting position of content =5 ptr=new DiskAddress(4,0); //This will atempt to update the pointer of the record with nodeId=1 (STATE 6) i=SI.setContent(1,ptr); System.out.println("The exit status after updating the pointer using setContent method is:"+i); //This code will try to get the entire record with ndeid=1 and store it in a object of StructureIndexRecord StructureIndexRecord rec=SI.getRecordById(1); System.out.println("The returned record has the node id:"+rec.nodeId); //This code tries to get the record in one of the structure pages given an 'PageId' and the 'record number' //when we inserted the record, we got the DiskAddress. Lets use the same object as parameter here and see if we get back the same record StructureIndexRecord rec1=SI.getRecordAddress(ret); System.out.println("The returned record has the node id:"+rec1.nodeId); //if '1' is the nodeID, then we retreived the correct record //this moehod is used to delete a record given a node It. lets try to delete the above record i=SI.deleteEntry(rec1.nodeId); rec1=SI.getRecordAddress(ret); if(rec1!=null)//assuming 'null' is returned when no record is found { System.out.println("The record has not been deleted!");//that means there is some problem with the getRecordAddress stub } System.out.println("__________________________________________________________________________________________"); // creating an object of XMLContent class to test its methods XMLContent XC=new XMLContent(buf,filename1,0); //(STATE 7) //This code would append the content String s=new String("New String"); DiskAddress addr=XC.appendContent(s); System.out.println("The appened content adress has Pageid="+addr.PageId+" and starts from "+addr.RecordNumber); //in our case should return pageid=4 and starts from 31(if you start addressing the page starting from 1) //This code will test the getContent method String str=XC.getContent(addr,10); System.out.println("The returned string is "+str); if(str.equals(s)) //Check wether the arguments passed are correct depending upon your scheme of layout of pages { System.out.println("The getContent method runs fine"); } else { System.out.println("The getContent method did not return what we intended"); } //this code will get the size of the whole content int x=XC.getSize(); System.out.println("The total size of the content is"+x); //this should return 40.(we must insert the content for the first 30 bytes to get this value) x=XC.deleteContent(addr,10); System.out.println("This Exit status of deleteContent is"+x); System.out.println("__________________________________________________________________________________________"); //testing the attribute index method AttributeIndex AI=new AttributeIndex(buf,filename1,0); //create the record in structure index as we did before proceeding //insert an record in attribute index int y=AI.insertEntry(1,"x","123"); System.out.println("The exit code for inserting an entry of an attribute 'x' is "+y); //insert another record y=AI.insertEntry(1,"top","2"); //The following code will try to get the attribute value of 'x' attribute for element occurence having nodeID=1 String av=AI.getValue(1,"x"); System.out.println("The attribute value for 'x' attribute is "+av); //delete the attribute "x" for nodeid=1 y=AI.deleteEntry(1,"x"); av=AI.getValue(1,"x"); System.out.println("The attribute value for 'x' attribute is "+av);//this sould not return anything as we delted this attribute. // EI.close(); //remove the comments if you intend to close the file }//end of main() }//end of Testcasesall class