Commit bead500c authored by Liang Ding's avatar Liang Ding

#12188

parent 4fe8cff6
<?xml version="1.0" encoding="UTF-8"?>
<!--
Description: Solo POM.
Version: 3.14.1.27, Sep 24, 2016
Version: 3.15.1.27, Nov 1, 2016
Author: <a href="http://88250.b3log.org">Liang Ding</a>
Author: <a href="http://www.annpeter.cn">Ann Peter</a>
-->
......@@ -106,7 +106,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.b3log.latke.version>2.3.3</org.b3log.latke.version>
<org.b3log.latke.version>2.3.5</org.b3log.latke.version>
<servlet.version>3.1.0</servlet.version>
<slf4j.version>1.7.5</slf4j.version>
......@@ -222,6 +222,14 @@
<artifactId>jodd-http</artifactId>
<version>${jodd.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
/*
* Copyright (c) 2010-2016, b3log.org & hacpai.com
*
* 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.b3log.solo.processor;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.b3log.latke.model.User;
import org.b3log.solo.AbstractTestCase;
import org.b3log.solo.service.InitService;
import org.b3log.solo.service.UserQueryService;
import org.json.JSONObject;
import static org.mockito.Mockito.*;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* {@link BlogProcessor} test case.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Nov 1, 2016
* @since 1.7.0
*/
@Test(suiteName = "processor")
public class BlogProcessorTestCase extends AbstractTestCase {
/**
* Init.
*
* @throws Exception exception
*/
@Test
public void init() throws Exception {
final InitService initService = getInitService();
final JSONObject requestJSONObject = new JSONObject();
requestJSONObject.put(User.USER_EMAIL, "test@gmail.com");
requestJSONObject.put(User.USER_NAME, "Admin");
requestJSONObject.put(User.USER_PASSWORD, "pass");
initService.init(requestJSONObject);
final UserQueryService userQueryService = getUserQueryService();
Assert.assertNotNull(userQueryService.getUserByEmail("test@gmail.com"));
}
/**
* getBlogInfo.
*
* @throws Exception exception
*/
@Test(dependsOnMethods = "init")
public void getBlogInfo() throws Exception {
final HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getServletContext()).thenReturn(mock(ServletContext.class));
when(request.getRequestURI()).thenReturn("/blog/info");
when(request.getMethod()).thenReturn("GET");
final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
dispatcherServlet.init();
final StringWriter stringWriter = new StringWriter();
final PrintWriter printWriter = new PrintWriter(stringWriter);
final HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getWriter()).thenReturn(printWriter);
dispatcherServlet.service(request, response);
final String content = stringWriter.toString();
Assert.assertTrue(StringUtils.startsWith(content, "{\"staticServePath\":\"http://localhost:8080\""));
}
}
package org.b3log.solo.processor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.b3log.latke.servlet.HTTPRequestContext;
import org.b3log.latke.servlet.HttpControl;
import org.b3log.latke.servlet.handler.AdviceHandler;
import org.b3log.latke.servlet.handler.ArgsHandler;
import org.b3log.latke.servlet.handler.Handler;
import org.b3log.latke.servlet.handler.MethodInvokeHandler;
import org.b3log.latke.servlet.handler.RequestDispatchHandler;
import org.b3log.latke.servlet.handler.RequestPrepareHandler;
import org.b3log.latke.servlet.renderer.AbstractHTTPResponseRenderer;
import org.b3log.latke.servlet.renderer.HTTP404Renderer;
import org.b3log.latke.servlet.renderer.HTTP500Renderer;
/**
* Mock dispatcher servlet for unit tests.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Nov 1, 2016
* @since 1.7.0
*/
public class MockDispatcherServlet {
/**
* the holder of all the sys-handler.
*/
public static final List<Handler> SYS_HANDLER = new ArrayList<Handler>();
public void init() throws ServletException {
SYS_HANDLER.add(new RequestPrepareHandler());
SYS_HANDLER.add(new RequestDispatchHandler());
SYS_HANDLER.add(new ArgsHandler());
SYS_HANDLER.add(new AdviceHandler());
SYS_HANDLER.add(new MethodInvokeHandler());
}
protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
final HTTPRequestContext httpRequestContext = new HTTPRequestContext();
httpRequestContext.setRequest(req);
httpRequestContext.setResponse(resp);
final HttpControl httpControl = new HttpControl(SYS_HANDLER.iterator(), httpRequestContext);
try {
httpControl.nextHandler();
} catch (final Exception e) {
httpRequestContext.setRenderer(new HTTP500Renderer(e));
}
result(httpRequestContext);
}
/**
* To http repsonse.
*
* @param context {@link HTTPRequestContext}
* @throws IOException IOException
*/
public static void result(final HTTPRequestContext context) throws IOException {
final HttpServletResponse response = context.getResponse();
if (response.isCommitted()) { // Response sends redirect or error
return;
}
AbstractHTTPResponseRenderer renderer = context.getRenderer();
if (null == renderer) {
renderer = new HTTP404Renderer();
}
renderer.render(context);
}
}
......@@ -15,7 +15,6 @@
*/
package org.b3log.solo.repository.impl;
import junit.framework.Assert;
import org.b3log.latke.Keys;
import org.b3log.latke.repository.Transaction;
import org.b3log.solo.AbstractTestCase;
......@@ -23,6 +22,7 @@ import org.b3log.solo.model.ArchiveDate;
import org.b3log.solo.model.Article;
import org.b3log.solo.repository.ArchiveDateArticleRepository;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
......@@ -66,16 +66,12 @@ public class ArchiveDateArticleRepositoryImplTestCase extends AbstractTestCase {
*/
@Test(dependsOnMethods = "add")
public void getByArchiveDateId() throws Exception {
final ArchiveDateArticleRepository archiveDateArticleRepository =
getArchiveDateArticleRepository();
final ArchiveDateArticleRepository archiveDateArticleRepository = getArchiveDateArticleRepository();
final JSONObject found =
archiveDateArticleRepository.getByArchiveDateId(
"archiveDateId", 1, Integer.MAX_VALUE);
final JSONObject found = archiveDateArticleRepository.getByArchiveDateId("archiveDateId", 1, Integer.MAX_VALUE);
Assert.assertNotNull(found);
final JSONObject notFound = archiveDateArticleRepository.
getByArchiveDateId("not found", 1, Integer.MAX_VALUE);
final JSONObject notFound = archiveDateArticleRepository.getByArchiveDateId("not found", 1, Integer.MAX_VALUE);
Assert.assertNotNull(notFound);
}
......@@ -86,12 +82,9 @@ public class ArchiveDateArticleRepositoryImplTestCase extends AbstractTestCase {
*/
@Test(dependsOnMethods = "add")
public void getByArticleId() throws Exception {
final ArchiveDateArticleRepository archiveDateArticleRepository =
getArchiveDateArticleRepository();
final ArchiveDateArticleRepository archiveDateArticleRepository = getArchiveDateArticleRepository();
Assert.assertNotNull(
archiveDateArticleRepository.getByArticleId("articleId"));
Assert.assertNull(
archiveDateArticleRepository.getByArticleId("not found"));
Assert.assertNotNull(archiveDateArticleRepository.getByArticleId("articleId"));
Assert.assertNull(archiveDateArticleRepository.getByArticleId("not found"));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment