ClientsFileTest debugged and verified.
[vamsas.git] / src / org / vamsas / test / simpleclient / ClientsFileTest.java
1 package org.vamsas.test.simpleclient;
2
3 import java.util.Iterator;
4 import java.util.Vector;
5
6 import org.vamsas.client.ClientHandle;
7 import org.vamsas.client.simpleclient.ClientsFile;
8
9 public class ClientsFileTest {
10   private static Vector commands;
11   static {
12     ClientsFileTest.commands=new Vector();
13     ClientsFileTest.commands.add(new String("add"));
14     ClientsFileTest.commands.add(new String("remove"));
15     ClientsFileTest.commands.add(new String("list"));    
16     ClientsFileTest.commands.add(new String("clear"));
17   }
18   private static void complainArgs(int argl, int argpos, String cmd, int argneed, String msg) {
19     if (argl-argpos<argneed)
20       throw new Error(cmd+" needs "+argneed+" arguments : "+msg);
21   }
22   public static void main(String[] args) {
23     java.io.File cf = new java.io.File(args[0]);
24     System.out.println("Connecting to clientFile "+args[0]);
25     ClientsFile cfhand;
26     try {
27       cfhand = new ClientsFile(cf);
28     }
29     catch (Exception e) {
30       e.printStackTrace(System.err);
31       return;
32     }
33     int argc=1;
34     while (argc<args.length) {
35       Iterator coms = commands.iterator();
36       int com=-1;
37       while ((coms!=null) && coms.hasNext()) {
38         com++;
39         if (args[argc].toLowerCase().equals((String) coms.next())) {
40           System.out.println("Doing "+args[argc]);
41           ClientHandle ch;
42           argc++;
43           switch (com) {
44           case 0:
45             // Add
46             ClientsFileTest.complainArgs(args.length, argc, "add", 2, "for the Client's 'Name' and 'Version'");
47             int pos = cfhand.addClient(ch=new ClientHandle(args[argc],args[argc+1]));
48             argc+=2;
49             if (pos!=0)
50               System.out.println("Client added at "+pos+" as urn:"+ch.getClientUrn());
51             else
52               System.out.println("Client was not added.");
53             break;
54           case 1:
55             // remove
56             ClientsFileTest.complainArgs(args.length, argc, "remove", 3, "for the Client's 'Name', Version and URN");
57             ch=new ClientHandle(args[argc], args[argc+1]);
58             ch.setClientUrn(args[argc+2]);
59             argc+=3;
60             cfhand.removeClient(ch);
61             System.out.println("Client removed (apparently)");
62             break;
63           case 2:
64             // list
65             ClientHandle[] chlist = cfhand.retrieveClientList();
66             if (chlist!=null) {
67               for (int chi=0,che=chlist.length; chi<che; chi++) {
68                 System.out.println("Client "+chi+" ("+chlist[chi].getClientName()+" "+chlist[chi].getVersion()+" "+chlist[chi].getClientUrn()+")");
69               }
70             } else {
71               System.out.println("Client list is empty.");
72             }
73             break;
74           case 3:
75             // clear
76             cfhand = null;
77             cf.delete();
78             try {
79               cf.createNewFile();
80               cfhand = new ClientsFile(cf);
81             }
82             catch (Exception e) {
83               System.err.println("Failed on new empty clientfile creation!");
84               e.printStackTrace(System.err);
85             }
86             break;
87           }
88           coms = null;
89         }
90       }
91       if (coms!=null) {
92         System.err.println("Unknown command : "+args[argc++] + "*Ignored!*");
93       };
94     }
95   }
96
97 }