github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

DomDerrien / two-tiers-utils

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 1
    • 0
  • Source
  • Commits
  • Network (0)
  • Issues (2)
  • Downloads (0)
  • Wiki (3)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Set of utilities to ease the development of modern Web applications — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Fix TMX unit key starting by a hash (#) and control exception messages on 
multiple lines 
DomDerrien (author)
Mon Feb 08 14:16:20 -0800 2010
commit  df4fe69e3e73debc70e1f9dab3511fecbd5d3a74
tree    2b53bb2983c674ff262ddb164f470c5caa3a19c0
parent  a69708bf995f455e8feaf382c5963d28bec51da9
two-tiers-utils / src / Java / com / google / apphosting / api / MockAppEngineEnvironment.java src/Java/com/google/apphosting/api/MockAppEngineEnvironment.java
100644 186 lines (158 sloc) 7.165 kb
edit raw blame history
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
package com.google.apphosting.api;
 
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
 
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
 
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.api.datastore.dev.LocalDatastoreService;
import com.google.appengine.api.labs.taskqueue.QueueFactory;
import com.google.appengine.api.labs.taskqueue.dev.LocalTaskQueue;
import com.google.appengine.api.labs.taskqueue.dev.QueueStateInfo;
import com.google.appengine.api.labs.taskqueue.dev.QueueStateInfo.TaskStateInfo;
import com.google.appengine.tools.development.ApiProxyLocalImpl;
 
/**
* Mock for the App Engine Java environment used by the JDO wrapper.
*
* This class has been build with information gathered on:
* - App Engine documentation: http://code.google.com/appengine/docs/java/howto/unittesting.html
* - App Engine Fan blog: http://blog.appenginefan.com/2009/05/jdo-and-unit-tests.html
*
* @author Dom Derrien
*/
public class MockAppEngineEnvironment {
 
    private class MockApiProxyEnvironment implements ApiProxy.Environment {
        public String getAppId() {
            return "test";
        }
 
        public String getVersionId() {
            return "1.0";
        }
 
        public String getEmail() {
            throw new UnsupportedOperationException();
        }
 
        public boolean isLoggedIn() {
            throw new UnsupportedOperationException();
        }
 
        public boolean isAdmin() {
            throw new UnsupportedOperationException();
        }
 
        public String getAuthDomain() {
            throw new UnsupportedOperationException();
        }
 
        public String getRequestNamespace() {
            return "";
        }
 
        public Map<String, Object> getAttributes() {
            Map<String, Object> out = new HashMap<String, Object>();
 
            // Only be necessary for tasks that are added when there is no "live" request
            // See: http://groups.google.com/group/google-appengine-java/msg/8f5872b052144c8d?pli=1
            out.put("com.google.appengine.server_url_key", "http://localhost:8080");
 
            return out;
        }
    };
 
    private final ApiProxy.Environment env;
    private PersistenceManagerFactory pmf;
 
    public MockAppEngineEnvironment() {
        env = new MockApiProxyEnvironment();
    }
 
    /**
* Setup the mock environment
*/
    public void setUp() throws Exception {
        // Setup the App Engine services
        ApiProxy.setEnvironmentForCurrentThread(env);
        ApiProxyLocalImpl proxy = new ApiProxyLocalImpl(new File(".")) {};
 
        // Setup the App Engine data store
        proxy.setProperty(LocalDatastoreService.NO_STORAGE_PROPERTY, Boolean.TRUE.toString());
        ApiProxy.setDelegate(proxy);
 
        // Setup the Task Queue
        // Nothing special here, just a flush during tearDown()
    }
 
    /**
* Clean up the mock environment
*/
    public void tearDown() throws Exception {
        // Verify that there's no pending transaction (ie pm.close() has been called)
        Transaction transaction = DatastoreServiceFactory.getDatastoreService().getCurrentTransaction(null);
        boolean transactionPending = transaction != null;
        if (transactionPending) {
            transaction.rollback();
        }
 
        // Clean up the App Engine data store
        ApiProxyLocalImpl proxy = (ApiProxyLocalImpl) ApiProxy.getDelegate();
        if (proxy != null) {
            LocalDatastoreService datastoreService = (LocalDatastoreService) proxy.getService("datastore_v3");
            datastoreService.clearProfiles();
        }
 
        // Clean up the App Engine services
        ApiProxy.setDelegate(null);
        ApiProxy.clearEnvironmentForCurrentThread();
 
        // Clean-uup the Task Queue
        // See: http://groups.google.com/group/google-appengine-java/browse_thread/thread/fe334c9e461026fa/8f5872b052144c8d?#8f5872b052144c8d
        if (proxy != null) {
            LocalTaskQueue ltq = (LocalTaskQueue) proxy.getService(LocalTaskQueue.PACKAGE);
            ltq.flushQueue(QueueFactory.getDefaultQueue().getQueueName());
        }
 
        // Report the issue with the transaction still open
        if (transactionPending) {
            throw new IllegalStateException("Found a transaction nor commited neither rolled-back. Probably related to a missing PersistenceManager.close() call.");
        }
    }
 
    /**
* Creates a PersistenceManagerFactory on the fly, with the exact same information
* stored in the<war-dir>/WEB-INF/META-INF/jdoconfig.xml file.
*/
    public PersistenceManagerFactory getPersistenceManagerFactory() {
        if (pmf == null) {
            Properties newProperties = new Properties();
            newProperties.put("javax.jdo.PersistenceManagerFactoryClass",
                    "org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory");
            newProperties.put("javax.jdo.option.ConnectionURL", "appengine");
            newProperties.put("javax.jdo.option.NontransactionalRead", "true");
            newProperties.put("javax.jdo.option.NontransactionalWrite", "true");
            newProperties.put("javax.jdo.option.RetainValues", "true");
            newProperties.put("datanucleus.appengine.autoCreateDatastoreTxns", "true");
            newProperties.put("datanucleus.appengine.autoCreateDatastoreTxns", "true");
            pmf = JDOHelper.getPersistenceManagerFactory(newProperties);
        }
        return pmf;
    }
 
    /**
* Gets an instance of the PersistenceManager class
*/
    public PersistenceManager getPersistenceManager() {
        return getPersistenceManagerFactory().getPersistenceManager();
    }
 
    /**
* Return the list of TaskStateInfo instance added to the specified queue.
*
* Note: TaskOptions instances added to the queue have been transformed
* into TaskStateInfo ones. Use the following accessors to verify that the
* expected tasks have been added to the queue as expected.
* - TaskStateInfo.getMethod(): GET/POST/PUT/DELETE
* - TaskStateInfo.getUrl(): URL with request parameters if method == GET
* - TaskStateInfo.getBody(): Request parameters if method == POST/PUT
*
* @param queueName Name of the queue to query
*
* @return List of TaskStateInfo representing the processed TaskOptions
* added to the specified queue
*/
    public List<TaskStateInfo> getTaskStateInfo(String queueName) {
        ApiProxyLocalImpl proxy = (ApiProxyLocalImpl) ApiProxy.getDelegate();
        if (proxy != null) {
            LocalTaskQueue taskQueue = (LocalTaskQueue) proxy.getService(LocalTaskQueue.PACKAGE);
            QueueStateInfo queueStateInfo = taskQueue.getQueueStateInfo().get(queueName);
            if (queueStateInfo != null) {
                return queueStateInfo.getTaskInfo();
            }
            return null;
        }
        return null;
    }
}
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server