11package org .jooby .assets ;
22
3+ import static org .easymock .EasyMock .eq ;
34import static org .easymock .EasyMock .expect ;
45import static org .easymock .EasyMock .isA ;
56import static org .junit .Assert .assertTrue ;
1011import org .jooby .Route ;
1112import org .jooby .Route .Definition ;
1213import org .jooby .Route .Filter ;
14+ import org .jooby .Routes ;
1315import org .jooby .handlers .AssetHandler ;
1416import org .jooby .internal .assets .AssetHandlerWithCompiler ;
17+ import org .jooby .internal .assets .AssetVars ;
1518import org .jooby .internal .assets .LiveCompiler ;
1619import org .jooby .test .MockUnit ;
1720import org .junit .Test ;
3437@ PrepareForTest ({Assets .class , AssetCompiler .class , Multibinder .class , LiveCompiler .class })
3538public class AssetsTest {
3639
37- @ SuppressWarnings ("unchecked" )
3840 @ Test
3941 public void configure () throws Exception {
4042 Config conf = ConfigFactory .empty ()
@@ -48,20 +50,17 @@ public void configure() throws Exception {
4850 expect (compiler .patterns ()).andReturn (Sets .newHashSet ("/assets/**" ));
4951 unit .registerMock (AssetCompiler .class , compiler );
5052 }).expect (unit -> {
51- Binder binder = unit .get (Binder .class );
53+ Definition assetVars = unit .mock (Definition .class );
54+ expect (assetVars .name ("/assets/vars" )).andReturn (assetVars );
55+ Routes routes = unit .mock (Routes .class );
56+ expect (routes .use (eq ("*" ), eq ("*" ), unit .capture (AssetVars .class ))).andReturn (assetVars );
5257
53- LinkedBindingBuilder <Definition > varsLBB = unit .mock (LinkedBindingBuilder .class );
54- varsLBB .toInstance (unit .capture (Route .Definition .class ));
58+ Definition assetHandlerWithCompiler = unit .mock (Definition .class );
59+ expect (routes .get (eq ("/assets/**" ), isA (AssetHandlerWithCompiler .class )))
60+ .andReturn (assetHandlerWithCompiler );
5561
56- LinkedBindingBuilder <Definition > handlerLBB = unit .mock (LinkedBindingBuilder .class );
57- handlerLBB .toInstance (unit .capture (Route .Definition .class ));
58-
59- Multibinder <Definition > mb = unit .mock (Multibinder .class );
60- expect (mb .addBinding ()).andReturn (varsLBB );
61- expect (mb .addBinding ()).andReturn (handlerLBB );
62-
63- unit .mockStatic (Multibinder .class );
64- expect (Multibinder .newSetBinder (binder , Route .Definition .class )).andReturn (mb );
62+ Env env = unit .get (Env .class );
63+ expect (env .routes ()).andReturn (routes );
6564 }).expect (unit -> {
6665 Env env = unit .get (Env .class );
6766
@@ -86,17 +85,12 @@ public void configure() throws Exception {
8685 }).run (unit -> {
8786 new Assets ()
8887 .configure (unit .get (Env .class ), conf , unit .get (Binder .class ));
89- } , unit -> {
90- Filter handler = unit .captured (Route .Definition .class ).iterator ().next ().filter ();
91- handler .handle (unit .get (Request .class ), unit .get (Response .class ),
92- unit .get (Route .Chain .class ));
93- } , unit -> {
94- Filter handler = unit .captured (Route .Definition .class ).get (1 ).filter ();
95- assertTrue (handler instanceof AssetHandlerWithCompiler );
88+ }, unit -> {
89+ unit .captured (AssetVars .class ).iterator ().next ().handle (unit .get (Request .class ),
90+ unit .get (Response .class ), unit .get (Route .Chain .class ));
9691 });
9792 }
9893
99- @ SuppressWarnings ("unchecked" )
10094 @ Test
10195 public void configureWithWatch () throws Exception {
10296 Config conf = ConfigFactory .empty ()
@@ -110,42 +104,35 @@ public void configureWithWatch() throws Exception {
110104 expect (compiler .patterns ()).andReturn (Sets .newHashSet ("/assets/**" ));
111105 unit .registerMock (AssetCompiler .class , compiler );
112106 }).expect (unit -> {
113- Binder binder = unit .get (Binder .class );
107+ AssetCompiler compiler = unit .get (AssetCompiler .class );
114108
115- LinkedBindingBuilder <Definition > varsLBB = unit .mock (LinkedBindingBuilder .class );
116- varsLBB .toInstance (unit .capture (Route .Definition .class ));
109+ Definition assetVars = unit .mock (Definition .class );
110+ expect (assetVars .name ("/assets/vars" )).andReturn (assetVars );
111+ Routes routes = unit .mock (Routes .class );
112+ expect (routes .use (eq ("*" ), eq ("*" ), unit .capture (AssetVars .class ))).andReturn (assetVars );
117113
118- LinkedBindingBuilder <Definition > handlerLBB = unit .mock (LinkedBindingBuilder .class );
119- handlerLBB .toInstance (unit .capture (Route .Definition .class ));
114+ Definition assetHandlerWithCompiler = unit .mock (Definition .class );
115+ expect (routes .get (eq ("/assets/**" ), isA (AssetHandlerWithCompiler .class )))
116+ .andReturn (assetHandlerWithCompiler );
120117
121- Multibinder <Definition > mb = unit .mock (Multibinder .class );
122- expect (mb .addBinding ()).andReturn (varsLBB );
123- expect (mb .addBinding ()).andReturn (handlerLBB );
124- unit .registerMock (Multibinder .class , mb );
118+ Definition liveCompilerRoute = unit .mock (Definition .class );
119+ expect (liveCompilerRoute .name ("/assets/compiler" )).andReturn (liveCompilerRoute );
125120
126- unit .mockStatic (Multibinder .class );
127- expect (Multibinder .newSetBinder (binder , Route .Definition .class )).andReturn (mb );
121+ LiveCompiler liveCompiler = unit .constructor (LiveCompiler .class )
122+ .args (Config .class , AssetCompiler .class )
123+ .build (conf , compiler );
124+ expect (routes .use (eq ("*" ), eq ("*" ), eq (liveCompiler ))).andReturn (liveCompilerRoute );
125+
126+ Env env = unit .get (Env .class );
127+ expect (env .routes ()).andReturn (routes );
128128 }).expect (unit -> {
129129 Env env = unit .get (Env .class );
130130
131131 expect (env .name ()).andReturn ("dev" );
132132 }).expect (unit -> {
133- AssetCompiler compiler = unit .get (AssetCompiler .class );
134-
135- unit .constructor (LiveCompiler .class )
136- .args (Config .class , AssetCompiler .class )
137- .build (conf , compiler );
138-
139133 Env env = unit .get (Env .class );
140134 expect (env .onStart (isA (CheckedRunnable .class ))).andReturn (env );
141135 expect (env .onStop (isA (CheckedRunnable .class ))).andReturn (env );
142-
143- LinkedBindingBuilder <Definition > lbblc = unit .mock (LinkedBindingBuilder .class );
144- lbblc .toInstance (unit .capture (Route .Definition .class ));
145-
146- Multibinder <Definition > mbr = unit .get (Multibinder .class );
147- expect (mbr .addBinding ()).andReturn (lbblc );
148-
149136 }).expect (unit -> {
150137 AssetCompiler compiler = unit .get (AssetCompiler .class );
151138 expect (compiler .keySet ()).andReturn (Sets .newHashSet ("home" ));
@@ -164,17 +151,12 @@ public void configureWithWatch() throws Exception {
164151 }).run (unit -> {
165152 new Assets ()
166153 .configure (unit .get (Env .class ), conf , unit .get (Binder .class ));
167- } , unit -> {
168- Filter handler = unit .captured (Route .Definition .class ).iterator ().next ().filter ();
169- handler .handle (unit .get (Request .class ), unit .get (Response .class ),
170- unit .get (Route .Chain .class ));
171- } , unit -> {
172- Filter handler = unit .captured (Route .Definition .class ).get (1 ).filter ();
173- assertTrue (handler instanceof LiveCompiler );
154+ }, unit -> {
155+ unit .captured (AssetVars .class ).iterator ().next ().handle (unit .get (Request .class ),
156+ unit .get (Response .class ), unit .get (Route .Chain .class ));
174157 });
175158 }
176159
177- @ SuppressWarnings ("unchecked" )
178160 @ Test
179161 public void configuredist () throws Exception {
180162 Config conf = ConfigFactory .empty ()
@@ -193,20 +175,17 @@ public void configuredist() throws Exception {
193175 expect (compiler .patterns ()).andReturn (Sets .newHashSet ("/assets/**" ));
194176 unit .registerMock (AssetCompiler .class , compiler );
195177 }).expect (unit -> {
196- Binder binder = unit .get (Binder .class );
178+ Definition assetVars = unit .mock (Definition .class );
179+ expect (assetVars .name ("/assets/vars" )).andReturn (assetVars );
180+ Routes routes = unit .mock (Routes .class );
181+ expect (routes .use (eq ("*" ), eq ("*" ), unit .capture (AssetVars .class ))).andReturn (assetVars );
197182
198- LinkedBindingBuilder <Definition > varsLBB = unit .mock (LinkedBindingBuilder .class );
199- varsLBB .toInstance (unit .capture (Route .Definition .class ));
200-
201- LinkedBindingBuilder <Definition > handlerLBB = unit .mock (LinkedBindingBuilder .class );
202- handlerLBB .toInstance (unit .capture (Route .Definition .class ));
203-
204- Multibinder <Definition > mb = unit .mock (Multibinder .class );
205- expect (mb .addBinding ()).andReturn (varsLBB );
206- expect (mb .addBinding ()).andReturn (handlerLBB );
183+ Definition assetHandlerWithCompiler = unit .mock (Definition .class );
184+ expect (routes .get (eq ("/assets/**" ), isA (AssetHandler .class )))
185+ .andReturn (assetHandlerWithCompiler );
207186
208- unit .mockStatic ( Multibinder .class );
209- expect (Multibinder . newSetBinder ( binder , Route . Definition . class )).andReturn (mb );
187+ Env env = unit .get ( Env .class );
188+ expect (env . routes ( )).andReturn (routes );
210189 }).expect (unit -> {
211190 Env env = unit .get (Env .class );
212191
@@ -230,13 +209,9 @@ public void configuredist() throws Exception {
230209 }).run (unit -> {
231210 new Assets ()
232211 .configure (unit .get (Env .class ), conf , unit .get (Binder .class ));
233- } , unit -> {
234- Filter handler = unit .captured (Route .Definition .class ).iterator ().next ().filter ();
235- handler .handle (unit .get (Request .class ), unit .get (Response .class ),
236- unit .get (Route .Chain .class ));
237- } , unit -> {
238- Filter handler = unit .captured (Route .Definition .class ).get (1 ).filter ();
239- assertTrue (handler instanceof AssetHandler );
212+ }, unit -> {
213+ unit .captured (AssetVars .class ).iterator ().next ().handle (unit .get (Request .class ),
214+ unit .get (Response .class ), unit .get (Route .Chain .class ));
240215 });
241216 }
242217
@@ -289,11 +264,11 @@ public void configureFails() throws Exception {
289264 }).run (unit -> {
290265 new Assets ()
291266 .configure (unit .get (Env .class ), conf , unit .get (Binder .class ));
292- } , unit -> {
267+ }, unit -> {
293268 Filter handler = unit .captured (Route .Definition .class ).iterator ().next ().filter ();
294269 handler .handle (unit .get (Request .class ), unit .get (Response .class ),
295270 unit .get (Route .Chain .class ));
296- } , unit -> {
271+ }, unit -> {
297272 Filter handler = unit .captured (Route .Definition .class ).get (1 ).filter ();
298273 assertTrue (handler instanceof AssetHandlerWithCompiler );
299274 });
0 commit comments