Permalink
Browse files

Support for CouchCocoa tests.

...and a bunch of other stuff.
  • Loading branch information...
1 parent b623966 commit d6f1ff64bab7b122053965ce5d814e298908c89a @snej snej committed Oct 5, 2011
View
@@ -16,8 +16,10 @@
@property (strong, nonatomic) IBOutlet UIWindow *window;
@property (strong, nonatomic) IBOutlet UINavigationController *navController;
-/** The database this app is using; observable.
- (The unit tests access this property; see CouchTestCase.m.) */
-@property (readonly, retain, nonatomic) CouchDatabase* database;
+/** The URL of the Couchbase server. */
+@property (readonly, retain, nonatomic) NSURL* serverURL;
@end
+
+
+extern NSString* const AppDelegateCouchRestartedNotification;
View
@@ -7,7 +7,9 @@
//
#import "AppDelegate.h"
-#import <CouchCocoa/CouchCocoa.h>
+
+
+NSString* const AppDelegateCouchRestartedNotification = @"AppDelegateCouchRestarted";
/** This is the name of the database the app will use -- customize it as you like,
@@ -16,19 +18,19 @@
@interface AppDelegate ()
-@property (readwrite, retain, nonatomic) CouchDatabase* database; // settable internally
+@property (readwrite, retain, nonatomic) NSURL* serverURL; // settable internally
@end
@implementation AppDelegate
@synthesize window = _window, navController = _navController;
-@synthesize database = _database;
+@synthesize serverURL = _serverURL;
- (void)dealloc
{
[_window release];
- [_database release];
+ [_serverURL release];
[super dealloc];
}
@@ -61,29 +63,14 @@ - (BOOL)application:(UIApplication *)application
-(void)couchbaseMobile:(CouchbaseMobile*)couchbase didStart:(NSURL*)serverURL
{
gCouchLogLevel = 1; // You can increase this to 2 (or even 3, which is overkill)
- gRESTLogLevel = kRESTLogNothing; // You can increase this to kRESTLogRequestURLs or higher
-
- if (!self.database) {
- // Do this on launch, but not when returning to the foreground:
- CouchServer* server = [[CouchServer alloc] initWithURL:serverURL];
- // Track active operations so we can wait for their completion in didEnterBackground, below
- server.tracksActiveOperations = YES;
- CouchDatabase* database = [server databaseNamed:kDatabaseName];
-
- // Create the database on the first run of the app.
- if (![[database GET] wait]) {
- [[database create] wait];
- }
-
- self.database = database;
- [server release];
- }
-
- // For most purposes you will want to track changes.
- self.database.tracksChanges = YES;
+ gRESTLogLevel = kRESTLogRequestURLs; // You can increase this to kRESTLogRequestURLs or higher
- NSLog(@"Couchbase is ready, go!");
- // TODO: Now that the database is ready, add your setup code here.
+ if (!self.serverURL)
+ self.serverURL = serverURL;
+ else
+ [[NSNotificationCenter defaultCenter]
+ postNotificationName: AppDelegateCouchRestartedNotification
+ object: self];
}
-(void)couchbaseMobile:(CouchbaseMobile*)couchbase failedToStart:(NSError*)error
@@ -92,22 +79,4 @@ -(void)couchbaseMobile:(CouchbaseMobile*)couchbase failedToStart:(NSError*)error
NSAssert(NO, @"Couchbase failed to initialize: %@", error);
}
-- (void)applicationDidEnterBackground:(UIApplication *)application
-{
- NSLog(@"------ applicationDidEnterBackground");
- // Turn off the _changes watcher:
- self.database.tracksChanges = NO;
-
- // Make sure all transactions complete, because going into the background will
- // close down the CouchDB server:
- [RESTOperation wait: self.database.server.activeOperations];
-}
-
-- (void)applicationWillEnterForeground:(UIApplication *)application
-{
- NSLog(@"------ applicationWillEnterForeground");
- // Don't reconnect to the server yet ... wait for it to tell us it's back up,
- // by calling couchbaseMobile:didStart: again.
-}
-
@end
View
@@ -0,0 +1,41 @@
+//
+// BeeCouchTest.h
+// Worker Bee
+//
+// Created by Jens Alfke on 10/5/11.
+// Copyright (c) 2011 Couchbase, Inc. All rights reserved.
+//
+
+#import "BeeTest.h"
+#import <CouchCocoa/CouchCocoa.h>
+
+
+@interface BeeCouchTest : BeeTest
+
+/** The embedded Couchbase server. */
+@property (readonly) CouchServer* server;
+
+/** A database to use for your test.
+ The database will exist but be empty at the start of the test. */
+@property (readonly) CouchDatabase* database;
+
+/** The URL of the embedded Couchbase server.
+ You can access this directly, instead of the .server property, if you don't want to use a CouchServer object. */
+@property (readonly) NSURL* serverURL;
+
+/** The name of the database to create.
+ Defaults to the class name lowercased with "-db" appended.
+ You can override this method to use a different name. */
+@property (readonly) NSString* databaseName;
+
+@property (readonly) BOOL suspended;
+
+/** Called when the application enters the background.
+ You can override this to do extra work, but call the inherited method *at the end*. */
+- (void)serverWillSuspend;
+
+/** Called when the server resumes after the app returns to the foreground.
+ You can override this to do extra work, but call the inherited method first. */
+- (void) serverDidResume;
+
+@end
View
@@ -0,0 +1,148 @@
+//
+// BeeCouchTest.m
+// Worker Bee
+//
+// Created by Jens Alfke on 10/5/11.
+// Copyright (c) 2011 Couchbase, Inc. All rights reserved.
+//
+
+#import "BeeCouchTest.h"
+#import "AppDelegate.h"
+
+
+@interface BeeCouchTest ()
+@property (readwrite) BOOL suspended;
+@end
+
+
+@implementation BeeCouchTest
+{
+ CouchServer* _server;
+ CouchDatabase* _database;
+ BOOL _createdDatabase;
+}
+
+
++ (BOOL) isAbstractTest {
+ return self == [BeeCouchTest class];
+}
+
+
+- (void)dealloc {
+ [_server release];
+ [super dealloc];
+}
+
+
+- (NSURL*) serverURL {
+ return ((AppDelegate*)[[UIApplication sharedApplication] delegate]).serverURL;
+}
+
+
+- (NSString*) databaseName {
+ return [[NSStringFromClass([self class]) lowercaseString] stringByAppendingString: @"-db"];
+}
+
+
+- (CouchServer*) server {
+ if (!_server) {
+ _server = [[CouchServer alloc] initWithURL: self.serverURL];
+ // Track active operations so we can wait for their completion in didEnterBackground, below
+ _server.tracksActiveOperations = YES;
+ }
+ return _server;
+}
+
+
+- (CouchDatabase*) database {
+ if (self.suspended)
+ [self logMessage: @"WARNING: Accessing database while suspended"];
+ if (!_createdDatabase) {
+ _createdDatabase = YES;
+ CouchDatabase* database = [self.server databaseNamed: self.databaseName];
+ NSAssert(database, @"Failed to create CouchDatabase object");
+ // Delete and re-create the database:
+ RESTOperation* op = [database DELETE];
+ if ([op wait] || op.httpStatus == 404) {
+ op = [database create];
+ [op wait];
+ }
+ if (op.error) {
+ self.error = op.error;
+ return nil;
+ }
+ database.tracksChanges = YES;
+ _database = [database retain];
+ }
+ return _database;
+}
+
+
+- (void) setUp {
+ [super setUp];
+
+ _createdDatabase = NO;
+ NSNotificationCenter* nctr = [NSNotificationCenter defaultCenter];
+ [nctr addObserver: self
+ selector: @selector(serverDidResume)
+ name: AppDelegateCouchRestartedNotification
+ object: nil];
+}
+
+- (void) tearDown {
+ NSNotificationCenter* nctr = [NSNotificationCenter defaultCenter];
+ [nctr removeObserver: self
+ name:AppDelegateCouchRestartedNotification
+ object: nil];
+ [_database release];
+ _database = nil;
+ [_server release];
+ _server = nil;
+ self.suspended = NO;
+
+ [super tearDown];
+}
+
+
+@synthesize suspended = _suspended;
+
+
+- (void)applicationDidEnterBackground: (NSNotification*)notification
+{
+ [self serverWillSuspend];
+}
+
+
+- (void)applicationWillEnterForeground: (NSNotification*)notification
+{
+ [super applicationWillEnterForeground: notification];
+ // Wait for notification that Couchbase server has restarted
+ self.status = @"Waiting for server to resume...";
+}
+
+
+- (void)serverWillSuspend
+{
+ [self logMessage: @"Suspending"];
+
+ self.suspended = YES;
+ self.status = @"Server suspended";
+
+ // Turn off the _changes watcher:
+ _database.tracksChanges = NO;
+
+ // Make sure all transactions complete, because going into the background will
+ // close down the CouchDB server:
+ [RESTOperation wait: _server.activeOperations];
+}
+
+
+- (void) serverDidResume {
+ [self logMessage: @"Server resumed"];
+ _database.tracksChanges = YES;
+ self.suspended = NO;
+ self.status = @"Resumed";
+}
+
+
+@end
View
@@ -28,12 +28,28 @@
@property (assign) id<BeeTestDelegate> delegate;
@property BOOL running;
-@property (readonly) NSError* error;
+@property (copy) NSString* status;
+@property (copy) NSError* error;
+@property (copy) NSString* errorMessage;
@property (readonly) NSArray* messages;
+@property (readonly, retain) NSDate* startTime;
+@property (readonly, retain) NSDate* endTime;
+
+- (void) clearMessages;
+
+#pragma mark For subclasses to override:
+
+- (void) setUp;
+- (void) tearDown;
+
+- (void)applicationDidEnterBackground: (NSNotification*)notification;
+- (void)applicationWillEnterForeground: (NSNotification*)notification;
+
+#pragma mark For subclasses to call:
+
- (void) logMessage: (NSString*)message;
- (void) logFormat: (NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
- (BOOL) addTimestamp: (NSString*)message;
-- (void) clearMessages;
@end
Oops, something went wrong.

0 comments on commit d6f1ff6

Please sign in to comment.