001: /*
002: * $Id: TestDataProvider.java 476710 2006-11-19 05:05:14Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.showcase.application;
022:
023: import java.io.Serializable;
024: import java.util.Arrays;
025: import java.util.Date;
026:
027: import org.apache.log4j.Logger;
028: import org.apache.struts2.showcase.dao.EmployeeDao;
029: import org.apache.struts2.showcase.dao.SkillDao;
030: import org.apache.struts2.showcase.exception.StorageException;
031: import org.apache.struts2.showcase.model.Employee;
032: import org.apache.struts2.showcase.model.Skill;
033: import org.springframework.beans.factory.InitializingBean;
034:
035: /**
036: * TestDataProvider.
037: *
038: */
039:
040: public class TestDataProvider implements Serializable, InitializingBean {
041:
042: private static final long serialVersionUID = 1L;
043:
044: private static final Logger log = Logger
045: .getLogger(TestDataProvider.class);
046:
047: public static final String[] POSITIONS = { "Developer",
048: "System Architect", "Sales Manager", "CEO" };
049:
050: public static final String[] LEVELS = { "Junior", "Senior",
051: "Master" };
052:
053: private static final Skill[] TEST_SKILLS = {
054: new Skill("WW-SEN", "Struts Senior Developer"),
055: new Skill("WW-JUN", "Struts Junior Developer"),
056: new Skill("SPRING-DEV", "Spring Developer") };
057:
058: public static final Employee[] TEST_EMPLOYEES = {
059: new Employee(new Long(1), "Alan", "Smithee", new Date(),
060: new Float(2000f), true, POSITIONS[0],
061: TEST_SKILLS[0], null, "alan", LEVELS[0], "Nice guy"),
062: new Employee(new Long(2), "Robert", "Robson", new Date(),
063: new Float(10000f), false, POSITIONS[1],
064: TEST_SKILLS[1], Arrays.asList(TEST_SKILLS).subList(
065: 1, TEST_SKILLS.length), "rob", LEVELS[1],
066: "Smart guy") };
067:
068: private SkillDao skillDao;
069: private EmployeeDao employeeDao;
070:
071: public void setSkillDao(SkillDao skillDao) {
072: this .skillDao = skillDao;
073: }
074:
075: public void setEmployeeDao(EmployeeDao employeeDao) {
076: this .employeeDao = employeeDao;
077: }
078:
079: protected void addTestSkills() {
080: try {
081: for (int i = 0, j = TEST_SKILLS.length; i < j; i++) {
082: skillDao.merge(TEST_SKILLS[i]);
083: }
084: if (log.isInfoEnabled()) {
085: log
086: .info("TestDataProvider - [addTestSkills]: Added test skill data.");
087: }
088: } catch (StorageException e) {
089: log
090: .error("TestDataProvider - [addTestSkills]: Exception catched: "
091: + e.getMessage());
092: }
093: }
094:
095: protected void addTestEmployees() {
096: try {
097: for (int i = 0, j = TEST_EMPLOYEES.length; i < j; i++) {
098: employeeDao.merge(TEST_EMPLOYEES[i]);
099: }
100: if (log.isInfoEnabled()) {
101: log
102: .info("TestDataProvider - [addTestEmployees]: Added test employee data.");
103: }
104: } catch (StorageException e) {
105: log
106: .error("TestDataProvider - [addTestEmployees]: Exception catched: "
107: + e.getMessage());
108: }
109: }
110:
111: protected void addTestData() {
112: addTestSkills();
113: addTestEmployees();
114: }
115:
116: public void afterPropertiesSet() throws Exception {
117: addTestData();
118: }
119:
120: }
|