GroovyModelFactory extends AbstractWroModelFactory { @Override public WroModel create() { final StopWatch stopWatch = new StopWatch("Create Wro Model from Groovy"); final Script script; try { stopWatch.start("parseStream"); script = new GroovyShell().parse(new InputStreamReader(new AutoCloseInputStream(getModelResourceAsStream()))); LOG.debug("Parsing groovy script to build the model"); stopWatch.stop(); stopWatch.start("parseScript"); final WroModel model = GroovyModelParser.parse(script); stopWatch.stop(); LOG.debug("groovy model: {}", model); if (model == null) { throw new WroRuntimeException("Invalid content provided, cannot build model!"); } return model; } catch (final IOException e) { throw new WroRuntimeException("Invalid model found!", e); } finally { LOG.debug(stopWatch.prettyPrint()); } }  @Override WroModel create(); static final String ALIAS; }
@Test(expected = WroRuntimeException.class) public void testInvalidStream() throws Exception { factory = new GroovyModelFactory() { @Override protected InputStream getModelResourceAsStream() throws IOException { throw new IOException(); } }; factory.create(); }
@Test public void createValidModel() { factory = new GroovyModelFactory() { @Override protected InputStream getModelResourceAsStream() throws IOException { return TestGroovyModelFactory.class.getResourceAsStream("wro.groovy"); }; }; final WroModel model = factory.create(); assertNotNull(model); final WroModelInspector modelInspector = new WroModelInspector(model); assertEquals(Arrays.asList("g1", "g2"), new WroModelInspector(model).getGroupNames()); assertEquals(2, modelInspector.getGroupByName("g1").getResources().size()); assertTrue(modelInspector.getGroupByName("g1").getResources().get(0).isMinimize()); assertEquals("/static/app.js", modelInspector.getGroupByName("g1").getResources().get(0).getUri()); assertEquals(ResourceType.JS, modelInspector.getGroupByName("g1").getResources().get(0).getType()); assertTrue(modelInspector.getGroupByName("g1").getResources().get(1).isMinimize()); assertEquals("/static/app.css", modelInspector.getGroupByName("g1").getResources().get(1).getUri()); assertEquals(ResourceType.CSS, modelInspector.getGroupByName("g1").getResources().get(1).getType()); assertEquals(2, modelInspector.getGroupByName("g2").getResources().size()); assertFalse(modelInspector.getGroupByName("g2").getResources().get(1).isMinimize()); LOG.debug("model: ", model); }
@Test public void createValidModelContainingHiphen() { factory = new GroovyModelFactory() { @Override protected InputStream getModelResourceAsStream() throws IOException { return getClass().getResourceAsStream("wroWithHiphen.groovy"); } }; final WroModel model = factory.create(); assertNotNull(new WroModelInspector(model).getGroupByName("group-with-hiphen")); }
@Test public void createGroupReferenceOrderShouldNotMatter() { factory = new GroovyModelFactory() { @Override protected InputStream getModelResourceAsStream() throws IOException { return getClass().getResourceAsStream("wroGroupRefOrder.groovy"); } }; assertNotNull(factory.create()); }
@Test public void shouldSupportAbstractGroup() { factory = new GroovyModelFactory() { @Override protected InputStream getModelResourceAsStream() throws IOException { return getClass().getResourceAsStream("abstractGroup.groovy"); } }; final WroModel model = factory.create(); assertNotNull(model); assertEquals(1, model.getGroups().size()); final Group group = model.getGroups().iterator().next(); assertEquals("nonAbstractGroup", group.getName()); assertEquals(1, group.getResources().size()); }
@Test(expected = RecursiveGroupDefinitionException.class) public void testRecursiveGroupReference() { factory = new GroovyModelFactory() { @Override protected InputStream getModelResourceAsStream() throws IOException { return getClass().getResourceAsStream("wroRecursiveReference.groovy"); } }; factory.create(); }
@Test(expected = WroRuntimeException.class) public void testDuplicateGroupName() { factory = new GroovyModelFactory() { @Override protected InputStream getModelResourceAsStream() throws IOException { return getClass().getResourceAsStream("wroDuplicateGroupName.groovy"); } }; factory.create(); }
@Test(expected = WroRuntimeException.class) public void createIncompleteModel() { factory = new GroovyModelFactory() { @Override protected InputStream getModelResourceAsStream() throws IOException { return getClass().getResourceAsStream("IncompleteWro.groovy"); } }; factory.create(); }
@Test public void shouldBeThreadSafe() throws Exception { factory = new GroovyModelFactory() { @Override protected InputStream getModelResourceAsStream() throws IOException { return TestGroovyModelFactory.class.getResourceAsStream("wro.groovy"); }; }; WroTestUtils.init(factory); final WroModel expectedModel = factory.create(); WroTestUtils.runConcurrently(new Callable<Void>() { @Override public Void call() throws Exception { assertEquals(expectedModel, factory.create()); return null; } }); }
@Test public void decoratedModelShouldBeThreadSafe() throws Exception { final List<Transformer<WroModel>> modelTransformers = new ArrayList<Transformer<WroModel>>(); modelTransformers.add(new WildcardExpanderModelTransformer()); factory = DefaultWroModelFactoryDecorator.decorate(new GroovyModelFactory() { @Override protected InputStream getModelResourceAsStream() throws IOException { return TestGroovyModelFactory.class.getResourceAsStream("wro.groovy"); }; }, modelTransformers); WroTestUtils.init(factory); final WroModel expectedModel = factory.create(); WroTestUtils.runConcurrently(new ContextPropagatingCallable<Void>(new Callable<Void>() { @Override public Void call() throws Exception { assertEquals(expectedModel, factory.create()); return null; } })); }