Introduced method variants with NSError handling; simplified exception creation

This commit is contained in:
Gianluca Bertani
2015-08-28 19:43:47 +02:00
parent 1a91d92c51
commit faa7e4d897
13 changed files with 495 additions and 182 deletions
+1
View File
@@ -44,6 +44,7 @@
- (IBAction) zipUnzip2;
- (IBAction) zipCheck1;
- (IBAction) zipCheck2;
- (IBAction) errorWrapping;
@end
+78 -11
View File
@@ -48,6 +48,7 @@
- (void) test2;
- (void) test3;
- (void) test4;
- (void) test5;
#pragma mark -
@@ -97,6 +98,12 @@
[_testThread start];
}
- (IBAction) errorWrapping {
_testThread= [[NSThread alloc] initWithTarget:self selector:@selector(test5) object:nil];
[_testThread start];
}
#pragma mark -
#pragma mark Test 1: zip & unzip
@@ -156,14 +163,14 @@
[self log:@"Test 1: - %@ %@ %d (%d)", info.name, info.date, info.size, info.level];
[self log:@"Test 1: opening first file..."];
[unzipFile goToFirstFileInZip];
[unzipFile goToFirstFileInZip];
OZZipReadStream *read1= [unzipFile readCurrentFileInZip];
[self log:@"Test 1: reading from first file's stream..."];
NSMutableData *data1= [[NSMutableData alloc] initWithLength:256];
int bytesRead1= [read1 readDataWithBuffer:data1];
NSUInteger bytesRead1= [read1 readDataWithBuffer:data1];
BOOL ok= NO;
if (bytesRead1 == 3) {
@@ -189,7 +196,7 @@
[self log:@"Test 1: reading from second file's stream..."];
NSMutableData *data2= [[NSMutableData alloc] initWithLength:256];
int bytesRead2= [read2 readDataWithBuffer:data2];
NSUInteger bytesRead2= [read2 readDataWithBuffer:data2];
ok= NO;
if (bytesRead2 == 3) {
@@ -216,7 +223,7 @@
} @catch (OZZipException *ze) {
[self log:@"Test 1: caught a ZipException (see logs), terminating..."];
NSLog(@"Test 1: ZipException caught: %d - %@", ze.error, [ze reason]);
NSLog(@"Test 1: ZipException caught: %ld - %@", (long) ze.error, [ze reason]);
} @catch (id e) {
[self log:@"Test 1: caught a generic exception (see logs), terminating..."];
@@ -286,7 +293,7 @@
[self log:@"Test 2: reading from file's stream..."];
for (int i= 0; i < HUGE_TEST_NUMBER_OF_BLOCKS; i++) {
int bytesRead= [read readDataWithBuffer:buffer];
NSUInteger bytesRead= [read readDataWithBuffer:buffer];
BOOL ok= NO;
if (bytesRead == [data length]) {
@@ -315,7 +322,7 @@
} @catch (OZZipException *ze) {
[self log:@"Test 2: caught a ZipException (see logs), terminating..."];
NSLog(@"Test 2: ZipException caught: %d - %@", ze.error, [ze reason]);
NSLog(@"Test 2: ZipException caught: %ld - %@", (long) ze.error, [ze reason]);
} @catch (id e) {
[self log:@"Test 2: caught a generic exception (see logs), terminating..."];
@@ -352,7 +359,7 @@
NSMutableData *buffer= [[NSMutableData alloc] initWithLength:1024];
int bytesRead= [read readDataWithBuffer:buffer];
NSUInteger bytesRead= [read readDataWithBuffer:buffer];
NSString *fileText= [[NSString alloc] initWithBytes:[buffer bytes] length:bytesRead encoding:NSUTF8StringEncoding];
if ([fileText isEqualToString:@"Objective-Zip Mac test file\n"])
@@ -373,7 +380,7 @@
} @catch (OZZipException *ze) {
[self log:@"Test 3: caught a ZipException (see logs), terminating..."];
NSLog(@"Test 3: ZipException caught: %d - %@", ze.error, [ze reason]);
NSLog(@"Test 3: ZipException caught: %ld - %@", (long) ze.error, [ze reason]);
} @catch (id e) {
[self log:@"Test 3: caught a generic exception (see logs), terminating..."];
@@ -406,7 +413,7 @@
NSMutableData *buffer= [[NSMutableData alloc] initWithLength:1024];
int bytesRead= [read readDataWithBuffer:buffer];
NSUInteger bytesRead= [read readDataWithBuffer:buffer];
NSString *fileText= [[NSString alloc] initWithBytes:[buffer bytes] length:bytesRead encoding:NSUTF8StringEncoding];
if ([fileText isEqualToString:@"Objective-Zip Windows test file\r\n"])
@@ -427,7 +434,7 @@
} @catch (OZZipException *ze) {
[self log:@"Test 4: caught a ZipException (see logs), terminating..."];
NSLog(@"Test 4: ZipException caught: %d - %@", ze.error, [ze reason]);
NSLog(@"Test 4: ZipException caught: %ld - %@", (long) ze.error, [ze reason]);
} @catch (id e) {
[self log:@"Test 4: caught a generic exception (see logs), terminating..."];
@@ -437,6 +444,66 @@
}
}
#pragma mark -
#pragma mark Test 5: error wrapping
- (void) test5 {
@autoreleasepool {
NSString *filePath= @"/root.zip";
@try {
[self log:@"Test 5: opening impossible zip file for writing..."];
OZZipFile *zipFile= [[OZZipFile alloc] initWithFileName:filePath mode:OZZipFileModeCreate];
[self log:@"Test 5: test failed, no error reported"];
[zipFile close];
} @catch (OZZipException *ze) {
if (ze.error != ERROR_NO_SUCH_FILE) {
[self log:@"Test 5: test failed, wrong error reported"];
} else {
[self log:@"Test 5: correct error reported"];
}
} @catch (id e) {
[self log:@"Test 5: caught a generic exception (see logs)"];
NSLog(@"Test 5: Exception caught: %@ - %@", [[e class] description], [e description]);
}
@try {
[self log:@"Test 5: opening again impossible zip file for writing..."];
NSError *error= nil;
OZZipFile *zipFile= [[OZZipFile alloc] initWithFileName:filePath mode:OZZipFileModeCreate error:&error];
if (zipFile || (!error)) {
[self log:@"Test 5: test failed, no error reported"];
[zipFile close];
} else {
if (error.code != ERROR_NO_SUCH_FILE) {
[self log:@"Test 5: test failed, wrong error reported"];
} else {
[self log:@"Test 5: correct error reported"];
[self log:@"Test 5: test terminated succesfully"];
}
}
} @catch (id e) {
[self log:@"Test 5: caught a generic exception (see logs), terminating..."];
NSLog(@"Test 5: Exception caught: %@ - %@", [[e class] description], [e description]);
}
}
}
#pragma mark -
#pragma mark Logging