Review Board 1.7.22


WAVE-298: Wave doesn't compile: error: no suitable method found for thenReturn(StringTokenizer)

Review Request #2932 - Created Nov. 25, 2011 and submitted

Antonio Bello
WAVE-298
Reviewers
wave
wave
This patch solves the problem in my case. 

I'm running ubuntu x64, and experienced the same problem. 

What I did is simply change the parameter type passed to the thenReturn() method: 
* convert StringTokenizer to Enumeration<String> in ActiveApiServletTest 
* replace ImmutableMap.of() with new Hashtable<String, String[]>() in DataApiServletTest 
ant test runs with no errors on ubuntu x64 10.04
/test/org/waveprotocol/box/server/robots/active/ActiveApiServletTest.java
Revision 1206126 New Change
[20] 52 lines
[+20]
53
import java.io.PrintWriter;
53
import java.io.PrintWriter;
54
import java.io.StringReader;
54
import java.io.StringReader;
55
import java.io.StringWriter;
55
import java.io.StringWriter;
56
import java.lang.reflect.Type;
56
import java.lang.reflect.Type;
57
import java.util.Collections;
57
import java.util.Collections;

    
   
58
import java.util.Enumeration;
58
import java.util.List;
59
import java.util.List;
59
import java.util.StringTokenizer;
60
import java.util.StringTokenizer;
60

    
   
61

   
61
import javax.servlet.http.HttpServletRequest;
62
import javax.servlet.http.HttpServletRequest;
62
import javax.servlet.http.HttpServletResponse;
63
import javax.servlet.http.HttpServletResponse;
[+20] [20] 31 lines
[+20] [+] protected void setUp() throws Exception {
94
    when(accountStore.getAccount(ROBOT)).thenReturn(
95
    when(accountStore.getAccount(ROBOT)).thenReturn(
95
        new RobotAccountDataImpl(ROBOT, "", "secret", null, true));
96
        new RobotAccountDataImpl(ROBOT, "", "secret", null, true));
96

    
   
97

   
97
    req = mock(HttpServletRequest.class);
98
    req = mock(HttpServletRequest.class);
98
    when(req.getRequestURL()).thenReturn(new StringBuffer("www.example.com/robot"));
99
    when(req.getRequestURL()).thenReturn(new StringBuffer("www.example.com/robot"));
99
    when(req.getHeaderNames()).thenReturn(new StringTokenizer("Authorization"));
100
    when(req.getHeaderNames()).thenReturn(convertRawEnumerationToGeneric(new StringTokenizer("Authorization")));
100
    when(req.getReader()).thenReturn(new BufferedReader(new StringReader("")));
101
    when(req.getReader()).thenReturn(new BufferedReader(new StringReader("")));
101

    
   
102

   
102
    resp = mock(HttpServletResponse.class);
103
    resp = mock(HttpServletResponse.class);
103
    outputWriter = new StringWriter();
104
    outputWriter = new StringWriter();
104
    when(resp.getWriter()).thenReturn(new PrintWriter(outputWriter));
105
    when(resp.getWriter()).thenReturn(new PrintWriter(outputWriter));
105

    
   
106

   
106
    servlet =
107
    servlet =
107
        new ActiveApiServlet(robotSerializer, converterManager, waveletProvider, operationRegistry,
108
        new ActiveApiServlet(robotSerializer, converterManager, waveletProvider, operationRegistry,
108
            conversationUtil, oAuthServiceProvider, validator, accountStore);
109
            conversationUtil, oAuthServiceProvider, validator, accountStore);
109
  }
110
  }
110

    
   
111

   
111
  public void testDoPostExecutesAndWritesResponse() throws Exception {
112
  public void testDoPostExecutesAndWritesResponse() throws Exception {
112
    when(req.getHeaders("Authorization")).thenReturn(generateOAuthHeader(ROBOT.getAddress()));
113
    when(req.getHeaders("Authorization")).thenReturn(convertRawEnumerationToGeneric(

    
   
114
        generateOAuthHeader(ROBOT.getAddress())));
113

    
   
115

   
114
    String operationId = "op1";
116
    String operationId = "op1";
115
    OperationRequest operation = new OperationRequest("wavelet.create", operationId);
117
    OperationRequest operation = new OperationRequest("wavelet.create", operationId);
116
    List<OperationRequest> operations = Collections.singletonList(operation);
118
    List<OperationRequest> operations = Collections.singletonList(operation);
117
    when(robotSerializer.deserializeOperations(anyString())).thenReturn(operations);
119
    when(robotSerializer.deserializeOperations(anyString())).thenReturn(operations);
[+20] [20] 13 lines
[+20] protected void setUp() throws Exception {
131
        outputWriter.toString());
133
        outputWriter.toString());
132
  }
134
  }
133

    
   
135

   
134
  public void testDoPostUnauthorizedWhenParticipantInvalid() throws Exception {
136
  public void testDoPostUnauthorizedWhenParticipantInvalid() throws Exception {
135
    when(req.getHeaders("Authorization")).thenReturn(
137
    when(req.getHeaders("Authorization")).thenReturn(
136
        generateOAuthHeader("invalid#$example.com"));
138
        convertRawEnumerationToGeneric(generateOAuthHeader("invalid#$example.com")));
137

    
   
139

   
138
    servlet.doPost(req, resp);
140
    servlet.doPost(req, resp);
139

    
   
141

   
140
    verify(resp).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
142
    verify(resp).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
141
  }
143
  }
142

    
   
144

   
143

    
   
145

   
144
  public void testDoPostUnauthorizedWhenParticipantUnknown() throws Exception {
146
  public void testDoPostUnauthorizedWhenParticipantUnknown() throws Exception {
145
    when(req.getHeaders("Authorization")).thenReturn(generateOAuthHeader(UNKNOWN.getAddress()));
147
    when(req.getHeaders("Authorization")).thenReturn(

    
   
148
        convertRawEnumerationToGeneric(generateOAuthHeader(UNKNOWN.getAddress())));
146

    
   
149

   
147
    servlet.doPost(req, resp);
150
    servlet.doPost(req, resp);
148

    
   
151

   
149
    verify(resp).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
152
    verify(resp).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
150
  }
153
  }
151

    
   
154

   
152
  public void testDoPostUnauthorizedWhenValidationFails() throws Exception {
155
  public void testDoPostUnauthorizedWhenValidationFails() throws Exception {
153
    when(req.getHeaders("Authorization")).thenReturn(generateOAuthHeader(ROBOT.getAddress()));
156
    when(req.getHeaders("Authorization")).thenReturn(

    
   
157
        convertRawEnumerationToGeneric(generateOAuthHeader(ROBOT.getAddress())));
154
    doThrow(new OAuthException("")).when(validator).validateMessage(
158
    doThrow(new OAuthException("")).when(validator).validateMessage(
155
        any(OAuthMessage.class), any(OAuthAccessor.class));
159
        any(OAuthMessage.class), any(OAuthAccessor.class));
156

    
   
160

   
157
    servlet.doPost(req, resp);
161
    servlet.doPost(req, resp);
158

    
   
162

   
159
    verify(resp).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
163
    verify(resp).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
160
  }
164
  }
161

    
   
165

   
162
  private StringTokenizer generateOAuthHeader(String address) {
166
  private StringTokenizer generateOAuthHeader(String address) {
163
    return new StringTokenizer("OAuth " + OAuth.OAUTH_CONSUMER_KEY + "=\"" + address + "\"", "");
167
    return new StringTokenizer("OAuth " + OAuth.OAUTH_CONSUMER_KEY + "=\"" + address + "\"", "");
164
  }
168
  }

    
   
169
  

    
   
170
  /**

    
   
171
   * Convert a <code>{@link StringTokenizer}</code> into an 

    
   
172
   * <code>@{link {@link Enumeration<String>}</code>

    
   
173
   * 

    
   
174
   * This method solves issue WAVE-298

    
   
175
   * 

    
   
176
   * @author jeden17@gmail.com (Antonio Bello)

    
   
177
   * 

    
   
178
   * @param tokens

    
   
179
   * @return

    
   
180
   */

    
   
181
  private Enumeration<String> convertRawEnumerationToGeneric(final StringTokenizer tokens)   {

    
   
182
    return new Enumeration<String>() {

    
   
183
      @Override

    
   
184
      public String nextElement() {

    
   
185
        return (String) tokens.nextElement();

    
   
186
      }

    
   
187
      

    
   
188
      @Override

    
   
189
      public boolean hasMoreElements() {

    
   
190
        return tokens.hasMoreElements();

    
   
191
      }

    
   
192
    };

    
   
193
  }
165
}
194
}
/test/org/waveprotocol/box/server/robots/dataapi/DataApiServletTest.java
Revision 1206126 New Change
 
  1. /test/org/waveprotocol/box/server/robots/active/ActiveApiServletTest.java: Loading...
  2. /test/org/waveprotocol/box/server/robots/dataapi/DataApiServletTest.java: Loading...