Created
November 18, 2012 02:08
-
-
Save msakamoto-sf/4102858 to your computer and use it in GitHub Desktop.
Java + Groovy + TestNG + Maven combination example3 (replace singleton field)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>gjt3</groupId> | |
<artifactId>gjt3</artifactId> | |
<packaging>jar</packaging> | |
<version>1.0-SNAPSHOT</version> | |
<name>gjt3</name> | |
<url>http://maven.apache.org</url> | |
<dependencies> | |
<dependency> | |
<groupId>org.codehaus.groovy</groupId> | |
<artifactId>groovy-all</artifactId> | |
<version>1.8.8</version> | |
</dependency> | |
<dependency> | |
<groupId>org.testng</groupId> | |
<artifactId>testng</artifactId> | |
<version>[6.8,)</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.codehaus.gmaven</groupId> | |
<artifactId>gmaven-plugin</artifactId> | |
<version>1.4</version> | |
<configuration> | |
<providerSelection>1.8</providerSelection> | |
</configuration> | |
<executions> | |
<execution> | |
<goals> | |
<goal>generateStubs</goal> | |
<goal>compile</goal> | |
<goal>generateTestStubs</goal> | |
<goal>testCompile</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gjt3; | |
public class Singleton { | |
private static Singleton instance = null; | |
private String myarg; | |
private Singleton(String arg) { | |
myarg = arg; | |
} | |
public synchronized static Singleton getInstance(String arg) { | |
if (Singleton.instance == null) { | |
Singleton.instance = new Singleton(arg); | |
} | |
return Singleton.instance; | |
} | |
public String getArg() { | |
return myarg; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gjt3; | |
import org.testng.Assert; | |
import org.testng.annotations.Test; | |
public class SingletonTest { | |
@Test | |
public void ignoreSingleton() { | |
Singleton s1 = Singleton.getInstance("s1"); | |
Assert.assertEquals(s1.getArg(), "s1"); | |
Singleton s2 = Singleton.getInstance("s2"); | |
Assert.assertEquals(s2.getArg(), "s1"); | |
Singleton s3 = new Singleton("s3"); | |
Assert.assertEquals(s3.getArg(), "s3"); | |
Singleton.instance = s3; | |
Singleton s4 = Singleton.getInstance("s4"); | |
Assert.assertEquals(s4.getArg(), "s3"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment