forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCase.java
More file actions
365 lines (283 loc) · 10.5 KB
/
Copy pathTestCase.java
File metadata and controls
365 lines (283 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// TestCase.java
/**
* Copyright (C) 2008 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.util;
import com.mongodb.BasicDBObject;
import com.mongodb.CommandResult;
import com.mongodb.Mongo;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class TestCase extends MyAsserts {
static class Test {
Test( Object o , Method m ){
_o = o;
_m = m;
}
Result run(){
try {
_m.invoke( _o );
return new Result( this );
}
catch ( IllegalAccessException e ){
return new Result( this , e );
}
catch ( InvocationTargetException ite ){
return new Result( this , ite.getTargetException() );
}
}
public String toString(){
String foo = _o.getClass().getName() + "." + _m.getName();
if ( _name == null )
return foo;
return _name + "(" + foo + ")";
}
protected String _name = null;
final Object _o;
final Method _m;
}
static class Result {
Result( Test t ){
this( t , null );
}
Result( Test t , Throwable error ){
_test = t;
_error = error;
}
boolean ok(){
return _error == null;
}
public String toString(){
StringBuilder buf = new StringBuilder();
buf.append( _test );
Throwable error = _error;
while ( error != null ){
buf.append( "\n\t" + error + "\n" );
for ( StackTraceElement ste : error.getStackTrace() ){
buf.append( "\t\t" + ste + "\n" );
}
error = error.getCause();
}
return buf.toString();
}
final Test _test;
final Throwable _error;
}
/**
* this is for normal class tests
*/
public TestCase(){
this( null );
}
public TestCase( String name ){
for ( Method m : getClass().getMethods() ){
if ( ! m.getName().startsWith( "test" ) )
continue;
if ( ( m.getModifiers() & Member.PUBLIC ) > 0 )
continue;
Test t = new Test( this , m );
t._name = name;
_tests.add( t );
}
}
public TestCase( Object o , String m )
throws NoSuchMethodException {
this( o , o.getClass().getDeclaredMethod( m ) );
}
public TestCase( Object o , Method m ){
_tests.add( new Test( o , m ) );
}
public void add( TestCase tc ){
_tests.addAll( tc._tests );
}
public String cleanupDB = null;
public Mongo cleanupMongo = null;
@org.testng.annotations.AfterClass
public void cleanup() {
if (cleanupMongo != null) {
if (cleanupDB != null) {
cleanupMongo.dropDatabase(cleanupDB);
}
cleanupMongo.close();
}
}
/**
* @return true if everything succeeds
*/
public boolean runConsole(){
List<Result> errors = new ArrayList<Result>();
List<Result> fails = new ArrayList<Result>();
System.out.println( "Num Tests : " + _tests.size() );
System.out.println( "----" );
for ( Test t : _tests ){
Result r = t.run();
if ( r.ok() ){
System.out.print(".");
continue;
}
System.out.print( "x" );
if ( r._error instanceof MyAssert )
fails.add( r );
else
errors.add( r );
}
cleanup();
System.out.println( "\n----" );
int pass = _tests.size() - ( errors.size() + fails.size() );
System.out.println( "Passes : " + pass + " / " + _tests.size() );
System.out.println( "% Pass : " + ( ((double)pass*100) / _tests.size() ) );
if ( pass == _tests.size() ){
System.out.println( "SUCCESS" );
return true;
}
System.err.println( "Num Pass : " + ( _tests.size() - ( errors.size() + fails.size() ) ) );
System.err.println( "Num Erros : " + ( errors.size() ) );
System.err.println( "Num Fails : " + ( fails.size() ) );
System.err.println( "---------" );
System.err.println( "ERRORS" );
for ( Result r : errors )
System.err.println( r );
System.err.println( "---------" );
System.err.println( "FAILS" );
for ( Result r : fails )
System.err.println( r );
return false;
}
public String toString(){
return "TestCase numCase:" + _tests.size();
}
final List<Test> _tests = new ArrayList<Test>();
protected static void run( String args[] ){
Args a = new Args( args );
boolean foundMe = false;
String theClass = null;
for ( StackTraceElement ste : Thread.currentThread().getStackTrace() ){
if ( foundMe ){
theClass = ste.getClassName();
break;
}
if ( ste.getClassName().equals( "com.mongodb.util.TestCase" ) &&
ste.getMethodName().equals( "run" ) )
foundMe = true;
}
if ( theClass == null )
throw new RuntimeException( "something is broken" );
try {
TestCase tc = (TestCase) Class.forName( theClass ).newInstance();
if ( a.getOption( "m" ) != null )
tc = new TestCase( tc , a.getOption( "m" ) );
tc.runConsole();
}
catch ( Exception e ){
throw new RuntimeException( e );
}
}
/**
*
* @param version must be a major version, e.g. 1.8, 2,0, 2.2
* @return true if server is at least specified version
*/
protected boolean serverIsAtLeastVersion(double version) {
String serverVersion = (String) cleanupMongo.getDB("admin").command("serverStatus").get("version");
return Double.parseDouble(serverVersion.substring(0, 3)) >= version;
}
/**
*
* @param mongo the connection
* @return true if connected to a standalone server
*/
protected boolean isStandalone(Mongo mongo) {
return runReplicaSetStatusCommand(mongo) == null;
}
@SuppressWarnings({"unchecked"})
protected String getPrimaryAsString(Mongo mongo) {
return getMemberNameByState(mongo, "primary");
}
@SuppressWarnings({"unchecked"})
protected String getASecondaryAsString(Mongo mongo) {
return getMemberNameByState(mongo, "secondary");
}
@SuppressWarnings({"unchecked"})
protected String getMemberNameByState(Mongo mongo, String stateStrToMatch) {
CommandResult replicaSetStatus = runReplicaSetStatusCommand(mongo);
for (final BasicDBObject member : (List<BasicDBObject>) replicaSetStatus.get("members")) {
String hostnameAndPort = member.getString("name");
if (!hostnameAndPort.contains(":"))
hostnameAndPort = hostnameAndPort + ":27017";
final String stateStr = member.getString("stateStr");
if (stateStr.equalsIgnoreCase(stateStrToMatch))
return hostnameAndPort;
}
throw new IllegalStateException("No member found in state " + stateStrToMatch);
}
@SuppressWarnings("unchecked")
protected int getReplicaSetSize(Mongo mongo) {
int size = 0;
CommandResult replicaSetStatus = runReplicaSetStatusCommand(mongo);
for (final BasicDBObject member : (List<BasicDBObject>) replicaSetStatus.get("members")) {
final String stateStr = member.getString("stateStr");
if (stateStr.equals("PRIMARY") || stateStr.equals("SECONDARY"))
size++;
}
return size;
}
protected CommandResult runReplicaSetStatusCommand(final Mongo pMongo) {
// Check to see if this is a replica set... if not, get out of here.
final CommandResult result = pMongo.getDB("admin").command(new BasicDBObject("replSetGetStatus", 1));
final String errorMsg = result.getErrorMessage();
if (errorMsg != null && errorMsg.indexOf("--replSet") != -1) {
System.err.println("---- SecondaryReadTest: This is not a replica set - not testing secondary reads");
return null;
}
return result;
}
public static void main( String args[] )
throws Exception {
String dir = "src/test";
if ( args != null && args.length > 0 )
dir = args[0];
Process p = Runtime.getRuntime().exec( "find " + dir );
BufferedReader in = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
TestCase theTestCase = new TestCase();
String line;
while ( ( line = in.readLine() ) != null ){
if ( ! line.endsWith( "Test.java" ) ) {
continue;
}
line = line.substring( "src/test/".length() );
line = line.substring( 0 , line.length() - ".java".length() );
line = line.replaceAll( "//+" , "/" );
line = line.replace( '/' , '.' );
try {
Class c = Class.forName( line );
Object thing = c.newInstance();
if ( ! ( thing instanceof TestCase ) )
continue;
System.out.println( line );
TestCase tc = (TestCase)thing;
theTestCase._tests.addAll( tc._tests );
}
catch ( Exception e ){
e.printStackTrace();
}
}
theTestCase.runConsole();
}
}