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