/
ManagedObjectSenTestCase.m
106 lines (80 loc) · 2.84 KB
/
ManagedObjectSenTestCase.m
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
#import "ManagedObjectSenTestCase.h"
#import "LPManagedObjectContext.h"
@implementation ManagedObjectSenTestCase
@synthesize coordinator;
@synthesize context;
@synthesize model;
-(void) setUp
{
@try
{
pool = [[NSAutoreleasePool alloc] init];
NSBundle* bundle = [NSBundle bundleWithIdentifier:@"eu.brugger.martin.CDDP"];
//NSLog(@"bundle: %@", bundle);
NSString* path = [bundle pathForResource:@"CoreDataDependentPropertiesDataModel" ofType:@"mom"];
//NSLog(@"path: %@", path);
NSURL* modelURL = [NSURL URLWithString:path];
self.model = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] autorelease];
self.coordinator = [[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.model] autorelease];
NSLog(@"create persistent store");
// create persistent store
NSString* tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"modeltest"];
NSFileManager * fileManager = [NSFileManager defaultManager];
NSError* error = nil;
if (![fileManager fileExistsAtPath:tempFilePath])
{
STAssertTrue([fileManager createDirectoryAtPath:tempFilePath withIntermediateDirectories: YES attributes:nil error:&error], @"error creating temp dir: %@", error);
}
NSString *persistentStoreFileString = [tempFilePath stringByAppendingPathComponent:@"test.sqlite"];
if ([fileManager fileExistsAtPath:persistentStoreFileString])
{
[fileManager removeItemAtPath:persistentStoreFileString error:&error];
}
[self.coordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:[NSURL fileURLWithPath:persistentStoreFileString]
options:nil
error:&error];
NSLog(@"create context");
LPManagedObjectContext* tempContext = [[[LPManagedObjectContext alloc] init] autorelease];
[tempContext setPersistentStoreCoordinator:coordinator];
[tempContext prepareDependentProperties];
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
tempContext.undoManager = [[[NSUndoManager alloc] init] autorelease];
#endif
self.context = tempContext;
}
@catch (NSException * e)
{
STAssertTrue(e == nil, @"error - %@", e);
}
}
-(void) tearDown
{
NSLog(@"BEGIN: ManagedObjectSenTestCase tearDown");
@try
{
self.context= nil;
self.model = nil;
self.coordinator = nil;
[pool release];
pool = nil;
}
@catch (NSException * e)
{
NSLog(@"%@",e);
//NSLog(@"%@", [e callStackSymbols]);
NSLog(@"context reset failed!");
@throw(e);
}
NSLog(@"END: ManagedObjectSenTestCase tearDown");
}
-(void) testSetup
{
STAssertNotNil(self.model, @"error loading model");
STAssertNotNil(self.coordinator, @"error loading coordinator");
STAssertNotNil(self.context, @"error loading context");
NSArray* allEntities = [model entities];
STAssertTrue(allEntities.count > 0, @"no entities in bundle!");
}
@end