1 | // Copyright (C) 2010 Google Inc. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package com.google.caja.plugin; |
16 | |
17 | import com.google.caja.lang.css.CssSchema; |
18 | import com.google.caja.lang.html.HtmlSchema; |
19 | import com.google.caja.parser.quasiliteral.ModuleManager; |
20 | import com.google.caja.reporting.TestBuildInfo; |
21 | import com.google.caja.util.CajaTestCase; |
22 | import com.google.caja.util.Lists; |
23 | import com.google.caja.util.MoreAsserts; |
24 | import com.google.caja.util.Pipeline; |
25 | |
26 | import java.util.Arrays; |
27 | import java.util.List; |
28 | |
29 | public class PipelineMakerTest extends CajaTestCase { |
30 | public final void testDefaultPipeline() throws Exception { |
31 | assertPipeline( |
32 | PipelineMaker.DEFAULT_PRECONDS, |
33 | PipelineMaker.DEFAULT_GOALS, |
34 | "LegacyNamespaceFixupStage", |
35 | "ResolveUriStage", |
36 | "RewriteHtmlStage", |
37 | "InlineCssImportsStage", |
38 | "SanitizeHtmlStage", |
39 | "ValidateCssStage", |
40 | "RewriteCssStage", |
41 | "HtmlToBundleStage", |
42 | "OptimizeJavascriptStage", |
43 | "ValidateJavascriptStage", |
44 | "ConsolidateCodeStage", |
45 | "CheckForErrorsStage"); |
46 | } |
47 | |
48 | public final void testJsOnlyOutput() throws Exception { |
49 | assertPipeline( |
50 | PipelineMaker.DEFAULT_PRECONDS, |
51 | PipelineMaker.DEFAULT_GOALS.without(PipelineMaker.HTML_SAFE_STATIC), |
52 | "LegacyNamespaceFixupStage", |
53 | "ResolveUriStage", |
54 | "RewriteHtmlStage", |
55 | "InlineCssImportsStage", |
56 | "SanitizeHtmlStage", |
57 | "ValidateCssStage", |
58 | "RewriteCssStage", |
59 | "HtmlToJsStage", |
60 | "OptimizeJavascriptStage", |
61 | "ValidateJavascriptStage", |
62 | "ConsolidateCodeStage", |
63 | "CheckForErrorsStage"); |
64 | } |
65 | |
66 | public final void testDebuggingModePipeline() throws Exception { |
67 | assertPipeline( |
68 | PipelineMaker.DEFAULT_PRECONDS, |
69 | PipelineMaker.DEFAULT_GOALS |
70 | .without(PipelineMaker.ONE_CAJOLED_MODULE) |
71 | .with(PipelineMaker.ONE_CAJOLED_MODULE_DEBUG), |
72 | "LegacyNamespaceFixupStage", |
73 | "ResolveUriStage", |
74 | "RewriteHtmlStage", |
75 | "InlineCssImportsStage", |
76 | "SanitizeHtmlStage", |
77 | "ValidateCssStage", |
78 | "RewriteCssStage", |
79 | "HtmlToBundleStage", |
80 | "OptimizeJavascriptStage", |
81 | "ValidateJavascriptStage", |
82 | "ConsolidateCodeStage", |
83 | "InferFilePositionsStage", // extra |
84 | "DebuggingSymbolsStage", // extra |
85 | "CheckForErrorsStage"); |
86 | } |
87 | |
88 | public final void testHtmlAlreadyNamespaced() throws Exception { |
89 | assertPipeline( |
90 | PipelineMaker.HTML_XMLNS.with(PipelineMaker.JS).with(PipelineMaker.CSS), |
91 | PipelineMaker.DEFAULT_GOALS, |
92 | "ResolveUriStage", |
93 | "RewriteHtmlStage", |
94 | "InlineCssImportsStage", |
95 | "SanitizeHtmlStage", |
96 | "ValidateCssStage", |
97 | "RewriteCssStage", |
98 | "HtmlToBundleStage", |
99 | "OptimizeJavascriptStage", |
100 | "ValidateJavascriptStage", |
101 | "ConsolidateCodeStage", |
102 | "CheckForErrorsStage"); |
103 | } |
104 | |
105 | public final void testJsOnly() throws Exception { |
106 | assertPipeline( |
107 | PipelineMaker.JS, |
108 | PipelineMaker.ONE_CAJOLED_MODULE.with(PipelineMaker.SANITY_CHECK), |
109 | "OptimizeJavascriptStage", |
110 | "ValidateJavascriptStage", |
111 | "ConsolidateCodeStage", |
112 | "CheckForErrorsStage"); |
113 | } |
114 | |
115 | public final void testFailEarly() { |
116 | try { |
117 | PipelineMaker.planState("bogus"); |
118 | } catch (IllegalArgumentException ex) { |
119 | assertEquals("bogus", ex.getMessage()); |
120 | return; |
121 | } |
122 | fail("completed"); |
123 | } |
124 | |
125 | public final void testNoPath() { |
126 | PipelineMaker pm = new PipelineMaker( |
127 | CssSchema.getDefaultCss21Schema(mq), HtmlSchema.getDefault(mq), |
128 | new ModuleManager( |
129 | TestBuildInfo.getInstance(), UriFetcher.NULL_NETWORK, false, mq), |
130 | PipelineMaker.HTML, PipelineMaker.CSS); |
131 | long t0 = System.nanoTime(); |
132 | try { |
133 | pm.populate(Lists.<Pipeline.Stage<Jobs>>newArrayList()); |
134 | } catch (Planner.UnsatisfiableGoalException ex) { |
135 | long t1 = System.nanoTime(); |
136 | System.err.println("Took " + ((t1 - t0) / 1e9) + "s"); |
137 | return; |
138 | } |
139 | fail("completed"); |
140 | } |
141 | |
142 | private void assertPipeline( |
143 | Planner.PlanState preconds, Planner.PlanState goals, String... expected) |
144 | throws Exception { |
145 | |
146 | List<Pipeline.Stage<Jobs>> stages = Lists.newArrayList(); |
147 | PipelineMaker pm = new PipelineMaker( |
148 | CssSchema.getDefaultCss21Schema(mq), HtmlSchema.getDefault(mq), |
149 | new ModuleManager( |
150 | TestBuildInfo.getInstance(), UriFetcher.NULL_NETWORK, false, mq), |
151 | preconds, goals); |
152 | { |
153 | long t0 = System.nanoTime(); |
154 | pm.populate(stages); |
155 | long t1 = System.nanoTime(); |
156 | System.err.println("Took " + (t1 - t0) / 1e9 + "s"); |
157 | } |
158 | |
159 | List<String> classes = Lists.newArrayList(); |
160 | for (Pipeline.Stage<Jobs> stage : stages) { |
161 | classes.add(stage.getClass().getSimpleName()); |
162 | } |
163 | |
164 | MoreAsserts.assertListsEqual(Arrays.asList(expected), classes); |
165 | } |
166 | } |