View Javadoc

1   /*
2    * #%L
3    * xcode-maven-plugin
4    * %%
5    * Copyright (C) 2012 SAP AG
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.sap.prd.mobile.ios.mios;
21  
22  import static junit.framework.Assert.assertEquals;
23  import static junit.framework.Assert.fail;
24  
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.FileReader;
28  import java.io.IOException;
29  import java.util.logging.LogManager;
30  
31  import org.apache.commons.io.FileUtils;
32  import org.apache.commons.io.IOUtils;
33  import org.junit.Before;
34  import org.junit.BeforeClass;
35  import org.junit.Rule;
36  import org.junit.Test;
37  import org.junit.rules.TemporaryFolder;
38  
39  public class UpdateVersionInPListTaskTest
40  {
41  
42  
43    @Rule
44    public TemporaryFolder tmpFolder = new TemporaryFolder();
45  
46    private File infoPListMaster = new File("src/test/resources/MyApp-Info.plist");
47    private File infoPlist = null;
48  
49    @BeforeClass
50    public static void setupStatic() {
51      LogManager.getLogManager().addLogger(new XCodePluginLogger());
52    }
53    
54    @Before
55    public void setup() throws IOException
56    {
57  
58      infoPlist = tmpFolder.newFile(infoPListMaster.getName());
59      FileUtils.copyFile(infoPListMaster, infoPlist);
60    }
61  
62    @Test
63    public void testUpdateCFBundleShortVersionString() throws Exception
64    {
65      final String newVersion = "1.2." + System.currentTimeMillis();
66      new UpdateCFBundleShortVersionStringInPListTask().setPListFile(infoPlist).setVersion(newVersion)
67        .execute();
68      assertVersion("CFBundleShortVersionString", newVersion, infoPlist);
69    }
70  
71    @Test
72    public void testUpdateCFBundleShortVersionStringWithSnapshotVersion() throws Exception
73    {
74      final String newVersion = "1.2." + System.currentTimeMillis();
75      final String newSnapshotVersion = newVersion + "-SNAPSHOT";
76      new UpdateCFBundleShortVersionStringInPListTask().setPListFile(infoPlist).setVersion(newSnapshotVersion)
77        .execute();
78      assertVersion("CFBundleShortVersionString", newVersion, infoPlist);
79    }
80  
81    public void testUpdateCFBundleShortVersionStringWithFourDotVersion() throws Exception
82    {
83      final String newVersion = "1.2.3." + System.currentTimeMillis();
84      new UpdateCFBundleShortVersionStringInPListTask().setPListFile(infoPlist).setVersion(newVersion)
85        .execute();
86      assertVersion("CFBundleVersion", "1.2.3", infoPlist);
87    }
88  
89    @Test
90    public void testUpdateCFBundleVersion() throws Exception
91    {
92      final String newVersion = "1.2." + System.currentTimeMillis();
93      new UpdateCFBundleVersionInPListTask().setPListFile(infoPlist).setVersion(newVersion).execute();
94      assertVersion("CFBundleVersion", newVersion, infoPlist);
95    }
96  
97    @Test
98    public void testUpdateCFBundleVersionWithSnapshotVersion() throws Exception
99    {
100     final String newVersionWithoutSnapshot = "1.2." + System.currentTimeMillis();
101     final String newVersionWithSnapshot = newVersionWithoutSnapshot + "-SNAPSHOT";
102     new UpdateCFBundleVersionInPListTask().setPListFile(infoPlist).setVersion(newVersionWithSnapshot)
103       .execute();
104     assertVersion("CFBundleVersion", newVersionWithoutSnapshot, infoPlist);
105   }
106 
107   private static void assertVersion(final String key, final String version, final File infoPList) throws IOException
108   {
109 
110     //<key>CFBundleVersion</key>
111     //<string>1.2.3</string>
112 
113     final BufferedReader br = new BufferedReader(new FileReader(infoPList));
114 
115     String versionInPlist = null;
116 
117     try {
118       for (String line; (line = br.readLine()) != null;) {
119         if (!line.trim().equals("<key>" + key + "</key>")) {
120           continue;
121         }
122         else {
123           versionInPlist = br.readLine();
124           break;
125         }
126       }
127 
128       if (versionInPlist != null) {
129         assertEquals("Version update in plist file did not succeed.", "<string>" + version + "</string>",
130               versionInPlist.trim());
131       }
132       else {
133         fail(key + " not found in plist.");
134       }
135 
136     }
137     finally {
138       IOUtils.closeQuietly(br);
139     }
140   }
141 }