-
Notifications
You must be signed in to change notification settings - Fork 44
/
ApplicationTest.java
1845 lines (1601 loc) · 79.1 KB
/
ApplicationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.appengine.tools.admin;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import com.google.appengine.tools.info.AppengineSdk;
import com.google.apphosting.utils.config.AppEngineConfigException;
import com.google.apphosting.utils.config.AppEngineWebXml;
import com.google.apphosting.utils.config.CronXml;
import com.google.apphosting.utils.config.GenerationDirectory;
import com.google.apphosting.utils.config.RetryParametersXml;
import com.google.apphosting.utils.config.StagingOptions;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* Tests for the Application functionality
*
* @author fabbott@google.com (Freeland Abbott)
*/
@RunWith(Parameterized.class)
public class ApplicationTest {
private static final String SDK_ROOT_PROPERTY = "appengine.sdk.root";
private static final String TEST_FILES = getWarPath("sampleapp");
private static final String TEST_FILES_RUNTIME_DEFINED = getWarPath("sampleapp-runtime");
private static final String TEST_FILES_AUTOMATIC_MODULE =
getWarPath("sampleapp-automatic-module");
private static final String TEST_FILES_MANUAL_MODULE = getWarPath("sampleapp-manual-module");
private static final String TEST_FILES_BASIC_MODULE = getWarPath("sampleapp-basic-module");
private static final String TEST_FILES_BACKENDS = getWarPath("sampleapp-backends");
private static final String TEST_JSPX_FILES = getWarPath("sample-jspx");
private static final String TEST_TAGS_FILES = getWarPath("sample-jsptaglibrary");
private static final String TEST_ERROR_IN_TAGS_FILES = getWarPath("sample-error-in-tag-file");
private static final String NOJSP_TEST_FILES = getWarPath("sample-nojsps");
private static final String BAD_WEBXML_TEST_FILES = getWarPath("sample-badweb");
private static final String BAD_RUNTIME_CHANNEL = getWarPath("sample-badruntimechannel");
private static final String TEST_JAVA11 = getWarPath("sample-java11");
private static final String TEST_JAVA17 = getWarPath("sample-java17");
private static final String BAD_ENTRYPOINT = getWarPath("sample-badentrypoint");
private static final String BAD_APPENGINEWEBXML_TEST_FILES = getWarPath("sample-badaeweb");
private static final String BAD_INDEXESXML_TEST_FILES = getWarPath("sample-badindexes");
private static final String MISSING_APP_ID_TEST_FILES = getWarPath("sample-missingappid");
private static final String INCLUDE_HTTP_HEADERS_TEST_FILES = getWarPath("http-headers");
private static final String XMLORDERING_TEST_FILES = getWarPath("xmlorder");
private static final String BAD_CRONXML_TEST_FILES = getWarPath("badcron");
private static final String JAVA8_NO_WEBXML = getWarPath("java8-no-webxml");
private static final String CRON_RETRY_PARAMETERS_TEST_FILES =
getWarPath("cron-good-retry-parameters");
private static final String CRON_NEGATIVE_RETRY_LIMIT_TEST_FILES =
getWarPath("cron-negative-retry-limit");
private static final String CRON_NEGATIVE_MAX_BACKOFF_TEST_FILES =
getWarPath("cron-negative-max-backoff");
private static final String CRON_TWO_MAX_DOUBLINGS_TEST_FILES =
getWarPath("cron-two-max-doublings");
private static final String CRON_BAD_AGE_LIMIT_TEST_FILES = getWarPath("cron-bad-job-age-limit");
private static final String LEGACY_AUTO_ID_POLICY_TEST_FILES =
getWarPath("sample-legacy-auto-ids");
private static final String UNSPECIFIED_AUTO_ID_POLICY_TEST_FILES =
getWarPath("sample-unspecified-auto-ids");
private static final String DEFAULT_AUTO_ID_POLICY_TEST_FILES =
getWarPath("sample-default-auto-ids");
private static final String JAVA8_JAR_TEST_FILES = getWarPath("java8-jar");
private static final String CLASSES_TEST_FILES = getWarPath("sample-with-classes");
private static final String SDK_ROOT =getSDKRoot();
private static final String SERVLET3_STANDARD_APP_ROOT =getWarPath("bundle_standard");
private static final String SERVLET3_STANDARD_APP_NO_JSP_ROOT =
getWarPath("bundle_standard_with_no_jsp");
private static final String SERVLET3_STANDARD_WEBLISTENER_MEMCACHE =
getWarPath("bundle_standard_with_weblistener_memcache");
private static final String SERVLET3_STANDARD_APP_WITH_CONTAINER_INIT =
getWarPath("bundle_standard_with_container_initializer");
private static final String SERVLET3_APP_ID = "servlet3-demo";
private static final String STAGE_TEST_APP = getWarPath("stage-sampleapp");
private static final String STAGE_WITH_APPID_AND_VERSION_TEST_APP =
getWarPath("stage-with-appid-and-version");
private static final String STAGE_WITH_STAGING_OPTIONS = getWarPath("stage-with-staging-options");
private static final int RANDOM_HTML_SIZE = 704;
private static final String APPID = "sampleapp";
private static final String MODULE_ID = "stan";
private static final String APPVER = "1";
private String oldSdkRoot;
@Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
private static final String APPENGINE_WEB_XML_CONTENT =
"<appengine-web-app xmlns=\"http://appengine.google.com/ns/1.0\">"
+ "<application>sampleapp</application><version>1</version><static-files/>"
+ "<threadsafe>true</threadsafe><vm>true</vm><resource-files/></appengine-web-app>";
@Parameterized.Parameters
public static Collection jetty12() {
return Arrays.asList(new Object[][] {{true}, {false}});
}
public ApplicationTest(Boolean useJetty12) {
System.setProperty("appengine.use.jetty12", useJetty12.toString());
System.setProperty("appengine.sdk.root", "../../sdk_assembly/target/appengine-java-standard");
AppengineSdk.resetSdk();
}
private static String getWarPath(String directoryName) {
File currentDirectory = new File("").getAbsoluteFile();
String appRoot =
new File(
currentDirectory,
"../testlocalapps/"
+ directoryName
+ "/target/"
+ directoryName
+ "-2.0.22-SNAPSHOT")
.getAbsolutePath();
// assertThat(appRoot.isDirectory()).isTrue();
return appRoot;
}
private static String getSDKRoot() {
File currentDirectory = new File("").getAbsoluteFile();
String sdkRoot= null;
try {
sdkRoot =
new File(currentDirectory, "../../sdk_assembly/target/appengine-java-sdk")
.getCanonicalPath();
} catch (IOException ex) {
Logger.getLogger(ApplicationTest.class.getName()).log(Level.SEVERE, null, ex);
}
return sdkRoot;
}
/** Set the appengine.sdk.root system property to make SdkInfo happy. */
@Before
public void setUp() {
oldSdkRoot = System.setProperty(SDK_ROOT_PROPERTY, SDK_ROOT);
}
@After
public void tearDown() {
if (oldSdkRoot != null) {
System.setProperty(SDK_ROOT_PROPERTY, oldSdkRoot);
} else {
System.clearProperty(SDK_ROOT_PROPERTY);
}
}
private static void checkIfEntryIsInJarFile(File jarFile, String entryName) {
assertThat(jarFile.exists()).isTrue();
try {
JarFile jar = new JarFile(jarFile);
try {
Enumeration<JarEntry> jarEntries = jar.entries();
while (jarEntries.hasMoreElements()) {
JarEntry jarEntry = jarEntries.nextElement();
if (jarEntry.getName().equals(entryName)) {
return;
}
}
} finally {
jar.close();
}
} catch (IOException e) {
throw new AssertionError("Cannot read jar file: " + jarFile.getAbsolutePath(), e);
}
fail("Jar does not contain entry: " + entryName);
}
@Test
public void testModuleOverride() throws IOException {
Application testApp = Application.readApplication(TEST_FILES_AUTOMATIC_MODULE);
assertThat(testApp.getAppId()).isEqualTo(APPID);
assertThat(testApp.getModule()).isEqualTo(MODULE_ID);
assertThat(testApp.getVersion()).isEqualTo(APPVER);
testApp = Application.readApplication(TEST_FILES_AUTOMATIC_MODULE, null, "newModule", null);
assertThat(testApp.getAppId()).isEqualTo(APPID);
assertThat(testApp.getModule()).isEqualTo("newModule");
assertThat(testApp.getVersion()).isEqualTo(APPVER);
testApp =
Application.readApplication(
TEST_FILES_AUTOMATIC_MODULE, "newId", "newModule", "newVersion");
assertThat(testApp.getAppId()).isEqualTo("newId");
assertThat(testApp.getModule()).isEqualTo("newModule");
assertThat(testApp.getVersion()).isEqualTo("newVersion");
}
@Test
public void testAppIdAppVerOverride() throws IOException {
Application testApp = Application.readApplication(TEST_FILES);
assertThat(testApp.getAppId()).isEqualTo(APPID);
assertThat(testApp.getModule()).isNull();
assertThat(testApp.getVersion()).isEqualTo(APPVER);
testApp = Application.readApplication(TEST_FILES, null, null, null);
assertThat(testApp.getAppId()).isEqualTo(APPID);
assertThat(testApp.getModule()).isNull();
assertThat(testApp.getVersion()).isEqualTo(APPVER);
testApp = Application.readApplication(TEST_FILES, null, null, "newVersion");
assertThat(testApp.getAppId()).isEqualTo(APPID);
assertThat(testApp.getModule()).isNull();
assertThat(testApp.getVersion()).isEqualTo("newVersion");
testApp = Application.readApplication(TEST_FILES, "newId", null, null);
assertThat(testApp.getAppId()).isEqualTo("newId");
assertThat(testApp.getModule()).isNull();
assertThat(testApp.getVersion()).isEqualTo(APPVER);
testApp = Application.readApplication(TEST_FILES, null, "newModule", null);
assertThat(testApp.getAppId()).isEqualTo(APPID);
assertThat(testApp.getModule()).isEqualTo("newModule");
assertThat(testApp.getVersion()).isEqualTo(APPVER);
testApp = Application.readApplication(TEST_FILES, "newId", "newModule", "newVersion");
assertThat(testApp.getAppId()).isEqualTo("newId");
assertThat(testApp.getModule()).isEqualTo("newModule");
assertThat(testApp.getVersion()).isEqualTo("newVersion");
}
@Test
public void testMissingAppIdNoOverride() throws Exception {
Application testApp =
Application.readApplication(MISSING_APP_ID_TEST_FILES, null, null, "ignoredVersion");
assertThrows(AppEngineConfigException.class, () -> testApp.validate());
}
@Test
public void testMissingAppIdAndVersionWithOverride() throws Exception {
Application testApp =
Application.readApplication(MISSING_APP_ID_TEST_FILES, "newId", null, "newVersion");
assertThat(testApp.getAppId()).isEqualTo("newId");
assertThat(testApp.getVersion()).isEqualTo("newVersion");
}
@Test
public void testLegacyAutoIdPolicy() throws IOException {
Application testApp = Application.readApplication(LEGACY_AUTO_ID_POLICY_TEST_FILES);
assertThat(testApp.getAppId()).isEqualTo(APPID);
assertThat(testApp.getAppEngineWebXml().getAutoIdPolicy()).isEqualTo("legacy");
}
@Test
public void testDefaultAutoIdPolicy() throws IOException {
Application testApp = Application.readApplication(DEFAULT_AUTO_ID_POLICY_TEST_FILES);
assertThat(testApp.getAppId()).isEqualTo(APPID);
assertThat(testApp.getAppEngineWebXml().getAutoIdPolicy()).isEqualTo("default");
}
@Test
public void testUnspecifiedAutoIdPolicy() throws IOException {
Application testApp = Application.readApplication(UNSPECIFIED_AUTO_ID_POLICY_TEST_FILES);
assertThat(testApp.getAppId()).isEqualTo(APPID);
assertThat(testApp.getAppEngineWebXml().getAutoIdPolicy()).isNull();
}
@Test
public void testReadApplicationForStaging() throws IOException {
Application testApp = Application.readApplication(STAGE_TEST_APP, null, null, null);
assertThat(testApp.getAppId()).isNull();
assertThat(testApp.getVersion()).isNull();
// should not throw exception
testApp.validateForStaging();
}
@Test
public void testReadApplicationForStagingWithAppIdAndVersionFromCommandLine() throws IOException {
Application testApp =
Application.readApplication(STAGE_TEST_APP, "a-override", null, "v-override");
// should not throw exception
testApp.validateForStaging();
}
//TODO(ludo) @Test
public void testReadApplicationForStagingWithAppIdAndVersionFromFile() throws IOException {
Application testApp =
Application.readApplication(STAGE_WITH_APPID_AND_VERSION_TEST_APP, null, null, null);
// should not throw exception
testApp.validateForStaging();
}
@Test
public void testStagingWithFiles() throws Exception {
Application testApp = Application.readApplication(TEST_FILES);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
assertThat(testApp.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
opts.setStagingOptions(StagingOptions.builder().setSplitJarFiles(Optional.of(true)).build());
testApp.createStagingDirectory(opts);
testStagedFiles(testApp);
File stage = testApp.getStagingDir();
// Does not work when you are on github automation
// assertThat(new File(stage, "WEB-INF/classes/source-context.json").canRead()).isFalse();
File appYaml = new File(stage, "WEB-INF/appengine-generated/app.yaml");
assertFileContains(appYaml, "application: 'sampleapp'");
assertFileContains(appYaml, "\nversion: '1'");
}
@Test
public void testStagingWithRelativeFiles() throws Exception {
// If this assumption is false, presumably we're on Windows somehow, and our fabricated
// relative path probably won't work because of those stupid C: drive letter things.
assumeTrue(File.separatorChar == '/');
String currentDirAbsolute = Paths.get("").toAbsolutePath().toString();
assertThat(currentDirAbsolute).startsWith("/");
String currentDirToRoot = currentDirAbsolute.substring(1).replaceAll("([^/]+)", "..");
assertThat(TEST_FILES).startsWith("/");
String testFilesRelative = currentDirToRoot + TEST_FILES;
assertThat(new File(testFilesRelative).isDirectory()).isTrue();
Application testApp = Application.readApplication(testFilesRelative);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
assertThat(testApp.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
opts.setStagingOptions(StagingOptions.builder().setSplitJarFiles(Optional.of(true)).build());
testApp.createStagingDirectory(opts);
testStagedFiles(testApp);
File stage = testApp.getStagingDir();
// Does not work when you are on github automation
// assertThat(new File(stage, "WEB-INF/classes/source-context.json").canRead()).isFalse();
File appYaml = new File(stage, "WEB-INF/appengine-generated/app.yaml");
assertFileContains(appYaml, "application: 'sampleapp'");
assertFileContains(appYaml, "\nversion: '1'");
}
@Test
public void testZipClassesDirectoryWithoutClasses() throws Exception {
Application testApp = Application.readApplication(TEST_FILES);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
assertThat(testApp.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
opts.setStagingOptions(
StagingOptions.builder()
.setSplitJarFiles(Optional.of(true))
.setJarClasses(Optional.of(true))
.build());
testApp.createStagingDirectory(opts);
testStagedFiles(testApp);
File stage = testApp.getStagingDir();
File appYaml = new File(stage, "WEB-INF/appengine-generated/app.yaml");
assertFileContains(appYaml, "application: 'sampleapp'");
}
/**
* The sane staging defaults split jars and jars classes, hence re-using logic from the above test
* (testZipClassesDirectoryWithoutClasses).
*/
@Test
public void testSaneStagingDefaults() throws Exception {
Application testApp = Application.readApplication(TEST_FILES);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
assertThat(testApp.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
opts.setDefaultStagingOptions(StagingOptions.SANE_DEFAULTS);
testApp.createStagingDirectory(opts);
testStagedFiles(testApp);
File stage = testApp.getStagingDir();
File appYaml = new File(stage, "WEB-INF/appengine-generated/app.yaml");
assertFileContains(appYaml, "application: 'sampleapp'");
}
@Test
public void testStagingWithSourceContext() throws Exception {
RepoInfo.SourceContext testSourceContext =
RepoInfo.SourceContext.createFromUrl(
"https://source.developers.google.com/p/testing/r/default", "dummyrevision");
String testSourceContextJson =
"{\"cloudRepo\": {\"repoId\": {\"projectRepoId\":"
+ " {\"projectId\": \"testing\", \"repoName\": \"default\"}},"
+ " \"revisionId\": \"dummyrevision\"}}";
Application testApp = Application.readApplication(TEST_FILES, testSourceContext);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
assertThat(testApp.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
opts.setStagingOptions(StagingOptions.builder().setSplitJarFiles(Optional.of(true)).build());
testApp.createStagingDirectory(opts);
testStagedFiles(testApp);
File stage = testApp.getStagingDir();
File jsonFile = new File(stage, "WEB-INF/classes/source-context.json");
assertThat(jsonFile.isFile()).isTrue();
String contents = Files.asCharSource(jsonFile, StandardCharsets.UTF_8).read();
assertThat(testSourceContextJson).isEqualTo(contents);
File appYaml = new File(stage, "WEB-INF/appengine-generated/app.yaml");
assertFileContains(appYaml, "application: 'sampleapp'");
assertFileContains(appYaml, "\nversion: '1'");
}
@Test
public void testStagingForGcloudWithFilesAndConfigErasure() throws Exception {
Application testApp = Application.readApplication(TEST_FILES, null, null, null);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
testApp.validateForStaging();
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
opts.setStagingOptions(StagingOptions.builder().setSplitJarFiles(Optional.of(true)).build());
File stagingDir = testApp.createStagingDirectory(opts, temporaryFolder.newFolder());
testStagedFiles(testApp);
// user staging copies yaml files from appengine-generated into the root
File copiedAppYaml = new File(stagingDir, "app.yaml");
File originalAppYaml = new File(stagingDir, "WEB-INF/appengine-generated/app.yaml");
assertThat(copiedAppYaml.canRead()).isTrue();
// the copied app.yaml is the original with a precautionary new line and an
// extra 'skip_files' line.
List<String> expectedCopiedAppYaml = Files.readLines(originalAppYaml, UTF_8);
expectedCopiedAppYaml.add("");
expectedCopiedAppYaml.add("skip_files: app.yaml");
assertThat(Files.readLines(copiedAppYaml, UTF_8)).isEqualTo(expectedCopiedAppYaml);
String yamlString = Files.asCharSource(copiedAppYaml, UTF_8).read();
assertThat(yamlString.startsWith("application:")).isFalse();
assertThat(yamlString).doesNotContain("\napplication:");
assertThat(yamlString.startsWith("version:")).isFalse();
assertThat(yamlString).doesNotContain("\nversion:");
assertFileContains(copiedAppYaml, "\nskip_files: app.yaml");
}
private static void testStagedFiles(Application testApp) throws Exception {
File stage = testApp.getStagingDir();
File html = new File(stage, "__static__/random.html");
assertWithMessage("Can read " + html).that(html.canRead()).isTrue();
assertThat(html.length()).isEqualTo(RANDOM_HTML_SIZE);
assertThat(new File(stage, "WEB-INF").isDirectory()).isTrue();
assertThat(new File(stage, "WEB-INF/web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-web.xml").canRead()).isTrue();
File jspJar = new File(stage, "WEB-INF/lib/_ah_compiled_jsps-0000.jar");
assertThat(jspJar.exists()).isTrue();
// Test that the classes in the generated jar are for Java8.
assertThat(getJavaJarVersion(jspJar)).isEqualTo(8);
checkIfEntryIsInJarFile(jspJar, "org/apache/jsp/nested/testing_jsp.class");
assertThat(new File(stage, "nested/dukebanner.html").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/lib").isDirectory()).isTrue();
File appYaml = new File(stage, "WEB-INF/appengine-generated/app.yaml");
assertThat(Files.asCharSource(appYaml, UTF_8).read()).contains("api_version: 'user_defined'");
int count = 0;
for (File file : AppengineSdk.getSdk().getUserJspLibFiles()) {
if (file.getName().contains("apache-jsp")) {
count++;
}
}
// Cannot have both the -nolog.jar and the regular jar.
assertThat(count).isEqualTo(2); // org.eclipse and org.mortbay
}
//TODO(ludo) @Test
public void testStageForGcloudOnlyCopyAppYamlToRoot() throws IOException {
Application testApp =
Application.readApplication(getWarPath("stage-with-all-xmls"), null, null, null);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
File stagingDir = testApp.createStagingDirectory(opts, temporaryFolder.newFolder());
File generationDir = GenerationDirectory.getGenerationDirectory(stagingDir);
assertThat(new File(generationDir, "app.yaml").exists()).isTrue();
assertThat(new File(stagingDir, "app.yaml").exists()).isTrue();
assertThat(new File(generationDir, "cron.yaml").exists()).isTrue();
assertThat(new File(stagingDir, "cron.yaml").exists()).isFalse();
assertThat(new File(generationDir, "dispatch.yaml").exists()).isTrue();
assertThat(new File(stagingDir, "dispatch.yaml").exists()).isFalse();
assertThat(new File(generationDir, "dos.yaml").exists()).isTrue();
assertThat(new File(stagingDir, "dos.yaml").exists()).isFalse();
assertThat(new File(generationDir, "index.yaml").exists()).isTrue();
assertThat(new File(stagingDir, "index.yaml").exists()).isFalse();
assertThat(new File(generationDir, "queue.yaml").exists()).isTrue();
assertThat(new File(stagingDir, "queue.yaml").exists()).isFalse();
}
//TODO(ludo) @Test
public void testDoNotStageDispatchForUpdate() throws IOException {
Application testApp =
Application.readApplication(getWarPath("sample-dispatch"), null, null, null);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
File stagingDir = testApp.createStagingDirectory(opts);
File dispatchYaml = new File(stagingDir, "dispatch.yaml");
assertThat(dispatchYaml.exists()).isFalse();
}
@Test
public void testAppEngineApiJarIncluded() throws Exception {
File tmpDir = Files.createTempDir();
try {
doTestAppEngineApiJarIncluded(tmpDir, "impl", "lib/impl/appengine-api.jar");
} finally {
deleteRecursively(tmpDir.toPath());
}
}
private static void doTestAppEngineApiJarIncluded(File tmpDir, String testName, String apiJarPath)
throws Exception {
File sdkRoot = new File(SDK_ROOT);
File apiJar = new File(sdkRoot, apiJarPath);
assertWithMessage(apiJar.toString()).that(apiJar.exists()).isTrue();
//TODO(ludo) File remoteApiJar = new File(sdkRoot, "lib/appengine-remote-api.jar");
//TODO(ludo) assertWithMessage(remoteApiJar.toString()).that(remoteApiJar.exists()).isTrue();
File testDir = new File(tmpDir, testName);
File webInf = new File(testDir, "WEB-INF");
File webInfLib = new File(webInf, "lib");
boolean madeWebInfLib = webInfLib.mkdirs();
assertThat(madeWebInfLib).isTrue();
Files.copy(apiJar, new File(webInfLib, "appengine-api.jar"));
//TODO(ludo) Files.copy(remoteApiJar, new File(webInfLib, "appengine-remote-api.jar"));
File testAppRoot = new File(TEST_FILES);
Files.copy(new File(testAppRoot, "WEB-INF/web.xml"), new File(webInf, "web.xml"));
Files.copy(
new File(testAppRoot, "WEB-INF/appengine-web.xml"), new File(webInf, "appengine-web.xml"));
Application application = Application.readApplication(testDir.getAbsolutePath());
application.setDetailsWriter(new PrintWriter(new OutputStreamWriter(System.out, UTF_8)));
assertThat(application.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
application.createStagingDirectory(opts);
File stage = application.getStagingDir();
File appYaml = new File(stage, "WEB-INF/appengine-generated/app.yaml");
assertFileContains(appYaml, "api_version: 'user_defined'");
File stagedApiJar = new File(stage, "WEB-INF/lib/appengine-api.jar");
assertWithMessage(stagedApiJar.toString()).that(stagedApiJar.exists()).isTrue();
byte[] originalApiJarBytes = Files.toByteArray(apiJar);
byte[] stagedApiJarBytes = Files.toByteArray(stagedApiJar);
assertThat(originalApiJarBytes).isEqualTo(stagedApiJarBytes);
}
@Test
public void testStagingJava8() throws Exception {
Application testApp = Application.readApplication(TEST_FILES);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
assertThat(testApp.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
testApp.createStagingDirectory(opts);
File stage = testApp.getStagingDir();
assertThat(new File(stage, "WEB-INF").isDirectory()).isTrue();
assertThat(new File(stage, "WEB-INF/web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-generated/app.yaml").canRead()).isTrue();
assertFileContains(new File(stage, "WEB-INF/appengine-generated/app.yaml"), "runtime: java8\n");
}
@Test
public void testStagingJava11() throws Exception {
Application testApp = Application.readApplication(TEST_JAVA11);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
testApp.createStagingDirectory(opts);
File stage = testApp.getStagingDir();
assertThat(new File(stage, "WEB-INF").isDirectory()).isTrue();
assertThat(new File(stage, "WEB-INF/web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-generated/app.yaml").canRead()).isTrue();
File generatedAppYaml = new File(stage, "WEB-INF/appengine-generated/app.yaml");
assertFileContains(generatedAppYaml, "runtime: java11\n");
assertFileContains(generatedAppYaml, "runtime_channel: canary\n");
assertFileContains(generatedAppYaml, "entrypoint: 'java -jar foo.jar'\n");
}
@Test
public void testStagingJava17() throws Exception {
Application testApp = Application.readApplication(TEST_JAVA17);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
testApp.createStagingDirectory(opts);
File stage = testApp.getStagingDir();
assertThat(new File(stage, "WEB-INF").isDirectory()).isTrue();
assertThat(new File(stage, "WEB-INF/web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-generated/app.yaml").canRead()).isTrue();
File generatedAppYaml = new File(stage, "WEB-INF/appengine-generated/app.yaml");
assertFileContains(generatedAppYaml, "runtime: java17\n");
assertFileContains(generatedAppYaml, "runtime_channel: canary\n");
assertFileContains(generatedAppYaml, "entrypoint: 'java -jar foo.jar'\n");
}
@Test
public void testJspCompilerJava8() throws Exception {
Application testApp = Application.readApplication(SERVLET3_STANDARD_APP_ROOT);
assertThat(testApp.getJSPCClassName())
.contains("com.google.appengine.tools.development.jetty");
assertThat(testApp.getJSPCClassName())
.contains("LocalJspC");
}
@Test
public void testStagingJava8RuntimeJava8JarFile() throws Exception {
Application testApp = Application.readApplication(JAVA8_JAR_TEST_FILES);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
opts.setAllowAnyRuntime(true);
opts.setRuntime("java8");
File stage = testApp.createStagingDirectory(opts);
assertThat(new File(stage, "WEB-INF").isDirectory()).isTrue();
assertThat(new File(stage, "WEB-INF/web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-generated/app.yaml").canRead()).isTrue();
assertFileContains(new File(stage, "WEB-INF/appengine-generated/app.yaml"), "runtime: java8\n");
}
@Test
public void testStagingManuallySetUnsupportedRuntime() throws IOException {
try {
Application testApp = Application.readApplication(TEST_FILES);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
assertThat(testApp.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
opts.setRuntime("foo");
testApp.createStagingDirectory(opts);
fail("Did not get expected AppEngineConfigException");
} catch (AppEngineConfigException expected) {
// Expect an exception flagging the runtime id as invalid.
assertThat(expected).hasMessageThat().contains("Invalid runtime id: foo");
}
}
@Test
public void testStagingManuallySetUnsupportedRuntimeAllowAnyRuntime() throws Exception {
Application testApp = Application.readApplication(TEST_FILES);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
assertThat(testApp.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
opts.setRuntime("foo");
opts.setAllowAnyRuntime(true);
testApp.createStagingDirectory(opts);
File stage = testApp.getStagingDir();
assertThat(new File(stage, "WEB-INF").isDirectory()).isTrue();
assertThat(new File(stage, "WEB-INF/web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-generated/app.yaml").canRead()).isTrue();
assertFileContains(new File(stage, "WEB-INF/appengine-generated/app.yaml"), "runtime: foo\n");
}
private static void assertFileContains(File file, String expectedSubstring) throws Exception {
String contentsAsString = Files.asCharSource(file, UTF_8).read();
assertThat(contentsAsString).contains(expectedSubstring);
}
@Test
public void testWithJspx() throws IOException {
Application testApp = Application.readApplication(TEST_JSPX_FILES);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
assertThat(testApp.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
opts.setStagingOptions(StagingOptions.builder().setSplitJarFiles(Optional.of(true)).build());
testApp.createStagingDirectory(opts);
File stage = testApp.getStagingDir();
File html = new File(stage, "__static__/random.html");
assertThat(html.canRead()).isTrue();
assertThat(html.length()).isEqualTo(RANDOM_HTML_SIZE);
assertThat(new File(stage, "WEB-INF").isDirectory()).isTrue();
assertThat(new File(stage, "WEB-INF/web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-web.xml").canRead()).isTrue();
checkIfEntryIsInJarFile(
new File(stage, "WEB-INF/lib/_ah_compiled_jsps-0000.jar"),
"org/apache/jsp/nested/testing_jspx.class");
assertThat(new File(stage, "nested/dukebanner.html").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/lib").isDirectory()).isTrue();
}
/* @Test
public void testWithBigJarWithTlds() throws Exception {
Application testApp =
Application.readApplication(
TestUtil.getSrcDir()
+ "/google3/javatests/com/google/appengine/tools/admin/deploy_fileset_big_jar_tld");
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
opts.setStagingOptions(StagingOptions.builder().setSplitJarFiles(Optional.of(true)).build());
testApp.createStagingDirectory(opts);
File stage = testApp.getStagingDir();
assertThat(new File(stage, "WEB-INF").isDirectory()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/lib").isDirectory()).isTrue();
assertThat(new File(stage, "WEB-INF/lib/bigjar_with_tlds-0000.jar").canRead()).isTrue();
// checkIfEntryIsInJarFile(new File(stage, "WEB-INF/lib/bigjar_with_tlds-0000.jar"),
// "META-INF/fn.tl");
File quickstartXml = new File(stage, "WEB-INF/quickstart-web.xml");
assertThat(quickstartXml.canRead()).isTrue();
Document quickstartDoc =
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(quickstartXml);
// We want to verify that the list of tlds defined by:
// <context-param>
// <param-value><![CDATA[
// "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-0163.jar!/META-INF/fmt-1_0-rt.tld",
// "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-0163.jar!/META-INF/fmt.tld",
// "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-0163.jar!/META-INF/c.tld",
// "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-0163.jar!/META-INF/permittedTaglibs.tld",
// "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-0163.jar!/META-INF/x-1_0.tld",
// "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-0163.jar!/META-INF/fn.tld",
// "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-0163.jar!/META-INF/sql-1_0.tld",
// "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-0163.jar!/META-INF/fmt-1_0.tld",
// "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-0163.jar!/META-INF/sql-1_0-rt.tld",
// "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-0163.jar!/META-INF/x.tld",
// "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-0163.jar!/META-INF/scriptfree.tld",
// "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-0163.jar!/META-INF/c-1_0.tld",
// "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-0163.jar!/META-INF/sql.tld",
// "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-0163.jar!/META-INF/c-1_0-rt.tld",
// "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-0163.jar!/META-INF/x-1_0-rt.tld"
// ...
// We are not sure about the number of split jars, we just need to check if an entry
// contains "jar:${WAR.uri}/WEB-INF/lib/bigjar_with_tlds-" and ".jar!/META-INF/fmt.tld"
NodeList nodeList = quickstartDoc.getElementsByTagName("context-param");
boolean match = false;
for (int i = 0; i < nodeList.getLength(); i++) {
Node contextParam = nodeList.item(i).getFirstChild();
do {
String nodeName = contextParam.getNodeName();
if (nodeName.equals("param-value")) {
String content = contextParam.getFirstChild().getTextContent();
if (content.contains("bigjar_with_tlds")) {
match |= CONTENT_PATTERN.matcher(content).find();
}
}
} while ((contextParam = contextParam.getNextSibling()) != null);
}
assertThat(match).isTrue();
}
*/
private static final Pattern CONTENT_PATTERN =
Pattern.compile(
"\"jar:\\$\\{WAR\\.uri}/WEB-INF/lib/bigjar_with_tlds"
+ "-\\d.*\\.jar!/META-INF/fmt.tld\",?");
@Test
public void testJspWithTag() throws IOException {
Application testApp = Application.readApplication(TEST_TAGS_FILES);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
assertThat(testApp.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
testApp.createStagingDirectory(opts);
File stage = testApp.getStagingDir();
assertThat(new File(stage, "WEB-INF").isDirectory()).isTrue();
assertThat(new File(stage, "WEB-INF/web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-web.xml").canRead()).isTrue();
checkIfEntryIsInJarFile(
new File(stage, "WEB-INF/lib/_ah_compiled_jsps-0000.jar"),
"org/apache/jsp/nested/message_jsp.class");
checkIfEntryIsInJarFile(
new File(stage, "WEB-INF/lib/_ah_compiled_jsps-0000.jar"),
"org/apache/jsp/tag/web/ui/page_tag.class");
assertThat(new File(stage, "nested/message.jsp").canRead()).isTrue();
}
@Test
public void testJspWithTagOnJava8gStandardRuntime() throws Exception {
doTestJspWithRuntime("java8g");
}
@Test
public void testJspWithTagOnJava8StandardRuntime() throws Exception {
doTestJspWithRuntime("java8");
}
private static void doTestJspWithRuntime(String runtime) throws Exception {
Application testApp = Application.readApplication(TEST_TAGS_FILES);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
assertThat(testApp.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
opts.setRuntime(runtime);
opts.setAllowAnyRuntime(true);
// do not jar the generated classes for JSPs.
opts.setStagingOptions(StagingOptions.builder().setJarJsps(Optional.of(false)).build());
testApp.createStagingDirectory(opts);
File stage = testApp.getStagingDir();
File appYaml = new File(stage, "WEB-INF/appengine-generated/app.yaml");
assertFileContains(appYaml, "runtime: " + runtime);
assertThat(new File(stage, "nested/message.jsp").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF").isDirectory()).isTrue();
assertThat(new File(stage, "WEB-INF/web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-web.xml").canRead()).isTrue();
File class1 = new File(stage, "WEB-INF/classes/org/apache/jsp/nested/message_jsp.class");
assertThat(class1.exists()).isTrue();
File class2 = new File(stage, "WEB-INF/classes/org/apache/jsp/tag/web/ui/page_tag.class");
assertThat(class2.exists()).isTrue();
File genCodeDir = testApp.getJspJavaFilesGeneratedTempDirectory();
File servlet2 = new File(genCodeDir, "org/apache/jsp/tag/web/ui/page_tag.java");
assertThat(servlet2.exists()).isTrue();
assertThat(Files.asCharSource(servlet2, UTF_8).read())
.contains("* Version: JspC/ApacheTomcat");
}
@Test
public void testDeleteJSPs() throws IOException {
Application testApp = Application.readApplication(TEST_TAGS_FILES);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
assertThat(testApp.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
opts.setStagingOptions(StagingOptions.builder().setDeleteJsps(Optional.of(true)).build());
testApp.createStagingDirectory(opts);
File stage = testApp.getStagingDir();
assertThat(new File(stage, "WEB-INF").isDirectory()).isTrue();
assertThat(new File(stage, "WEB-INF/web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-web.xml").canRead()).isTrue();
checkIfEntryIsInJarFile(
new File(stage, "WEB-INF/lib/_ah_compiled_jsps-0000.jar"),
"org/apache/jsp/nested/message_jsp.class");
checkIfEntryIsInJarFile(
new File(stage, "WEB-INF/lib/_ah_compiled_jsps-0000.jar"),
"org/apache/jsp/tag/web/ui/page_tag.class");
assertThat(new File(stage, "nested/message.jsp").exists()).isFalse();
}
@Test
public void testDoNotJarJSPs() throws IOException {
Application testApp = Application.readApplication(TEST_TAGS_FILES);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
assertThat(testApp.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
opts.setStagingOptions(StagingOptions.builder().setJarJsps(Optional.of(false)).build());
testApp.createStagingDirectory(opts);
File stage = testApp.getStagingDir();
assertThat(new File(stage, "WEB-INF").isDirectory()).isTrue();
assertThat(new File(stage, "WEB-INF/web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/classes/org/apache/jsp/nested/message_jsp.class").exists())
.isTrue();
assertThat(new File(stage, "WEB-INF/lib/_ah_compiled_jsps-0000.jar").exists()).isFalse();
assertThat(new File(stage, "nested/message.jsp").exists()).isTrue();
}
@Test
public void testErrorInTagFile() throws IOException {
Application testApp = Application.readApplication(TEST_ERROR_IN_TAGS_FILES);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
assertThat(testApp.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
JspCompilationException exception =
assertThrows(JspCompilationException.class, () -> testApp.createStagingDirectory(opts));
assertThat(exception).hasMessageThat().isEqualTo("Failed to compile jsp files.");
}
@Test
public void testZipClassesWithoutJSPs() throws IOException {
Application testApp = Application.readApplication(CLASSES_TEST_FILES);
testApp.setDetailsWriter(
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
assertThat(testApp.getAppId()).isEqualTo(APPID);
ApplicationProcessingOptions opts = new ApplicationProcessingOptions();
opts.setStagingOptions(StagingOptions.builder().setJarClasses(Optional.of(true)).build());
testApp.createStagingDirectory(opts);
File stage = testApp.getStagingDir();
assertThat(new File(stage, "WEB-INF").isDirectory()).isTrue();
assertThat(new File(stage, "WEB-INF/web.xml").canRead()).isTrue();
assertThat(new File(stage, "WEB-INF/appengine-web.xml").canRead()).isTrue();
checkIfEntryIsInJarFile(
new File(stage, "WEB-INF/lib/_ah_webinf_classes-0000.jar"), "foo/AClass.class");
assertThat(new File(stage, "WEB-INF/classes/foo/AClass.class").exists()).isFalse();
}
/**
* Check that precedence is respected, where defaults, appengine-web.xml and flags are applied in
* order. Flags have the highest precedence.