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 org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNull;
24  
25  import java.io.File;
26  import java.io.FileNotFoundException;
27  import java.io.IOException;
28  
29  import org.junit.After;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  public class PListAccessorTest extends XCodeTest
34  {
35  
36    private PListAccessor plist;
37  
38    private File tempFile;
39  
40    @Before
41    public void before() throws IOException
42    {
43      File srcFile = new File("src/test/resources/MyApp-Info.plist");
44      tempFile = createTempFile();
45      org.apache.commons.io.FileUtils.copyFile(srcFile, tempFile);
46      loadPList();
47    }
48  
49    private File createTempFile() throws IOException
50    {
51      File temp = File.createTempFile("Info", ".plist");
52      temp.deleteOnExit();
53      return temp;
54    }
55  
56    @After
57    public void after()
58    {
59      tempFile.delete();
60    }
61  
62    private void loadPList()
63    {
64      plist = new PListAccessor(tempFile);
65    }
66  
67    @Test
68    public void readString() throws IOException
69    {
70      String appId = plist.getStringValue(PListAccessor.KEY_BUNDLE_IDENTIFIER);
71      assertEquals("com.sap.myapp", appId);
72    }
73  
74    @Test
75    public void writeString() throws IOException
76    {
77      testWrite("com.sap.myapp.internal");
78    }
79  
80    @Test
81    public void writeStringWithWhiteSpace() throws IOException
82    {
83      testWrite("com.sap.myapp.internal with space");
84    }
85  
86    private void testWrite(String expectedAppId) throws IOException
87    {
88      plist.updateStringValue(PListAccessor.KEY_BUNDLE_IDENTIFIER, expectedAppId);
89      String appId = plist.getStringValue(PListAccessor.KEY_BUNDLE_IDENTIFIER);
90      assertEquals(expectedAppId, appId);
91  
92      loadPList();
93      appId = plist.getStringValue(PListAccessor.KEY_BUNDLE_IDENTIFIER);
94      assertEquals(expectedAppId, appId);
95    }
96  
97    @Test(expected = FileNotFoundException.class)
98    public void readStringFromNonExistingPList() throws IOException
99    {
100     PListAccessor plist = new PListAccessor(new File("/foo"));
101     plist.getStringValue(PListAccessor.KEY_BUNDLE_IDENTIFIER);
102   }
103 
104   @Test(expected = FileNotFoundException.class)
105   public void writeStringToNonExistingPList() throws IOException
106   {
107     PListAccessor plist = new PListAccessor(new File("/foo"));
108     plist.updateStringValue(PListAccessor.KEY_BUNDLE_IDENTIFIER, "foo");
109   }
110 
111   @Test
112   public void readStringFromNonExistingKey() throws IOException
113   {
114     assertNull(plist.getStringValue("foo"));
115   }
116 
117   @Test(expected = IllegalStateException.class)
118   public void writeStringToNonExistingKey() throws IOException
119   {
120     plist.updateStringValue("foo", "com.sap.myapp.internal");
121     String appId = plist.getStringValue("foo");
122     assertEquals("com.sap.myapp.internal", appId);
123 
124     loadPList();
125     appId = plist.getStringValue("foo");
126     assertEquals("com.sap.myapp.internal", appId);
127   }
128 
129   @Test
130   public void testAddEntry() throws Exception
131   {
132     plist.addStringValue("hugo", "test");
133     assertEquals("test", plist.getStringValue("hugo"));
134   }
135 
136 }