39
CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~ mak

CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Embed Size (px)

Citation preview

Page 1: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

CS 160: Software EngineeringNovember 10 Class Meeting

Department of Computer ScienceSan Jose State University

Fall 2014Instructor: Ron Mak

www.cs.sjsu.edu/~mak

Page 2: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

2

One-to-One Association

Recall:

Id Last First Contact_id

7003 Rogers Tom 207

7008 Thompson Art 190

7012 Lane John 458

7051 Flynn Mabel 856

Id Email_address

458 [email protected]

856 [email protected]

207 [email protected]

190 [email protected]

Teacher Contact_Info

Student Contact_Info

Also:

Page 3: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

3

ContactInfo.java@Entity@Table(name="Contact_Info")public class ContactInfo { private long id; private String emailAddress; public ContactInfo() {} public ContactInfo(String address) { this.emailAddress = address; } @Id @GeneratedValue @Column(name="id") public long getId() { return id; } public void setId(long id) { this.id = id; }

@Column(name="email_address") public String getEmailAddress() { return emailAddress; } public void setEmailAddress(String address) { this.emailAddress = address; }}

Page 4: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

4

Student.java (One-to-One)

@Entitypublic class Student { ...

private ContactInfo contactInfo; @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY) @JoinColumn(name="contact_id") public ContactInfo getContactInfo() { return contactInfo; } public void setContactInfo(ContactInfo info) { this.contactInfo = info; }

...}

Page 5: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

5

Student.java (One-to-One)

@Entitypublic class Student { ...

public static void load() { Session session = HibernateContext.getSession(); // Load the Student table in a transaction. Transaction tx = session.beginTransaction(); { session.save(new Student("Mary", "Jane", new ContactInfo("[email protected]"))); session.save(new Student("Kim", "Smith", new ContactInfo("[email protected]"))); session.save(new Student("John", "Doe", new ContactInfo("[email protected]"))); session.save(new Student("Tim", "Novak", new ContactInfo("[email protected]"))); session.save(new Student("Leslie", "Klein", new ContactInfo("[email protected]"))); } tx.commit(); session.close();

System.out.println("Student table loaded."); }

...}

Page 6: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

6

Student.java (One-to-One)@Entitypublic class Student { ...

private ContactInfo contactInfo; ...

public void print() { System.out.printf("%d: %s %s (%s)\n", id, firstName, lastName, contactInfo.getEmailAddress()); }

...} SchoolDemo3

Page 7: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

7

Lazy Fetching

Each Student object has a one-to-one association with a ContactInfo object via its contactInfo field.

Whenever our Java program fetches a record from the Student table, Hibernate does not fetch the associated record from the Contact_Info table until our program references any field of the ContactInfo object. This is a Hibernate performance optimization.

private ContactInfo contactInfo;...@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)@JoinColumn(name="contact_id")public ContactInfo getContactInfo() { return contactInfo; }public void setContactInfo(ContactInfo info) { this.contactInfo = info; }

Page 8: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

8

Lazy Initialization Exception Hibernate throws the dreaded

LazyInitializationException whenever your program attempts to access the field of an object whose corresponding table record has not yet been fetched, often due to lazy fetching.

In SchoolDemo3, we attempted to print the value of field emailAddress of a ContactInfo object.

The corresponding record had not yet been fetched from the Contact_Info table.

Hibernate can fetch table records only within the context of a session.

Page 9: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

9

Lazy Initialization Exception Recall that after we fetched a matching record

from the Student table, we closed the session.

Therefore, Hibernate couldn’t fetch the associated Contact_Info record, and we got the exception when we attempted to print the emailAddress field of the ContactInfo object.

public static Student find(long id){ Session session = HibernateContext.getSession(); Query query = session.createQuery("from Student where id = :idvar"); query.setLong("idvar", id); Student student = (Student) query.uniqueResult(); session.close(); return student;}

Page 10: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

10

Lazy Initialization Exception

One solution: Use eager fetching. Whenever Hibernate fetches a table record, it

immediately fetches records from associated tables.

private ContactInfo contactInfo;...@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)@JoinColumn(name="contact_id")public ContactInfo getContactInfo() { return contactInfo; }public void setContactInfo(ContactInfo info) { this.contactInfo = info; }

Page 11: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

11

Lazy Initialization Exception However, eager fetching is not always a good idea.

Don’t defeat Hibernate’s performance optimization, especially if there are many associations and you’re fetching many records!

Better: Keep lazy fetching. Attach the Student object to another Hibernate session.

Inside of this session, Hibernate can fetch the corresponding Contact_Info record to allow you to print ContactInfo fields.

public void printInSession(){ Session session = HibernateContext.getSession(); session.update(this); print(); session.close();}

Student.java

Page 12: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

12

Two Common Errors

1. Attempting to access a Java object whose corresponding database record has not yet been fetched, likely due to lazy fetching.

Attach the object to a new session to force Hibernate to fetch the record.

2. Taking a Java object already attached to a session and attempting to attach it to another session.

Did you forget to close the first session?

Page 13: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

13

One-to-Many / Many-to-One Associations

The Teacher table has a one-to-many association with the Class table.

The Class table has a many-to-one association with the Teacher table._

Id Last First

7003 Rogers Tom

7008 Thompson Art

7012 Lane John

7051 Flynn Mabel

Code Teacher_id Subject

908 7008 Data structures

926 7003 Java programming

931 7051 Compilers

951 7012 Software engineering

974 7012 Operating systems

Teacher Class

Page 14: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

14

Teacher.java

@Entitypublic class Teacher { ... private List<Klass> klasses; ... @OneToMany(mappedBy="teacher", targetEntity=Klass.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER) public List<Klass> getKlasses() { return klasses; } public void setKlasses(List<Klass> klasses) { this.klasses = klasses; } ...}

Page 15: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

15

Klass.java

@Entity@Table(name="Class")public class Klass{ ... private Teacher teacher; ... @ManyToOne @JoinColumn(name="teacher_id") public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } ...}

Page 16: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

16

Klass.javapublic static void load(){ Session session = HibernateContext.getSession(); Teacher rogers = Teacher.find("Rogers"); Teacher thompson = Teacher.find("Thompson"); Teacher lane = Teacher.find("Lane"); Teacher flynn = Teacher.find("Flynn"); Klass java = new Klass("Java programming"); java.setTeacher(rogers);

Klass ds = new Klass("Data structures"); ds.setTeacher(thompson);

Klass se = new Klass("Software engineering"); se.setTeacher(lane); Klass os = new Klass("Operating systems"); os.setTeacher(lane);

Klass compilers = new Klass("Compilers"); compilers.setTeacher(flynn); ...}

Find each teacher.

Create classes andassign teachers to them.

Page 17: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

17

Klass.java

public static void load() { ... Transaction tx = session.beginTransaction(); { session.save(java); session.save(ds); session.save(se); session.save(os); session.save(compilers); } tx.commit(); session.close(); System.out.println("Class table loaded."); }

Persist the objects.

SchoolDemo4

Page 18: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

18

Many-to-Many Association

Id Last First

1001 Doe John

1005 Novak Tim

1009 Klein Leslie

1014 Jane Mary

1021 Smith Kim

Code Teacher_id Subject

908 7008 Data structures

926 7003 Java programming

931 7051 Compilers

951 7012 Software engineering

974 7012 Operating systems

Student_id Class_code

1001 926

1001 951

1001 908

1005 974

1005 908

1014 931

1021 926

1021 974

1021 931

Student Class

Student_Class

Class

Student

Student-Class

Page 19: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

19

Student.java

@Entitypublic class Student { ... private List<Klass> klasses = new ArrayList<Klass>(); ... @ManyToMany @JoinTable(name="Student_Class", joinColumns={@JoinColumn(name="student_id")}, inverseJoinColumns={@JoinColumn(name="class_code")}) public List<Klass> getKlasses() { return klasses; } public void setKlasses(List<Klass> klasses) { this.klasses = klasses; } ...}

Hibernate will automatically create andpopulate the Student_Class join table.

Student_id Class_code

1001 926

1001 951

... ...

Student_Class

Page 20: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

20

Klass.java@Entity@Table(name="Class")public class Klass{ ... private List<Student> students = new ArrayList<Student>(); ... @ManyToMany @JoinTable(name="Student_Class", joinColumns={@JoinColumn(name="class_code")}, inverseJoinColumns={@JoinColumn(name="student_id")}) public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } ...}

Student_id Class_code

1001 926

1001 951

... ...

Student_Class

Page 21: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

21

Klass.java

public static void load(){ ... Student doe = Student.find("Doe"); Student jane = Student.find("Jane"); Student novak = Student.find("Novak"); Student smith = Student.find("Smith"); java.getStudents().add(smith); java.getStudents().add(doe); ds.getStudents().add(doe); ds.getStudents().add(novak); se.getStudents().add(doe); os.getStudents().add(novak); os.getStudents().add(smith); compilers.getStudents().add(smith); compilers.getStudents().add(jane); ...}

SchoolDemo5

Find each student.

Assign studentsto classes.

Page 22: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

22

Software Validation and Verification (V&V)

Validation Work with the client to make sure that the product

requirements are complete and correct. Make sure that the proposed product meets all the

requirements. “Are we building the right product?”

Verification Work with the developers to make sure that the

product is being developed with high quality standards.

“Are we building the product right?”

Page 23: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

23

Software Reliability

Reliable software has a low probability of failure while operating for a specified period of time under specified operating conditions.

The specified time period and the specified operating conditions are part of the nonfunctional requirements.

For some applications, reliability may be more important than the other requirements. mission-critical applications medical applications

Page 24: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

24

Software Reliability, cont’d

Reliable software is the result of good software quality assurance (SQA) throughout an application’s life cycle.

Design Good requirements elicitation Good object-oriented design and analysis Good architecture

Page 25: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

25

Software Reliability, cont’d

Development Good management

(e.g., source control, reasonable schedules) Good coding practices (e.g., design patterns) Good testing practices

Deployment Preventive maintenance (e.g., training) Performance monitoring Failure analysis (when failures do occur)

Page 26: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

26

Software Testing

What is testing? What is a successful test? Who does testing? When does testing occur? What are the different types of testing? What testing tools are available? How do you know your tests

covered everything? When can you stop testing?

Page 27: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

27

What is testing?

Testing is a systematic procedure to discover faults in software in order to prevent failure.

Failure: A deviation of the software’s behavior from its specified behavior, according to its requirements. Can be minor to major (such as a crash).

Page 28: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

28

What is testing?

Erroneous state: A state that the operating software is in that will lead to a failure. Example: low on memory

Fault: What caused the software to enter an erroneous state. AKA: defect, bug Example: a memory leak

fault erroneous state failure

Page 29: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

29

What is a successful test?

A successful test is one that finds bugs.

Testing is the opposite of coding.

Coding: Create software and try to get it to work. Testing: Break the software and demonstrate that

it doesn’t work.

Page 30: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

30

What is a successful test?

Testing and coding require different mind sets.

It can be very difficult for developers to test their own code.

If you wrote the code, you psychologically want it to work and not see it break.

Since you know how the code should be used, you may not think to try using it in ways other than as you intended.

Page 31: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

31

Who does testing?

Developers As difficult as it may be, you must test your own code.

unit testing Test each other’s code

peer testing

Testers Members of the quality assurance (QA) department. Software engineers who did not write the code. Manual writers and trainers who

create examples and demos.

Page 32: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

32

Who does testing? cont’d

Users As beta testers As customers

Page 33: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

33

When does testing occur?

Recall the Old Waterfall Model:

Requirements

Design

Implementation

Testing

In the new Agile Methodology, testing is part of each and every iteration. Testing occurs throughout development,

not just at the end.

XXX

Page 34: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

34

What are the different types of testing?

Functional testing Does the code do what it’s supposed to do? Tests derived from use cases.

Unit testing Developers test an individual “unit”. Unit: A small set of related components.

Regression testing Rerun previous tests to ensure that the latest code

changes didn’t break something. Often run automatically from scripts.

Page 35: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

35

Different types of testing, cont’d

Integration testing Developers test how well their units

work together with other units.

Usability testing Is the user interface easy to use?

Page 36: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

36

Different types of testing, cont’d

System testing Test how an entire system works.

Performance testing How quickly does the system respond?

Stress testing How much can the system tolerate before breaking?

Page 37: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

37

Alpha Testing vs. Beta Testing

Alpha testing

Usability and system testing of a nearly complete application in the development environment.

Beta testing

Usability and system testing of a complete or nearly complete application in the user’s environment.

It is not uncommon for software companies to release an application to the public for beta testing.

New releases of web-based applications are put “into beta” by software companies.

Page 38: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

38

Black Box Testing vs. White Box Testing

Black box testing Deals only with the input/output behavior of the unit.

Calling parameters Return values

The internals of the unit are not considered. Commonly known as functional testing.

White box testing Tests the internal behavior of the unit.

execution paths state transitions

Part of each developer’s unit testing.

Page 39: CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak mak

Computer Science Dept.Fall 2014: November 10

CS 160: Software Engineering© R. Mak

39

Unit Testing

Each unit test focuses on components created by a developer.

Done by the developer before committing the code to the source repository.

Easier to find and fix bugs when there are fewer components.

Bottom-up testing.

Developers create test cases to do unit tests.