Merge pull request #6727 from SmartyAnsh/master
BAEL-2661 - Groovy def keyword
This commit is contained in:
		
						commit
						b2b80c89e1
					
				| @ -2,4 +2,6 @@ | |||||||
| 
 | 
 | ||||||
| ## Relevant articles: | ## Relevant articles: | ||||||
| 
 | 
 | ||||||
| - [String Matching in Groovy](http://www.baeldung.com/) | - [String Matching in Groovy](http://www.baeldung.com/) | ||||||
|  | - [Groovy def Keyword] | ||||||
|  | 
 | ||||||
|  | |||||||
| @ -14,12 +14,33 @@ | |||||||
|     </parent> |     </parent> | ||||||
| 
 | 
 | ||||||
|     <dependencies> |     <dependencies> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.codehaus.groovy</groupId> | ||||||
|  |             <artifactId>groovy</artifactId> | ||||||
|  |             <version>${groovy.version}</version> | ||||||
|  |         </dependency> | ||||||
|         <dependency> |         <dependency> | ||||||
|             <groupId>org.codehaus.groovy</groupId> |             <groupId>org.codehaus.groovy</groupId> | ||||||
|             <artifactId>groovy-all</artifactId> |             <artifactId>groovy-all</artifactId> | ||||||
|             <version>${groovy-all.version}</version> |             <version>${groovy-all.version}</version> | ||||||
|             <type>pom</type> |             <type>pom</type> | ||||||
|         </dependency> |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.codehaus.groovy</groupId> | ||||||
|  |             <artifactId>groovy-dateutil</artifactId> | ||||||
|  |             <version>${groovy.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.codehaus.groovy</groupId> | ||||||
|  |             <artifactId>groovy-sql</artifactId> | ||||||
|  |             <version>${groovy-sql.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.junit.platform</groupId> | ||||||
|  |             <artifactId>junit-platform-runner</artifactId> | ||||||
|  |             <version>${junit.platform.version}</version> | ||||||
|  |             <scope>test</scope> | ||||||
|  |         </dependency> | ||||||
|         <dependency> |         <dependency> | ||||||
|             <groupId>org.hsqldb</groupId> |             <groupId>org.hsqldb</groupId> | ||||||
|             <artifactId>hsqldb</artifactId> |             <artifactId>hsqldb</artifactId> | ||||||
| @ -43,12 +64,39 @@ | |||||||
|                 <executions> |                 <executions> | ||||||
|                     <execution> |                     <execution> | ||||||
|                         <goals> |                         <goals> | ||||||
|  |                             <goal>addSources</goal> | ||||||
|  |                             <goal>addTestSources</goal> | ||||||
|                             <goal>compile</goal> |                             <goal>compile</goal> | ||||||
|                             <goal>compileTests</goal> |                             <goal>compileTests</goal> | ||||||
|                         </goals> |                         </goals> | ||||||
|                     </execution> |                     </execution> | ||||||
|                 </executions> |                 </executions> | ||||||
|             </plugin> |             </plugin> | ||||||
|  |             <plugin> | ||||||
|  |                 <artifactId>maven-failsafe-plugin</artifactId> | ||||||
|  |                 <version>${maven-failsafe-plugin.version}</version> | ||||||
|  |                 <dependencies> | ||||||
|  |                     <dependency> | ||||||
|  |                         <groupId>org.junit.platform</groupId> | ||||||
|  |                         <artifactId>junit-platform-surefire-provider</artifactId> | ||||||
|  |                         <version>${junit.platform.version}</version> | ||||||
|  |                     </dependency> | ||||||
|  |                 </dependencies> | ||||||
|  |                 <executions> | ||||||
|  |                     <execution> | ||||||
|  |                         <id>junit5</id> | ||||||
|  |                         <goals> | ||||||
|  |                             <goal>integration-test</goal> | ||||||
|  |                             <goal>verify</goal> | ||||||
|  |                         </goals> | ||||||
|  |                         <configuration> | ||||||
|  |                             <includes> | ||||||
|  |                                 <include>**/*Test5.java</include> | ||||||
|  |                             </includes> | ||||||
|  |                         </configuration> | ||||||
|  |                     </execution> | ||||||
|  |                 </executions> | ||||||
|  |             </plugin> | ||||||
|             <plugin> |             <plugin> | ||||||
|                 <artifactId>maven-surefire-plugin</artifactId> |                 <artifactId>maven-surefire-plugin</artifactId> | ||||||
|                 <version>2.20.1</version> |                 <version>2.20.1</version> | ||||||
| @ -71,10 +119,13 @@ | |||||||
|     </repositories> |     </repositories> | ||||||
| 
 | 
 | ||||||
|     <properties> |     <properties> | ||||||
|  |         <junit.platform.version>1.0.0</junit.platform.version> | ||||||
|  |         <groovy.version>2.5.6</groovy.version> | ||||||
|         <groovy-all.version>2.5.6</groovy-all.version> |         <groovy-all.version>2.5.6</groovy-all.version> | ||||||
|  |         <groovy-sql.version>2.5.6</groovy-sql.version> | ||||||
|         <hsqldb.version>2.4.0</hsqldb.version> |         <hsqldb.version>2.4.0</hsqldb.version> | ||||||
|         <spock-core.version>1.3-groovy-2.5</spock-core.version> |         <spock-core.version>1.1-groovy-2.4</spock-core.version> | ||||||
|         <gmavenplus-plugin.version>1.6.3</gmavenplus-plugin.version> |         <gmavenplus-plugin.version>1.6</gmavenplus-plugin.version> | ||||||
|     </properties> |     </properties> | ||||||
| </project> | </project> | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -0,0 +1,79 @@ | |||||||
|  | package com.baeldung.defkeyword | ||||||
|  | 
 | ||||||
|  | import org.codehaus.groovy.runtime.NullObject | ||||||
|  | import org.codehaus.groovy.runtime.typehandling.GroovyCastException | ||||||
|  | 
 | ||||||
|  | import groovy.transform.TypeChecked | ||||||
|  | import groovy.transform.TypeCheckingMode | ||||||
|  | 
 | ||||||
|  | @TypeChecked | ||||||
|  | class DefUnitTest extends GroovyTestCase { | ||||||
|  |      | ||||||
|  |     def id | ||||||
|  |     def firstName = "Samwell" | ||||||
|  |     def listOfCountries = ['USA', 'UK', 'FRANCE', 'INDIA'] | ||||||
|  |      | ||||||
|  |     @TypeChecked(TypeCheckingMode.SKIP) | ||||||
|  |     def multiply(x, y) { | ||||||
|  |         return x*y | ||||||
|  |     } | ||||||
|  |          | ||||||
|  |     @TypeChecked(TypeCheckingMode.SKIP) | ||||||
|  |     void testDefVariableDeclaration() { | ||||||
|  |          | ||||||
|  |         def list | ||||||
|  |         assert list.getClass() == org.codehaus.groovy.runtime.NullObject | ||||||
|  |         assert list.is(null) | ||||||
|  |          | ||||||
|  |         list = [1,2,4] | ||||||
|  |         assert list instanceof ArrayList | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     @TypeChecked(TypeCheckingMode.SKIP) | ||||||
|  |     void testTypeVariables() {    | ||||||
|  |         int rate = 200 | ||||||
|  |         try { | ||||||
|  |             rate = [12] //GroovyCastException | ||||||
|  |             rate = "nill" //GroovyCastException | ||||||
|  |         } catch(GroovyCastException) { | ||||||
|  |             println "Cannot assign anything other than integer" | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |       | ||||||
|  |     @TypeChecked(TypeCheckingMode.SKIP) | ||||||
|  |     void testDefVariableMultipleAssignment() { | ||||||
|  |         def rate | ||||||
|  |         assert rate == null | ||||||
|  |         assert rate.getClass() == org.codehaus.groovy.runtime.NullObject | ||||||
|  |          | ||||||
|  |         rate = 12 | ||||||
|  |         assert rate instanceof Integer | ||||||
|  |          | ||||||
|  |         rate = "Not Available" | ||||||
|  |         assert rate instanceof String | ||||||
|  |          | ||||||
|  |         rate = [1, 4] | ||||||
|  |         assert rate instanceof List | ||||||
|  |          | ||||||
|  |         assert divide(12, 3) instanceof BigDecimal | ||||||
|  |         assert divide(1, 0) instanceof String | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     def divide(int x, int y) { | ||||||
|  |         if(y==0) { | ||||||
|  |             return "Should not divide by 0" | ||||||
|  |         } else { | ||||||
|  |             return x/y | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     def greetMsg() { | ||||||
|  |         println "Hello! I am Groovy" | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     void testDefVsType() { | ||||||
|  |         def int count | ||||||
|  |         assert count instanceof Integer | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										2
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								pom.xml
									
									
									
									
									
								
							| @ -376,7 +376,7 @@ | |||||||
|                 <module>cdi</module> |                 <module>cdi</module> | ||||||
|                 <module>checker-plugin</module> |                 <module>checker-plugin</module> | ||||||
|                 <module>core-groovy</module> |                 <module>core-groovy</module> | ||||||
| 				<module>core-groovy-2</module> |                 <module>core-groovy-2</module> | ||||||
|                 <!-- <module>core-java-10</module> --> <!-- We haven't upgraded to java 10. Fixing in BAEL-10841 --> |                 <!-- <module>core-java-10</module> --> <!-- We haven't upgraded to java 10. Fixing in BAEL-10841 --> | ||||||
|                 <!-- <module>core-java-11</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 --> |                 <!-- <module>core-java-11</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 --> | ||||||
|                 <!-- <module>core-java-12</module> --> <!-- We haven't upgraded to java 12. Fixing in BAEL-10841 --> |                 <!-- <module>core-java-12</module> --> <!-- We haven't upgraded to java 12. Fixing in BAEL-10841 --> | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user