Beispiel #1
0
  public static void main(String[] args) {
    boolean ok = true;
    if (args != null && args.length == 2) {
      String edmxFilePath = args[0];
      String targetDirectoryStr = args[1];
      File edmxFile = new File(edmxFilePath);
      File targetDirectory = new File(targetDirectoryStr);

      // check our configuration
      if (!edmxFile.exists()) {
        System.out.println("EDMX file not found");
        ok = false;
      }
      if (!targetDirectory.exists() || !targetDirectory.isDirectory()) {
        System.out.println("Target directory is invalid");
        ok = false;
      }

      if (ok) {
        JPAResponderGen rg = new JPAResponderGen();
        System.out.println("Writing source and configuration to [" + targetDirectory + "]");
        ok =
            rg.generateArtifacts(
                new EDMXAdapter(edmxFilePath), targetDirectory, targetDirectory, true);
      }
    } else {
      ok = false;
    }
    if (!ok) {
      System.out.print(usage());
    }
  }
  @Test
  public void testJPAEntityFieldInfoAnnotations() {
    JPAResponderGen rg = new JPAResponderGen();

    EntityMetadata mdEntity = new EntityMetadata("Flight");
    Vocabulary voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.INTEGER_NUMBER));
    voc.setTerm(new TermIdField(true));
    mdEntity.setPropertyVocabulary("ID", voc);
    voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.TIMESTAMP));
    mdEntity.setPropertyVocabulary("departureDT", voc);
    voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.DATE));
    mdEntity.setPropertyVocabulary("departureDate", voc);
    voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.TIME));
    mdEntity.setPropertyVocabulary("departureTime", voc);
    EntityInfo p = rg.createEntityInfoFromEntityMetadata("AirlineModel", mdEntity);

    // Annotations
    FieldInfo dateOnlyFI = p.getFieldInfos().get(0);
    assertEquals(1, dateOnlyFI.getAnnotations().size());
    assertEquals("@Temporal(TemporalType.DATE)", dateOnlyFI.getAnnotations().get(0));

    FieldInfo timeFI = p.getFieldInfos().get(1);
    assertEquals(1, timeFI.getAnnotations().size());
    assertEquals("@Temporal(TemporalType.TIME)", timeFI.getAnnotations().get(0));

    FieldInfo dateFI = p.getFieldInfos().get(2);
    assertEquals(1, dateFI.getAnnotations().size());
    assertEquals("@Temporal(TemporalType.TIMESTAMP)", dateFI.getAnnotations().get(0));
  }
  @Test
  public void testJPAEntityFieldInfo() {
    JPAResponderGen rg = new JPAResponderGen();

    EntityMetadata mdEntity = new EntityMetadata("Flight");
    Vocabulary voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.INTEGER_NUMBER));
    voc.setTerm(new TermIdField(true));
    mdEntity.setPropertyVocabulary("ID", voc);
    voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.INTEGER_NUMBER));
    mdEntity.setPropertyVocabulary("flightID", voc);
    mdEntity.setPropertyVocabulary("number", new Vocabulary());
    mdEntity.setPropertyVocabulary("runway", new Vocabulary());
    voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.TIMESTAMP));
    mdEntity.setPropertyVocabulary("departureDT", voc);
    voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.DATE));
    mdEntity.setPropertyVocabulary("departureDate", voc);
    voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.TIME));
    mdEntity.setPropertyVocabulary("departureTime", voc);
    EntityInfo p = rg.createEntityInfoFromEntityMetadata("AirlineModel", mdEntity);

    assertEquals(6, p.getFieldInfos().size());
    assertTrue(p.getFieldInfos().contains(new FieldInfo("flightID", "Long", null)));
    assertTrue(p.getFieldInfos().contains(new FieldInfo("number", "String", null)));
    assertTrue(p.getFieldInfos().contains(new FieldInfo("runway", "String", null)));
    assertTrue(p.getFieldInfos().contains(new FieldInfo("departureDT", "java.util.Date", null)));
    assertTrue(p.getFieldInfos().contains(new FieldInfo("departureDate", "java.util.Date", null)));
    assertTrue(p.getFieldInfos().contains(new FieldInfo("departureTime", "java.util.Date", null)));
  }
  @Test
  public void testJPAEntityKeyInfo() {
    JPAResponderGen rg = new JPAResponderGen();

    EntityMetadata mdEntity = new EntityMetadata("Flight");
    Vocabulary voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.INTEGER_NUMBER));
    voc.setTerm(new TermIdField(true));
    mdEntity.setPropertyVocabulary("flightID", voc);
    EntityInfo p = rg.createEntityInfoFromEntityMetadata("AirlineModel", mdEntity);

    assertEquals("flightID", p.getKeyInfo().getName());
    assertEquals("Long", p.getKeyInfo().getType());
    assertEquals(0, p.getFieldInfos().size());
  }
  @Test
  public void testEntityInfo() {
    JPAResponderGen rg = new JPAResponderGen();

    EntityMetadata mdEntity = new EntityMetadata("Flight");
    Vocabulary voc = new Vocabulary();
    voc.setTerm(new TermValueType(TermValueType.INTEGER_NUMBER));
    voc.setTerm(new TermIdField(true));
    mdEntity.setPropertyVocabulary("ID", voc);
    EntityInfo p = rg.createEntityInfoFromEntityMetadata("AirlineModel", mdEntity);

    assertEquals("Flight", p.getClazz());
    assertEquals("AirlineModel", p.getPackage());
    assertEquals("AirlineModel.Flight", p.getFQTypeName());
  }
 @Test
 public void testFormClassFilename() {
   assertEquals(
       "/tmp/blah/com/some/package/SomeClass.java",
       JPAResponderGen.formClassFilename(
           "/tmp/blah/com/some/package",
           new EntityInfo("SomeClass", "com.some.package", null, null)));
 }
  @Test
  public void testGenJPAConfiguration() {
    JPAResponderGen rg = new JPAResponderGen();

    List<EntityInfo> entitiesInfo = new ArrayList<EntityInfo>();
    entitiesInfo.add(new EntityInfo("Flight", "AirlineModel", null, null));
    entitiesInfo.add(new EntityInfo("Airport", "AirlineModel", null, null));
    entitiesInfo.add(new EntityInfo("FlightSchedule", "AirlineModel", null, null));
    String generatedPersistenceConfig = rg.generateJPAConfiguration(entitiesInfo);

    assertTrue(
        generatedPersistenceConfig.contains(
            "<!-- Generated JPA configuration for IRIS MockResponder -->"));
    assertTrue(generatedPersistenceConfig.contains("<class>AirlineModel.Flight</class>"));
    assertTrue(generatedPersistenceConfig.contains("<class>AirlineModel.Airport</class>"));
    assertTrue(generatedPersistenceConfig.contains("<class>AirlineModel.FlightSchedule</class>"));
  }
  @Test
  public void testGenResponderDML() {
    JPAResponderGen rg = new JPAResponderGen();

    List<EntityInfo> entitiesInfo = new ArrayList<EntityInfo>();

    List<FieldInfo> properties = new ArrayList<FieldInfo>();
    properties.add(new FieldInfo("number", "Long", null));
    properties.add(new FieldInfo("fitHostiesName", "String", null));
    properties.add(new FieldInfo("runway", "String", null));
    entitiesInfo.add(new EntityInfo("Flight", "AirlineModel", null, properties));
    entitiesInfo.add(new EntityInfo("Airport", "AirlineModel", null, null));
    entitiesInfo.add(new EntityInfo("FlightSchedule", "AirlineModel", null, null));
    String generatedResponderDML = rg.generateResponderDML(entitiesInfo);

    assertTrue(
        generatedResponderDML.contains(
            "INSERT INTO `Flight`(`number` , `fitHostiesName` , `runway`) VALUES('1' , 'abc' , 'abc');"));
    assertTrue(generatedResponderDML.contains("INSERT INTO `Airport`() VALUES();"));
    assertTrue(generatedResponderDML.contains("INSERT INTO `FlightSchedule`() VALUES();"));
  }
  @Test
  public void testGenJPAEntity() {
    JPAResponderGen rg = new JPAResponderGen();

    FieldInfo keyInfo = new FieldInfo("flightID", "Long", null);
    List<FieldInfo> properties = new ArrayList<FieldInfo>();
    properties.add(new FieldInfo("number", "String", null));
    properties.add(new FieldInfo("fitHostiesName", "String", null));
    properties.add(new FieldInfo("runway", "String", null));
    properties.add(new FieldInfo("passengers", "Integer", null));

    List<String> annotations = new ArrayList<String>();
    annotations.add("@Temporal(TemporalType.TIMESTAMP)");
    properties.add(new FieldInfo("departureDT", "java.util.Date", annotations));

    properties.add(new FieldInfo("dinnerServed", "java.sql.Timestamp", null));

    EntityInfo jpaEntity = new EntityInfo("Flight", "AirlineModel.model", keyInfo, properties);
    String generatedClass = rg.generateJPAEntityClass(jpaEntity);

    assertTrue(generatedClass.contains("package AirlineModel.model;"));
    assertTrue(generatedClass.contains("import javax.persistence.Entity;"));
    assertTrue(generatedClass.contains("@Entity"));
    assertTrue(generatedClass.contains("public class Flight {"));
    assertTrue(generatedClass.contains("@Id"));
    assertTrue(generatedClass.contains("@Basic(optional = false)"));
    assertTrue(generatedClass.contains("private Long flightID;"));
    assertTrue(generatedClass.contains("private String number;"));
    assertTrue(generatedClass.contains("private String fitHostiesName;"));
    assertTrue(generatedClass.contains("private String runway;"));
    assertTrue(generatedClass.contains("private Integer passengers;"));

    // date handling needs some special support
    assertTrue(generatedClass.contains("@Temporal(TemporalType.TIMESTAMP)"));
    assertTrue(generatedClass.contains("private java.util.Date departureDT;"));

    assertTrue(generatedClass.contains("private java.sql.Timestamp dinnerServed;"));
    assertTrue(generatedClass.contains("public Flight() {"));
  }
  @Test
  public void testGenJPAEntitySimpleJoin() {
    JPAResponderGen rg = new JPAResponderGen();

    FieldInfo keyInfo = new FieldInfo("flightID", "Long", null);

    List<FieldInfo> properties = new ArrayList<FieldInfo>();
    properties.add(new FieldInfo("number", "String", null));
    List<String> annotations = new ArrayList<String>();
    annotations.add("@Temporal(TemporalType.TIMESTAMP)");
    properties.add(new FieldInfo("departureDT", "java.util.Date", annotations));

    List<JoinInfo> joins = new ArrayList<JoinInfo>();
    List<String> joinAnnotations = new ArrayList<String>();
    joinAnnotations.add("@ManyToMany(mappedBy=\"Flight\")");
    joins.add(new JoinInfo("passengers", "Person", joinAnnotations));

    EntityInfo jpaEntity =
        new EntityInfo("Flight", "AirlineModel.model", keyInfo, properties, joins, true);
    String generatedClass = rg.generateJPAEntityClass(jpaEntity);

    assertTrue(generatedClass.contains("package AirlineModel.model;"));
    assertTrue(generatedClass.contains("import javax.persistence.Entity;"));
    assertTrue(generatedClass.contains("@Entity"));
    assertTrue(generatedClass.contains("public class Flight {"));
    assertTrue(generatedClass.contains("@Id"));
    assertTrue(generatedClass.contains("@Basic(optional = false)"));
    assertTrue(generatedClass.contains("private Long flightID;"));
    assertTrue(generatedClass.contains("private String number;"));

    assertTrue(generatedClass.contains("@Temporal(TemporalType.TIMESTAMP)"));
    assertTrue(generatedClass.contains("private java.util.Date departureDT;"));

    assertTrue(generatedClass.contains("@ManyToMany(mappedBy=\"Flight\")"));
    assertTrue(generatedClass.contains("private Collection<Person> passengers;"));

    assertTrue(generatedClass.contains("public Flight() {"));
  }
  @Test
  public void testGetRIM() {
    // Parse the test metadata
    MetadataParser parser = new MetadataParser();
    InputStream is =
        parser.getClass().getClassLoader().getResourceAsStream(METADATA_AIRLINE_XML_FILE);
    Metadata metadata = parser.parse(is);
    Assert.assertNotNull(metadata);

    // Define the interaction model
    InteractionModel interactionModel = new InteractionModel(metadata);

    IMResourceStateMachine rsmFlightSchedule =
        interactionModel.findResourceStateMachine("FlightSchedule");
    IMResourceStateMachine rsmAirport = interactionModel.findResourceStateMachine("Airport");
    IMResourceStateMachine rsmFlight = interactionModel.findResourceStateMachine("Flight");
    IMResourceStateMachine rsmPassenger = interactionModel.findResourceStateMachine("Passenger");
    rsmFlight.addTransitionToEntityState(
        "flight", rsmFlightSchedule, "flightschedule", "flightScheduleNum", null);
    rsmFlightSchedule.addTransitionToEntityState(
        "flightschedule", rsmAirport, "departureAirport", "departureAirportCode", null);
    rsmFlightSchedule.addTransitionToEntityState(
        "flightschedule", rsmAirport, "arrivalAirport", "arrivalAirportCode", null);
    rsmAirport.addTransitionToCollectionState(
        "airport",
        rsmFlightSchedule,
        "departures",
        "departureAirportCode eq '{code}'",
        "departureAirportCode",
        null);
    rsmAirport.addTransitionToCollectionState(
        "airport",
        rsmFlightSchedule,
        "arrivals",
        "arrivalAirportCode eq '{code}'",
        "arrivalAirportCode",
        null);
    rsmPassenger.addTransitionToEntityState("passenger", rsmFlight, "flight", "flightID", null);

    // Run the generator
    JPAResponderGen generator = new JPAResponderGen(true);
    Commands commands = JPAResponderGen.getDefaultCommands();
    String rimDSL = null;
    try {
      InputStream isRimDsl = generator.getRIM(interactionModel, commands);
      assertNotNull(isRimDsl);

      rimDSL = CharStreams.toString(new InputStreamReader(isRimDsl, "UTF-8"));
    } catch (Exception age) {
      fail(age.getMessage());
    }

    // Check the rim dsl
    assertTrue(rimDSL.contains("initial resource ServiceDocument"));
    assertTrue(rimDSL.contains("GET -> FlightSchedules"));
    assertTrue(rimDSL.contains("resource FlightSchedules"));
    assertTrue(
        rimDSL.contains(
            "GET *-> flightschedule {"
                + RIM_LINE_SEP
                + "\t\tparameters [ id=\"{flightScheduleID}\" ]"
                + RIM_LINE_SEP
                + "\t}"));
    assertTrue(
        rimDSL.contains(
            "GET *-> flightschedule_departureAirport {"
                + RIM_LINE_SEP
                + "\t\tparameters [ id=\"{flightScheduleID}\" ]"
                + RIM_LINE_SEP
                + "\t}"));
    assertTrue(rimDSL.contains("resource flightschedule_departureAirport"));
    assertTrue(rimDSL.contains("path: \"/FlightSchedules({id})/departureAirport\""));
    assertTrue(rimDSL.contains("GET -> Passengers"));
    assertTrue(rimDSL.contains("resource Passengers"));
  }
 @Test(expected = AssertionError.class)
 public void testGenJPAEntityNull() {
   JPAResponderGen rg = new JPAResponderGen();
   assertNull(rg.generateJPAEntityClass(null));
 }