/*
* Copyright 2013 The Sculptor Project Team, including the original
* author or authors.
*
* 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
*
* http://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 org.sculptor.framework.test;
import org.sculptor.framework.context.ServiceContext;
import org.sculptor.framework.context.ServiceContextFactory;
import org.sculptor.framework.util.FactoryConfiguration;
import com.google.appengine.api.mail.dev.LocalMailService;
import com.google.appengine.tools.development.ApiProxyLocal;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalMailServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalMemcacheServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig;
import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig;
import com.google.apphosting.api.ApiProxy;
public class AppEngineTestHelper {
private static ThreadLocal<LocalServiceTestHelper> currentHelper = new ThreadLocal<LocalServiceTestHelper>();
static {
ServiceContextFactory.setConfiguration(new FactoryConfiguration() {
@Override
public String getFactoryImplementationClassName() {
return "org.sculptor.framework.context.AppEngineJUnitServiceContextFactory";
}
});
}
private AppEngineTestHelper() {
}
public static LocalServiceTestHelper getCurrentHelper() {
return currentHelper.get();
}
public static void setCurrentHelper(LocalServiceTestHelper helper) {
currentHelper.set(helper);
}
public static void setUpAppEngine(ApiProxy.Environment testEnvironment) {
LocalServiceTestHelper testHelper = getCurrentHelper();
if (testHelper == null) {
LocalServiceTestConfig[] configs = { new LocalDatastoreServiceTestConfig(),
new LocalMailServiceTestConfig(), new LocalMemcacheServiceTestConfig(),
new LocalTaskQueueTestConfig(), new LocalUserServiceTestConfig() };
testHelper = new LocalServiceTestHelper(configs);
setCurrentHelper(testHelper);
}
testHelper.setEnvAppId(testEnvironment.getAppId());
testHelper.setEnvAuthDomain(testEnvironment.getAuthDomain());
testHelper.setEnvEmail(testEnvironment.getEmail());
testHelper.setEnvIsAdmin(testEnvironment.isAdmin());
testHelper.setEnvIsLoggedIn(testEnvironment.isLoggedIn());
// testHelper.setEnvRequestNamespace(NamespaceManager.getGoogleAppsNamespace());
// testHelper.setEnvRequestNamespace(testEnvironment.getRequestNamespace());
testHelper.setEnvVersionId(testEnvironment.getVersionId());
testHelper.setEnvAttributes(testEnvironment.getAttributes());
testHelper.setUp();
}
public static void tearDownAppEngine() {
LocalServiceTestHelper testHelper = getCurrentHelper();
try {
if (testHelper != null) {
testHelper.tearDown();
}
} finally {
setCurrentHelper(null);
}
}
public static void clearSentEmailMessages() {
LocalMailService mailService = getLocalMailService();
mailService.clearSentMessages();
}
public static LocalMailService getLocalMailService() {
ApiProxyLocal proxy = (ApiProxyLocal) ApiProxy.getDelegate();
LocalMailService mailService = (LocalMailService) proxy.getService("mail");
return mailService;
}
public static ServiceContext getServiceContext() {
return ServiceContextFactory.createServiceContext(ApiProxy.getCurrentEnvironment().getAppId());
}
}