Contract c = new Contract("gavin", "phone"); ContractVariation cv1 = new ContractVariation(1, c); cv1.setText("expensive"); ContractVariation cv2 = new ContractVariation(2, c); cv2.setText("more expensive"); Session s = openSession(); Transaction t = s.beginTransaction(); s.persist(c); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); cv1 = (ContractVariation) c.getVariations().iterator().next(); cv1.setText("blah blah"); s.update( c ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); c = (Contract) s.createCriteria(Contract.class).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); Iterator it = c.getVariations().iterator(); cv1 = (ContractVariation) it.next(); assertEquals( cv1.getText(), "expensive" ); cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); s.delete(c); assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) ); assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) ); t.commit(); s.close();
Contract c = new Contract("gavin", "phone"); ContractVariation cv1 = new ContractVariation(1, c); cv1.setText("expensive"); ContractVariation cv2 = new ContractVariation(2, c); cv2.setText("more expensive"); Session s = openSession(); Transaction t = s.beginTransaction(); s.persist(c); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); c.getVariations().add( new ContractVariation(3, c) ); s.merge( c ); try { t.commit(); fail( "should have failed because an immutable collection was changed"); } catch ( HibernateException ex ) { // expected t.rollback(); } finally { s.close(); } s = openSession(); t = s.beginTransaction(); c = (Contract) s.createCriteria(Contract.class).uniqueResult(); assertEquals( c.getCustomerName(), "gavin" ); assertEquals( c.getVariations().size(), 2 ); Iterator it = c.getVariations().iterator(); cv1 = (ContractVariation) it.next(); assertEquals( cv1.getText(), "expensive" ); cv2 = (ContractVariation) it.next(); assertEquals( cv2.getText(), "more expensive" ); s.delete(c); assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) ); assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) ); t.commit(); s.close();
Clone fragments detected by clone detection tool
File path: /hibernate-distribution-3.3.2.GA/project/testsuite/src/test/java/org/hibernate/test/immutable/ImmutableTest.java File path: /hibernate-distribution-3.3.2.GA/project/testsuite/src/test/java/org/hibernate/test/immutable/ImmutableTest.java
Method name: void testImmutableChildEntityWithUpdate() Method name: void testImmutableCollectionWithMerge()
Number of AST nodes: 32 Number of AST nodes: 32
1
Contract c = new Contract("gavin", "phone");
1
Contract c = new Contract("gavin", "phone");
2
		ContractVariation cv1 = new ContractVariation(1, c);
2
		ContractVariation cv1 = new ContractVariation(1, c);
3
		cv1.setText("expensive");
3
		cv1.setText("expensive");
4
		ContractVariation cv2 = new ContractVariation(2, c);
4
		ContractVariation cv2 = new ContractVariation(2, c);
5
		cv2.setText("more expensive");
5
		cv2.setText("more expensive");
6
		Session s = openSession();
6
		Session s = openSession();
7
		Transaction t = s.beginTransaction();
7
		Transaction t = s.beginTransaction();
8
		s.persist(c);
8
		s.persist(c);
9
		t.commit();
9
		t.commit();
10
		s.close();
10
		s.close();
11
		s = openSession();
11
		s = openSession();
12
		t = s.beginTransaction();
12
		t = s.beginTransaction();
13
		cv1 = (ContractVariation) c.getVariations().iterator().nex
13
		c.getVariations().add( new ContractVariation(3, c) );
14
		s.merge( c );
15
		try {
14
t();
16
			t.commit();
15
		cv1.setText("blah blah");
17
			
16
		s.update( c );
17
		t.commit();
18
fail( "should have failed because an immutable collection was changed");
19
		}
20
		catch ( HibernateException ex ) {
21
			// expected
22
			t.rollback();
23
		}
24
		finally {
18
		s.close();
25
			s.close();
26
		}
19
		s = openSession();
27
		s = openSession();
20
		t = s.beginTransaction();
28
		t = s.beginTransaction();
21
		c = (Contract) s.createCriteria(Contract.class).uniqueResult();
29
		c = (Contract) s.createCriteria(Contract.class).uniqueResult();
22
		assertEquals( c.getCustomerName(), "gavin" );
30
		assertEquals( c.getCustomerName(), "gavin" );
23
		assertEquals( c.getVariations().size(), 2 );
31
		assertEquals( c.getVariations().size(), 2 );
24
		Iterator it = c.getVariations().iterator();
32
		Iterator it = c.getVariations().iterator();
25
		cv1 = (ContractVariation) it.next();
33
		cv1 = (ContractVariation) it.next();
26
		assertEquals( cv1.getText(), "expensive" );
34
		assertEquals( cv1.getText(), "expensive" );
27
		cv2 = (ContractVariation) it.next();
35
		cv2 = (ContractVariation) it.next();
28
		assertEquals( cv2.getText(), "more expensive" );
36
		assertEquals( cv2.getText(), "more expensive" );
29
		s.delete(c);
37
		s.delete(c);
30
		assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) );
38
		assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) );
31
		assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) );
39
		assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) );
32
		t.commit();
40
		t.commit();
33
		s.close();
41
		s.close();
Summary
Number of common nesting structure subtrees1
Number of refactorable cases0
Number of non-refactorable cases1
Time elapsed for finding largest common nesting structure subtrees (ms)0.0
Clones locationClones are declared in the same class
Number of node comparisons331
  1. {Non-refactorable}
    Mapping Summary
    Number of mapped statements27
    Number of unmapped statements in the first code fragment5
    Number of unmapped statements in the second code fragment2
    Time elapsed for statement mapping (ms)0.0
    Clone typeType 3
    Mapped Statements
    ID Statement ID Statement
    1
    Contract c = new Contract("gavin", "phone");
    1
    Contract c = new Contract("gavin", "phone");
    2
    ContractVariation cv1 = new ContractVariation(1, c);
    2
    ContractVariation cv1 = new ContractVariation(1, c);
    3
    cv1.setText("expensive");
    3
    cv1.setText("expensive");
    4
    ContractVariation cv2 = new ContractVariation(2, c);
    4
    ContractVariation cv2 = new ContractVariation(2, c);
    5
    cv2.setText("more expensive");
    5
    cv2.setText("more expensive");
    6
    Session s = openSession();
    6
    Session s = openSession();
    7
    Transaction t = s.beginTransaction();
    7
    Transaction t = s.beginTransaction();
    8
    s.persist(c);
    8
    s.persist(c);
    9
    t.commit();
    9
    t.commit();
    10
    s.close();
    10
    s.close();
    11
    s = openSession();
    11
    s = openSession();
    12
    t = s.beginTransaction();
    12
    t = s.beginTransaction();
                                                                                                                
    13
    c.getVariations().add(new ContractVariation(3, c));
    Preondition Violations
    Unmatched statement c.getVariations().add(new ContractVariation(3,c)); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    13
    c.getVariations().add(new ContractVariation(3, c));
    13
    cv1 = (ContractVariation)c.getVariations().iterator().next();
    13
    cv1 = (ContractVariation)c.getVariations().iterator().next();
    Preondition Violations
    Unmatched statement cv1=(ContractVariation)c.getVariations().iterator().next(); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                                                                                                                                  
                                  
    14
    s.merge(c);
    Preondition Violations
    Unmatched statement s.merge(c); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    14
    s.merge(c);
    14
    cv1.setText("blah blah");
    14
    cv1.setText("blah blah");
    Preondition Violations
    Unmatched statement cv1.setText("blah blah"); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                                                              
    15
    s.update(c);
    15
    s.update(c);
    Preondition Violations
    Unmatched statement s.update(c); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                                    
    16
    t.commit();
    16
    t.commit();
    Preondition Violations
    Unmatched statement t.commit(); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                                  
    17
    s.close();
    17
    s.close();
    Preondition Violations
    Unmatched statement s.close(); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
                                
    18
    s = openSession();
    18
    s = openSession();
    19
    t = s.beginTransaction();
    19
    t = s.beginTransaction();
    20
    c = (Contract)s.createCriteria(Contract.class).uniqueResult();
    20
    c = (Contract)s.createCriteria(Contract.class).uniqueResult();
    21
    assertEquals(c.getCustomerName(), "gavin");
    21
    assertEquals(c.getCustomerName(), "gavin");
    22
    assertEquals(c.getVariations().size(), 2);
    22
    assertEquals(c.getVariations().size(), 2);
    23
    Iterator it = c.getVariations().iterator();
    23
    Iterator it = c.getVariations().iterator();
    24
    cv1 = (ContractVariation)it.next();
    24
    cv1 = (ContractVariation)it.next();
    25
    assertEquals(cv1.getText(), "expensive");
    25
    assertEquals(cv1.getText(), "expensive");
    26
    cv2 = (ContractVariation)it.next();
    26
    cv2 = (ContractVariation)it.next();
    27
    assertEquals(cv2.getText(), "more expensive");
    27
    assertEquals(cv2.getText(), "more expensive");
    28
    s.delete(c);
    28
    s.delete(c);
    29
    assertEquals(s.createCriteria(Contract.class).setProjection(Projections.rowCount()).uniqueResult(), new Integer(0));
    29
    assertEquals(s.createCriteria(Contract.class).setProjection(Projections.rowCount()).uniqueResult(), new Integer(0));
    30
    assertEquals(s.createCriteria(ContractVariation.class).setProjection(Projections.rowCount()).uniqueResult(), new Integer(0));
    30
    assertEquals(s.createCriteria(ContractVariation.class).setProjection(Projections.rowCount()).uniqueResult(), new Integer(0));
    31
    t.commit();
    31
    t.commit();
    32
    s.close();
    32
    s.close();
    Precondition Violations (8)
    Row Violation
    1Unmatched statement c.getVariations().add(new ContractVariation(3,c)); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    2Unmatched statement cv1=(ContractVariation)c.getVariations().iterator().next(); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    3Unmatched statement s.merge(c); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    4Unmatched statement cv1.setText("blah blah"); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    5Unmatched statement s.update(c); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    6Unmatched statement t.commit(); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    7Unmatched statement s.close(); cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    8Clone fragment #1 returns variables , while Clone fragment #2 returns variables s, t