コード例 #1
0
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/test-dependencies.xml"})
public class StoreJobSurveyTest extends FamIBatisTezt {

  String id = FamDateFormat.getLangIndependantShortDate();

  @Test
  public synchronized void storeAndGetAllJobs() {
    int i = 20;
    while (i-- > 0) {
      this.clearDatabase();

      TimeBooking booking = TeztBeanSimpleFactory.getNewValidBooking();
      booking.setBooked();
      booking.insert();

      Job document = new Job();
      document.setJobId(booking.getId());
      document.setUsername("foo");
      document.setStep(0);
      document.setIdJobDataProcessing("foo");
      document.setJobSurvey(new JSONObject());
      long docSizesBefore = FamCouchDBDao.getInstance().documentCount();
      assertTrue(FamCouchDBDao.getInstance().createDocument(document));
      assertEquals(docSizesBefore + 1, FamCouchDBDao.getInstance().documentCount());
    }
  }

  @Test
  public void storeAndGetJobAttachment() throws IOException {
    this.clearDatabase();
    User admin = TeztBeanSimpleFactory.getAdmin();

    File testFileOriginal =
        new File(
            System.getProperty("user.dir")
                + File.separator
                + ".."
                + File.separator
                + "fam-web"
                + File.separator
                + "src"
                + File.separator
                + "main"
                + File.separator
                + "webapp"
                + File.separator
                + "demo"
                + File.separator
                + "invoice_demo_nursery_school.pdf");
    assertTrue(testFileOriginal.canRead());

    File directory =
        new File(
            FamConnector.fileExchangeDir()
                + File.separator
                + "users"
                + File.separator
                + admin.getUsername());
    if (!directory.exists()) {
      directory.mkdir();
      directory.setWritable(true);
    }
    assertTrue(directory.canWrite());

    File testFile = new File(directory.getAbsolutePath() + File.separator + "test.pdf");
    if (testFile.exists() == false
        || testFile.canRead() == false
        || testFile.length() != testFileOriginal.length()) {
      InputStream in = new FileInputStream(testFileOriginal);
      OutputStream out = new FileOutputStream(testFile);

      byte[] buf = new byte[1024];
      int len;
      while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
      }
      in.close();
      out.close();
    }

    assertEquals(testFileOriginal.length(), testFile.length());

    TimeBooking booking = TeztBeanSimpleFactory.getNewValidBooking();
    booking.setBooked();
    booking.insert();

    Job document = new Job();
    document.setJobId(booking.getId());
    document.setUsername(admin.getUsername());
    document.setStep(0);
    document.setIdJobDataProcessing("foo");
    document.setJobSurvey(new JSONObject());

    List<File> attachments = new ArrayList<File>(1);
    attachments.add(testFile);
    document.addAttachments(attachments);
    assertTrue(document.insertOrUpdate());

    // get it back
    byte[] got_back =
        FamCouchDBDao.getInstance().getAttachment(document.getId(), testFile.getName());
    assertEquals(got_back.length, testFile.length());
    assertEquals(got_back.length, testFileOriginal.length());

    Byte[] testBs = new File2ByteArray().process(testFile);
    Byte[] testBs2 = new File2ByteArray().process(testFileOriginal);
    for (int i = 0; i < testBs.length; i++) {
      assertEquals(testBs[i].byteValue(), got_back[i]);
      assertEquals(testBs2[i].byteValue(), got_back[i]);
    }
  }

  @Test
  public synchronized void storeAndUpdateJob() {
    this.clearDatabase();

    long docSizesBefore = FamCouchDBDao.getInstance().documentCount();

    int step = 1;

    TimeBooking booking = TeztBeanSimpleFactory.getNewValidBooking();
    booking.setBooked();
    booking.insert();

    Job document = new Job();
    document.setJobId(booking.getId());
    document.setUsername("foo");
    document.setStep(step);
    document.setIdJobDataProcessing("foo");
    document.setJobSurvey(new JSONObject());
    assertTrue(FamCouchDBDao.getInstance().createDocument(document));

    Job back = CouchDBDao4Jobs.me().getJob(booking.getId(), step);
    assertNotNull(back);
    long createdBefore = back.getCreated();
    assertEquals(back.getIdJobDataProcessing(), "foo");
    assertEquals(docSizesBefore + 1, FamCouchDBDao.getInstance().documentCount());

    document.setIdJobDataProcessing("bar");
    document.insertOrUpdate();
    back = CouchDBDao4Jobs.me().getJob(booking.getId(), step);
    assertNotNull(back);
    assertTrue(createdBefore != back.getCreated());
    assertEquals(back.getIdJobDataProcessing(), "bar");
    assertEquals(docSizesBefore + 1, FamCouchDBDao.getInstance().documentCount());
  }

  @Test
  public void storeAndGetJobSimpleJobSurvey() {
    this.clearDatabase();
    try {
      int step = 0;

      TimeBooking booking = TeztBeanSimpleFactory.getNewValidBooking();
      booking.setBooked();
      booking.insert();

      Job job = new Job();
      job.setUsername(TeztBeanSimpleFactory.getAdmin().getUsername());
      job.setStep(step);
      job.setJobSurvey(new JSONObject());
      job.setJobId(booking.getId());
      JobDataProcessing jdp = TeztBeanSimpleFactory.getNewValidJobDataProcessing();
      jdp.insertOrUpdate();
      job.setIdJobDataProcessing(jdp.getId());
      assertTrue(job.insertOrUpdate());

      Job back = CouchDBDao4Jobs.me().getJob(booking.getId(), step);
      assertNotNull(back);
    } catch (Exception e) {
      fail("should not throw exception " + e);
    }
  }

  @SuppressWarnings("unchecked")
  @Test
  public void storeAndGetJob() {
    this.clearDatabase();
    try {
      int step = 0;
      Job job = new Job();
      job.setStep(step);
      JSONObject jobsurvey = new JSONObject();

      JSONObject jobsurvey2 = new JSONObject();
      jobsurvey2.put("input 2", "value 2");
      jobsurvey2.put("input 3", "value 3");

      JSONObject jobsurvey3 = new JSONObject();
      jobsurvey3.put("input 4", "value 4");
      jobsurvey3.put("input 5", "value 5");

      JSONArray innerarray = new JSONArray();
      innerarray.put("hallo 1");
      innerarray.put("hallo 2");

      JSONArray innerarray2 = new JSONArray();
      innerarray2.put("hallo 3");
      innerarray2.put("hallo 4");

      innerarray.put(innerarray2);
      innerarray.put(jobsurvey3);

      jobsurvey.put("input 0", "value 0");
      jobsurvey.put("input 1", "value 1");
      jobsurvey.put("innerobj", jobsurvey2);
      jobsurvey.put("innerarray", innerarray);

      job.setJobSurvey(jobsurvey);

      assertEquals(job.getJobSurvey().get("input 0").toString(), "value 0");
      assertEquals(job.getJobSurvey().get("input 1").toString(), "value 1");
      Map inner = (Map) job.getJobSurvey().get("innerobj");
      assertEquals(inner.get("input 2"), "value 2");

      job.setUsername(TeztBeanSimpleFactory.getAdmin().getUsername());
      TimeBooking booking = TeztBeanSimpleFactory.getNewValidBooking();
      booking.setBooked();
      booking.insert();
      job.setJobId(booking.getId());
      JobDataProcessing jdp = TeztBeanSimpleFactory.getNewValidJobDataProcessing();
      jdp.insertOrUpdate();
      job.setIdJobDataProcessing(jdp.getId());
      job.insertOrUpdate();

      Job back = CouchDBDao4Jobs.me().getJob(booking.getId(), step);
      assertNotNull(back);
      assertNotNull(back.getJobSurvey());
      assertEquals(back.getJobSurvey().get("input 0").toString(), "value 0");
      assertEquals(back.getJobSurvey().get("input 1").toString(), "value 1");
      Map innerback = (Map) back.getJobSurvey().get("innerobj");
      assertEquals(innerback.get("input 2"), "value 2");
    } catch (JSONException e) {
      fail("should not throw exception " + e);
    } catch (Exception e) {
      fail("should not throw exception " + e);
    }
  }
}
 private HtmlElement getTimeInput(HttpServletRequest request) {
   HtmlElement result = HtmlFactory.get("div");
   Integer interval = RequestInterpreter.getInterval(request);
   if (interval != null) {
     HtmlElement pStart = HtmlFactory.get("p");
     HtmlElement labeldiv = HtmlFactory.get("span", "Start time:"); // INTLANG
     pStart.add(labeldiv + HtmlFactory.get("br").toString());
     HtmlElement pEnd = HtmlFactory.get("p");
     labeldiv = HtmlFactory.get("span", "End time:"); // INTLANG
     pEnd.add(labeldiv + HtmlFactory.get("br").toString());
     Calendar today = Calendar.getInstance();
     if (interval.intValue() == FacilityAvailability.ONE_TIME) {
       pStart.add(
           FamCalendarHtmlFactory.getDateSelect(
               today, 365, QueryKeys.QUERY_KEY_FROM + QueryKeys.QUERY_KEY_DAY, 0, true));
       pEnd.add(
           FamCalendarHtmlFactory.getDateSelect(
               today, 365, QueryKeys.QUERY_KEY_TO + QueryKeys.QUERY_KEY_DAY, 1, true));
     } else if (interval.intValue() == FacilityAvailability.EACH_YEAR) {
       Calendar firstDayOfYear = Calendar.getInstance();
       firstDayOfYear.set(Calendar.DAY_OF_YEAR, 1);
       pStart.add(
           FamCalendarHtmlFactory.getDateSelect(
               firstDayOfYear,
               365,
               QueryKeys.QUERY_KEY_FROM + QueryKeys.QUERY_KEY_DAY,
               today.get(Calendar.DAY_OF_YEAR),
               false));
       pEnd.add(
           FamCalendarHtmlFactory.getDateSelect(
               firstDayOfYear,
               365,
               QueryKeys.QUERY_KEY_TO + QueryKeys.QUERY_KEY_DAY,
               today.get(Calendar.DAY_OF_YEAR) + 1,
               false));
     } else if (interval.intValue() == FacilityAvailability.EACH_MONTH) {
       int i = 1;
       Calendar firstDayOfMonth = Calendar.getInstance();
       firstDayOfMonth.set(Calendar.DAY_OF_MONTH, 1);
       List<String> optiontexts = new ArrayList<String>();
       while (i <= 28) {
         optiontexts.add(FamDateFormat.getNumeralFormat(i));
         i++;
       }
       pStart.add(
           FamCalendarHtmlFactory.getDateSelect(
               firstDayOfMonth,
               QueryKeys.QUERY_KEY_FROM + QueryKeys.QUERY_KEY_DAY,
               today.get(Calendar.DAY_OF_MONTH) - 1,
               optiontexts));
       pEnd.add(
           FamCalendarHtmlFactory.getDateSelect(
               firstDayOfMonth,
               QueryKeys.QUERY_KEY_TO + QueryKeys.QUERY_KEY_DAY,
               today.get(Calendar.DAY_OF_MONTH),
               optiontexts));
     } else if (interval.intValue() == FacilityAvailability.EACH_WEEK) {
       Calendar weekday = FamCalendar.getInstance();
       weekday.set(Calendar.DAY_OF_WEEK, weekday.getFirstDayOfWeek());
       int pointer = 0;
       List<String> optiontexts = new ArrayList<String>();
       while (pointer < 7) {
         optiontexts.add(
             weekday.getDisplayName(
                 Calendar.DAY_OF_WEEK, Calendar.LONG, FamRequestContainer.locale()));
         weekday.add(Calendar.DAY_OF_YEAR, 1);
         pointer++;
       }
       weekday.add(Calendar.DAY_OF_YEAR, -7);
       pStart.add(
           FamCalendarHtmlFactory.getDateSelect(
               weekday,
               QueryKeys.QUERY_KEY_FROM + QueryKeys.QUERY_KEY_DAY,
               today.get(Calendar.DAY_OF_WEEK) - 1,
               optiontexts));
       pEnd.add(
           FamCalendarHtmlFactory.getDateSelect(
               weekday,
               QueryKeys.QUERY_KEY_TO + QueryKeys.QUERY_KEY_DAY,
               today.get(Calendar.DAY_OF_WEEK),
               optiontexts));
     } else { // each day or each hour
       QueryString qs =
           QueryStringFactory.get(
               QueryKeys.QUERY_KEY_FROM + QueryKeys.QUERY_KEY_DAY,
               QueryKeys.getEncodeStringOfDate(today));
       pStart.add(qs.getAsHtmlInputsTypeHidden());
       List<String> optiontexts = new ArrayList<String>(2);
       optiontexts.add("same day"); // INTLANG
       optiontexts.add("next day"); // INTLANG
       pEnd.add(
           FamCalendarHtmlFactory.getDateSelect(
               today, QueryKeys.QUERY_KEY_TO + QueryKeys.QUERY_KEY_DAY, 0, optiontexts));
     }
     pStart.add(
         FamCalendarHtmlFactory.getTimeSelectCompact(
             QueryKeys.QUERY_KEY_FROM + QueryKeys.QUERY_KEY_HOUR_OF_DAY, 0));
     pEnd.add(
         FamCalendarHtmlFactory.getTimeSelectCompact(
             QueryKeys.QUERY_KEY_TO + QueryKeys.QUERY_KEY_HOUR_OF_DAY, 0));
     result.add(pStart);
     result.add(pEnd);
   }
   return result;
 }