View Javadoc

1   package com.jayway.maven.plugins.android.standalonemojos;
2   
3   import java.io.File;
4   import java.io.IOException;
5   
6   import org.apache.commons.io.FileUtils;
7   import org.apache.commons.lang.StringUtils;
8   import org.apache.maven.plugin.MojoFailureException;
9   import org.junit.Assert;
10  
11  import com.jayway.maven.plugins.android.AbstractAndroidMojoTestCase;
12  
13  public class ManifestUpdateMojoTest extends AbstractAndroidMojoTestCase<ManifestUpdateMojo> {
14      @Override
15      public String getPluginGoalName() {
16          return "manifest-update";
17      }
18  
19      public void testAndroidApplicationChanges() throws Exception {
20          ManifestUpdateMojo mojo = createMojo("manifest-tests/application-changes");
21          mojo.execute();
22          File dir = getProjectDir(mojo);
23          File manifestFile = new File(dir, "AndroidManifest.xml");
24          assertExpectedAndroidManifest(manifestFile, dir);
25      }    
26      
27      public void testBasicAndroidProjectVersion() throws Exception {
28          ManifestUpdateMojo mojo = createMojo("manifest-tests/basic-android-project");
29          mojo.execute();
30          File dir = getProjectDir(mojo);
31          File manifestFile = new File(dir, "AndroidManifest.xml");
32          assertExpectedAndroidManifest(manifestFile, dir);
33      }
34  
35      public void testBasicAndroidProjectManifest() throws Exception {
36          ManifestUpdateMojo mojo = createMojo("manifest-tests/basic-android-project-manifest");
37          mojo.execute();
38          File dir = getProjectDir(mojo);
39          File manifestFile = new File(dir, "AndroidManifest.xml");
40          assertExpectedAndroidManifest(manifestFile, dir);
41      }
42  
43      public void testBasicJarProject() throws Exception {
44          ManifestUpdateMojo mojo = createMojo("manifest-tests/basic-jar-project");
45          mojo.execute();
46          File dir = getProjectDir(mojo);
47          File manifestFile = new File(dir, "AndroidManifest.xml");
48          Assert.assertFalse("Should not have an AndroidManifest for a jar project", manifestFile.exists());
49      }
50  
51      public void testVersionlessAndroidProject() throws Exception {
52          ManifestUpdateMojo mojo = createMojo("manifest-tests/versionless-android-project");
53          mojo.execute();
54          File dir = getProjectDir(mojo);
55          File manifestFile = new File(dir, "androidManifest.xml"); // intentionally small lowercase 'a'
56          assertExpectedAndroidManifest(manifestFile, dir);
57      }
58  
59      public void testManyVersionsAndroidProject() throws Exception {
60          ManifestUpdateMojo mojo = createMojo("manifest-tests/manyversions-android-project");
61          for (int i = 0; i < 50; i++) { // Simulate 50 runs of the mojo
62              mojo.execute();
63          }
64          File dir = getProjectDir(mojo);
65          File manifestFile = new File(dir, "AndroidManifest.xml");
66          assertExpectedAndroidManifest(manifestFile, dir);
67      }
68  
69      public void testMinorVersionAndroidProject() throws Exception {
70          ManifestUpdateMojo mojo = createMojo("manifest-tests/minorversion-android-project");
71          mojo.execute();
72          File dir = getProjectDir(mojo);
73          File manifestFile = new File(dir, "AndroidManifest.xml");
74          assertExpectedAndroidManifest(manifestFile, dir);
75      }
76  
77      public void testWhenNewVersionHasLessDigitsItshouldBePaddedSoVersionCodeIsNotLess() throws Exception {
78          ManifestUpdateMojo mojo = createMojo("manifest-tests/differentLengthVersion-android-project");
79          mojo.execute();
80          File dir = getProjectDir(mojo);
81          File manifestFile = new File(dir, "AndroidManifest.xml");
82          assertExpectedAndroidManifest(manifestFile, dir);
83      }
84  
85      public void testVersionCodeUpdateAndIncrementFail() throws Exception {
86          ManifestUpdateMojo mojo = createMojo("manifest-tests/bad-android-project1");
87          try {
88  			mojo.execute();
89          } catch (MojoFailureException e) {
90  	        Assert.assertTrue(e.getMessage().startsWith("versionCodeAutoIncrement, versionCodeUpdateFromVersion and versionCode"));
91  	        return;
92          }
93  		Assert.assertTrue("bad-android-project1 did not throw MojoFailureException", false);
94      }
95  
96  	public void testVersionCodeAndVersionCodeUpdateFail() throws Exception {
97  	    ManifestUpdateMojo mojo = createMojo("manifest-tests/bad-android-project2");
98  	    try {
99  			mojo.execute();
100 	    } catch (MojoFailureException e) {
101 		    Assert.assertTrue(e.getMessage().startsWith("versionCodeAutoIncrement, versionCodeUpdateFromVersion and versionCode"));
102 		    return;
103 	    }
104 		Assert.assertTrue("bad-android-project2 did not throw MojoFailureException", false);
105 	}
106 
107 	public void testVersionCodeAndVersionIncrementFail() throws Exception {
108 	    ManifestUpdateMojo mojo = createMojo("manifest-tests/bad-android-project3");
109 	    try {
110 			mojo.execute();
111 	    } catch (MojoFailureException e) {
112 		    Assert.assertTrue(e.getMessage().startsWith("versionCodeAutoIncrement, versionCodeUpdateFromVersion and versionCode"));
113 		    return;
114 	    }
115 		Assert.assertTrue("bad-android-project3 did not throw MojoFailureException", false);
116 	}
117 
118     public void testSupportsScreensUpdate() throws Exception {
119         ManifestUpdateMojo mojo = createMojo("manifest-tests/supports-screens-android-project");
120         mojo.execute();
121         File dir = getProjectDir(mojo);
122         File manifestFile = new File(dir, "AndroidManifest.xml");
123         // this asserts that:
124         // 1) the values of anyDensity, largeScreens and normalScreens will be changed via the POM
125         // 2) the value of smallScreens will remain untouched (not overridden in POM)
126         // 3) the value of xlargeScreens will be added (defined in POM but not in Manifest)
127         // 4) the value of resizeable will be ignored (undefined in both Manifest and POM)
128         assertExpectedAndroidManifest(manifestFile, dir);
129     }
130 
131     public void DISABLED_testCompatibleScreensUpdate() throws Exception {
132         ManifestUpdateMojo mojo = createMojo("manifest-tests/compatible-screens-android-project");
133         mojo.execute();
134         File dir = getProjectDir(mojo);
135         File manifestFile = new File(dir, "AndroidManifest.xml");
136         // this asserts that:
137         // 1) the screen small/ldpi will be changed via the POM
138         // 2) the screen normal/mdpi will remain untouched (overridden in POM but equal)
139         // 3) the screen normal/hdpi will remain untouched (not overridden in POM)
140         // 4) the screen large/xhdpi will be added (defined in POM but not in Manifest)
141         assertExpectedAndroidManifest(manifestFile, dir);
142     }
143 
144     public void testProviderAuthoritiesUpdate() throws Exception {
145         ManifestUpdateMojo mojo = createMojo("manifest-tests/provider-authorities-project");
146         mojo.execute();
147         File dir = getProjectDir(mojo);
148         File manifestFile = new File(dir, "AndroidManifest.xml");
149         assertExpectedAndroidManifest(manifestFile, dir);
150     }
151 
152     public void testUsesSdkMinVersionUpdate() throws Exception {
153         ManifestUpdateMojo mojo = createMojo("manifest-tests/uses-sdk-project1");
154         mojo.execute();
155         File dir = getProjectDir(mojo);
156         File manifestFile = new File(dir, "AndroidManifest.xml");
157         assertExpectedAndroidManifest(manifestFile, dir);
158     }
159 
160     public void testUsesSdkMaxVersionUpdate() throws Exception {
161         ManifestUpdateMojo mojo = createMojo("manifest-tests/uses-sdk-project2");
162         mojo.execute();
163         File dir = getProjectDir(mojo);
164         File manifestFile = new File(dir, "AndroidManifest.xml");
165         assertExpectedAndroidManifest(manifestFile, dir);
166     }
167 
168     public void testUsesTargetSdkVersionUpdate() throws Exception {
169         ManifestUpdateMojo mojo = createMojo("manifest-tests/uses-sdk-project3");
170         mojo.execute();
171         File dir = getProjectDir(mojo);
172         File manifestFile = new File(dir, "AndroidManifest.xml");
173         assertExpectedAndroidManifest(manifestFile, dir);
174     }
175 
176     public void testUsesSdkVersionFullUpdate() throws Exception {
177         ManifestUpdateMojo mojo = createMojo("manifest-tests/uses-sdk-project4");
178         mojo.execute();
179         File dir = getProjectDir(mojo);
180         File manifestFile = new File(dir, "AndroidManifest.xml");
181         assertExpectedAndroidManifest(manifestFile, dir);
182     }
183 
184     private void assertExpectedAndroidManifest(File manifestFile, File testdir) throws IOException {
185         File expectFile = new File(testdir, "AndroidManifest-expected.xml");
186         // different white space causes issues when between going Windows and *nix via git and wrongly configured
187         // autocrlf .. since we dont need to worry about whitespace.. we strip it out
188         String actual = StringUtils.deleteWhitespace(FileUtils.readFileToString(manifestFile));
189         String expected = StringUtils.deleteWhitespace(FileUtils.readFileToString(expectFile));
190         Assert.assertEquals(expected, actual);
191     }
192 }