1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.sap.prd.mobile.ios.mios;
21
22 import static org.junit.Assert.assertArrayEquals;
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.File;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 import junit.framework.Assert;
33
34 import org.apache.commons.io.FileUtils;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37
38 public class XCodeContextTest
39 {
40
41 private static File projectDirectory;
42
43 @BeforeClass
44 public static void setup()
45 {
46 projectDirectory = new File(new File(".").getAbsoluteFile(), "src/test/projects/MyLibrary");
47 }
48
49 @Test
50 public void testStraightForward()
51 {
52
53 final String projectName = "MyLibrary.xcodeproj";
54
55 Map<String, String> managedOptions = new HashMap<String, String>();
56 managedOptions.put(Options.ManagedOption.CONFIGURATION.getOptionName(), "Release");
57 managedOptions.put(Options.ManagedOption.SDK.getOptionName(), "mysdk");
58 managedOptions.put(Options.ManagedOption.PROJECT.getOptionName(), projectName);
59 Options options = new Options(null, managedOptions);
60
61 HashMap<String, String> managedSettings = new HashMap<String, String>();
62 managedSettings.put(Settings.ManagedSetting.CODE_SIGN_IDENTITY.name(), "MyCodeSignIdentity");
63 managedSettings.put(Settings.ManagedSetting.PROVISIONING_PROFILE.name(), "MyProvisioningProfile");
64 Settings settings = new Settings(null, managedSettings);
65
66 final XCodeContext xCodeContext = new XCodeContext(Arrays.asList("clean",
67 "build"), projectDirectory, System.out, settings, options);
68
69 assertEquals(projectName, xCodeContext.getProjectName());
70 assertArrayEquals(new String[] { "clean", "build" }, xCodeContext.getBuildActions().toArray());
71 assertEquals("MyCodeSignIdentity", xCodeContext.getCodeSignIdentity());
72 assertEquals("MyProvisioningProfile", xCodeContext.getProvisioningProfile());
73 }
74
75 @Test(expected = IllegalArgumentException.class)
76 public void testEmptyBuildActions()
77 {
78 new XCodeContext(new ArrayList<String>(), projectDirectory, System.out, null, null);
79 }
80
81 @Test(expected = IllegalArgumentException.class)
82 public void testBuildActionWithEmptyEntry()
83 {
84 new XCodeContext(Arrays.asList("clean", "", "build"), projectDirectory, System.out, null, null);
85 }
86
87 @Test(expected = XCodeContext.InvalidBuildActionException.class)
88 public void TestBuildActionEntryWithBlank()
89 {
90 new XCodeContext(Arrays.asList("clean", "build foo"), projectDirectory, System.out, null, null);
91 }
92
93 @Test(expected = XCodeContext.InvalidBuildActionException.class)
94 public void testBuildActionWithNullElement()
95 {
96 new XCodeContext(Arrays.asList("clean", null, "build"), projectDirectory, System.out, null, null);
97 }
98
99 @Test(expected = Options.IllegalOptionException.class)
100 public void testXCodeContextWithEmptyProjectName()
101 {
102 HashMap<String, String> managedOptions = new HashMap<String, String>();
103 managedOptions.put(Options.ManagedOption.PROJECT.getOptionName(), "");
104 Options options = new Options(null, managedOptions);
105 try {
106 new XCodeContext(Arrays.asList("clean", "build"), projectDirectory, System.out, null, options);
107 }
108 catch (Options.IllegalOptionException ex) {
109
110 assertEquals(Options.ManagedOption.PROJECT, ex.getViolated());
111 throw ex;
112 }
113 }
114
115 @Test(expected = Options.IllegalOptionException.class)
116 public void testXCodeContextWithoutProjectName()
117 {
118 try {
119 new XCodeContext(Arrays.asList("clean", "build"), projectDirectory, System.out, null, null);
120 }
121 catch (Options.IllegalOptionException ex) {
122 assertEquals(Options.ManagedOption.PROJECT, ex.getViolated());
123 throw ex;
124 }
125 }
126
127 @Test(expected = IllegalArgumentException.class)
128 public void testXCodeContextWithoutPrintStream()
129 {
130 new XCodeContext(Arrays.asList("clean", "build"), projectDirectory, null, null, null);
131 }
132
133 @Test
134 public void testCodeSignIdentityIsNull() throws Exception
135 {
136 Map<String, String> managedOptions = new HashMap<String, String>();
137 managedOptions.put(Options.ManagedOption.CONFIGURATION.getOptionName(), "Release");
138 managedOptions.put(Options.ManagedOption.SDK.getOptionName(), "mysdk");
139 managedOptions.put(Options.ManagedOption.PROJECT.getOptionName(), "MyLibrary.xcodeproj");
140 Options options = new Options(null, managedOptions);
141
142 final XCodeContext xCodeContext = new XCodeContext(Arrays.asList("clean", "build"), projectDirectory, System.out,
143 null, options);
144 Assert.assertNull(xCodeContext.getCodeSignIdentity());
145 }
146
147 @Test
148 public void testCodeSignIdentityIsEmpty() throws Exception
149 {
150 Map<String, String> managedOptions = new HashMap<String, String>();
151 managedOptions.put(Options.ManagedOption.CONFIGURATION.getOptionName(), "Release");
152 managedOptions.put(Options.ManagedOption.SDK.getOptionName(), "mysdk");
153 managedOptions.put(Options.ManagedOption.PROJECT.getOptionName(), "MyLibrary.xcodeproj");
154 Options options = new Options(null, managedOptions);
155
156 HashMap<String, String> managedSettings = new HashMap<String, String>();
157 managedSettings.put(Settings.ManagedSetting.CODE_SIGN_IDENTITY.name(), "");
158 Settings settings = new Settings(null, managedSettings);
159
160 XCodeContext context = new XCodeContext(Arrays.asList("clean", "build"), projectDirectory, System.out, settings,
161 options);
162
163 assertEquals("", context.getCodeSignIdentity());
164
165 }
166
167 @Test
168 public void testProvisioningProfileIsNull() throws Exception
169 {
170 Map<String, String> managedOptions = new HashMap<String, String>();
171 managedOptions.put(Options.ManagedOption.CONFIGURATION.getOptionName(), "Release");
172 managedOptions.put(Options.ManagedOption.SDK.getOptionName(), "mysdk");
173 managedOptions.put(Options.ManagedOption.PROJECT.getOptionName(), "MyLibrary.xcodeproj");
174 Options options = new Options(null, managedOptions);
175
176 final XCodeContext xCodeContext = new XCodeContext(Arrays.asList("clean", "build"), projectDirectory, System.out,
177 null, options);
178 Assert.assertNull(xCodeContext.getProvisioningProfile());
179 }
180
181 @Test
182 public void testIsImmutable() throws Exception
183 {
184 Map<String, String> userOptions = new HashMap<String, String>();
185 Map<String, String> managedOptions = new HashMap<String, String>();
186 managedOptions.put(Options.ManagedOption.CONFIGURATION.getOptionName(), "Release");
187 managedOptions.put(Options.ManagedOption.SDK.getOptionName(), "mysdk");
188 managedOptions.put(Options.ManagedOption.PROJECT.getOptionName(), "MyLibrary.xcodeproj");
189 Options options = new Options(userOptions, managedOptions);
190
191 HashMap<String, String> userSettings = new HashMap<String, String>();
192 HashMap<String, String> managedSettings = new HashMap<String, String>();
193 managedSettings.put(Settings.ManagedSetting.CODE_SIGN_IDENTITY.name(), "");
194 Settings settings = new Settings(userSettings, managedSettings);
195
196 List<String> buildActions = new ArrayList<String>(Arrays.asList("clean", "build"));
197
198 File _projectDir = new File(projectDirectory.getParentFile().getCanonicalFile(), "MyLib2");
199 _projectDir.deleteOnExit();
200
201 FileUtils.copyDirectory(projectDirectory, _projectDir);
202
203 final XCodeContext xCodeContext = new XCodeContext(buildActions, _projectDir, System.out, settings, options);
204
205 int hashCode = xCodeContext.hashCode();
206
207 managedOptions.put("managedOptionAddedAfterwards", "xxxx");
208 managedSettings.put("managedSettigAddedAfterwards", "yyyy");
209
210 userOptions.put("userOptionAddedAfterwards", "1111");
211 userSettings.put("userSettigAddedAfterwards", "2222");
212
213 buildActions.add("addedAfterwards");
214
215 _projectDir.renameTo(new File(projectDirectory.getParent(), "MyLib3"));
216
217 assertEquals("Context is not immutable.", hashCode, xCodeContext.hashCode());
218 }
219 }