Kawai Wiki

Compare Revisions

Ignore whitespace Rev 195 → Rev 196

/trunk/src/test/java/org/kawai/action/PageActionTest.java
55,10 → 55,10
* Attribute jdbcPageDAO of PageActionTest.
*/
private JdbcPageDAO jdbcPageDAO;
/**
* Attribute beanSession of PageActionTest.
*/
private BeanSession beanSession;
/**
* Attribute beanSession of PageActionTest.
*/
private BeanSession beanSession;
 
/**
* Default setUp method.
67,39 → 67,39
@Before
public void setUp() throws Exception {
 
BeanManager beanManager = new BeanManager();
BeanConfig beanConfig = new BeanConfig(Page.class, "Pages");
beanConfig.pk("id", "id", DBTypes.AUTOINCREMENT);
beanConfig.field("name", DBTypes.STRING);
beanConfig.field("title", DBTypes.STRING);
beanConfig.field("body", DBTypes.STRING);
beanConfig.field("languageId", "language_id", DBTypes.STRING);
beanConfig.field("systemPage", "system_page", DBTypes.BOOLEANSTRING);
beanConfig.field("frontPage", "front_page", DBTypes.BOOLEANSTRING);
beanConfig.field("modifiedById", "modified_by", DBTypes.INTEGER);
beanConfig.field("modifiedOn", "modified_on", DBTypes.NOW_ON_UPDATE_TIMESTAMP);
beanConfig.field("createdById", "created_by", DBTypes.INTEGER);
beanConfig.field("createdOn", "created_on", DBTypes.NOW_ON_INSERT_TIMESTAMP);
beanConfig.field("deleted", DBTypes.BOOLEANSTRING);
BeanManager beanManager = new BeanManager();
BeanConfig beanConfig = new BeanConfig(Page.class, "Pages");
beanConfig.pk("id", "id", DBTypes.AUTOINCREMENT);
beanConfig.field("name", DBTypes.STRING);
beanConfig.field("title", DBTypes.STRING);
beanConfig.field("body", DBTypes.STRING);
beanConfig.field("languageId", "language_id", DBTypes.STRING);
beanConfig.field("systemPage", "system_page", DBTypes.BOOLEANSTRING);
beanConfig.field("frontPage", "front_page", DBTypes.BOOLEANSTRING);
beanConfig.field("modifiedById", "modified_by", DBTypes.INTEGER);
beanConfig.field("modifiedOn", "modified_on", DBTypes.NOW_ON_UPDATE_TIMESTAMP);
beanConfig.field("createdById", "created_by", DBTypes.INTEGER);
beanConfig.field("createdOn", "created_on", DBTypes.NOW_ON_INSERT_TIMESTAMP);
beanConfig.field("deleted", DBTypes.BOOLEANSTRING);
 
beanManager.addBeanConfig(beanConfig);
beanManager.addBeanConfig(beanConfig);
 
beanConfig = new BeanConfig(User.class, "Users");
beanConfig.pk("id", DBTypes.AUTOINCREMENT);
beanConfig.field("username", DBTypes.STRING);
beanConfig.field("password", DBTypes.STRING);
beanConfig.field("groupId", "group_id", DBTypes.INTEGER);
beanConfig = new BeanConfig(User.class, "Users");
beanConfig.pk("id", DBTypes.AUTOINCREMENT);
beanConfig.field("username", DBTypes.STRING);
beanConfig.field("password", DBTypes.STRING);
beanConfig.field("groupId", "group_id", DBTypes.INTEGER);
 
beanManager.addBeanConfig(beanConfig);
beanManager.addBeanConfig(beanConfig);
 
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/Kawai?autoReconnect=true", "kawai", "1kawai23");
beanSession = new MySQLBeanSession(beanManager, conn);
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/Kawai?autoReconnect=true", "kawai", "1kawai23");
beanSession = new MySQLBeanSession(beanManager, conn);
 
userDAO = new JdbcUserDAO(beanSession);
pageDAO = new JdbcPageDAO(beanSession);
userDAO = new JdbcUserDAO(beanSession);
pageDAO = new JdbcPageDAO(beanSession);
 
action = new PageAction(userDAO, pageDAO);
action = new PageAction(userDAO, pageDAO);
mockAction = new MockAction(action);
action.setInput(mockAction.getInput());
action.setOutput(mockAction.getOutput());
111,8 → 111,8
@Ignore
@Test
public void executeTest() {
action.getInput().setValue("name", "Page");
action.getInput().setValue("lang", "pt");
action.getInput().setValue("name", "Page");
action.getInput().setValue("lang", "pt");
action.execute();
}
 
122,11 → 122,87
@Ignore
@Test
public void getLastRevisionsTest() {
action.getInput().setValue("lang", "en_US");
action.getLastRevisions();
action.getInput().setValue("lang", "en_US");
action.getLastRevisions();
}
 
/**
* Test method bypassAuthenticationTest.
*/
@Test
public void bypassAuthenticationTest() {
action.bypassAuthentication("getCSS");
action.bypassAuthentication("getPrintCSS");
action.bypassAuthentication("getLastRevisions");
}
 
/**
* Test method beforeValidationTest.
*/
@Test
public void beforeValidationTest() {
action.beforeValidation("");
}
 
/**
* Test method afterValidationTest.
*/
@Test
public void afterValidationTest() {
action.afterValidation("add", true);
}
 
/**
* Test method checkPageDoesNotExistTest.
*/
@Test
public void checkPageDoesNotExistTest() {
action.getInput().setValue("newPage", new Page());
action.checkPageDoesNotExist();
}
 
/**
* Test method checkNotSystemPageTest.
*/
@Test
public void checkNotSystemPageTest() {
action.getInput().setValue("newPage", new Page());
action.checkNotSystemPage();
}
 
/**
* Test method getTest.
*/
@Test
public void getTest() {
action.getInput().setValue("name", "Page1");
action.getInput().setValue("lang", "pt");
action.getInput().setValue("revision", -1);
action.get();
}
 
/**
* Test method getPreviewTest.
*/
@Test
public void getPreviewTest() {
action.getInput().setValue("name", "Page1");
action.getInput().setValue("lang", "pt");
action.getPreview();
}
 
/**
* Test method getRevisionsTest.
*/
@Ignore
@Test
public void getRevisionsTest() {
action.getInput().setValue("name", "Page1");
action.getInput().setValue("lang", "pt");
action.getRevisions();
}
 
/**
* Test method prepareValidatorTest.
*/
@Test
/trunk/src/test/java/org/kawai/action/UserActionTest.java
New file
0,0 → 1,137
package org.kawai.action;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.junit.Before;
import org.junit.Test;
import org.kawai.dao.UserDAO;
import org.kawai.dao.jdbc.JdbcPageDAO;
import org.kawai.dao.jdbc.JdbcUserDAO;
import org.kawai.model.Page;
import org.kawai.model.User;
import org.mentabean.BeanConfig;
import org.mentabean.BeanManager;
import org.mentabean.BeanSession;
import org.mentabean.DBTypes;
import org.mentabean.jdbc.MySQLBeanSession;
import org.mentawai.util.MockAction;
import org.mentawai.validation.Validator;
 
/**
* Test class UserActionTest.
* @author helio frota
*
*/
public class UserActionTest {
 
/**
* Attribute action of UserActionTest.
*/
private UserAction action;
/**
* Attribute mockAction of UserActionTest.
*/
private MockAction mockAction;
/**
* Attribute userDAO of UserActionTest.
*/
private UserDAO userDAO;
/**
* Attribute beanSession of UserActionTest.
*/
private BeanSession beanSession;
/**
* Attribute jdbcUserDAO of UserActionTest.
*/
private JdbcUserDAO jdbcUserDAO;
 
/**
* Default setUp method.
* @throws Exception
*/
@Before
public void setUp() throws Exception {
 
BeanManager beanManager = new BeanManager();
BeanConfig beanConfig = new BeanConfig(User.class, "Users");
beanConfig.pk("id", DBTypes.AUTOINCREMENT);
beanConfig.field("username", DBTypes.STRING);
beanConfig.field("password", DBTypes.STRING);
beanConfig.field("groupId", "group_id", DBTypes.INTEGER);
 
beanManager.addBeanConfig(beanConfig);
 
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/Kawai?autoReconnect=true", "kawai", "1kawai23");
beanSession = new MySQLBeanSession(beanManager, conn);
 
userDAO = new JdbcUserDAO(beanSession);
 
action = new UserAction(userDAO);
mockAction = new MockAction(action);
action.setInput(mockAction.getInput());
action.setOutput(mockAction.getOutput());
}
 
/**
* Test method bypassAuthenticationTest.
*/
@Test
public void bypassAuthenticationTest() {
action.bypassAuthentication("check");
action.bypassAuthentication("add");
}
 
/**
* Test method beforeValidationTest.
*/
@Test
public void beforeValidationTest() {
action.beforeValidation("add");
}
 
/**
* Test method afterValidationTest.
*/
@Test
public void afterValidationTest() {
action.afterValidation("add", true);
}
 
/**
* Test method checkUsernameAddTest.
*/
@Test
public void checkUsernameAddTest() {
action.checkUsernameAdd("test");
}
 
/**
* Test method checkTest.
*/
@Test
public void checkTest() {
action.getInput().setValue("username", "test");
action.check();
}
 
/**
* Test method prepareValidatorTest.
*/
@Test
public void prepareValidatorTest() {
Map<String, Object> ruleMap = new HashMap< String, Object >();
List < String > valueList = new ArrayList < String >();
ruleMap.put("ruleMap", valueList);
Map<String, Object> errorMap = new HashMap< String, Object >();
errorMap.put("errorMap", new HashMap < String, Object >());
Validator validator = new Validator(ruleMap, errorMap);
action.prepareValidator(validator, "add");
}
 
}
/trunk/src/test/java/AllTests.java
3,6 → 3,7
import org.junit.runners.Suite.SuiteClasses;
import org.kawai.action.LoginActionTest;
import org.kawai.action.PageActionTest;
import org.kawai.action.UserActionTest;
import org.kawai.dao.jdbc.JdbcPageDAOTest;
import org.kawai.dao.jdbc.JdbcUserDAOTest;
import org.kawai.model.ModelTest;
17,6 → 18,7
ModelTest.class,
LoginActionTest.class,
PageActionTest.class,
UserActionTest.class,
JdbcUserDAOTest.class,
JdbcPageDAOTest.class
})
/trunk/src/main/java/org/kawai/action/UserAction.java
11,103 → 11,127
import org.mentawai.validation.ValidationInterceptor;
import org.mentawai.validation.Validator;
 
/**
* Action class UserAction.
*
* @author Sergio Oliveira
*
*/
public class UserAction extends BaseAction implements Validatable, AuthenticationFree, ValidationInterceptor {
private final UserDAO userDAO;
public UserAction(UserDAO userDAO) {
this.userDAO = userDAO;
}
 
// You cannot log to register...
@Override
public boolean bypassAuthentication(String method) {
if (method == null) return false;
if (method.equals("add")) return true;
if (method.equals("check")) return true;
return false;
}
// Validate the fields for the user form...
@Override
public void prepareValidator(Validator val, String method) {
String username_regex = "^[A-Za-z][A-Za-z0-9\\-\\_\\.]*[A-Za-z0-9]$";
if (method != null && method.equals("add") && isPost()) {
val.requiredFields("Required Field", "username", "password", "groupId");
val.requiredLists("Required Field", "groupId");
val.add("username", RegexRule.getInstance(username_regex), "Invalid Username");
val.add("username", MethodRule.getInstance(this, "checkUsernameAdd"), "Username already exists!");
val.add("password", EqualRule.getInstance("password", "passconf"), "Passwords do not match!");
}
}
@Override
public boolean beforeValidation(String method) {
return true; // go ahead with validation...
}
@Override
public void afterValidation(String method, boolean wasOk) {
if (method == null) return;
if (method.equals("add") && !wasOk) {
output.setValue("showForm", "addNewUser");
}
}
boolean checkUsernameAdd(String username) {
return userDAO.findByUsername(username) == null;
}
public String check() {
if (!isPost()) return ERROR;
String username = input.getString("username");
String sessionUsername = input.getString("sessionUsername");
if (isEmpty(username)) return ERROR;
User u = userDAO.findByUsername(username);
if (u == null) return SUCCESS; // username does not exist
if (sessionUsername != null && u.getUsername().equals(sessionUsername)) return SUCCESS;
return ALREADY;
}
public String add() {
if (!isPost()) {
// we only want to allow post to add an user...
return ERROR;
} else {
User u = input.getObject(User.class);
userDAO.insert(u);
 
addMessage("User " + u.getUsername() + " added!", true);
return CREATED;
}
}
}
/** Attribute userDAO of UserAction. */
private final UserDAO userDAO;
 
/**
* Parametric constructor.
* @param userDAO UserDAO
*/
public UserAction(UserDAO userDAO) {
this.userDAO = userDAO;
}
 
/**
* {@inheritDoc}
*/
// You cannot log to register...
@Override
public boolean bypassAuthentication(String method) {
 
if (method == null) return false;
 
if (method.equals("add")) return true;
if (method.equals("check")) return true;
 
return false;
}
 
/**
* {@inheritDoc}
*/
// Validate the fields for the user form...
@Override
public void prepareValidator(Validator val, String method) {
 
String username_regex = "^[A-Za-z][A-Za-z0-9\\-\\_\\.]*[A-Za-z0-9]$";
 
if (method != null && method.equals("add") && isPost()) {
 
val.requiredFields("Required Field", "username", "password", "groupId");
 
val.requiredLists("Required Field", "groupId");
 
val.add("username", RegexRule.getInstance(username_regex), "Invalid Username");
 
val.add("username", MethodRule.getInstance(this, "checkUsernameAdd"), "Username already exists!");
 
val.add("password", EqualRule.getInstance("password", "passconf"), "Passwords do not match!");
}
}
 
/**
* {@inheritDoc}
*/
@Override
public boolean beforeValidation(String method) {
return true; // go ahead with validation...
}
 
/**
* {@inheritDoc}
*/
@Override
public void afterValidation(String method, boolean wasOk) {
 
if (method == null) return;
 
if (method.equals("add") && !wasOk) {
 
output.setValue("showForm", "addNewUser");
}
}
 
boolean checkUsernameAdd(String username) {
 
return userDAO.findByUsername(username) == null;
}
 
public String check() {
 
if (!isPost()) return ERROR;
 
String username = input.getString("username");
 
String sessionUsername = input.getString("sessionUsername");
 
if (isEmpty(username)) return ERROR;
 
User u = userDAO.findByUsername(username);
 
if (u == null) return SUCCESS; // username does not exist
 
if (sessionUsername != null && u.getUsername().equals(sessionUsername)) return SUCCESS;
 
return ALREADY;
}
 
public String add() {
 
if (!isPost()) {
 
// we only want to allow post to add an user...
 
return ERROR;
 
} else {
 
User u = input.getObject(User.class);
 
userDAO.insert(u);
 
addMessage("User " + u.getUsername() + " added!", true);
 
return CREATED;
}
}
}
/trunk/src/main/java/org/kawai/action/FileAction.java
12,20 → 12,26
import org.mentawai.core.ApplicationManager;
import org.mentawai.filter.AuthenticationFree;
 
/**
* Action class LoginAction.
*
* @author Sergio Oliveira
*
*/
public class FileAction extends AbstractAction implements AuthenticationFree {
 
private static final String UPLOAD_DIR = "uploads";
 
public FileAction(UserDAO userDAO, PageDAO pageDAO) {
super(userDAO, pageDAO);
}
 
@Override
public boolean bypassAuthentication(String method) {
if (method != null && method.equals("upload")) return false;
return false;
}
 
private String getFolderFromName(String name) {
name = name.toLowerCase();
if (name.endsWith(".jpg") || name.endsWith(".gif") || name.endsWith(".jpeg") || name.endsWith(".png")) {
36,28 → 42,28
}
return "others";
}
 
public String upload() {
 
FileItem fileItem = (FileItem) input.getValue("Filedata");
 
if (fileItem != null) {
 
String filename = fileItem.getName();
String folder = getFolderFromName(filename);
String dir = ApplicationManager.getRealPath() + File.separator + UPLOAD_DIR + File.separator + folder + File.separator;
 
File file = new File(dir + filename);
if (file.exists()) {
output.setValue("status", "error");
output.setValue("msg", "File already exists: " + filename);
return AJAX;
}
 
try {
 
FileOutputStream fos = new FileOutputStream(file);
 
InputStream is = fileItem.getInputStream();
byte[] data = new byte[4096];
int read;
66,32 → 72,32
}
is.close();
fos.close();
 
output.setValue("status", "success");
output.setValue("msg", "File uploaded to folder <i>" + folder + "</i>: " + filename);
 
return AJAX;
 
} catch(Exception e) {
e.printStackTrace();
output.setValue("status", "error");
output.setValue("msg", "Error saving file: " + filename);
return AJAX;
}
 
} else {
output.setValue("status", "error");
output.setValue("msg", "Error uploading file!");
return AJAX;
}
}
 
public String listFiles() {
 
File[] images = (new File(ApplicationManager.getRealPath() + File.separator + "uploads" + File.separator + "images")).listFiles();
File[] archive = (new File(ApplicationManager.getRealPath() + File.separator + "uploads" + File.separator + "archive")).listFiles();
File[] others = (new File(ApplicationManager.getRealPath() + File.separator + "uploads" + File.separator + "others")).listFiles();
 
Set<String> files = new TreeSet<String>();
if (images != null) for(File f : images) {
if (f.isHidden()) continue;
105,7 → 111,7
if (f.isHidden()) continue;
files.add("/uploads/others/" + f.getName());
}
 
output.setValue("files", files);
return AJAX;
}