[sword-svn] r3372 - in trunk/bindings/objc: ObjCSword.xcodeproj src test

mdbergmann at crosswire.org mdbergmann at crosswire.org
Mon Apr 20 07:55:34 MST 2015


Author: mdbergmann
Date: 2015-04-20 07:55:34 -0700 (Mon, 20 Apr 2015)
New Revision: 3372

Modified:
   trunk/bindings/objc/ObjCSword.xcodeproj/project.pbxproj
   trunk/bindings/objc/src/SwordInstallSource.mm
   trunk/bindings/objc/src/SwordInstallSourceManager.h
   trunk/bindings/objc/src/SwordInstallSourceManager.mm
   trunk/bindings/objc/src/SwordManager.mm
   trunk/bindings/objc/test/SwordInstallSourceManagerTest.m
Log:
More tests.
Improvement regarding memory management. Setting new array instances instead of cleaning instance arrays. This should ARC handling and releasing too early.

Modified: trunk/bindings/objc/ObjCSword.xcodeproj/project.pbxproj
===================================================================
--- trunk/bindings/objc/ObjCSword.xcodeproj/project.pbxproj	2015-04-14 07:15:40 UTC (rev 3371)
+++ trunk/bindings/objc/ObjCSword.xcodeproj/project.pbxproj	2015-04-20 14:55:34 UTC (rev 3372)
@@ -7558,6 +7558,7 @@
 				LD_RUNPATH_SEARCH_PATHS = "@loader_path/Frameworks";
 				LIBRARY_SEARCH_PATHS = "$(inherited)";
 				MACOSX_DEPLOYMENT_TARGET = 10.7;
+				OBJROOT = "$(PROJECT_DIR)/Build/Intermediates";
 				ONLY_ACTIVE_ARCH = YES;
 				OTHER_CFLAGS = "-DDEBUG";
 				OTHER_LDFLAGS = (
@@ -7567,6 +7568,7 @@
 				);
 				PRODUCT_NAME = ObjCSword;
 				SDKROOT = macosx;
+				SYMROOT = "$(PROJECT_DIR)/Build/Products";
 				VALID_ARCHS = x86_64;
 				WARNING_CFLAGS = "-Wall";
 				WRAPPER_EXTENSION = framework;
@@ -7601,6 +7603,7 @@
 				LD_RUNPATH_SEARCH_PATHS = "@loader_path/Frameworks";
 				LIBRARY_SEARCH_PATHS = "$(inherited)";
 				MACOSX_DEPLOYMENT_TARGET = 10.7;
+				OBJROOT = "$(PROJECT_DIR)/Build/Intermediates";
 				OTHER_CFLAGS = "-DRELEASE";
 				OTHER_LDFLAGS = (
 					"-lcurl",
@@ -7609,6 +7612,7 @@
 				);
 				PRODUCT_NAME = ObjCSword;
 				SDKROOT = macosx;
+				SYMROOT = "$(PROJECT_DIR)/Build/Products";
 				VALID_ARCHS = x86_64;
 				WARNING_CFLAGS = "-Wall";
 				WRAPPER_EXTENSION = framework;
@@ -7624,6 +7628,7 @@
 				GCC_VERSION = "";
 				GCC_WARN_ABOUT_RETURN_TYPE = YES;
 				GCC_WARN_UNUSED_VARIABLE = YES;
+				OBJROOT = Build/Intermediates;
 				ONLY_ACTIVE_ARCH = YES;
 				SDKROOT = macosx;
 				VALID_ARCHS = x86_64;
@@ -7638,6 +7643,7 @@
 				GCC_VERSION = "";
 				GCC_WARN_ABOUT_RETURN_TYPE = YES;
 				GCC_WARN_UNUSED_VARIABLE = YES;
+				OBJROOT = Build/Intermediates;
 				SDKROOT = macosx;
 				VALID_ARCHS = x86_64;
 			};
@@ -7928,6 +7934,7 @@
 				SDKROOT = macosx;
 				SWIFT_OBJC_BRIDGING_HEADER = "test/Tests-Bridging-Header.h";
 				SWIFT_OPTIMIZATION_LEVEL = "-Onone";
+				SYMROOT = "$(PROJECT_DIR)/Build/Products";
 				VALID_ARCHS = x86_64;
 			};
 			name = Debug;
@@ -7959,6 +7966,7 @@
 				PRODUCT_NAME = "$(TARGET_NAME)";
 				SDKROOT = macosx;
 				SWIFT_OBJC_BRIDGING_HEADER = "test/Tests-Bridging-Header.h";
+				SYMROOT = "$(PROJECT_DIR)/Build/Products";
 				VALID_ARCHS = x86_64;
 			};
 			name = Release;

Modified: trunk/bindings/objc/src/SwordInstallSource.mm
===================================================================
--- trunk/bindings/objc/src/SwordInstallSource.mm	2015-04-14 07:15:40 UTC (rev 3371)
+++ trunk/bindings/objc/src/SwordInstallSource.mm	2015-04-20 14:55:34 UTC (rev 3372)
@@ -155,7 +155,7 @@
     if(swordManager == nil) {
         // create SwordManager from the SWMgr of this source
         sword::SWMgr *mgr;
-        if([[self source] isEqualToString:@"localhost"]) {
+        if([self isLocalSource]) {
             // create SwordManager from new SWMgr of path
             mgr = new sword::SWMgr([[self directory] UTF8String], true, NULL, false, false);
         } else {

Modified: trunk/bindings/objc/src/SwordInstallSourceManager.h
===================================================================
--- trunk/bindings/objc/src/SwordInstallSourceManager.h	2015-04-14 07:15:40 UTC (rev 3371)
+++ trunk/bindings/objc/src/SwordInstallSourceManager.h	2015-04-20 14:55:34 UTC (rev 3372)
@@ -39,8 +39,8 @@
 
 // ------------------- properties -------------------
 /** Dictionary of InstallSources. Key: Caption */
- at property (strong, readwrite) NSMutableDictionary *installSources;
- at property (strong, readwrite) NSMutableArray *installSourceList;
+ at property (strong, readonly) NSDictionary *installSources;
+ at property (strong, readonly) NSArray *installSourceList;
 @property (strong, nonatomic) NSString *ftpUser;
 @property (strong, nonatomic) NSString *ftpPassword;
 @property (strong, readwrite) NSString *configPath;

Modified: trunk/bindings/objc/src/SwordInstallSourceManager.mm
===================================================================
--- trunk/bindings/objc/src/SwordInstallSourceManager.mm	2015-04-14 07:15:40 UTC (rev 3371)
+++ trunk/bindings/objc/src/SwordInstallSourceManager.mm	2015-04-20 14:55:34 UTC (rev 3372)
@@ -22,6 +22,9 @@
 
 @interface SwordInstallSourceManager ()
 
+ at property (strong, readwrite) NSDictionary *installSources;
+ at property (strong, readwrite) NSArray *installSourceList;
+
 @end
 
 @implementation SwordInstallSourceManager
@@ -46,8 +49,8 @@
     if(self) {
         [self setCreateConfigPath:NO];
         [self setConfigPath:nil];
-        [self setInstallSources:[NSMutableDictionary dictionary]];
-        [self setInstallSourceList:[NSMutableArray array]];
+        [self setInstallSources:[NSDictionary dictionary]];
+        [self setInstallSourceList:[NSArray array]];
         [self setFtpUser:@"ftp"];
         [self setFtpPassword:@"ObjCSword at crosswire.org"];
     }
@@ -177,19 +180,19 @@
 }
 
 - (void)setupInstallSources {
-    ALog(@"Cleaning all install sources...");
-    [[self installSources] removeAllObjects];
-    [[self installSourceList] removeAllObjects];
-    ALog(@"Cleaning all install sources...done");
-
+    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
+    NSMutableArray *arr = [NSMutableArray array];
     for(InstallSourceMap::iterator it = swInstallMgr->sources.begin(); it != swInstallMgr->sources.end(); it++) {
         sword::InstallSource *sis = it->second;
         SwordInstallSource *is = [[SwordInstallSource alloc] initWithSource:sis];
 
         ALog(@"Adding install source: %@", [is caption]);
-        [self installSources][[is caption]] = is;
-        [[self installSourceList] addObject:is];
+        dict[[is caption]] = is;
+        [arr addObject:is];
     }
+
+    [self setInstallSources:dict];
+    [self setInstallSourceList:arr];
 }
 
 - (NSString *)createInstallMgrConfPath {
@@ -198,9 +201,13 @@
 
 - (void)addInstallSource:(SwordInstallSource *)is {
     ALog(@"Adding install source: %@", [is caption]);
-    [self installSources][[is caption]] = is;
-    [[self installSourceList] addObject:is];
 
+    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[self installSources]];
+    NSMutableArray *arr = [NSMutableArray arrayWithArray:[self installSourceList]];
+
+    dict[[is caption]] = is;
+    [arr addObject:is];
+
     // modify conf file
     sword::SWConfig config([[self createInstallMgrConfPath] UTF8String]);
 	if([[is type] isEqualToString:INSTALLSOURCE_TYPE_FTP]) {
@@ -210,13 +217,17 @@
 	}
     config.Save();
 
+    [self setInstallSources:dict];
+    [self setInstallSourceList:arr];
     [self readInstallMgrConf];
 }
 
 - (void)removeInstallSource:(SwordInstallSource *)is {
     ALog(@"Removing install source: %@", [is caption]);
-    [self.installSources removeObjectForKey:[is caption]];
-    [self.installSourceList removeObject:is];
+    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[self installSources]];
+    NSMutableArray *arr = [NSMutableArray arrayWithArray:[self installSourceList]];
+    [dict removeObjectForKey:[is caption]];
+    [arr removeObject:is];
 
     // modify conf file
     sword::SWConfig config([[self createInstallMgrConfPath] UTF8String]);
@@ -233,6 +244,8 @@
     }
     config.Save();
 
+    [self setInstallSources:dict];
+    [self setInstallSourceList:arr];
     [self readInstallMgrConf];
 }
 
@@ -245,7 +258,7 @@
 // installation/unInstallation
 - (int)installModule:(SwordModule *)aModule fromSource:(SwordInstallSource *)is withManager:(SwordManager *)manager {
     int stat;
-    if([[is source] isEqualToString:@"localhost"]) {
+    if([is isLocalSource]) {
         stat = swInstallMgr->installModule([manager swManager], [[is directory] UTF8String], [[aModule name] UTF8String]);
     } else {
         stat = swInstallMgr->installModule([manager swManager], 0, [[aModule name] UTF8String], [is installSource]);

Modified: trunk/bindings/objc/src/SwordManager.mm
===================================================================
--- trunk/bindings/objc/src/SwordManager.mm	2015-04-14 07:15:40 UTC (rev 3371)
+++ trunk/bindings/objc/src/SwordManager.mm	2015-04-20 14:55:34 UTC (rev 3372)
@@ -183,20 +183,23 @@
     if(!temporaryManager) {
         delete swManager;
     }
-
 }
 
 - (void)reInit {
 	[managerLock lock];
     if(modulesPath && [modulesPath length] > 0) {
         
+        NSFileManager *fm = [NSFileManager defaultManager];
+        if(![fm fileExistsAtPath:modulesPath]) {
+            [self createModuleFolderTemplate];
+        }
+
         // modulePath is the main sw manager
         swManager = new sword::SWMgr([modulesPath UTF8String], true, new sword::EncodingFilterMgr(sword::ENC_UTF8));
 
         if(!swManager) {
             ALog(@"Cannot create SWMgr instance for default module path!");
         } else {
-            NSFileManager *fm = [NSFileManager defaultManager];
             NSArray *subDirs = [fm contentsOfDirectoryAtPath:modulesPath error:NULL];
             // for all sub directories add module
             BOOL directory;
@@ -231,6 +234,13 @@
 	[managerLock unlock];    
 }
 
+- (void)createModuleFolderTemplate {
+    NSFileManager *fm = [NSFileManager defaultManager];
+    [fm createDirectoryAtPath:modulesPath withIntermediateDirectories:NO attributes:nil error:NULL];
+    [fm createDirectoryAtPath:[modulesPath stringByAppendingPathComponent:@"mods.d"] withIntermediateDirectories:NO attributes:nil error:NULL];
+    [fm createDirectoryAtPath:[modulesPath stringByAppendingPathComponent:@"modules"] withIntermediateDirectories:NO attributes:nil error:NULL];
+}
+
 - (void)addModulesPath:(NSString *)path {
 	[managerLock lock];
 	if(swManager == nil) {
@@ -247,7 +257,7 @@
 
 - (SwordModule *)moduleWithName:(NSString *)name {
     
-	SwordModule	*ret = [modules objectForKey:[name lowercaseString]];
+	SwordModule	*ret = modules[[name lowercaseString]];
     if(ret == nil) {
         sword::SWModule *mod = [self getSWModuleWithName:name];
         if(mod == NULL) {
@@ -262,7 +272,7 @@
 
             if(ret != nil) {
                 NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:modules];
-                [dict setObject:ret forKey:[name lowercaseString]];
+                dict[[name lowercaseString]] = ret;
                 self.modules = dict;                
             }
         }        
@@ -309,7 +319,7 @@
     }
 	
     // sort
-    NSArray *sortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]];
+    NSArray *sortDescriptors = @[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]];
     [ret sortUsingDescriptors:sortDescriptors];
 
 	return [NSArray arrayWithArray:ret];
@@ -324,7 +334,7 @@
     }
     
     // sort
-    NSArray *sortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]];
+    NSArray *sortDescriptors = @[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]];
     [ret sortUsingDescriptors:sortDescriptors];
     
 	return [NSArray arrayWithArray:ret];
@@ -339,7 +349,7 @@
     }
     
     // sort
-    NSArray *sortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]];
+    NSArray *sortDescriptors = @[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]];
     [ret sortUsingDescriptors:sortDescriptors];
     
 	return [NSArray arrayWithArray:ret];    

Modified: trunk/bindings/objc/test/SwordInstallSourceManagerTest.m
===================================================================
--- trunk/bindings/objc/test/SwordInstallSourceManagerTest.m	2015-04-14 07:15:40 UTC (rev 3371)
+++ trunk/bindings/objc/test/SwordInstallSourceManagerTest.m	2015-04-20 14:55:34 UTC (rev 3372)
@@ -9,6 +9,7 @@
 #import <XCTest/XCTest.h>
 #import "SwordInstallSourceManager.h"
 #import "SwordInstallSource.h"
+#import "SwordManager.h"
 
 @interface SwordInstallSourceManagerTest : XCTestCase
 
@@ -17,10 +18,14 @@
 @implementation SwordInstallSourceManagerTest
 
 NSString *testConfigPath = @"/tmp/testmodinst";
+NSString *testModuleManagerPath = @"/tmp/testmodmgr";
+#define PROJECT_PATH [@"~/Development/MySources/crosswire/Eloquent_MacOS/trunk/sword_src/sword-trunk/bindings/objc" stringByExpandingTildeInPath]
+#define LOCAL_INSTALLSOURCE_PATH [PROJECT_PATH stringByAppendingPathComponent:@"LocalTestInstallSource"]
 
 - (void)setUp {
     NSFileManager *fm = [NSFileManager defaultManager];
     [fm removeItemAtPath:testConfigPath error:NULL];
+    [fm removeItemAtPath:testModuleManagerPath error:NULL];
 
     [super setUp];
 }
@@ -34,6 +39,81 @@
     XCTAssertTrue(mgr != nil, @"");
 }
 
+- (void)testLocalInstallSource {
+    SwordInstallSource *is = [[SwordInstallSource alloc] init];
+    [is setSource:@"localhost"];
+    [is setDirectory:LOCAL_INSTALLSOURCE_PATH];
+    [is setCaption:@"LocalTest"];
+
+    SwordInstallSourceManager *mgr = [[SwordInstallSourceManager alloc] initWithPath:testConfigPath createPath:YES];
+    [mgr initManager];
+    [mgr addInstallSource:is];
+
+    NSArray *mods = [mgr listModulesForSource:is];
+    XCTAssertTrue([mods count] == 1);
+    XCTAssertTrue([[((SwordModule *) mods[0]) name] isEqualToString:@"KJV"]);
+}
+
+- (void)testLocalInstallSourceStatusNew {
+    SwordInstallSource *is = [[SwordInstallSource alloc] init];
+    [is setSource:@"localhost"];
+    [is setDirectory:LOCAL_INSTALLSOURCE_PATH];
+    [is setCaption:@"LocalTest"];
+
+    SwordInstallSourceManager *mgr = [[SwordInstallSourceManager alloc] initWithPath:testConfigPath createPath:YES];
+    [mgr initManager];
+    [mgr addInstallSource:is];
+
+    SwordManager *swMgr = [SwordManager managerWithPath:testModuleManagerPath];
+    NSArray *stats = [mgr moduleStatusInInstallSource:is baseManager:swMgr];
+    for(SwordModule *mod in stats) {
+        NSLog(@"mod.name=%@", [mod name]);
+        NSLog(@"mod.stat=%i", [mod status]);
+    }
+
+    XCTAssertTrue([((SwordModule *) stats[0]).name isEqualToString:@"KJV"]);
+    XCTAssertTrue(((SwordModule *) stats[0]).status == ModStatNew);
+}
+
+- (void)testLocalInstallSourceStatusSame {
+    SwordInstallSource *is = [[SwordInstallSource alloc] init];
+    [is setSource:@"localhost"];
+    [is setDirectory:LOCAL_INSTALLSOURCE_PATH];
+    [is setCaption:@"LocalTest"];
+
+    SwordManager *swMgr = [SwordManager managerWithPath:testModuleManagerPath];
+    SwordInstallSourceManager *mgr = [[SwordInstallSourceManager alloc] initWithPath:testConfigPath createPath:YES];
+    [mgr initManager];
+    [mgr addInstallSource:is];
+
+    [mgr installModule:[mgr listModulesForSource:is][0] fromSource:is withManager:swMgr];
+
+    [swMgr reInit];
+    NSArray *stats = [mgr moduleStatusInInstallSource:is baseManager:swMgr];
+    for(SwordModule *mod in stats) {
+        NSLog(@"mod.name=%@", [mod name]);
+        NSLog(@"mod.stat=%i", [mod status]);
+    }
+
+    XCTAssertTrue([((SwordModule *) stats[0]).name isEqualToString:@"KJV"]);
+    XCTAssertTrue(((SwordModule *) stats[0]).status == ModStatSameVersion);
+}
+
+- (void)testInstallModuleFromLocalSource {
+    SwordInstallSource *is = [[SwordInstallSource alloc] init];
+    [is setSource:@"localhost"];
+    [is setDirectory:LOCAL_INSTALLSOURCE_PATH];
+    [is setCaption:@"LocalTest"];
+
+    SwordManager *swMgr = [SwordManager managerWithPath:testModuleManagerPath];
+    SwordInstallSourceManager *mgr = [[SwordInstallSourceManager alloc] initWithPath:testConfigPath createPath:YES];
+    [mgr initManager];
+    [mgr addInstallSource:is];
+
+    int stat = [mgr installModule:[mgr listModulesForSource:is][0] fromSource:is withManager:swMgr];
+    XCTAssertTrue(stat == 0);
+}
+
 - (void)testInitManagerCheckConfigPath {
     // make sure this folder doesn't exist at start
     NSFileManager *fm = [NSFileManager defaultManager];




More information about the sword-cvs mailing list