@Test
  public void convertsJodaTimeTypesCorrectly() {

    List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
    converters.add(new LocalDateToDateConverter());
    converters.add(new DateToLocalDateConverter());

    List<Class<?>> customSimpleTypes = new ArrayList<Class<?>>();
    customSimpleTypes.add(LocalDate.class);
    mappingContext.setCustomSimpleTypes(customSimpleTypes);

    converter = new MappingMongoConverter(mappingContext);
    converter.setConverters(converters);
    converter.afterPropertiesSet();

    Person person = new Person();
    person.birthDate = new LocalDate();

    DBObject dbObject = new BasicDBObject();
    converter.write(person, dbObject);

    assertTrue(dbObject.get("birthDate") instanceof Date);

    Person result = converter.read(Person.class, dbObject);
    assertThat(result.birthDate, is(notNullValue()));
  }
Пример #2
0
 @Test
 public void testGetName() {
   // fail("Not yet implemented");
   Person p = new Person("Last", "First");
   String name = p.getName();
   assertEquals(name, "First Last");
 }
Пример #3
0
 @Test
 public void testGetPerson() {
   Book b1 = new Book("Learn SQL");
   Person p1 = new Person("John Doe");
   b1.setPerson(p1);
   Person testPerson = b1.getPerson();
   assertEquals(p1, testPerson);
   assertEquals("John Doe", testPerson.getName());
 }
Пример #4
0
  @Test
  public void test() {

    Person quote =
        secureRestTemplate.getForObject(
            "http://localhost:8080/persona/55d3ce238439cfaa984ef08f", Person.class);
    System.out.println("Made it:" + quote.toString());
    // log.info(quote.toString());
  }
 @Test
 public void testFindAndUpdateUpsert() {
   template.insert(new Person("Tom", 21));
   template.insert(new Person("Dick", 22));
   Query query = new Query(Criteria.where("firstName").is("Harry"));
   Update update = new Update().set("age", 23);
   Person p =
       template.findAndModify(
           query, update, new FindAndModifyOptions().upsert(true).returnNew(true), Person.class);
   assertThat(p.getFirstName(), is("Harry"));
   assertThat(p.getAge(), is(23));
 }
  @Test
  public void insertsSimpleEntityCorrectly() throws Exception {

    Person person = new Person("Oliver");
    person.setAge(25);
    template.insert(person);

    List<Person> result =
        template.find(new Query(Criteria.where("_id").is(person.getId())), Person.class);
    assertThat(result.size(), is(1));
    assertThat(result, hasItem(person));
  }
 @Test
 @Sql(
     statements = {
       "INSERT INTO Person (id, firstName, lastName)" + " VALUES (42, 'John', 'Smith')"
     })
 public void testGenerateSchema() throws Exception {
   entityManager.getMetamodel().entity(Person.class);
   // When the above call does not throw an exception,
   // the Person class is mapped as a persistent entity.
   Person p = entityManager.find(Person.class, 42L);
   assertEquals("John", p.getFirstName());
   assertEquals("Smith", p.getLastName());
 }
  @Test
  public void writesTypeDiscriminatorIntoRootObject() {

    Person person = new Person();
    person.birthDate = new LocalDate();

    DBObject result = new BasicDBObject();
    converter.write(person, result);

    assertThat(result.containsField(MappingMongoConverter.CUSTOM_TYPE_KEY), is(true));
    assertThat(
        result.get(MappingMongoConverter.CUSTOM_TYPE_KEY).toString(), is(Person.class.getName()));
  }
  @Test
  public void bogusUpdateDoesNotTriggerException() throws Exception {

    MongoTemplate mongoTemplate = new MongoTemplate(factory);
    mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);

    Person person = new Person("Oliver2");
    person.setAge(25);
    mongoTemplate.insert(person);

    Query q = new Query(Criteria.where("BOGUS").gt(22));
    Update u = new Update().set("firstName", "Sven");
    mongoTemplate.updateFirst(q, u, Person.class);
  }
Пример #10
0
  @Test
  public void testJaxb() throws JAXBException {
    Person pb = new Person();
    pb.setGivenName("Anton");
    pb.setFamilyName("Johansson");
    pb.setInstitution("TFE");
    List<String> emails = new ArrayList<String>();
    emails.add("*****@*****.**");
    emails.add("*****@*****.**");
    pb.setEmails(emails);

    JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);

    Marshaller m = jaxbContext.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter writer = new StringWriter();

    m.marshal(pb, writer);
    System.out.println("======= Marshalling =============");
    System.out.println(writer);

    System.out.println("======= Unmarshalling =============");
    Unmarshaller um = jaxbContext.createUnmarshaller();
    Reader reader = new StringReader(writer.toString());
    Person umPb = (Person) um.unmarshal(reader);
    assertEquals(pb.getEmails().get(0), umPb.getEmails().get(0));
    assertEquals(pb.getEmails().get(1), umPb.getEmails().get(1));
    assertEquals(pb, umPb);
  }
Пример #11
0
  /*
   * Also tests that if you add a person, number pair then modify the person,
   * you can't get the number out of the phone book again.
   */
  @Test
  public void testCantAccessNumIfChangePersonObj2() {
    PhoneBook myBook = new PhoneBook();
    Person person1 = new Person("Jane");
    PhoneNumber num1 = new PhoneNumber("5105551234");

    myBook.addEntry(person1, num1);
    person1.changeName("Eungie");
    try {
      PhoneNumber numReturned = myBook.getNumber(person1);
      assertNull(numReturned);
    } catch (IllegalArgumentException e) {
    }
  }
Пример #12
0
  @Test
  public void testGetRSVPModifyNamesAfterAdded() {
    Party party = new Party();
    Person a1 = new Person(new String(KANY_GARCIA));
    Person a2 = new Person(new String(KANY_GARCIA));

    party.addInvited(a1);
    party.addRSVP(a1);

    a1.setName(new String(LUIS_MIGUEL));

    ArrayList<Person> list = party.getRSVP();
    assertEquals("Incorrect result", 1, list.size());
    assertTrue("Incorrect result", list.contains(a2));
  }
Пример #13
0
 @Test
 public void testIsEmbeddableAnnotated() {
   assertTrue(finder.isEmbeddableAnnotated(job.getClass()));
   assertFalse(finder.isEmbeddableAnnotated(person.getClass()));
   assertFalse(finder.isEmbeddableAnnotated(getClass()));
   assertFalse(finder.isEmbeddableAnnotated(null));
 }
  @Test
  @Points("85.2")
  public void test5() {
    int odotettu = pekka.getWeight() + 1;
    feed(ref, pekka);

    assertEquals(
        "Feeding should increase the weight by one kilo. Check code: \n"
            + "ref = Reformatory(); "
            + "h = new Person(\"Pekka\", 33, 175, 78); "
            + "ref.feed(h); "
            + "System.out.println( h.getWeight() )",
        odotettu,
        pekka.getWeight());

    assertEquals(
        "Feeding should increase the weight by one kilo. Check code: \n"
            + "ref = Reformatory(); "
            + "h = new Person(\"Pekka\", 33, 175, 78); "
            + "ref.feed(h); "
            + "System.out.println( ref.getWeight(h) )",
        odotettu,
        ref.weight(pekka));

    feed(ref, pekka);
    feed(ref, pekka);
    feed(ref, pekka);
    feed(ref, pekka);

    assertEquals(
        "Feeding should increase the weight by one kilo. Check code: \n"
            + "ref = Reformatory(); "
            + "pekka = new Person(\"Pekka\", 33, 175, 78); "
            + "ref.feed(pekka); "
            + "ref.feed(pekka); "
            + "ref.feed(pekka); "
            + "ref.feed(pekka); "
            + "ref.feed(pekka); "
            + "System.out.println( pekka.getWeight() )",
        odotettu + 4,
        pekka.getWeight());
  }
Пример #15
0
  @Test
  public void testGetRSVPModifyNamesReturned() {
    Party party = new Party();
    Person a = new Person(new String(KANY_GARCIA));

    party.addInvited(a);
    party.addRSVP(a);

    ArrayList<Person> list;

    list = party.getRSVP();
    assertEquals("Incorrect result", 1, list.size());
    for (Person p : list) {
      p.setName(new String(MIGUEL_RIOS));
    }

    list = party.getRSVP();
    assertEquals("Incorrect result", 1, list.size());
    assertTrue("Incorrect result", list.contains(a));
  }
  @Test
  public void testEnsureIndex() throws Exception {

    Person p1 = new Person("Oliver");
    p1.setAge(25);
    template.insert(p1);
    Person p2 = new Person("Sven");
    p2.setAge(40);
    template.insert(p2);

    template
        .indexOps(Person.class)
        .ensureIndex(new Index().on("age", Order.DESCENDING).unique(Duplicates.DROP));

    DBCollection coll = template.getCollection(template.getCollectionName(Person.class));
    List<DBObject> indexInfo = coll.getIndexInfo();
    assertThat(indexInfo.size(), is(2));
    String indexKey = null;
    boolean unique = false;
    boolean dropDupes = false;
    for (DBObject ix : indexInfo) {
      if ("age_-1".equals(ix.get("name"))) {
        indexKey = ix.get("key").toString();
        unique = (Boolean) ix.get("unique");
        dropDupes = (Boolean) ix.get("dropDups");
      }
    }
    assertThat(indexKey, is("{ \"age\" : -1}"));
    assertThat(unique, is(true));
    assertThat(dropDupes, is(true));

    List<IndexInfo> indexInfoList = template.indexOps(Person.class).getIndexInfo();
    System.out.println(indexInfoList);
    assertThat(indexInfoList.size(), is(2));
    IndexInfo ii = indexInfoList.get(1);
    assertThat(ii.isUnique(), is(true));
    assertThat(ii.isDropDuplicates(), is(true));
    assertThat(ii.isSparse(), is(false));
    assertThat(ii.getFieldSpec().containsKey("age"), is(true));
    assertThat(ii.getFieldSpec().containsValue(Order.DESCENDING), is(true));
  }
Пример #17
0
  @Test
  public void testGetInvitedModifyNamesAfterAdded() {
    Party party = new Party();
    Person a = new Person(new String(KANY_GARCIA));
    Person b = new Person(new String(LAURA_PAUSINI));

    Person a1 = new Person(new String(KANY_GARCIA));
    Person b1 = new Person(new String(LAURA_PAUSINI));

    party.addInvited(a);
    party.addInvited(b);

    a.setName(new String(LUIS_MIGUEL));
    b.setName(new String(LU));

    ArrayList<Person> list;

    list = party.getInvited();
    assertEquals("Incorrect result", 2, list.size());
    assertTrue("Incorrect result", list.contains(a1));
    assertTrue("Incorrect result", list.contains(b1));
  }
Пример #18
0
 @Test
 public void testFindFieldColumns() {
   Map<String, Class> columnMap = new HashMap<String, Class>();
   finder.findFieldColumns(job.getClass(), null, columnMap, false);
   String columnType = columnMap.get("summary").getCanonicalName();
   assertEquals(
       "expecting 'java.lang.String' but found " + columnType, columnType, "java.lang.String");
   columnMap.clear();
   finder.findFieldColumns(job.getClass(), null, columnMap, true);
   assertNotNull(
       "expecting not null but found " + columnMap.get("summary"), columnMap.get("summary"));
   assertTrue("expecting size == 1 but found " + columnMap.size(), columnMap.size() == 1);
   columnMap.clear();
   finder.findFieldColumns(person.getClass(), null, columnMap, false);
   columnType = columnMap.get("summary").getCanonicalName();
   assertTrue(
       "expecting 'java.lang.String' but found " + columnType,
       columnType.equals("java.lang.String"));
   columnMap.clear();
   finder.findFieldColumns(person.getClass(), null, columnMap, true);
   assertTrue("expecting size == 0 but found " + columnMap.size(), columnMap.isEmpty());
   columnMap.clear();
   finder.findFieldColumns(job.getClass(), "zipCode", columnMap, false);
   assertTrue("expecting size == 0 but found " + columnMap.size(), columnMap.isEmpty());
   columnMap.clear();
   finder.findFieldColumns(job.getClass(), "zipCode", columnMap, true);
   assertTrue("expecting size == 0 but found " + columnMap.size(), columnMap.isEmpty());
   columnMap.clear();
   finder.findFieldColumns(job.getClass(), "name", columnMap, true);
   assertTrue("expecting size == 0 but found " + columnMap.size(), columnMap.isEmpty());
   columnMap.clear();
   finder.findFieldColumns(job.getClass(), "dummyFieldName", columnMap, false);
   assertTrue("expecting size == 0 but found " + columnMap.size(), columnMap.isEmpty());
   columnMap.clear();
   finder.findFieldColumns(job.getClass(), "description", columnMap, true);
   assertTrue("expecting size == 1 but found " + columnMap.size(), columnMap.size() == 1);
   assertNotNull(
       "expecting not null but found " + columnMap.get("summary"), columnMap.get("summary"));
 }
  private void prepareTestData() {

    r1 = newRoom(RoomType.apartment, 1);
    r2 = newRoom(RoomType.apartment, 2);
    r3 = newRoom(RoomType.bungalow, 3);

    p1 = newPerson("Jozko1", "Mrkvička1", "obc3211", "tel6541", "[email protected]");
    p2 = newPerson("Jozko2", "Mrkvička2", "obc3212", "tel6542", "[email protected]");
    p3 = newPerson("Jozko3", "Mrkvička3", "obc3213", "tel6543", "[email protected]");
    p4 = newPerson("Jozko4", "Mrkvička4", "obc3214", "tel6544", "[email protected]");
    p5 = newPerson("Jozko5", "Mrkvička5", "obc3215", "tel6545", "[email protected]");

    personManager.createPerson(p1);
    personManager.createPerson(p2);
    personManager.createPerson(p3);
    personManager.createPerson(p4);
    personManager.createPerson(p5);

    roomManager.createRoom(r1);
    roomManager.createRoom(r2);
    roomManager.createRoom(r3);

    roomWithNullId = newRoom(RoomType.apartment, 1);
    roomNotInDB = newRoom(RoomType.apartment, 1);
    roomNotInDB.setId(r3.getId() + 100);
    personWithNullId =
        newPerson(
            "Jozko null", "Mrkvička null", "obc321 null", "tel654 null", "[email protected] null");
    personNotInDB =
        newPerson(
            "Jozko not in db",
            "Mrkvička not in db",
            "obc321 not in db",
            "tel654 not in db",
            "[email protected] not in db");
    personNotInDB.setId(p5.getId() + 100);
  }
Пример #20
0
  @Test
  public void testFindAllColumnNamesFrom() {
    checkColumnNameWith(finder.findAllColumnNamesFrom(job.getClass(), null, false));
    checkColumnNameWith(finder.findAllColumnNamesFrom(person.getClass(), "", false));

    Map<String, Class> columnMap = finder.findAllColumnNamesFrom(person.getClass(), "name", false);
    assertEquals(columnMap.get("job_name").getCanonicalName(), "java.lang.String");
    assertTrue("expecting size == 1 but found " + columnMap.size(), columnMap.size() == 1);

    columnMap = finder.findAllColumnNamesFrom(person.getClass(), "dummy", false);
    assertTrue("expecting size == 0 but found " + columnMap.size(), columnMap.isEmpty());

    columnMap = finder.findAllColumnNamesFrom(job.getClass(), null, true);
    assertTrue("expecting size == 2 but found " + columnMap.size(), columnMap.size() == 2);
    assertNotNull(
        "expecting not null but found " + columnMap.get("job_name"), columnMap.get("job_name"));
    assertNotNull(
        "expecting not null but found " + columnMap.get("summary"), columnMap.get("summary"));

    columnMap = finder.findAllColumnNamesFrom(person.getClass(), "", true);
    assertTrue("expecting size == 0 but found " + columnMap.size(), columnMap.isEmpty());
    columnMap = finder.findAllColumnNamesFrom(person.getClass(), "name", true);
    assertTrue("expecing size == 0 but found " + columnMap.size(), columnMap.isEmpty());
  }
  @Test
  @Points("85.1")
  public void test2() {
    assertEquals(
        "check code: ref = Reformatory(); "
            + "h = new Person(\"Pekka\", 33, 175, 78); "
            + "System.out.println( ref.weight(h) )",
        pekka.getWeight(),
        ref.weight(pekka));

    for (int i = 0; i < 5; i++) {
      int paino = 60 + arpa.nextInt(60);
      Person mikko = new Person("Mikko", 45, 181, paino);

      assertEquals(
          "check code: ref = Reformatory(); "
              + "h = new Person(\"Mikko\", 45, 181, "
              + paino
              + "); "
              + "System.out.println( ref.weight(h) )",
          mikko.getWeight(),
          ref.weight(mikko));
    }
  }
Пример #22
0
 @Test
 public void sortByExample3() {
   // We have the names of some people in the right order, but then we want to sort the actual
   // Person objects
   // into the same order.  Providing a mapping function from the Person to his/her name is all we
   // need.
   final Person huey = new Person("huey");
   final Person dewey = new Person("dewey");
   final Person louie = new Person("louie");
   List<String> example = asList("donald", huey.getName(), dewey.getName(), louie.getName());
   assertEquals(
       asList(huey, dewey, louie),
       Algorithms.sortByExample(
           example,
           asList(dewey, louie, huey),
           new Function<Person, String>() {
             public String apply(Person p) {
               return p.getName();
             }
           }));
 }
 @Test
 public void correctDescription() {
   assertEquals("Julie, Trousers, Shoes, Pullover", p1.getDescription());
 }
  /** @see DATAMONGO-234 */
  @Test
  public void testFindAndUpdate() {

    template.insert(new Person("Tom", 21));
    template.insert(new Person("Dick", 22));
    template.insert(new Person("Harry", 23));

    Query query = new Query(Criteria.where("firstName").is("Harry"));
    Update update = new Update().inc("age", 1);
    Person p = template.findAndModify(query, update, Person.class); // return old
    assertThat(p.getFirstName(), is("Harry"));
    assertThat(p.getAge(), is(23));
    p = template.findOne(query, Person.class);
    assertThat(p.getAge(), is(24));

    p = template.findAndModify(query, update, Person.class, "person");
    assertThat(p.getAge(), is(24));
    p = template.findOne(query, Person.class);
    assertThat(p.getAge(), is(25));

    p =
        template.findAndModify(
            query, update, new FindAndModifyOptions().returnNew(true), Person.class);
    assertThat(p.getAge(), is(26));

    p = template.findAndModify(query, update, null, Person.class, "person");
    assertThat(p.getAge(), is(26));
    p = template.findOne(query, Person.class);
    assertThat(p.getAge(), is(27));

    Query query2 = new Query(Criteria.where("firstName").is("Mary"));
    p =
        template.findAndModify(
            query2, update, new FindAndModifyOptions().returnNew(true).upsert(true), Person.class);
    assertThat(p.getFirstName(), is("Mary"));
    assertThat(p.getAge(), is(1));
  }
 @Test
 public void correctAmount() {
   assertEquals(260.0, p1.clothingCost(), 0.0);
 }
  @Test
  public void multipleOperations_ShouldReturnWorkCorrectly() {
    boolean isAdded = persons.addPerson("*****@*****.**", "Pesho", 28, "Plovdiv");
    assertTrue(isAdded);
    assertEquals(1, persons.size());

    isAdded = persons.addPerson("*****@*****.**", "Pesho2", 222, "Plovdiv222");
    assertFalse(isAdded);
    assertEquals(1, persons.size());

    persons.addPerson("*****@*****.**", "Kiril", 22, "Plovdiv");
    assertEquals(2, persons.size());

    persons.addPerson("*****@*****.**", "Asen", 22, "Sofia");
    assertEquals(3, persons.size());

    Person person = persons.findPerson("non-existing person");
    assertNull(person);

    person = persons.findPerson("*****@*****.**");
    assertNotNull(person);
    assertEquals("*****@*****.**", person.getEmail());
    assertEquals("Pesho", person.getName());
    assertEquals(28, person.getAge());
    assertEquals("Plovdiv", person.getTown());

    List<Person> personsGmail = (List<Person>) persons.findPersons("gmail.com");
    assertArrayEquals(
        new String[] {"*****@*****.**", "*****@*****.**"},
        personsGmail.stream().map(p -> p.getEmail()).toArray());

    List<Person> personsPeshoPlovdiv = (List<Person>) persons.findPersons("Pesho", "Plovdiv");
    assertArrayEquals(
        new String[] {"*****@*****.**"},
        personsPeshoPlovdiv.stream().map(p -> p.getEmail()).toArray());

    List<Person> personsPeshoSofia = (List<Person>) persons.findPersons("Pesho", "Sofia");
    assertEquals(0, personsPeshoSofia.size());

    List<Person> personsKiroPlovdiv = (List<Person>) persons.findPersons("Kiro", "Plovdiv");
    assertEquals(0, personsKiroPlovdiv.size());

    List<Person> personsAge22To28 = (List<Person>) persons.findPersons(22, 28);
    assertArrayEquals(
        new String[] {"*****@*****.**", "*****@*****.**", "*****@*****.**"},
        personsAge22To28.stream().map(p -> p.getEmail()).toArray());

    List<Person> personsAge22To28Plovdiv = (List<Person>) persons.findPersons(22, 28, "Plovdiv");
    assertArrayEquals(
        new String[] {"*****@*****.**", "*****@*****.**"},
        personsAge22To28Plovdiv.stream().map(p -> p.getEmail()).toArray());

    boolean isDeleted = persons.deletePerson("*****@*****.**");
    assertTrue(isDeleted);

    isDeleted = persons.deletePerson("*****@*****.**");
    assertFalse(isDeleted);

    person = persons.findPerson("*****@*****.**");
    assertNull(person);

    personsGmail = (List<Person>) persons.findPersons("gmail.com");
    assertArrayEquals(
        new String[] {"*****@*****.**"}, personsGmail.stream().map(p -> p.getEmail()).toArray());

    personsPeshoPlovdiv = (List<Person>) persons.findPersons("Pesho", "Plovdiv");
    assertArrayEquals(
        new String[] {}, personsPeshoPlovdiv.stream().map(p -> p.getEmail()).toArray());

    personsPeshoSofia = (List<Person>) persons.findPersons("Pesho", "Sofia");
    assertEquals(0, personsPeshoSofia.size());

    personsKiroPlovdiv = (List<Person>) persons.findPersons("Kiro", "Plovdiv");
    assertEquals(0, personsKiroPlovdiv.size());

    personsAge22To28 = (List<Person>) persons.findPersons(22, 28);
    assertArrayEquals(
        new String[] {"*****@*****.**", "*****@*****.**"},
        personsAge22To28.stream().map(p -> p.getEmail()).toArray());

    personsAge22To28Plovdiv = (List<Person>) persons.findPersons(22, 28, "Plovdiv");
    assertArrayEquals(
        new String[] {"*****@*****.**"},
        personsAge22To28Plovdiv.stream().map(p -> p.getEmail()).toArray());
  }
Пример #27
0
 @Test
 @FileParameters(value = "src/test/resources/test.csv", mapper = PersonMapper.class)
 public void loadParamsFromFileWithCustomMapper(Person person) {
   assertTrue(person.getAge() > 0);
 }