1717import grails .util .BuildSettings ;
1818import grails .util .BuildSettingsHolder ;
1919import groovy .lang .GroovyClassLoader ;
20+
21+ import java .io .File ;
22+ import java .io .StringWriter ;
23+ import java .util .Map ;
24+
2025import junit .framework .TestCase ;
26+
2127import org .codehaus .groovy .grails .commons .DefaultGrailsDomainClass ;
2228import org .codehaus .groovy .grails .commons .GrailsClassUtils ;
2329import org .codehaus .groovy .grails .commons .GrailsDomainClass ;
30+ import org .codehaus .groovy .grails .plugins .MockGrailsPluginManager ;
31+ import org .codehaus .groovy .grails .plugins .PluginManagerHolder ;
2432import org .springframework .beans .BeanWrapper ;
2533import org .springframework .beans .BeanWrapperImpl ;
2634
27- import java .io .File ;
28- import java .io .StringWriter ;
29- import java .util .Map ;
30-
3135/**
3236 * @author Graeme Rocher
33- * @since 09-Feb-2006
3437 */
38+ @ SuppressWarnings ("unchecked" )
3539public class GrailsTemplateGeneratorsTests extends TestCase {
3640
41+ @ Override
3742 protected void setUp () {
3843 BuildSettings buildSettings = new BuildSettings (new File ("." ));
3944 BuildSettingsHolder .setSettings (buildSettings );
45+ MockGrailsPluginManager pluginManager = new MockGrailsPluginManager ();
46+ PluginManagerHolder .setPluginManager (pluginManager );
47+ pluginManager .registerMockPlugin (DefaultGrailsTemplateGeneratorTests .fakeHibernatePlugin );
4048 }
4149
50+ @ Override
4251 protected void tearDown () {
4352 BuildSettingsHolder .setSettings (null );
53+ PluginManagerHolder .setPluginManager (null );
4454 }
4555
46-
4756 public void testGenerateController () throws Exception {
4857 GrailsTemplateGenerator generator ;
4958
@@ -55,37 +64,35 @@ public void testGenerateController() throws Exception {
5564 GrailsDomainClass domainClass = new DefaultGrailsDomainClass (dc );
5665
5766 File generatedFile = new File ("test/grails-app/controllers/TestController.groovy" );
58- if (generatedFile .exists ()) {
59- generatedFile .delete ();
67+ if (generatedFile .exists ()) {
68+ generatedFile .delete ();
6069 }
6170
62-
6371 StringWriter sw = new StringWriter ();
6472 generator .generateController (domainClass ,sw );
6573
66-
6774 String text = sw .toString ();
6875
6976 Class controllerClass = gcl .parseClass (text );
7077 BeanWrapper bean = new BeanWrapperImpl (controllerClass .newInstance ());
71-
78+
7279 assertEquals ("TestController" , controllerClass .getName ());
73-
80+
7481 assertTrue (bean .isReadableProperty ("list" ));
7582 assertTrue (bean .isReadableProperty ("update" ));
7683 assertTrue (bean .isReadableProperty ("create" ));
7784 assertTrue (bean .isReadableProperty ("list" ));
7885 assertTrue (bean .isReadableProperty ("show" ));
7986 assertTrue (bean .isReadableProperty ("edit" ));
8087 assertTrue (bean .isReadableProperty ("delete" ));
81-
88+
8289 Object propertyValue = GrailsClassUtils .getStaticPropertyValue (controllerClass , "allowedMethods" );
8390 assertTrue ("allowedMethods property was the wrong type" , propertyValue instanceof Map );
8491 Map map = (Map ) propertyValue ;
8592 assertTrue ("allowedMethods did not contain the delete action" , map .containsKey ("delete" ));
8693 assertTrue ("allowedMethods did not contain the save action" , map .containsKey ("save" ));
8794 assertTrue ("allowedMethods did not contain the update action" , map .containsKey ("update" ));
88-
95+
8996 assertEquals ("allowedMethods had incorrect value for delete action" , "POST" , map .get ("delete" ));
9097 assertEquals ("allowedMethods had incorrect value for save action" , "POST" , map .get ("save" ));
9198 assertEquals ("allowedMethods had incorrect value for update action" , "POST" , map .get ("update" ));
@@ -98,27 +105,23 @@ public void testGenerateViews() throws Exception {
98105
99106 generator = new DefaultGrailsTemplateGenerator ();
100107
101- Class dc = gcl .parseClass ("class Test { " +
102- "\n Long id;" +
103- "\n Long version;" +
104- "\n String name;" +
105- "\n TimeZone tz;" +
106- "\n Locale locale;" +
107- "\n Currency currency;" +
108- "\n Boolean active;" +
109- "\n Date age }" );
108+ Class dc = gcl .parseClass (
109+ "class Test { " +
110+ "\n Long id;" +
111+ "\n Long version;" +
112+ "\n String name;" +
113+ "\n TimeZone tz;" +
114+ "\n Locale locale;" +
115+ "\n Currency currency;" +
116+ "\n Boolean active;" +
117+ "\n Date age }" );
110118 GrailsDomainClass domainClass = new DefaultGrailsDomainClass (dc );
111119
112120 generator .generateViews (domainClass ,"test" );
113121
114- File showFile = new File ("test/grails-app/views/test/show.gsp" );
115- assertTrue (showFile .exists ());
116- File listFile = new File ("test/grails-app/views/test/list.gsp" );
117- assertTrue (listFile .exists ());
118- File editFile = new File ("test/grails-app/views/test/edit.gsp" );
119- assertTrue (editFile .exists ());
120- File createFile = new File ("test/grails-app/views/test/create.gsp" );
121- assertTrue (createFile .exists ());
122+ assertTrue (new File ("test/grails-app/views/test/show.gsp" ).exists ());
123+ assertTrue (new File ("test/grails-app/views/test/list.gsp" ).exists ());
124+ assertTrue (new File ("test/grails-app/views/test/edit.gsp" ).exists ());
125+ assertTrue (new File ("test/grails-app/views/test/create.gsp" ).exists ());
122126 }
123-
124127}
0 commit comments