1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.mpt;
21
22 import java.io.Reader;
23 import java.io.StringReader;
24
25
26
27
28
29
30 public class ScriptedUserAdder implements UserAdder {
31
32 private static final String SCRIPT_NAME = "Add User Script";
33 private static final String PASSWORD_VARIABLE_NAME = "password";
34 private static final String USER_VARIABLE_NAME = "user";
35
36 private final String host;
37 private final int port;
38 private final String script;
39 private final Monitor monitor;
40
41
42
43
44
45
46
47 public ScriptedUserAdder(final String host, final int port)
48 {
49 this(host, port, (String) null);
50 }
51
52 public ScriptedUserAdder(final String host, final int port, final String script) {
53 this(host, port, script, new NullMonitor());
54 }
55
56
57
58
59
60
61
62 public ScriptedUserAdder(final String host, final int port, final Monitor monitor) {
63 this(host, port, null, monitor);
64 }
65
66 public ScriptedUserAdder(final String host, final int port, final String script, final Monitor monitor) {
67 this.host = host;
68 this.port = port;
69 this.script = script;
70 this.monitor = monitor;
71 }
72
73
74
75
76
77
78
79
80 public void addUser(final String user, final String password) throws Exception {
81 final StringReader reader = new StringReader(script);
82 addUser(user, password, reader);
83 }
84
85
86
87
88
89
90
91
92 public void addUser(final String user, final String password, final Reader reader) throws Exception {
93 final ProtocolSessionBuilder builder = new ProtocolSessionBuilder();
94 builder.setVariable(USER_VARIABLE_NAME, user);
95 builder.setVariable(PASSWORD_VARIABLE_NAME, password);
96
97 final Runner runner = new Runner();
98 builder.addProtocolLines(SCRIPT_NAME, reader, runner.getTestElements());
99 final ExternalSessionFactory factory = new ExternalSessionFactory(host, port, monitor, null);
100 runner.runSessions(factory);
101 }
102
103
104
105
106
107
108
109
110 public String toString()
111 {
112 final String TAB = " ";
113
114 String result = "ScriptedUserAdder ( "
115 + super.toString() + TAB
116 + "host = " + this.host + TAB
117 + "port = " + this.port + TAB
118 + "script = " + this.script + TAB
119 + "monitor = " + this.monitor + TAB
120 + " )";
121
122 return result;
123 }
124
125
126 }