|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright 2016, Optimizely, Inc. and contributors * |
| 3 | + * * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); * |
| 5 | + * you may not use this file except in compliance with the License. * |
| 6 | + * You may obtain a copy of the License at * |
| 7 | + * * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 * |
| 9 | + * * |
| 10 | + * Unless required by applicable law or agreed to in writing, software * |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, * |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
| 13 | + * See the License for the specific language governing permissions and * |
| 14 | + * limitations under the License. * |
| 15 | + ***************************************************************************/ |
| 16 | + |
| 17 | +#import <XCTest/XCTest.h> |
| 18 | +#import "OPTLYFileManager.h" |
| 19 | + |
| 20 | +static NSString *const kTestFileName = @"testFileManager.txt"; |
| 21 | +static NSString *const kBadTestFileName = @"badTestFileManager.txt"; |
| 22 | +static NSString *const kTestString = @"testString"; |
| 23 | + |
| 24 | +@interface OPTLYFileManager() |
| 25 | +@property (nonatomic, strong) NSString *baseDir; |
| 26 | +- (NSString *)stringForDataTypeEnum:(OPTLYFileManagerDataType)dataType; |
| 27 | +@end |
| 28 | + |
| 29 | +@interface OPTLYFileManagerTest : XCTestCase |
| 30 | +@property (nonatomic, strong) OPTLYFileManager *fileManager; |
| 31 | +@property (nonatomic, strong) NSString *fileDir; |
| 32 | +@property (nonatomic, strong) NSString *filePath; |
| 33 | +@property (nonatomic, strong) NSData *testData; |
| 34 | +@end |
| 35 | + |
| 36 | +@implementation OPTLYFileManagerTest |
| 37 | + |
| 38 | +- (void)setUp { |
| 39 | + [super setUp]; |
| 40 | + self.fileManager = [OPTLYFileManager new]; |
| 41 | + self.testData = [kTestString dataUsingEncoding:NSUTF8StringEncoding]; |
| 42 | + self.fileDir = [self.fileManager.baseDir stringByAppendingPathComponent:[self.fileManager stringForDataTypeEnum:OPTLYFileManagerDataTypeDatafile]]; |
| 43 | + self.filePath = [self.fileDir stringByAppendingPathComponent:kTestFileName]; |
| 44 | +} |
| 45 | + |
| 46 | +- (void)tearDown { |
| 47 | + NSFileManager *fm = [NSFileManager defaultManager]; |
| 48 | + [fm removeItemAtPath:self.filePath error:nil]; |
| 49 | + self.fileManager = nil; |
| 50 | + self.testData = nil; |
| 51 | + self.fileDir = nil; |
| 52 | + self.filePath = nil; |
| 53 | + [super tearDown]; |
| 54 | +} |
| 55 | + |
| 56 | +- (void)testSaveFile { |
| 57 | + |
| 58 | + NSError *error; |
| 59 | + [self.fileManager saveFile:kTestFileName |
| 60 | + data:self.testData |
| 61 | + type:OPTLYFileManagerDataTypeDatafile |
| 62 | + error:&error]; |
| 63 | + |
| 64 | + |
| 65 | + NSFileManager *defaultFileManager= [NSFileManager defaultManager]; |
| 66 | + |
| 67 | + // check if the file exists |
| 68 | + bool fileExists = [defaultFileManager fileExistsAtPath:self.filePath]; |
| 69 | + XCTAssertTrue(fileExists, @"Saved file not found."); |
| 70 | + |
| 71 | + // check the contents of the file |
| 72 | + NSData *fileData = [NSData dataWithContentsOfFile:self.filePath options:0 error:&error]; |
| 73 | + XCTAssert(fileData != nil, @"Saved file has no content."); |
| 74 | + XCTAssert([fileData isEqualToData:self.testData], @"Invalid file content of saved file."); |
| 75 | +} |
| 76 | + |
| 77 | +- (void)testGetFile { |
| 78 | + NSError *error; |
| 79 | + [self.fileManager saveFile:kTestFileName |
| 80 | + data:self.testData |
| 81 | + type:OPTLYFileManagerDataTypeDatafile |
| 82 | + error:&error]; |
| 83 | + |
| 84 | + NSData *fileData = [self.fileManager getFile:kTestFileName |
| 85 | + type:OPTLYFileManagerDataTypeDatafile |
| 86 | + error:&error]; |
| 87 | + XCTAssert([fileData isEqualToData:self.testData], @"Invalid file content from retrieved file."); |
| 88 | + |
| 89 | + fileData = [self.fileManager getFile:kBadTestFileName |
| 90 | + type:OPTLYFileManagerDataTypeDatafile |
| 91 | + error:&error]; |
| 92 | + XCTAssert(fileData == nil, @"Bad file name. getFile should return nil."); |
| 93 | +} |
| 94 | + |
| 95 | +- (void)testFileExists { |
| 96 | + NSError *error; |
| 97 | + [self.fileManager saveFile:kTestFileName |
| 98 | + data:self.testData |
| 99 | + type:OPTLYFileManagerDataTypeDatafile |
| 100 | + error:&error]; |
| 101 | + |
| 102 | + // check that the file exists |
| 103 | + bool fileExists = [self.fileManager fileExists:kTestFileName type:OPTLYFileManagerDataTypeDatafile]; |
| 104 | + XCTAssertTrue(fileExists, @"fileExists should return true."); |
| 105 | + |
| 106 | + // check that the file does no exist for a bad file name |
| 107 | + fileExists = [self.fileManager fileExists:kBadTestFileName type:OPTLYFileManagerDataTypeDatafile]; |
| 108 | + XCTAssertFalse(fileExists, @"fileExists should return false."); |
| 109 | +} |
| 110 | + |
| 111 | +- (void)testRemoveFile { |
| 112 | + NSError *error; |
| 113 | + [self.fileManager saveFile:kTestFileName |
| 114 | + data:self.testData |
| 115 | + type:OPTLYFileManagerDataTypeDatafile |
| 116 | + error:&error]; |
| 117 | + |
| 118 | + // check that the file exists after the file save |
| 119 | + bool fileExists = [self.fileManager fileExists:kTestFileName type:OPTLYFileManagerDataTypeDatafile]; |
| 120 | + XCTAssertTrue(fileExists, @"Saved file should not exist."); |
| 121 | + |
| 122 | + // check that the file does not exist after the file removal |
| 123 | + [self.fileManager removeFile:kTestFileName type:OPTLYFileManagerDataTypeDatafile error:&error]; |
| 124 | + fileExists = [self.fileManager fileExists:kTestFileName type:OPTLYFileManagerDataTypeDatafile]; |
| 125 | + XCTAssertFalse(fileExists, @"Deleted file should not exist."); |
| 126 | +} |
| 127 | + |
| 128 | +- (void)testRemoveAllFiles |
| 129 | +{ |
| 130 | + [self.fileManager saveFile:kTestFileName |
| 131 | + data:self.testData |
| 132 | + type:OPTLYFileManagerDataTypeDatafile |
| 133 | + error:nil]; |
| 134 | + [self.fileManager saveFile:kTestFileName |
| 135 | + data:self.testData |
| 136 | + type:OPTLYFileManagerDataTypeUserProfile |
| 137 | + error:nil]; |
| 138 | + [self.fileManager saveFile:kTestFileName |
| 139 | + data:self.testData |
| 140 | + type:OPTLYFileManagerDataTypeEventDispatcher |
| 141 | + error:nil]; |
| 142 | + [self.fileManager removeAllFiles:nil]; |
| 143 | + |
| 144 | + bool isDir = true; |
| 145 | + NSFileManager *defaultFileManager= [NSFileManager defaultManager]; |
| 146 | + bool optlyDir = [defaultFileManager fileExistsAtPath:self.fileManager.baseDir isDirectory:&isDir]; |
| 147 | + XCTAssertFalse(optlyDir, @"Optimizely file folder should not exist."); |
| 148 | +} |
| 149 | + |
| 150 | +@end |
0 commit comments