001    package org.kuali.rice.test.launch;
002    
003    import groovy.lang.GroovyClassLoader;
004    import groovy.lang.GroovyObject;
005    import org.apache.log4j.Logger;
006    import org.junit.Assert;
007    import org.junit.Before;
008    import org.junit.Test;
009    
010    
011    
012    import java.io.File;
013    import java.io.IOException;
014    
015    
016    /**
017     * Created by IntelliJ IDEA.
018     * User: sonam
019     * Date: 5/9/12
020     * Time: 3:32 PM
021     * To change this template use File | Settings | File Templates.
022     */
023    public class CreateProjectTest  {
024    
025        private static final Logger log = Logger.getLogger(CreateProjectTest.class);
026    
027        private static final String INTERNAL_TOOL_MODULE_NAME = "internal-tools";
028    
029        //The directory where the rice code has been checked out (referred to as RICE_DIR in the createproject.groovy script)
030        private String rdir;
031    
032         /**
033         * maven will set this property and find resources from the config based on it. This makes eclipse testing work because
034         * we have to put the basedir in our config files in order to find things when testing from maven
035         */
036       @Before
037        public  void setBaseDirSystemProperty() {
038            if (System.getProperty("basedir") == null) {
039                    final String userDir = System.getProperty("user.dir");
040    
041                System.setProperty("basedir", userDir + ((userDir.endsWith(File.separator + "it" + File.separator + INTERNAL_TOOL_MODULE_NAME)) ? "" : File.separator + "it" + File.separator + INTERNAL_TOOL_MODULE_NAME));
042            }
043           System.out.println("BASEDIR :"+System.getProperty("basedir"));
044        }
045    
046        @Test
047        public void testCreateProjectForSampleApp() throws IOException {
048            GroovyObject groovyObject = getGroovyObject();
049            Object[] args = {"-name", "MyRiceSampleApp","-pdir",getBaseDir()+"/target","-rdir",rdir,"-sampleapp","-testmode"};
050            groovyObject.invokeMethod("main", args);
051    
052        }
053    
054        @Test
055        public void testCreateProjectForSampleAppNoProjectDirectory() throws IOException {
056            GroovyObject groovyObject = getGroovyObject();
057            Object[] args = {"-name", "MyRiceSampleApp","-rdir",rdir,"-sampleapp","-testmode"};
058            groovyObject.invokeMethod("main", args);
059    
060        }
061    
062        @Test
063        public void testCreateProjectForStandaloneAppProjectDirectory() throws IOException {
064            GroovyObject groovyObject = getGroovyObject();
065    
066            Object[] args = {"-name", "MyRiceStandaloneApp","-rdir",rdir,"-standalone","-testmode"};
067            groovyObject.invokeMethod("main", args);
068    
069        }
070    
071        @Test
072        public void testCreateProjectForStandalone() throws IOException {
073         GroovyObject groovyObject = getGroovyObject();
074         Object[] args = {"-name", "MyRiceStandaloneApp","-pdir",getBaseDir()+"/target","-rdir",rdir,"-standalone","-testmode"};
075         groovyObject.invokeMethod("main", args);
076    
077         }
078    
079        protected String getBaseDir() {
080            return System.getProperty("basedir");
081        }
082        /**
083         * Helper method to create the groovy object instance so that it can be used to call the script
084         */
085        protected GroovyObject getGroovyObject(){
086            //Get the checkout location of rice from the working directory
087            File file = new File(getBaseDir());
088            String parentDir1 = file.getParent();
089    
090            file = new File(parentDir1);
091            String parentDir2 = file.getParent();
092    
093            rdir =  parentDir2;
094            // Create the GroovyClassLoader which is used to load Groovy classes using the current class loader as the parent
095            ClassLoader parent = new CreateProjectTest().getClass().getClassLoader();
096            GroovyClassLoader loader;
097            loader = new GroovyClassLoader(parent);
098            Class groovyClass = null;
099            try {
100                // Parse the script into a Java class capable of being run
101                groovyClass =  loader.parseClass(new File(rdir + "/scripts/createproject.groovy"));
102    
103            } catch (IOException e) {
104                e.printStackTrace();
105            }
106    
107           GroovyObject groovyObject = null;
108            try {
109                groovyObject = (GroovyObject) groovyClass.newInstance();
110            } catch (InstantiationException e) {
111                e.printStackTrace();
112            } catch (IllegalAccessException e) {
113                e.printStackTrace();
114            }
115           return groovyObject;
116        }
117    }