KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > test > tools > SimplePostSyncML


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  *This program 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  * This program 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 this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.test.tools;
20
21 import java.net.*;
22 import java.io.*;
23 import java.util.List JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import java.util.logging.Level JavaDoc;
27
28 import sync4j.framework.core.*;
29 import sync4j.framework.logging.Sync4jLogger;
30 import sync4j.framework.protocol.*;
31 import sync4j.framework.tools.IOTools;
32
33 import sync4j.test.TestFailedException;
34 import sync4j.test.tools.HttpClientConnection;
35
36
37 /**
38  * This class post a one or more SyncML messages to the given URL writing the
39  * response in the same directory of the request message.
40  *
41  * @author Stefano Fornari @ Funambol
42  * @version $Id: SimplePostSyncML.java,v 1.3 2005/03/02 20:57:40 harrie Exp $
43  */

44 public class SimplePostSyncML {
45
46     // --------------------------------------------------------------- Constants
47

48     public static String JavaDoc LOG_NAME = "test";
49
50     // ------------------------------------------------------------ Private data
51

52     private String JavaDoc nextURL = null;
53     private String JavaDoc[] msgs = null;
54     private String JavaDoc[] msgFiles = null;
55
56     private static final Logger JavaDoc log = Sync4jLogger.getLogger(LOG_NAME);
57
58     // ------------------------------------------------------------ Constructors
59

60     public SimplePostSyncML(String JavaDoc initialURL ,
61                             String JavaDoc[] msgFiles )
62     throws IOException {
63         if ((msgFiles == null) || (msgFiles.length == 0)) {
64             msgs = new String JavaDoc[0];
65         }
66
67         msgs = new String JavaDoc[msgFiles.length];
68
69         this.msgFiles = msgFiles;
70         for (int i=0; i<msgFiles.length; ++i) {
71             msgs[i] = IOTools.readFileString(new File(msgFiles[i]));
72         }
73
74         nextURL = initialURL;
75     }
76
77     //----------------------------------------------------------- Public methods
78

79     public void start() throws IOException, TestFailedException {
80         SyncML response = null;
81         String JavaDoc respURI = null;
82
83         File responseFile = null;
84         for (int i=0; i<msgs.length; ++i) {
85
86             log.info("Sending " + msgFiles[i]);
87
88             try {
89                 response = postRequest(msgs[i]);
90             } catch (RepresentationException e) {
91                 throw new TestFailedException ("XML syntax error: " + e.getMessage(), e);
92             } catch (Sync4jException e) {
93                 throw new TestFailedException ("XML syntax error: " + e.getMessage(), e);
94             }
95
96             //
97
// Write the messages responded by the server, than read the reference
98
// and make the comparison (excluding the XPaths specified by
99
// ignoreXPaths
100
//
101
responseFile = new File(msgFiles[i] + ".out");
102             log.info("Writing the response into " + responseFile);
103
104             try {
105
106                 IOTools.writeFile(Util.toXML(response), responseFile);
107
108             } catch(Exception JavaDoc e) {
109                 throw new TestFailedException ("XML syntax error: " + e.getMessage(), e);
110             }
111
112             respURI = response.getSyncHdr().getRespURI();
113
114             if (respURI != null) {
115                 nextURL = respURI;
116             }
117         }
118     }
119
120     // --------------------------------------------------------- Private methods
121

122     private SyncML postRequest(String JavaDoc request)
123     throws IOException, Sync4jException, RepresentationException {
124         HttpClientConnection syncMLConnection = new HttpClientConnection(nextURL);
125         return syncMLConnection.sendMessage(request);
126     }
127
128     private static void syntax() {
129         System.out.println("Syntax: " + PostSyncML.class.getName() + " <initial URL> <msg1> ... <msgN>");
130     }
131
132     // -------------------------------------------------------------------- Main
133

134     public static void main(String JavaDoc args[])
135     throws Exception JavaDoc {
136         if(args.length < 2) {
137             syntax();
138             return;
139         }
140
141         String JavaDoc[] msgFiles = new String JavaDoc[args.length-1];
142
143         System.arraycopy(args, 1, msgFiles, 0, msgFiles.length);
144
145         SimplePostSyncML postsyncml = new SimplePostSyncML(args[0], msgFiles);
146         postsyncml.start();
147     }
148 }
149
Popular Tags