View Javadoc
1   package net.sourceforge.jenesis4java.impl;
2   
3   /*
4    * #%L
5    * Jenesis 4 Java Code Generator
6    * %%
7    * Copyright (C) 2000 - 2016 jenesis4java
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  import net.sourceforge.jenesis4java.*;
26  import net.sourceforge.jenesis4java.util.InstanceReplacingVisitor;
27  import net.sourceforge.jenesis4java.util.StatementTestBase;
28  import net.sourceforge.jenesis4java.util.WithVmAndMethod;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.junit.experimental.runners.Enclosed;
32  import org.junit.runner.RunWith;
33  
34  import static net.sourceforge.jenesis4java.util.Shortcuts.dummyClass;
35  import static net.sourceforge.jenesis4java.util.TestHelper.LS;
36  import static net.sourceforge.jenesis4java.util.TestHelper.vm;
37  import static org.junit.Assert.assertEquals;
38  import static org.junit.Assert.assertSame;
39  
40  @RunWith(Enclosed.class)
41  public class SynchronizedTest {
42  
43      public static class Syntax extends WithVmAndMethod {
44  
45          @Test
46          public void synchronizedBlock() {
47              Synchronized synchronizedBlock = method.newSynchronized(vm().newFree("mutex"));
48              assertEquals("synchronized (mutex) {" + LS + "}", synchronizedBlock.toString());
49          }
50  
51      }
52  
53      public static class SettersAndGetters {
54  
55          private Synchronized synchronizedStmt;
56  
57          @Before
58          public void setUp() {
59              ClassMethod method = dummyClass().newMethod("method");
60              synchronizedStmt = method.newSynchronized(vm().newFree("mutex"));
61          }
62  
63          @Test
64          public void setIsChainable() {
65              Expression replacementMutex = vm().newFree("mutex");
66              Synchronized aSynchronized = synchronizedStmt.setMutex(replacementMutex);
67              assertSame(aSynchronized, synchronizedStmt);
68          }
69  
70          @Test
71          public void setAndGetMutex() {
72              Expression replacementMutex = vm().newFree("mutex");
73              synchronizedStmt.setMutex(replacementMutex);
74              assertEquals(replacementMutex, synchronizedStmt.getMutex());
75          }
76  
77      }
78  
79      public static class Visitors {
80  
81          @Test
82          public void replaceMutex() {
83              ClassMethod method = dummyClass().newMethod("method");
84              Expression original = vm().newFree("mutex");
85              Synchronized aSynchronized = method.newSynchronized(original);
86              Expression replacement = vm().newFree("mutex");
87              aSynchronized.visit(new InstanceReplacingVisitor(original, replacement));
88              assertEquals(replacement, aSynchronized.getMutex());
89          }
90      }
91  
92      public static class InheritedFunctionality extends StatementTestBase {
93  
94          @Override
95          public Statement getInstance() {
96              return dummyClass().newMethod("method").newSynchronized(vm().newFree("xx"));
97          }
98      }
99  }