Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

spock example created

  • Loading branch information...
commit 96599429f7a3d9ae2db3cd723868b2d61569e243 1 parent 561e030
@sody authored
View
99 testing/testing-spock/pom.xml
@@ -1,21 +1,88 @@
<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/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.example</groupId>
- <artifactId>testing-example</artifactId>
- <version>1.0-SNAPSHOT</version>
- </parent>
+ <parent>
+ <groupId>com.example</groupId>
+ <artifactId>testing-example</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
- <groupId>com.example</groupId>
- <artifactId>testing-spock</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>jar</packaging>
+ <groupId>com.example</groupId>
+ <artifactId>testing-spock</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>jar</packaging>
- <name>Testing Spock Framework Example</name>
- <description>
- This is an example application that demonstrates Spock Framework usage.
- </description>
+ <name>Testing Spock Framework Example</name>
+ <description>
+ This is an example application that demonstrates Spock Framework usage.
+ </description>
+
+ <properties>
+ <groovy-version>1.7.10</groovy-version>
+ <gmaven-version>1.3</gmaven-version>
+ <gmaven-provider>1.7</gmaven-provider>
+ <spock.version>0.5-groovy-1.7</spock.version>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.codehaus.groovy</groupId>
+ <artifactId>groovy-all</artifactId>
+ <version>${groovy-version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.spockframework</groupId>
+ <artifactId>spock-core</artifactId>
+ <version>${spock.version}</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.codehaus.gmaven</groupId>
+ <artifactId>gmaven-plugin</artifactId>
+ <version>${gmaven-version}</version>
+ <configuration>
+ <providerSelection>${gmaven-provider}</providerSelection>
+ </configuration>
+ <executions>
+ <execution>
+ <goals>
+ <goal>testCompile</goal>
+ </goals>
+ </execution>
+ </executions>
+ <dependencies>
+ <dependency>
+ <groupId>org.codehaus.gmaven.runtime</groupId>
+ <artifactId>gmaven-runtime-1.7</artifactId>
+ <version>${gmaven-version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.codehaus.groovy</groupId>
+ <artifactId>groovy-all</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ </dependencies>
+ </plugin>
+ <plugin>
+ <groupId>org.spockframework</groupId>
+ <artifactId>spock-maven</artifactId>
+ <version>${spock.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>find-specs</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
</project>
View
87 testing/testing-spock/src/main/java/com/example/LocaleUtils.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2008-2011 Ivan Khalopik.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.StringTokenizer;
+
+/**
+ * This class represents utility methods for working with localization and internationalization.
+ *
+ * @author Ivan Khalopik
+ * @since 1.0
+ */
+public abstract class LocaleUtils {
+
+ /**
+ * Root locale fix for java 1.5
+ */
+ public static final Locale ROOT_LOCALE = new Locale("");
+
+ private static final String LOCALE_SEPARATOR = "_";
+
+ /**
+ * Gets locale for specified string value.
+ *
+ * @param value string value to convert
+ * @return converted from string locale or null if can not be converted
+ */
+ public static Locale parseLocale(final String value) {
+ if (value != null) {
+ final StringTokenizer tokens = new StringTokenizer(value, LOCALE_SEPARATOR);
+ final String language = tokens.hasMoreTokens() ? tokens.nextToken() : "";
+ final String country = tokens.hasMoreTokens() ? tokens.nextToken() : "";
+ String variant = "";
+ String sep = "";
+ while (tokens.hasMoreTokens()) {
+ variant += sep + tokens.nextToken();
+ sep = LOCALE_SEPARATOR;
+ }
+ return new Locale(language, country, variant);
+ }
+ return null;
+ }
+
+ /**
+ * Gets locale candidates for specified locale.
+ *
+ * @param locale locale
+ * @return locale candidates for specified locale
+ */
+ public static List<Locale> getCandidateLocales(final Locale locale) {
+ final List<Locale> locales = new ArrayList<Locale>();
+ if (locale != null) {
+ final String language = locale.getLanguage();
+ final String country = locale.getCountry();
+ final String variant = locale.getVariant();
+
+ if (variant.length() > 0) {
+ locales.add(locale);
+ }
+ if (country.length() > 0) {
+ locales.add((locales.size() == 0) ? locale : new Locale(language, country));
+ }
+ if (language.length() > 0) {
+ locales.add((locales.size() == 0) ? locale : new Locale(language));
+ }
+ }
+ locales.add(ROOT_LOCALE);
+ return locales;
+ }
+}
View
14 testing/testing-spock/src/test/groovy/com/example/LocaleUtilsSpec.groovy
@@ -0,0 +1,14 @@
+package com.example
+
+import spock.lang.Specification
+
+/**
+ * @author Ivan Khalopik
+ */
+class LocaleUtilsSpec extends Specification {
+
+ def "Locale parser should ignore null values"() {
+ expect:
+ LocaleUtils.parseLocale(null) != null
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.