1 // Copyright 2012 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.tools
.development
.testing
;
5 import com
.google
.appengine
.api
.basement
.dev
.LocalBasementService
;
6 import com
.google
.appengine
.tools
.development
.ApiProxyLocal
;
8 import java
.io
.OutputStream
;
11 * Config for accessing the LocalBasementService in tests. In order to verify that the expected
12 * data is being written, you will first need to provide an {@link OutputStream} for the
13 * data to be written to:
15 * private ByteArrayOutputStream out = new ByteArrayOutputStream();
16 * LocalServiceTestHelper helper;
18 * public void createService() {
19 * helper = new LocalServiceTestHelper(
20 * new LocalBasementServiceTestConfig().setProtoStream(out));
25 * Before reading the data from the {@link OutputStream} the test code must end the request
26 * by calling {@link LocalServiceTestHelper#endRequest()}. After doing that, the following
27 * code will read the data back out:
30 * public void verifyBehavior() {
32 * LocalServiceTestHelper.endRequest();
33 * AppExtensions appExtensions = parseProtoRecord();
34 * // Verify the expected AppExtension messages was written.
37 * private AppExtensions parseProtoRecord() throws IOException {
38 * InputRecordStream in = InputRecordStream.newInstance(new ByteArrayInputStream(
39 * protoStream.toByteArray()));
40 * Iterator<ByteBuffer> iterator = in.iterator();
41 * assertTrue("Expecting at least one record.", iterator.hasNext());
42 * AppExtensions appExtensions = new AppExtensions();
43 * assertTrue(appExtensions.mergeFrom(iterator.next()));
44 * assertFalse("Expecting no more than one record.", iterator.hasNext());
45 * return appExtensions;
49 * If you need to simulate multiple requests from a single test, you will need to parse the full
52 * private List<AppExtensions> parseProtoStream() throws IOException {
53 * List<AppExtensions> appExtensionsList = new ArrayList<AppExtensions>();
54 * InputRecordStream in = InputRecordStream.newInstance(new ByteArrayInputStream(
55 * protoStream.toByteArray()));
56 * for (ByteBuffer buffer : in) {
57 * AppExtensions appExtensions = new AppExtensions();
58 * assertTrue(appExtensions.mergeFrom(buffer));
59 * appExtensionsList.add(appExtensions);
61 * return appExtensionsList;
66 public class LocalBasementServiceTestConfig
implements LocalServiceTestConfig
{
67 private boolean enable
= true;
68 private boolean ignore
= false;
69 private boolean logEveryCall
= false;
70 private OutputStream finalProtoStream
= null;
74 ApiProxyLocal proxy
= LocalServiceTestHelper
.getApiProxyLocal();
75 proxy
.setProperty(LocalBasementService
.ENABLE_PROPERTY
, Boolean
.toString(enable
));
76 proxy
.setProperty(LocalBasementService
.IGNORE_PROPERTY
, Boolean
.toString(ignore
));
77 proxy
.setProperty(LocalBasementService
.LOG_EVERY_CALL_PROPERTY
, Boolean
.toString(logEveryCall
));
78 proxy
.setProperty(LocalBasementService
.WRITE_FINAL_PROTO_PROPERTY
,
79 (finalProtoStream
!= null ? Boolean
.TRUE
: Boolean
.FALSE
).toString());
80 getLocalBasementService().injectOutputStream(finalProtoStream
);
84 public void tearDown() {}
86 public static LocalBasementService
getLocalBasementService() {
87 return (LocalBasementService
)
88 LocalServiceTestHelper
.getLocalService(LocalBasementService
.PACKAGE
);
92 * Configure the {@link LocalBasementServiceTestConfig} to create the {@code LocalBasementService}
93 * so that it will write a request's final merged proto to the specified {@link OutputStream}. If
94 * this method is not called or is called with {@code null}, the final merged proto will not
95 * be written. Defaults to {@code null} and must be called (note: only applies to unit tests, in
96 * the Dev App Server defaults to writing to the filesystem).
98 * The final merged proto is only written once the request is over. You can indicate this
99 * to the unit test by either calling {@link LocalServiceTestHelper#endRequest()} or {@link
100 * LocalServiceTestHelper#tearDown()}.
102 * This method must be called prior to calling {@link LocalServiceTestHelper#setUp()}.
104 * @param out A non-closed OutputStream.
105 * @return Itself for easy call chaining.
107 public LocalBasementServiceTestConfig
setProtoStream(OutputStream out
) {
108 finalProtoStream
= out
;
113 * Configure the {@link LocalBasementServiceTestConfig} to create the {@code LocalBasementService}
114 * with the specified enable state. When disabled, the {@LogToSawmillService} entry points will
115 * throw just like the production environment when the app is missing the
116 * SAWMILL_APP_EXTENSIONS_ENABLE permission. Defaults to {@code true}.
118 * This method must be called prior to calling {@link LocalServiceTestHelper#setUp()}.
120 * @param b True to allow the service to log.
121 * @return Itself for easy call chaining.
123 public LocalBasementServiceTestConfig
setEnable(boolean b
) {
129 * Configure the {@link LocalBasementServiceTestConfig} to create the {@code LocalBasementService}
130 * with the specified ignore state. When ignored, {@link LogToSawmillService#log(byte[])} will
131 * drop all date and the the {@LogToSawmillService} entry points will indicate this in their
132 * return value. Defaults to {@code false}.
134 * This method must be called prior to calling {@link LocalServiceTestHelper#setUp()}.
136 * @param b False to allow the service to log.
137 * @return Itself for easy call chaining.
139 public LocalBasementServiceTestConfig
setIgnore(boolean b
) {
145 * Configure the {@link LocalBasementServiceTestConfig} to create the {@code LocalBasementService}
146 * so that it will log every call to {@link LogToSawmillService#log(byte[])} with {@link Logger}.
147 * Defaults to {@code false} (note: only applies to unit tests; in the Dev App Server defaults
150 * This method must be called prior to calling {@link LocalServiceTestHelper#setUp()}.
152 * @param b True to have the local service log each call.
153 * @return Itself for easy call chaining.
155 public LocalBasementServiceTestConfig
setLogEveryCall(boolean b
) {