KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > presumo > jms > persistence > PersistentQueueTest


1 /**
2  * This file is part of Presumo.
3  *
4  * Presumo is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Presumo is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Presumo; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Copyright (c) 2001 Rob Cauble
19  */

20 package com.presumo.jms.persistence;
21
22 import com.presumo.jms.message.JmsMessage;
23 import com.presumo.jms.message.JmsTextMessage;
24 import com.presumo.jms.message.JmsObjectMessage;
25 import com.presumo.jms.message.JmsBytesMessage;
26 import com.presumo.jms.message.JmsMapMessage;
27 import com.presumo.jms.message.JmsStreamMessage;
28
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32
33 import java.util.Iterator JavaDoc;
34 import java.util.LinkedList JavaDoc;
35 import java.util.ListIterator JavaDoc;
36 import java.util.Random JavaDoc;
37
38 import junit.framework.Test;
39 import junit.framework.TestCase;
40 import junit.framework.TestSuite;
41
42 /**
43  * Tests to exercise the PersistentQueue API
44  */

45 public class PersistentQueueTest extends TestCase
46 {
47   private static final Random JavaDoc random = new Random JavaDoc();
48   private static boolean DEBUG = false;
49
50   private static void debug(Object JavaDoc obj)
51   {
52     if (DEBUG) {
53       System.out.println(obj);
54     }
55   }
56
57   public PersistentQueueTest(String JavaDoc name)
58   {
59     super(name);
60   }
61
62  
63   public static Test suite()
64   {
65     TestSuite suite = new TestSuite(PersistentQueueTest.class);
66     return suite;
67   }
68
69   public void testPersistentQueue() throws Exception JavaDoc
70   {
71     String JavaDoc dir = "./logs/queuetest";
72     TestOperation all_tests = makeTests(new File JavaDoc(dir));
73     TestState state = new TestState();
74     all_tests.operate(state);
75   }
76
77
78
79
80   private static class TestState
81   {
82     public int cur_value;
83     public PersistentQueue the_queue;
84     public LinkedList JavaDoc mainQueue;
85     public LinkedList JavaDoc pendingDelete;
86     public LinkedList JavaDoc deleted;
87     public LinkedList JavaDoc op_stack;
88   }
89
90   private static final int CP_FILE = 0;
91   private static final int TEMP_FILE = 1;
92   private static final int LOG_FILE = 2;
93
94   private static abstract class TestOperation
95   {
96     private void removeNonPersistent(LinkedList JavaDoc list)
97     {
98       ListIterator JavaDoc it = list.listIterator(0);
99       while (it.hasNext()) {
100         JmsMessage msg = (JmsMessage)it.next();
101         if (! PersistentQueue.isMessagePersistent(msg)) {
102           it.remove();
103         }
104       }
105     }
106
107     protected void removeNonPersistent(TestState st)
108     {
109       removeNonPersistent(st.mainQueue);
110       removeNonPersistent(st.pendingDelete);
111       removeNonPersistent(st.deleted);
112     }
113
114     protected File JavaDoc getFile(TestState state, int file_type)
115     {
116       File JavaDoc dir = state.the_queue.getDirectory();
117       String JavaDoc pre = state.the_queue.getFilePrefix();
118       String JavaDoc name = null;
119       switch (file_type) {
120       case CP_FILE:
121         name = PersistentQueue.CP_SUFFIX;
122         break;
123       case TEMP_FILE:
124         name = PersistentQueue.TEMP_CP_SUFFIX;
125         break;
126       case LOG_FILE:
127         name = PersistentQueue.LOG_SUFFIX;
128         break;
129       default:
130         throw new RuntimeException JavaDoc("Unknown type");
131       }
132       return new File JavaDoc(dir,pre+name);
133     }
134
135     protected void copyFile(File JavaDoc src, File JavaDoc dst) throws Exception JavaDoc
136     {
137       copyFile(src,dst,(int)src.length());
138     }
139
140     protected void copyFile(File JavaDoc src, File JavaDoc dst, int size) throws Exception JavaDoc
141     {
142       FileInputStream JavaDoc in = null;
143       FileOutputStream JavaDoc out = null;
144       try {
145         in = new FileInputStream JavaDoc(src);
146         out = new FileOutputStream JavaDoc(dst);
147         for (int i = 0; i < size; i++) {
148           byte temp = (byte)in.read();
149           out.write(temp);
150         }
151       }
152       finally {
153         if (in != null)
154           in.close();
155         if (out != null)
156           out.close();
157       }
158     }
159
160     protected void basicSanityCheck(TestState state) throws Exception JavaDoc
161     {
162       int act_num_pend = state.the_queue.getNumPendingDelete();
163       int act_num_main = state.the_queue.getNumNotPendingDelete();
164       int theory_num_main = state.mainQueue.size();
165       int theory_num_pend = state.pendingDelete.size();
166       if (act_num_pend != theory_num_pend) {
167         throw new Exception JavaDoc("Failed pending sanity check: "+act_num_pend+" "+theory_num_pend);
168       }
169       if (act_num_main != theory_num_main) {
170         throw new Exception JavaDoc("Failed main sanity check: "+act_num_main+" "+theory_num_main);
171       }
172       JmsMessage [] pd = state.the_queue.getPendingDelete();
173       Iterator JavaDoc it = state.pendingDelete.iterator();
174       for (int i = 0; i < pd.length; i++) {
175         JmsMessage msg = (JmsMessage)it.next();
176         if (!pd[i].getJMSMessageID().equals(msg.getJMSMessageID())) {
177           throw new Exception JavaDoc("Failed pd sanity check "+pd[i]+" "+msg);
178         }
179       }
180     }
181
182
183     protected String JavaDoc createRandomString()
184     {
185       return createRandomString(20);
186     }
187     protected String JavaDoc createRandomString(int maxsize)
188     {
189       int size = random.nextInt(maxsize)+1;
190       int lowchar = '0';
191       int highchar = '~';
192       int alpharange = highchar - lowchar;
193
194       byte [] b = new byte[size];
195       for (int i=0; i < size; i++) {
196         b[i] = (byte)( random.nextInt(alpharange) + lowchar );
197       }
198       return new String JavaDoc(b);
199     }
200
201
202     protected JmsMessage createMessage(int id, boolean persistent)
203       throws Exception JavaDoc
204     {
205       int messageType = random.nextInt(5);
206
207       JmsMessage message = null;
208       switch(messageType) {
209       case (0): message = createTextMessage(); break;
210       case (1): message = createObjectMessage(); break;
211       case (2): message = createBytesMessage(); break;
212       case (3): message = createMapMessage(); break;
213       case (4): message = createStreamMessage(); break;
214       default:
215         message = new JmsMessage("qt");
216       }
217
218       int numProperties = random.nextInt(10);
219       for (int i=0; i < numProperties; i++) {
220         String JavaDoc key = createRandomString() + i;
221         String JavaDoc value = createRandomString();
222         message.setStringProperty(key, value);
223       }
224
225       if (persistent) {
226         message.setJMSDeliveryMode(javax.jms.DeliveryMode.PERSISTENT);
227       } else {
228         message.setJMSDeliveryMode(javax.jms.DeliveryMode.NON_PERSISTENT);
229       }
230
231       return message;
232     }
233
234     private JmsMessage createTextMessage() throws Exception JavaDoc
235     {
236       JmsTextMessage msg = new JmsTextMessage("queuetest");
237       msg.setText(createRandomString(50));
238       return msg;
239     }
240
241     private JmsMessage createObjectMessage() throws Exception JavaDoc
242     {
243       JmsObjectMessage msg = new JmsObjectMessage("queuetest");
244       msg.setObject(createRandomString(50));
245       return msg;
246     }
247
248     private JmsMessage createBytesMessage() throws Exception JavaDoc
249     {
250       JmsBytesMessage msg = new JmsBytesMessage("queuetest");
251       msg.writeObject(createRandomString(100));
252       msg.writeObject(createRandomString(50));
253       msg.writeInt(random.nextInt());
254       return msg;
255     }
256
257     private JmsMessage createMapMessage() throws Exception JavaDoc
258     {
259       JmsMapMessage msg = new JmsMapMessage("queuetest");
260       int numProperties = random.nextInt(10);
261       for (int i=0; i < numProperties; i++) {
262         String JavaDoc key = createRandomString() + i;
263         String JavaDoc value = createRandomString();
264         msg.setObject(key, value);
265       }
266       return msg;
267     }
268
269     private JmsMessage createStreamMessage() throws Exception JavaDoc
270     {
271       JmsStreamMessage msg = new JmsStreamMessage("queuetest");
272       msg.writeString(createRandomString(50));
273       return msg;
274     }
275    
276     public abstract void operate(TestState state) throws Exception JavaDoc;
277   }
278
279   ////////////////////////////////////////////////////////////////////////////
280
// End Inner class: TestOperation //
281
////////////////////////////////////////////////////////////////////////////
282

283   private static abstract class ReversibleTestOperation extends TestOperation
284   {
285     public abstract void undo(TestState state) throws Exception JavaDoc;
286   }
287
288   ////////////////////////////////////////////////////////////////////////////
289
// Test operation INIT: Initialize a persistent queue //
290
////////////////////////////////////////////////////////////////////////////
291
private static _INIT INIT(File JavaDoc d, int m)
292   {
293     return new _INIT(d,m);
294   }
295   private static class _INIT extends TestOperation
296   {
297     private File JavaDoc directory;
298     private int max_log_file_size;
299
300     public _INIT(File JavaDoc directory,
301                  int max_log_file_size) {
302       this.directory = directory;
303       this.max_log_file_size = max_log_file_size;
304     }
305
306     public void operate(TestState state) throws Exception JavaDoc {
307       state.the_queue = new PersistentQueue(directory,
308                                             "test",
309                                             max_log_file_size);
310       state.mainQueue = new LinkedList JavaDoc();
311       state.pendingDelete = new LinkedList JavaDoc();
312       state.op_stack = new LinkedList JavaDoc();
313       state.deleted = new LinkedList JavaDoc();
314       state.cur_value = 0;
315     }
316   }
317
318
319   ////////////////////////////////////////////////////////////////////////////
320
// Test operation OPEN: Opens up the queue //
321
////////////////////////////////////////////////////////////////////////////
322
private static _OPEN OPEN() {
323     return new _OPEN();
324   }
325   private static class _OPEN extends TestOperation {
326     public void operate(TestState state) throws Exception JavaDoc {
327       state.the_queue.open();
328       basicSanityCheck(state);
329     }
330   }
331
332
333   ////////////////////////////////////////////////////////////////////////////
334
// Test operation CLOSE: Closes the queue in the given state //
335
////////////////////////////////////////////////////////////////////////////
336
private static _CLOSE CLOSE() {
337     return new _CLOSE();
338   }
339   private static class _CLOSE extends TestOperation {
340     public void operate(TestState state) throws Exception JavaDoc {
341       removeNonPersistent(state);
342       boolean empty = ( state.mainQueue.size() == 0 &&
343                         state.pendingDelete.size() == 0 );
344       state.the_queue.close();
345       if (empty &&
346           (getFile(state,CP_FILE).exists() ||
347            getFile(state,TEMP_FILE).exists() ||
348            getFile(state,LOG_FILE).exists())) {
349         throw new Exception JavaDoc("No files should exist");
350       }
351     }
352   }
353
354   ////////////////////////////////////////////////////////////////////////////
355
// Test operation FORCE_CP: Forces to the queue to a checkpoint //
356
////////////////////////////////////////////////////////////////////////////
357
private static _FORCE_CP FORCE_CP() {
358     return new _FORCE_CP();
359   }
360   private static class _FORCE_CP extends TestOperation {
361     public void operate(TestState state) throws Exception JavaDoc {
362       state.the_queue.forceCheckPoint();
363       basicSanityCheck(state);
364     }
365   }
366
367
368   ////////////////////////////////////////////////////////////////////////////
369
// Test operation SET_MAX_LOG_FILE_SIZE //
370
////////////////////////////////////////////////////////////////////////////
371
private static _SET_MAX_LOG_FILE_SIZE SET_MAX_LOG_FILE_SIZE(int s) {
372     return new _SET_MAX_LOG_FILE_SIZE(s);
373   }
374   private static class _SET_MAX_LOG_FILE_SIZE extends TestOperation {
375     private int size;
376     public _SET_MAX_LOG_FILE_SIZE(int size) {
377       this.size = size;
378     }
379     public void operate(TestState state) throws Exception JavaDoc {
380       state.the_queue.setMaxLogFileSize(size);
381       basicSanityCheck(state);
382     }
383   }
384
385
386   ////////////////////////////////////////////////////////////////////////////
387
// Test operation PUSH: Pushes the specified number of msgs on the queue //
388
////////////////////////////////////////////////////////////////////////////
389
private static _PUSH PUSH(int num_persistent,
390                             int num_non_persistent) {
391     return new _PUSH(num_persistent,num_non_persistent);
392   }
393   private static class _PUSH extends ReversibleTestOperation {
394     private int num_persistent,num_non_persistent;
395     public _PUSH(int num_per,
396                  int num_non) {
397       num_persistent = num_per;
398       num_non_persistent = num_non;
399     }
400     public void operate(TestState state) throws Exception JavaDoc {
401       JmsMessage [] entries = new JmsMessage[num_persistent+num_non_persistent];
402       for (int i = 0; i < num_persistent; i++) {
403         entries[i] = createMessage(state.cur_value++, true);
404         state.mainQueue.addLast(entries[i]);
405       }
406       for (int i = num_persistent; i < num_non_persistent+num_persistent; i++) {
407         entries[i] = createMessage(state.cur_value++, false);
408         state.mainQueue.addLast(entries[i]);
409       }
410       if (entries.length == 1) {
411         state.the_queue.push(entries[0]);
412       }
413       else {
414         state.the_queue.push(entries);
415       }
416       state.op_stack.addLast(this);
417       basicSanityCheck(state);
418     }
419     public void undo(TestState state) {
420       for (int i = 0; i < num_persistent+num_non_persistent; i++) {
421         state.mainQueue.removeLast();
422       }
423     }
424   }
425
426   ////////////////////////////////////////////////////////////////////////////
427
// Test operation DELETE: Perform the delte operation //
428
////////////////////////////////////////////////////////////////////////////
429
private static _DELETE DELETE(int n) {
430     return new _DELETE(n);
431   }
432   private static class _DELETE extends ReversibleTestOperation {
433     private int num_to_delete;
434     public _DELETE(int num_to_delete) {
435       this.num_to_delete = num_to_delete;
436     }
437     public void operate(TestState state) throws Exception JavaDoc {
438       String JavaDoc [] del = new String JavaDoc[num_to_delete];
439       Iterator JavaDoc it = state.pendingDelete.iterator();
440       for (int i = 0; i < num_to_delete; i++) {
441         JmsMessage msg = (JmsMessage)it.next();
442         del[i] = msg.getJMSMessageID();
443         it.remove();
444         state.deleted.addLast(msg);
445       }
446             
447       if (del.length == 1) {
448         state.the_queue.delete(del[0]);
449       }
450       else {
451         state.the_queue.delete(del);
452       }
453       state.op_stack.addLast(this);
454       basicSanityCheck(state);
455     }
456     public void undo(TestState state) throws Exception JavaDoc {
457       for (int i = 0; i < num_to_delete; i++) {
458         Object JavaDoc ent = state.deleted.removeLast();
459         state.pendingDelete.addFirst(ent);
460       }
461     }
462   }
463
464   ////////////////////////////////////////////////////////////////////////////
465
// Test operation GET_NEXT: //
466
////////////////////////////////////////////////////////////////////////////
467
private static _GET_NEXT GET_NEXT(int n) {
468     return new _GET_NEXT(n);
469   }
470   private static class _GET_NEXT extends ReversibleTestOperation {
471     private int num_to_get;
472     public _GET_NEXT(int num_to_get) {
473       this.num_to_get = num_to_get;
474     }
475     public void operate(TestState state) throws Exception JavaDoc {
476       int num_should_get = num_to_get;
477       if (num_should_get > state.mainQueue.size())
478         num_should_get = state.mainQueue.size();
479       JmsMessage [] msgs = state.the_queue.getNext(num_to_get);
480       if (msgs.length != num_should_get) {
481         throw new Exception JavaDoc("Unexpected number returned "+msgs.length+" "+num_should_get);
482       }
483       ListIterator JavaDoc it = state.mainQueue.listIterator();
484       for (int i = 0; i < msgs.length; i++) {
485         debug("Reading value: " + msgs[i]);
486        
487         JmsMessage message = (JmsMessage)it.next();
488         if (!message.getJMSMessageID().equals(msgs[i].getJMSMessageID())) {
489           throw new Exception JavaDoc("Bad data read from queue "+message+" "+msgs[i]);
490         }
491         it.remove();
492
493         // Persistent queue now automaticaly deletes non-persistent messages
494
// DTG
495
if (PersistentQueue.isMessagePersistent(message)) {
496           state.pendingDelete.addLast(message);
497         }
498       }
499       state.op_stack.addLast(this);
500       basicSanityCheck(state);
501     }
502     public void undo(TestState state) throws Exception JavaDoc {
503       for (int i = 0; i < num_to_get; i++) {
504         Object JavaDoc ent = state.pendingDelete.removeLast();
505         state.mainQueue.addFirst(ent);
506       }
507     }
508   }
509
510
511   ////////////////////////////////////////////////////////////////////////////
512
// Test operation CORRUPT_FILE: Corrupts a queue storage file //
513
////////////////////////////////////////////////////////////////////////////
514
private static _CORRUPT_FILE CORRUPT_FILE(int fi) {
515     return new _CORRUPT_FILE(fi);
516   }
517   private static class _CORRUPT_FILE extends TestOperation {
518     private int file_id;
519     public _CORRUPT_FILE(int file_id) {
520       this.file_id = file_id;
521     }
522     public void operate(TestState state) throws Exception JavaDoc {
523       //corrupt it by removing the last byte
524

525       File JavaDoc the_file = getFile(state,file_id);
526       File JavaDoc temp = File.createTempFile("prefix","suffix");
527       copyFile(the_file,temp);
528       copyFile(temp,the_file,(int)(temp.length()-1));
529       if (!temp.delete())
530         throw new Exception JavaDoc ("Couldn't delete"+temp);
531       if (file_id == LOG_FILE) {
532         ReversibleTestOperation rop = (ReversibleTestOperation)state.op_stack.removeLast();
533         rop.undo(state);
534       }
535     }
536   }
537
538
539   ////////////////////////////////////////////////////////////////////////////
540
// Test operation COPY_FILE //
541
////////////////////////////////////////////////////////////////////////////
542
private static _COPY_FILE COPY_FILE(int src,int dest) {
543     return new _COPY_FILE(src,dest);
544   }
545   private static class _COPY_FILE extends TestOperation {
546     private int src,dest;
547     _COPY_FILE(int src, int dest) {
548       this.src = src;
549       this.dest = dest;
550     }
551     public void operate(TestState state) throws Exception JavaDoc {
552       copyFile(getFile(state,src),getFile(state,dest));
553     }
554   }
555
556
557   ////////////////////////////////////////////////////////////////////////////
558
// Test operation CREATE_FILE //
559
////////////////////////////////////////////////////////////////////////////
560
private static _CREATE_FILE CREATE_FILE(int fi) {
561     return new _CREATE_FILE(fi);
562   }
563   private static class _CREATE_FILE extends TestOperation {
564     private int file_id;
565     public _CREATE_FILE(int file_id) {
566       this.file_id = file_id;
567     }
568     public void operate(TestState state) throws Exception JavaDoc {
569       File JavaDoc the_file = getFile(state,file_id);
570       debug("Creating "+the_file);
571      
572       FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(the_file);
573       out.close();
574     }
575   }
576
577
578   ////////////////////////////////////////////////////////////////////////////
579
// Test operation DELETE_FILE //
580
////////////////////////////////////////////////////////////////////////////
581
private static _DELETE_FILE DELETE_FILE(int fi) {
582     return new _DELETE_FILE(fi);
583   }
584   private static class _DELETE_FILE extends TestOperation {
585     private int file_id;
586     public _DELETE_FILE(int file_id) {
587       this.file_id = file_id;
588     }
589     public void operate(TestState state) throws Exception JavaDoc {
590       File JavaDoc the_file = getFile(state,file_id);
591       debug("Deleting "+the_file);
592       
593       if (!the_file.delete()) {
594         throw new Exception JavaDoc("Couldn't delete "+the_file);
595       }
596     }
597   }
598
599
600   ////////////////////////////////////////////////////////////////////////////
601
// Test operation REPEAT //
602
////////////////////////////////////////////////////////////////////////////
603
private static _REPEAT REPEAT(TestOperation o,
604                                 int n) {
605     return new _REPEAT(o,n);
606   }
607   private static class _REPEAT extends TestOperation {
608     private TestOperation the_op;
609     private int num_times;
610     public _REPEAT(TestOperation the_op,
611                    int num_times) {
612       this.the_op = the_op;
613       this.num_times = num_times;
614     }
615     public void operate(TestState state) throws Exception JavaDoc {
616       for (int i = 0; i < num_times; i++) {
617         the_op.operate(state);
618       }
619     }
620   }
621
622   ////////////////////////////////////////////////////////////////////////////
623
// Test operation SEQUENCE: Used to batch of test operations //
624
////////////////////////////////////////////////////////////////////////////
625
private static _SEQUENCE SEQUENCE(TestOperation op1,
626                                     TestOperation op2) {
627     return new _SEQUENCE(op1,op2);
628   }
629   private static _SEQUENCE SEQUENCE(TestOperation op1,
630                                     TestOperation op2,
631                                     TestOperation op3) {
632     return SEQUENCE(SEQUENCE(op1,op2),op3);
633   }
634   private static _SEQUENCE SEQUENCE(TestOperation op1,
635                                     TestOperation op2,
636                                     TestOperation op3,
637                                     TestOperation op4) {
638     return SEQUENCE(SEQUENCE(op1,op2,op3),op4);
639   }
640   private static _SEQUENCE SEQUENCE(TestOperation op1,
641                                     TestOperation op2,
642                                     TestOperation op3,
643                                     TestOperation op4,
644                                     TestOperation op5) {
645     return SEQUENCE(SEQUENCE(op1,op2,op3,op4),op5);
646   }
647   private static _SEQUENCE SEQUENCE(TestOperation op1,
648                                     TestOperation op2,
649                                     TestOperation op3,
650                                     TestOperation op4,
651                                     TestOperation op5,
652                                     TestOperation op6) {
653     return SEQUENCE(SEQUENCE(op1,op2,op3,op4,op5),op6);
654   }
655   private static _SEQUENCE SEQUENCE(TestOperation op1,
656                                     TestOperation op2,
657                                     TestOperation op3,
658                                     TestOperation op4,
659                                     TestOperation op5,
660                                     TestOperation op6,
661                                     TestOperation op7) {
662     return SEQUENCE(SEQUENCE(op1,op2,op3,op4,op5,op6),op7);
663   }
664    
665   private static _SEQUENCE SEQUENCE(TestOperation op1,
666                                     TestOperation op2,
667                                     TestOperation op3,
668                                     TestOperation op4,
669                                     TestOperation op5,
670                                     TestOperation op6,
671                                     TestOperation op7,
672                                     TestOperation op8) {
673     return SEQUENCE(SEQUENCE(op1,op2,op3,op4,op5,op6,op7),op8);
674   }
675   private static _SEQUENCE SEQUENCE(TestOperation op1,
676                                     TestOperation op2,
677                                     TestOperation op3,
678                                     TestOperation op4,
679                                     TestOperation op5,
680                                     TestOperation op6,
681                                     TestOperation op7,
682                                     TestOperation op8,
683                                     TestOperation op9) {
684     return SEQUENCE(SEQUENCE(op1,op2,op3,op4,op5,op6,op7,op8),op9);
685   }
686   private static _SEQUENCE SEQUENCE(TestOperation op1,
687                                     TestOperation op2,
688                                     TestOperation op3,
689                                     TestOperation op4,
690                                     TestOperation op5,
691                                     TestOperation op6,
692                                     TestOperation op7,
693                                     TestOperation op8,
694                                     TestOperation op9,
695                                     TestOperation op10) {
696     return SEQUENCE(SEQUENCE(op1,op2,op3,op4,op5,op6,op7,op8,op9),op10);
697   }
698   private static _SEQUENCE SEQUENCE(TestOperation op1,
699                                     TestOperation op2,
700                                     TestOperation op3,
701                                     TestOperation op4,
702                                     TestOperation op5,
703                                     TestOperation op6,
704                                     TestOperation op7,
705                                     TestOperation op8,
706                                     TestOperation op9,
707                                     TestOperation op10,
708                                     TestOperation op11) {
709     return SEQUENCE(SEQUENCE(op1,op2,op3,op4,op5,op6,op7,op8,op9,op10),op11);
710   }
711   private static _SEQUENCE SEQUENCE(TestOperation op1,
712                                     TestOperation op2,
713                                     TestOperation op3,
714                                     TestOperation op4,
715                                     TestOperation op5,
716                                     TestOperation op6,
717                                     TestOperation op7,
718                                     TestOperation op8,
719                                     TestOperation op9,
720                                     TestOperation op10,
721                                     TestOperation op11,
722                                     TestOperation op12) {
723     return SEQUENCE(SEQUENCE(op1,op2,op3,op4,op5,op6,op7,op8,op9,op10,op11),op12);
724   }
725
726   private static class _SEQUENCE extends TestOperation {
727     private TestOperation op1, op2;
728     public _SEQUENCE(TestOperation op1, TestOperation op2) {
729       this.op1 = op1;
730       this.op2 = op2;
731     }
732     public void operate(TestState state) throws Exception JavaDoc {
733       op1.operate(state);
734       op2.operate(state);
735     }
736   }
737     
738     
739
740   private static TestOperation makeTests(File JavaDoc directory) {
741         
742     TestOperation BASIC_TEST =
743       SEQUENCE(INIT(directory,
744                     10000),
745                OPEN(),
746                REPEAT(PUSH(5,5),10),
747                REPEAT(PUSH(10,10),20),
748                REPEAT(GET_NEXT(10),10),
749                REPEAT(PUSH(15,15),20),
750                REPEAT(DELETE(5),10),
751                GET_NEXT(1000),
752                DELETE(500),
753                CLOSE());
754     TestOperation LOG_FILE_TEST =
755       SEQUENCE(INIT(directory,
756                     1000000),
757                OPEN(),
758                REPEAT(PUSH(7,3),10),
759                FORCE_CP(),
760                REPEAT(GET_NEXT(10),10),
761                REPEAT(PUSH(8,0),10),
762                REPEAT(DELETE(7),10),
763                SEQUENCE(CLOSE(),
764                         OPEN(),
765                         GET_NEXT(40),
766                         DELETE(40)),
767                SEQUENCE(CLOSE(),
768                         OPEN(),
769                         GET_NEXT(40),
770                         DELETE(40)),
771                CLOSE());
772         
773     TestOperation ALL_NON_PERSISTENT_TEST =
774       SEQUENCE(INIT(directory,
775                     100000),
776                OPEN(),
777                REPEAT(PUSH(0,100),100),
778                REPEAT(GET_NEXT(100),100),
779                CLOSE());
780     TestOperation VERIFY_RECOVERS =
781       SEQUENCE(PUSH(10,0),
782                GET_NEXT(5),
783                DELETE(5),
784                CLOSE(),
785                OPEN(),
786                GET_NEXT(5),
787                DELETE(5),
788                CLOSE());
789     //start state CREATING_CP (4)
790
TestOperation START_CREATING_CP_GOOD =
791       SEQUENCE(INIT(directory,
792                     100000),
793                OPEN(),
794                PUSH(1,0),
795                FORCE_CP(),
796                CLOSE(),
797                DELETE_FILE(LOG_FILE),
798                OPEN(),
799                GET_NEXT(1),
800                DELETE(1),
801                VERIFY_RECOVERS);
802     TestOperation START_CREATING_CP_BAD =
803       SEQUENCE(INIT(directory,
804                     100000),
805                CREATE_FILE(CP_FILE),
806                OPEN(),
807                VERIFY_RECOVERS);
808     //start state READY_CP (6)
809
TestOperation START_READY_CP_BAD =
810       SEQUENCE(INIT(directory,
811                     100000),
812                OPEN(),
813                REPEAT(PUSH(2,0),3),
814                CLOSE(),
815                CORRUPT_FILE(LOG_FILE),
816                OPEN(),
817                GET_NEXT(4),
818                DELETE(4),
819                VERIFY_RECOVERS);
820     //start state CREATING_TEMP (7)
821
TestOperation START_CREATING_TEMP_GOOD =
822       SEQUENCE(INIT(directory,
823                     100000),
824                OPEN(),
825                PUSH(1,0),
826                FORCE_CP(),
827                CLOSE(),
828                COPY_FILE(CP_FILE,TEMP_FILE),
829                OPEN(),
830                GET_NEXT(1),
831                DELETE(1),
832                VERIFY_RECOVERS);
833     TestOperation START_CREATING_TEMP_BAD =
834       SEQUENCE(INIT(directory,
835                     100000),
836                OPEN(),
837                PUSH(1,0),
838                FORCE_CP(),
839                CLOSE(),
840                COPY_FILE(CP_FILE,TEMP_FILE),
841                CORRUPT_FILE(TEMP_FILE),
842                OPEN(),
843                GET_NEXT(1),
844                DELETE(1),
845                VERIFY_RECOVERS);
846     //start state TEMP_CORRECT_1 (3)
847
TestOperation START_TEMP_CORRECT_1 =
848       SEQUENCE(INIT(directory,100000),
849                OPEN(),
850                PUSH(1,0),
851                FORCE_CP(),
852                CLOSE(),
853                COPY_FILE(CP_FILE,TEMP_FILE),
854                DELETE_FILE(CP_FILE),
855                OPEN(),
856                GET_NEXT(1),
857                DELETE(1),
858                VERIFY_RECOVERS);
859     //start state TEMP_CORRECT_2 (1)
860
TestOperation START_TEMP_CORRECT_2 =
861       SEQUENCE(INIT(directory,100000),
862                OPEN(),
863                PUSH(1,0),
864                FORCE_CP(),
865                CLOSE(),
866                COPY_FILE(CP_FILE,TEMP_FILE),
867                DELETE_FILE(CP_FILE),
868                DELETE_FILE(LOG_FILE),
869                OPEN(),
870                GET_NEXT(1),
871                DELETE(1),
872                VERIFY_RECOVERS);
873     //start state COPYING_TO_CP (5)
874
TestOperation START_COPYING_TO_CP_GOOD =
875       SEQUENCE(INIT(directory,100000),
876                OPEN(),
877                PUSH(1,0),
878                FORCE_CP(),
879                CLOSE(),
880                SEQUENCE(COPY_FILE(CP_FILE,TEMP_FILE),
881                         DELETE_FILE(CP_FILE),
882                         DELETE_FILE(LOG_FILE),
883                         COPY_FILE(TEMP_FILE,CP_FILE)),
884                OPEN(),
885                GET_NEXT(1),
886                DELETE(1),
887                VERIFY_RECOVERS);
888     TestOperation START_COPYING_TO_CP_BAD =
889       SEQUENCE(INIT(directory,100000),
890                OPEN(),
891                PUSH(1,0),
892                FORCE_CP(),
893                CLOSE(),
894                SEQUENCE(COPY_FILE(CP_FILE,TEMP_FILE),
895                         DELETE_FILE(CP_FILE),
896                         DELETE_FILE(LOG_FILE),
897                         COPY_FILE(TEMP_FILE,CP_FILE),
898                         CORRUPT_FILE(CP_FILE)),
899                OPEN(),
900                GET_NEXT(1),
901                DELETE(1),
902                VERIFY_RECOVERS);
903         
904     return SEQUENCE(BASIC_TEST,
905                     LOG_FILE_TEST,
906                     ALL_NON_PERSISTENT_TEST,
907                     START_CREATING_CP_GOOD,
908                     START_CREATING_CP_BAD,
909                     START_READY_CP_BAD,
910                     START_CREATING_TEMP_GOOD,
911                     START_CREATING_TEMP_BAD,
912                     START_TEMP_CORRECT_1,
913                     START_TEMP_CORRECT_2,
914                     START_COPYING_TO_CP_GOOD,
915                     START_COPYING_TO_CP_BAD);
916   }
917     
918   public static void main(String JavaDoc [] args) throws Exception JavaDoc {
919     boolean logging = false;
920     String JavaDoc dir = null;
921     if (args.length > 1 || args[0].equals("-verbose")) {
922       DEBUG = true;
923       dir = args[1];
924     }
925     else {
926       dir = args[0];
927     }
928     TestOperation all_tests = makeTests(new File JavaDoc(dir));
929     TestState state = new TestState();
930     all_tests.operate(state);
931     debug("SUCCESS");
932   }
933 }
934
Popular Tags