View Javadoc
1   /**
2    * Copyright 2011-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.maven.plugins.fusion;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Enumeration;
22  import java.util.List;
23  import java.util.Properties;
24  
25  import javax.swing.tree.DefaultMutableTreeNode;
26  
27  import org.eclipse.jgit.api.errors.CheckoutConflictException;
28  import org.eclipse.jgit.api.errors.GitAPIException;
29  import org.eclipse.jgit.api.errors.InvalidRefNameException;
30  import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
31  import org.eclipse.jgit.api.errors.RefNotFoundException;
32  import org.junit.Assert;
33  import org.kuali.common.util.Version;
34  import org.kuali.common.util.VersionUtils;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  public class MojoHelperTest extends AbstractFusionMojoWithGit {
39  	
40  	private static final Logger logger = LoggerFactory.getLogger(MojoHelperTest.class);
41  	
42  	SVNUtils svnUtils = SVNUtils.getInstance();
43  
44  	MojoHelper helper = MojoHelper.getInstance(new LogOnlyTestMojo());
45  	private static final String POM = "pom.xml";
46  	private static final String IGNORE = "src,target,.svn,.git";
47  
48  	
49  	/**
50  	 * @param name
51  	 * @param bare
52  	 */
53  	public MojoHelperTest() {
54  		super("mojo-helper-test");
55  	}
56  
57  
58  	public void testIsKnownQualifier() {
59  		/*
60  		 * This used to fail but the new multi-part qualifier support doesn't care about ordering.
61  		 * 
62  		 * It detects the M6 part and will bump that up by 1.
63  		 */
64  		String version = "2.0.0-M6-CM20-SNAPSHOT";
65  		String nextVersion = "";
66  		
67  		boolean exception = false;
68  		try {
69  			nextVersion = helper.getNextVersion(version);
70  		} catch (IllegalArgumentException e) {
71  			exception = true;
72  		}
73  		
74  		Assert.assertEquals (true, exception);
75  		
76  		logger.info(nextVersion);
77  		Version v = VersionUtils.getVersion(version);
78  		String qualifier = v.getQualifier();
79  		logger.info(qualifier);
80  		Assert.assertFalse(helper.isKnownSubQualifier(qualifier));
81  	}
82  	
83  	private void assertVersionChange (String baseVersion, String expectedNextVersion, boolean assertEquals) {
84  		assertVersionChange(baseVersion, expectedNextVersion, assertEquals, false);
85  	}
86  	
87  	private void assertVersionChange (String baseVersion, String expectedNextVersion, boolean assertEquals, boolean throwException) {
88  			
89  		try {
90  			String nextVersion = helper.getNextVersion(baseVersion);
91  			
92  			if (assertEquals)
93  				Assert.assertEquals(expectedNextVersion, nextVersion);
94  			else
95  				Assert.assertNotEquals(expectedNextVersion, nextVersion);
96  		} catch (IllegalArgumentException e) {
97  			
98  			if (throwException)
99  				throw e;
100 			
101 			if (assertEquals) {
102 				Assert.fail (e.getMessage());
103 			}
104 		}
105 	}
106 	
107 
108 	public void testENRFoundersReleaseVersionScheme () {
109 		/*
110 		 * Verifies that the ENR founders release module naming scheme will work properly. 
111 		 */
112 		
113 		assertVersionChange("2.0.0-M9-SNAPSHOT", "2.0.0-M10-SNAPSHOT", false);
114 
115 		assertVersionChange("2.0.0-M6-CM20-SNAPSHOT", "2.0.0-M7-CM20-SNAPSHOT", false);
116 		
117 		boolean failed = false;
118 		try {
119 			assertVersionChange("2.0.0-FR1-RC-SNAPSHOT", "2.0.0-FR1-RC1-SNAPSHOT", true, true);
120 		} catch (IllegalArgumentException e) {
121 			failed = true;
122 		}
123 		
124 		Assert.assertTrue("Should fail if the qualifier is missing a number", failed);
125 		
126 		assertVersionChange("2.1.0-FR2-M10-SNAPSHOT", "2.1.0-FR2-M11-SNAPSHOT", true);
127 		
128 		assertVersionChange("2.1.0-fr2-M10-SNAPSHOT", "2.1.0-fr2-M11-SNAPSHOT", true);
129 		
130 		assertVersionChange("2.1.0-fr2-m10-SNAPSHOT", "2.1.0-fr2-m11-SNAPSHOT", true);
131 		
132 		assertVersionChange("2.0.0-FR1-SNAPSHOT", "2.0.0-FR2-SNAPSHOT", true);
133 		
134 		assertVersionChange("2.0.0-fr1-SNAPSHOT", "2.0.0-fr2-SNAPSHOT", true);
135 		
136 		assertVersionChange("2.0.0-M9-FR1-SNAPSHOT", "2.0.0-M10-FR1-SNAPSHOT", false);
137 	}
138 
139 
140 	public void testKSAPVersionScheme () {
141 		
142 		assertVersionChange("2.0.0-M8-KSAP-SNAPSHOT", "2.0.0-M9-KSAP-SNAPSHOT", false);
143 		
144 		assertVersionChange("2.0.0-KSAP-M8-SNAPSHOT", "2.0.0-KSAP-M9-SNAPSHOT", false);
145 		
146 	}
147 	
148 
149 	public void testOtherVersions () {
150 		
151 		assertVersionChange("2.0.0-SNAPSHOT", "2.0.1-SNAPSHOT", true);
152 		
153 	}
154 	
155 
156 	public void testIsReleaseCandidate() {
157 		String s1 = "rc1";
158 		String s2 = "Rc1";
159 		String s3 = "RC1";
160 		String s4 = "rC2";
161 
162 		Assert.assertTrue(helper.isKnownSubQualifier(s1));
163 		Assert.assertTrue(helper.isKnownSubQualifier(s2));
164 		Assert.assertTrue(helper.isKnownSubQualifier(s3));
165 		Assert.assertTrue(helper.isKnownSubQualifier(s4));
166 
167 	}
168 
169 
170 	private GAV getGAV (DefaultMutableTreeNode node) {
171 		Project project = (Project) node.getUserObject();
172 		GAV gav = project.getGav();
173 		
174 		return gav;
175 	}
176 	
177 	public void testValidate() throws Exception {
178 		
179 			GitTestRepositoryUtils.createFusedBaseStructure(this.repo);
180 		
181 			List<File> poms = helper.getPoms(repo.getWorkTree().getAbsoluteFile(), POM, IGNORE);
182 			List<DefaultMutableTreeNode> nodes = helper.getNodes(poms);
183 			DefaultMutableTreeNode tree = helper.getTree(repo.getWorkTree().getAbsoluteFile(), nodes, POM);
184 			
185 			// check that the group ids are null on the modules
186 			
187 			Enumeration<DefaultMutableTreeNode> enumeration = tree.children();
188 			
189 			Assert.assertEquals(true, enumeration.hasMoreElements());
190 			
191 			DefaultMutableTreeNode module1Node = (DefaultMutableTreeNode) enumeration.nextElement();
192 			
193 			Assert.assertEquals(true, enumeration.hasMoreElements());
194 			Assert.assertNull(getGAV(module1Node).getGroupId());
195 			
196 			DefaultMutableTreeNode module2Node = (DefaultMutableTreeNode) enumeration.nextElement();
197 			
198 			Assert.assertNull(getGAV(module2Node).getGroupId());
199 			
200 			helper.fillInGavs(tree);
201 			
202 			// check that the group ids are set on the modules
203 			
204 			Assert.assertEquals("org.kuali.maven.plugins", getGAV(module1Node).getGroupId());
205 			Assert.assertEquals("org.kuali.maven.plugins", getGAV(module2Node).getGroupId());
206 			
207 	}
208 
209 	 
210 	public void testUpdateVersions() throws RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, CheckoutConflictException, IOException, GitAPIException {
211 		 
212 		GitTestRepositoryUtils.createFusedBaseStructure(this.repo);
213 		
214 		 
215 		int buildNumber = 100;
216 		GAV gav = new GAV("org.kuali.student", "student", "2.0.0-SNAPSHOT");
217 		List<Mapping> mappings = new ArrayList<Mapping>();
218 		mappings.add(new Mapping("module1", "module1", "module1.version"));
219 		mappings.add(new Mapping("module2", "module2", "module2.version"));
220 
221 		Properties properties = new Properties();
222 		properties.setProperty("module1.version", "2.0.0-M5-SNAPSHOT");
223 		properties.setProperty("module2.version", "2.0.0-SNAPSHOT");
224 		
225 		File baseDir = repo.getWorkTree().getAbsoluteFile();
226 		
227 		List<File> files = helper.getPoms(baseDir, POM, IGNORE);
228 		List<DefaultMutableTreeNode> nodes = helper.getNodes(files);
229 		DefaultMutableTreeNode node = helper.getTree(baseDir, nodes, POM);
230 		BuildTag rootTag = helper.getBuildTag(baseDir, gav, TagStyle.BUILDNUMBER, buildNumber);
231 		helper.updateBuildInfo(node, rootTag, TagStyle.BUILDNUMBER, buildNumber);
232 		List<BuildTag> moduleTags = helper.getBuildTags(properties, mappings, TagStyle.BUILDNUMBER, buildNumber);
233 		
234 		assertEquals(2, moduleTags.size());
235 		
236 		BuildTag module1Tag = moduleTags.get(0);
237 		
238 		assertEquals("module1-2.0_2.0.0-M5_build-100", module1Tag.getName());
239 		
240 		BuildTag module2Tag = moduleTags.get(1);
241 		
242 		assertEquals("module2-2.0_2.0.0_build-100", module2Tag.getName());
243 		
244 		helper.updateBuildInfo(nodes, moduleTags, mappings, TagStyle.BUILDNUMBER, buildNumber);
245 		helper.updateGavs(node);
246 		helper.updateProperties(node, properties, mappings);
247 		
248 		// note this test does not effect the change into the fused aggregate.
249 		logger.info("\n" + helper.getDisplayString(node));
250 	}
251 
252 	 
253 	public void testGetTree() throws RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, CheckoutConflictException, IOException, GitAPIException {
254 		 
255 		 GitTestRepositoryUtils.createFusionBaseStructure(this.repo);
256 		 
257 			List<File> poms = helper.getPoms(repo.getWorkTree(), POM, IGNORE);
258 			List<DefaultMutableTreeNode> nodes = helper.getNodes(poms);
259 			DefaultMutableTreeNode tree = helper.getTree(repo.getWorkTree().getAbsoluteFile(), nodes, POM);
260 			String s = helper.getDisplayString(tree);
261 			logger.info("Maven Structure:\n" + s);
262 	}
263 
264 	 
265 	public void testGetFiles() throws RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, CheckoutConflictException, IOException, GitAPIException {
266 		 
267 		 GitTestRepositoryUtils.createFusedBaseStructure(this.repo);
268 		 
269 			List<File> poms = helper.getPoms(repo.getWorkTree().getAbsoluteFile(), POM, IGNORE);
270 			List<DefaultMutableTreeNode> nodes = helper.getNodes(poms);
271 			DefaultMutableTreeNode tree = helper.getTree(repo.getWorkTree().getAbsoluteFile(), nodes, POM);
272 			String s = helper.getDisplayString(tree, repo.getWorkTree(), POM);
273 			logger.info("Maven Structure:\n" + s);
274 	}
275 	
276 }