Commit bead500c authored by Liang Ding's avatar Liang Ding

#12188

parent 4fe8cff6
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
Description: Solo POM. 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://88250.b3log.org">Liang Ding</a>
Author: <a href="http://www.annpeter.cn">Ann Peter</a> Author: <a href="http://www.annpeter.cn">Ann Peter</a>
--> -->
...@@ -106,7 +106,7 @@ ...@@ -106,7 +106,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <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> <servlet.version>3.1.0</servlet.version>
<slf4j.version>1.7.5</slf4j.version> <slf4j.version>1.7.5</slf4j.version>
...@@ -222,6 +222,14 @@ ...@@ -222,6 +222,14 @@
<artifactId>jodd-http</artifactId> <artifactId>jodd-http</artifactId>
<version>${jodd.version}</version> <version>${jodd.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <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 @@ ...@@ -15,7 +15,6 @@
*/ */
package org.b3log.solo.repository.impl; package org.b3log.solo.repository.impl;
import junit.framework.Assert;
import org.b3log.latke.Keys; import org.b3log.latke.Keys;
import org.b3log.latke.repository.Transaction; import org.b3log.latke.repository.Transaction;
import org.b3log.solo.AbstractTestCase; import org.b3log.solo.AbstractTestCase;
...@@ -23,6 +22,7 @@ import org.b3log.solo.model.ArchiveDate; ...@@ -23,6 +22,7 @@ import org.b3log.solo.model.ArchiveDate;
import org.b3log.solo.model.Article; import org.b3log.solo.model.Article;
import org.b3log.solo.repository.ArchiveDateArticleRepository; import org.b3log.solo.repository.ArchiveDateArticleRepository;
import org.json.JSONObject; import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test; import org.testng.annotations.Test;
/** /**
...@@ -36,7 +36,7 @@ public class ArchiveDateArticleRepositoryImplTestCase extends AbstractTestCase { ...@@ -36,7 +36,7 @@ public class ArchiveDateArticleRepositoryImplTestCase extends AbstractTestCase {
/** /**
* Adds successfully. * Adds successfully.
* *
* @throws Exception exception * @throws Exception exception
*/ */
@Test @Test
...@@ -61,37 +61,30 @@ public class ArchiveDateArticleRepositoryImplTestCase extends AbstractTestCase { ...@@ -61,37 +61,30 @@ public class ArchiveDateArticleRepositoryImplTestCase extends AbstractTestCase {
/** /**
* Get By ArchiveDate Id. * Get By ArchiveDate Id.
* *
* @throws Exception exception * @throws Exception exception
*/ */
@Test(dependsOnMethods = "add") @Test(dependsOnMethods = "add")
public void getByArchiveDateId() throws Exception { public void getByArchiveDateId() throws Exception {
final ArchiveDateArticleRepository archiveDateArticleRepository = final ArchiveDateArticleRepository archiveDateArticleRepository = getArchiveDateArticleRepository();
getArchiveDateArticleRepository();
final JSONObject found = final JSONObject found = archiveDateArticleRepository.getByArchiveDateId("archiveDateId", 1, Integer.MAX_VALUE);
archiveDateArticleRepository.getByArchiveDateId(
"archiveDateId", 1, Integer.MAX_VALUE);
Assert.assertNotNull(found); Assert.assertNotNull(found);
final JSONObject notFound = archiveDateArticleRepository. final JSONObject notFound = archiveDateArticleRepository.getByArchiveDateId("not found", 1, Integer.MAX_VALUE);
getByArchiveDateId("not found", 1, Integer.MAX_VALUE);
Assert.assertNotNull(notFound); Assert.assertNotNull(notFound);
} }
/** /**
* Get By Archive Id. * Get By Archive Id.
* *
* @throws Exception exception * @throws Exception exception
*/ */
@Test(dependsOnMethods = "add") @Test(dependsOnMethods = "add")
public void getByArticleId() throws Exception { public void getByArticleId() throws Exception {
final ArchiveDateArticleRepository archiveDateArticleRepository = final ArchiveDateArticleRepository archiveDateArticleRepository = getArchiveDateArticleRepository();
getArchiveDateArticleRepository();
Assert.assertNotNull( Assert.assertNotNull(archiveDateArticleRepository.getByArticleId("articleId"));
archiveDateArticleRepository.getByArticleId("articleId")); Assert.assertNull(archiveDateArticleRepository.getByArticleId("not found"));
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