Support for properties associated with every user
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 07 Nov 2009 15:23:14 +0100
changeset 1434eb88f05c207
parent 142 62e85a5ecffa
child 144 cc04ede4cb5e
Support for properties associated with every user
webidor/pom.xml
webidor/src/main/java/cz/xelfi/quoridor/webidor/User.java
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java
webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/UsersTest.java
     1.1 --- a/webidor/pom.xml	Thu Nov 05 13:34:36 2009 +0100
     1.2 +++ b/webidor/pom.xml	Sat Nov 07 15:23:14 2009 +0100
     1.3 @@ -1,130 +1,130 @@
     1.4 -<?xml version="1.0" encoding="UTF-8"?>
     1.5 -<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">
     1.6 -  <modelVersion>4.0.0</modelVersion>
     1.7 -  <parent>
     1.8 -        <artifactId>all-quoridor</artifactId>
     1.9 -        <groupId>org.apidesign</groupId>
    1.10 -        <version>1.0</version>
    1.11 -  </parent>
    1.12 -  <groupId>org.apidesign</groupId>
    1.13 -  <artifactId>webidor</artifactId>
    1.14 -  <packaging>jar</packaging>
    1.15 -  <version>1.7</version>
    1.16 -  <name>webidor server</name>
    1.17 -  <url>http://maven.apache.org</url>
    1.18 -  <repositories>
    1.19 -    <repository>
    1.20 -        <id>maven2-repository.dev.java.net</id>
    1.21 -        <name>Java.net Repository for Maven</name>
    1.22 -        <url>http://download.java.net/maven/2/</url>
    1.23 -        <layout>default</layout>
    1.24 -    </repository>
    1.25 -    <repository>
    1.26 -        <id>maven-repository.dev.java.net</id>
    1.27 -        <name>Java.net Maven 1 Repository (legacy)</name>
    1.28 -        <url>http://download.java.net/maven/1</url>
    1.29 -        <layout>legacy</layout>
    1.30 -    </repository>
    1.31 -  </repositories>
    1.32 -  <dependencies>
    1.33 -
    1.34 -    <dependency>
    1.35 -      <groupId>junit</groupId>
    1.36 -      <artifactId>junit</artifactId>
    1.37 -      <version>4.5</version>
    1.38 -      <scope>test</scope>
    1.39 -    </dependency>
    1.40 -    <dependency>
    1.41 -      <groupId>com.sun.jersey</groupId>
    1.42 -      <artifactId>jersey-core</artifactId>
    1.43 -      <version>1.1.0-ea</version>
    1.44 -    </dependency>
    1.45 -    <dependency>
    1.46 -      <groupId>com.sun.jersey</groupId>
    1.47 -      <artifactId>jersey-server</artifactId>
    1.48 -      <version>1.1.0-ea</version>
    1.49 -    </dependency>
    1.50 -    <dependency>
    1.51 -      <groupId>com.sun.jersey</groupId>
    1.52 -      <artifactId>jersey-json</artifactId>
    1.53 -      <version>1.1.0-ea</version>
    1.54 -      <type>jar</type>
    1.55 -      <exclusions>
    1.56 -        <exclusion>
    1.57 -          <artifactId>jaxb-api</artifactId>
    1.58 -          <groupId>javax.xml.bind</groupId>
    1.59 -        </exclusion>
    1.60 -        <exclusion>
    1.61 -          <artifactId>stax-api</artifactId>
    1.62 -          <groupId>stax</groupId>
    1.63 -        </exclusion>
    1.64 -      </exclusions>
    1.65 -    </dependency>
    1.66 -    <dependency>
    1.67 -      <groupId>com.sun.jersey.test.framework</groupId>
    1.68 -      <artifactId>jersey-test-framework</artifactId>
    1.69 -      <version>1.1.0-ea</version>
    1.70 -      <scope>test</scope>
    1.71 -    </dependency>
    1.72 -    <dependency>
    1.73 -      <groupId>org.apidesign</groupId>
    1.74 -      <artifactId>quoridor</artifactId>
    1.75 -      <version>1.0</version>
    1.76 -      <type>jar</type>
    1.77 -    </dependency>
    1.78 -  </dependencies>
    1.79 -  <build>
    1.80 -    <plugins>
    1.81 -      <plugin>
    1.82 -        <groupId>org.apache.maven.plugins</groupId>
    1.83 -        <artifactId>maven-compiler-plugin</artifactId>
    1.84 -        <version>2.0.2</version>
    1.85 -        <configuration>
    1.86 -          <source>1.5</source>
    1.87 -          <target>1.5</target>
    1.88 -        </configuration>
    1.89 -      </plugin>
    1.90 -      <plugin>
    1.91 -        <groupId>org.codehaus.mojo</groupId>
    1.92 -        <artifactId>exec-maven-plugin</artifactId>
    1.93 -        <configuration>
    1.94 -            <mainClass>cz.xelfi.quoridor.webidor.resources.Quoridor</mainClass>
    1.95 -        </configuration>
    1.96 -      </plugin>
    1.97 -      <plugin>
    1.98 -        <artifactId>maven-assembly-plugin</artifactId>
    1.99 -        <version>2.2-beta-2</version>
   1.100 -        <executions>
   1.101 -          <execution>
   1.102 -            <id>create-executable-jar</id>
   1.103 -            <phase>package</phase>
   1.104 -            <goals>
   1.105 -              <goal>single</goal>
   1.106 -            </goals>
   1.107 -            <configuration>
   1.108 -              <descriptors>
   1.109 -                <descriptor>all-zip.xml</descriptor>
   1.110 -              </descriptors>
   1.111 -              <finalName>webidor-${version}</finalName>
   1.112 -            </configuration>
   1.113 -          </execution>
   1.114 -        </executions>
   1.115 -      </plugin>
   1.116 -      <plugin>
   1.117 -        <groupId>org.apache.maven.plugins</groupId>
   1.118 -        <artifactId>maven-jar-plugin</artifactId>
   1.119 -        <configuration>
   1.120 -            <archive>
   1.121 -                <manifest>
   1.122 -                    <addClasspath>true</addClasspath>
   1.123 -                    <classpathPrefix>lib/</classpathPrefix>
   1.124 -                    <mainClass>cz.xelfi.quoridor.webidor.resources.Quoridor</mainClass>
   1.125 -                </manifest>
   1.126 -            </archive>
   1.127 -        </configuration>
   1.128 -      </plugin>
   1.129 -    </plugins>
   1.130 -  </build>
   1.131 -</project>
   1.132 -
   1.133 -
   1.134 +<?xml version="1.0" encoding="UTF-8"?>
   1.135 +<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">
   1.136 +  <modelVersion>4.0.0</modelVersion>
   1.137 +  <parent>
   1.138 +        <artifactId>all-quoridor</artifactId>
   1.139 +        <groupId>org.apidesign</groupId>
   1.140 +        <version>1.0</version>
   1.141 +  </parent>
   1.142 +  <groupId>org.apidesign</groupId>
   1.143 +  <artifactId>webidor</artifactId>
   1.144 +  <packaging>jar</packaging>
   1.145 +  <version>1.8</version>
   1.146 +  <name>webidor server</name>
   1.147 +  <url>http://maven.apache.org</url>
   1.148 +  <repositories>
   1.149 +    <repository>
   1.150 +        <id>maven2-repository.dev.java.net</id>
   1.151 +        <name>Java.net Repository for Maven</name>
   1.152 +        <url>http://download.java.net/maven/2/</url>
   1.153 +        <layout>default</layout>
   1.154 +    </repository>
   1.155 +    <repository>
   1.156 +        <id>maven-repository.dev.java.net</id>
   1.157 +        <name>Java.net Maven 1 Repository (legacy)</name>
   1.158 +        <url>http://download.java.net/maven/1</url>
   1.159 +        <layout>legacy</layout>
   1.160 +    </repository>
   1.161 +  </repositories>
   1.162 +  <dependencies>
   1.163 +
   1.164 +    <dependency>
   1.165 +      <groupId>junit</groupId>
   1.166 +      <artifactId>junit</artifactId>
   1.167 +      <version>4.5</version>
   1.168 +      <scope>test</scope>
   1.169 +    </dependency>
   1.170 +    <dependency>
   1.171 +      <groupId>com.sun.jersey</groupId>
   1.172 +      <artifactId>jersey-core</artifactId>
   1.173 +      <version>1.1.0-ea</version>
   1.174 +    </dependency>
   1.175 +    <dependency>
   1.176 +      <groupId>com.sun.jersey</groupId>
   1.177 +      <artifactId>jersey-server</artifactId>
   1.178 +      <version>1.1.0-ea</version>
   1.179 +    </dependency>
   1.180 +    <dependency>
   1.181 +      <groupId>com.sun.jersey</groupId>
   1.182 +      <artifactId>jersey-json</artifactId>
   1.183 +      <version>1.1.0-ea</version>
   1.184 +      <type>jar</type>
   1.185 +      <exclusions>
   1.186 +        <exclusion>
   1.187 +          <artifactId>jaxb-api</artifactId>
   1.188 +          <groupId>javax.xml.bind</groupId>
   1.189 +        </exclusion>
   1.190 +        <exclusion>
   1.191 +          <artifactId>stax-api</artifactId>
   1.192 +          <groupId>stax</groupId>
   1.193 +        </exclusion>
   1.194 +      </exclusions>
   1.195 +    </dependency>
   1.196 +    <dependency>
   1.197 +      <groupId>com.sun.jersey.test.framework</groupId>
   1.198 +      <artifactId>jersey-test-framework</artifactId>
   1.199 +      <version>1.1.0-ea</version>
   1.200 +      <scope>test</scope>
   1.201 +    </dependency>
   1.202 +    <dependency>
   1.203 +      <groupId>org.apidesign</groupId>
   1.204 +      <artifactId>quoridor</artifactId>
   1.205 +      <version>1.0</version>
   1.206 +      <type>jar</type>
   1.207 +    </dependency>
   1.208 +  </dependencies>
   1.209 +  <build>
   1.210 +    <plugins>
   1.211 +      <plugin>
   1.212 +        <groupId>org.apache.maven.plugins</groupId>
   1.213 +        <artifactId>maven-compiler-plugin</artifactId>
   1.214 +        <version>2.0.2</version>
   1.215 +        <configuration>
   1.216 +          <source>1.5</source>
   1.217 +          <target>1.5</target>
   1.218 +        </configuration>
   1.219 +      </plugin>
   1.220 +      <plugin>
   1.221 +        <groupId>org.codehaus.mojo</groupId>
   1.222 +        <artifactId>exec-maven-plugin</artifactId>
   1.223 +        <configuration>
   1.224 +            <mainClass>cz.xelfi.quoridor.webidor.resources.Quoridor</mainClass>
   1.225 +        </configuration>
   1.226 +      </plugin>
   1.227 +      <plugin>
   1.228 +        <artifactId>maven-assembly-plugin</artifactId>
   1.229 +        <version>2.2-beta-2</version>
   1.230 +        <executions>
   1.231 +          <execution>
   1.232 +            <id>create-executable-jar</id>
   1.233 +            <phase>package</phase>
   1.234 +            <goals>
   1.235 +              <goal>single</goal>
   1.236 +            </goals>
   1.237 +            <configuration>
   1.238 +              <descriptors>
   1.239 +                <descriptor>all-zip.xml</descriptor>
   1.240 +              </descriptors>
   1.241 +              <finalName>webidor-${version}</finalName>
   1.242 +            </configuration>
   1.243 +          </execution>
   1.244 +        </executions>
   1.245 +      </plugin>
   1.246 +      <plugin>
   1.247 +        <groupId>org.apache.maven.plugins</groupId>
   1.248 +        <artifactId>maven-jar-plugin</artifactId>
   1.249 +        <configuration>
   1.250 +            <archive>
   1.251 +                <manifest>
   1.252 +                    <addClasspath>true</addClasspath>
   1.253 +                    <classpathPrefix>lib/</classpathPrefix>
   1.254 +                    <mainClass>cz.xelfi.quoridor.webidor.resources.Quoridor</mainClass>
   1.255 +                </manifest>
   1.256 +            </archive>
   1.257 +        </configuration>
   1.258 +      </plugin>
   1.259 +    </plugins>
   1.260 +  </build>
   1.261 +</project>
   1.262 +
   1.263 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/User.java	Sat Nov 07 15:23:14 2009 +0100
     2.3 @@ -0,0 +1,94 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * The contents of this file are subject to the terms of either the GNU
     2.8 + * General Public License Version 2 only ("GPL") or the Common
     2.9 + * Development and Distribution License("CDDL") (collectively, the
    2.10 + * "License"). You may not use this file except in compliance with the
    2.11 + * License. You can obtain a copy of the License at
    2.12 + * http://www.netbeans.org/cddl-gplv2.html
    2.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.14 + * specific language governing permissions and limitations under the
    2.15 + * License.  When distributing the software, include this License Header
    2.16 + * Notice in each file and include the License file at
    2.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    2.18 + * particular file as subject to the "Classpath" exception as provided
    2.19 + * by Sun in the GPL Version 2 section of the License file that
    2.20 + * accompanied this code. If applicable, add the following below the
    2.21 + * License Header, with the fields enclosed by brackets [] replaced by
    2.22 + * your own identifying information:
    2.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.24 + *
    2.25 + * Contributor(s):
    2.26 + *
    2.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    2.28 + */
    2.29 +
    2.30 +package cz.xelfi.quoridor.webidor;
    2.31 +
    2.32 +import java.util.ArrayList;
    2.33 +import java.util.List;
    2.34 +import javax.xml.bind.annotation.XmlAccessType;
    2.35 +import javax.xml.bind.annotation.XmlAccessorType;
    2.36 +import javax.xml.bind.annotation.XmlAttribute;
    2.37 +import javax.xml.bind.annotation.XmlElement;
    2.38 +import javax.xml.bind.annotation.XmlRootElement;
    2.39 +import javax.xml.bind.annotation.XmlValue;
    2.40 +
    2.41 +/**
    2.42 + *
    2.43 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.44 + */
    2.45 +@XmlRootElement
    2.46 +@XmlAccessorType(XmlAccessType.NONE)
    2.47 +public final class User extends Object {
    2.48 +    @XmlAttribute
    2.49 +    private String id;
    2.50 +    @XmlElement(name="property")
    2.51 +    private List<Property> properties;
    2.52 +
    2.53 +    User() {
    2.54 +    }
    2.55 +
    2.56 +    public User(String id) {
    2.57 +        this.id = id;
    2.58 +    }
    2.59 +
    2.60 +    public void addProperty(String name, String value) {
    2.61 +        if (properties == null) {
    2.62 +            properties = new ArrayList<Property>();
    2.63 +        }
    2.64 +        properties.add(new Property(name, value));
    2.65 +    }
    2.66 +
    2.67 +    public String getProperty(String name) {
    2.68 +        if (properties == null) {
    2.69 +            return null;
    2.70 +        }
    2.71 +        for (Property p : properties) {
    2.72 +            if (p.name.equals(name)) {
    2.73 +                return p.value;
    2.74 +            }
    2.75 +        }
    2.76 +        return null;
    2.77 +    }
    2.78 +
    2.79 +    public String getId() {
    2.80 +        return id;
    2.81 +    }
    2.82 +
    2.83 +    public static final class Property {
    2.84 +        @XmlAttribute
    2.85 +        private String name;
    2.86 +        @XmlValue
    2.87 +        private String value;
    2.88 +
    2.89 +        private Property() {
    2.90 +        }
    2.91 +
    2.92 +        Property(String name, String value) {
    2.93 +            this.name = name;
    2.94 +            this.value = value;
    2.95 +        }
    2.96 +    }
    2.97 +}
     3.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java	Thu Nov 05 13:34:36 2009 +0100
     3.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Quoridor.java	Sat Nov 07 15:23:14 2009 +0100
     3.3 @@ -55,6 +55,7 @@
     3.4  public final class Quoridor {
     3.5      private final File path;
     3.6      private Games games;
     3.7 +    private Users users;
     3.8      private final Map<UUID,String> loggedIn;
     3.9  
    3.10      public Quoridor() {
    3.11 @@ -75,6 +76,14 @@
    3.12          return games;
    3.13      }
    3.14  
    3.15 +    @Path("users")
    3.16 +    public Users getUsers() {
    3.17 +        if (users == null) {
    3.18 +            users = new Users(new File(path, "users"), this); // NOI18N
    3.19 +        }
    3.20 +        return users;
    3.21 +    }
    3.22 +
    3.23      @Path("login")
    3.24      @PUT
    3.25      @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Users.java	Sat Nov 07 15:23:14 2009 +0100
     4.3 @@ -0,0 +1,95 @@
     4.4 +/*
     4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6 + *
     4.7 + * The contents of this file are subject to the terms of either the GNU
     4.8 + * General Public License Version 2 only ("GPL") or the Common
     4.9 + * Development and Distribution License("CDDL") (collectively, the
    4.10 + * "License"). You may not use this file except in compliance with the
    4.11 + * License. You can obtain a copy of the License at
    4.12 + * http://www.netbeans.org/cddl-gplv2.html
    4.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    4.14 + * specific language governing permissions and limitations under the
    4.15 + * License.  When distributing the software, include this License Header
    4.16 + * Notice in each file and include the License file at
    4.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    4.18 + * particular file as subject to the "Classpath" exception as provided
    4.19 + * by Sun in the GPL Version 2 section of the License file that
    4.20 + * accompanied this code. If applicable, add the following below the
    4.21 + * License Header, with the fields enclosed by brackets [] replaced by
    4.22 + * your own identifying information:
    4.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    4.24 + *
    4.25 + * Contributor(s):
    4.26 + *
    4.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    4.28 + */
    4.29 +
    4.30 +package cz.xelfi.quoridor.webidor.resources;
    4.31 +
    4.32 +import cz.xelfi.quoridor.webidor.*;
    4.33 +import java.io.File;
    4.34 +import java.io.FileInputStream;
    4.35 +import java.io.FileNotFoundException;
    4.36 +import java.io.IOException;
    4.37 +import java.util.ArrayList;
    4.38 +import java.util.Collections;
    4.39 +import java.util.List;
    4.40 +import java.util.Properties;
    4.41 +import java.util.logging.Logger;
    4.42 +import javax.ws.rs.DefaultValue;
    4.43 +import javax.ws.rs.GET;
    4.44 +import javax.ws.rs.Path;
    4.45 +import javax.ws.rs.PathParam;
    4.46 +import javax.ws.rs.Produces;
    4.47 +import javax.ws.rs.QueryParam;
    4.48 +import javax.ws.rs.core.MediaType;
    4.49 +
    4.50 +/**
    4.51 + *
    4.52 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.53 + */
    4.54 +public final class Users {
    4.55 +    private final Quoridor quoridor;
    4.56 +    private final File dir;
    4.57 +    private static final Logger LOG = Logger.getLogger(Users.class.getName());
    4.58 +
    4.59 +    Users(File dir, Quoridor quoridor) {
    4.60 +        this.dir = dir;
    4.61 +        this.quoridor = quoridor;
    4.62 +        dir.mkdirs();
    4.63 +    }
    4.64 +
    4.65 +    @GET
    4.66 +    @Path("{id}")
    4.67 +    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    4.68 +    public User getUserInfo(
    4.69 +        @QueryParam("loginID") String loginId,
    4.70 +        @PathParam("id") String id
    4.71 +    ) throws IOException {
    4.72 +        String  myid = quoridor.isLoggedIn(loginId);
    4.73 +        Properties myp = getProp(myid);
    4.74 +        Properties p = getProp(id);
    4.75 +        User user = new User(id);
    4.76 +        for (String n : p.stringPropertyNames()) {
    4.77 +            if (n.startsWith("permission.")) {
    4.78 +                continue;
    4.79 +            }
    4.80 +            if (!id.equals(myid) && !"true".equals(myp.getProperty("permission." + n))) {
    4.81 +                continue;
    4.82 +            }
    4.83 +            user.addProperty(n, p.getProperty(n));
    4.84 +        }
    4.85 +        return user;
    4.86 +    }
    4.87 +
    4.88 +    private Properties getProp(String id) throws FileNotFoundException, IOException {
    4.89 +        Properties p = new Properties();
    4.90 +        if (id != null && id.length() > 0) {
    4.91 +            File f = new File(dir, id);
    4.92 +            FileInputStream is = new FileInputStream(f);
    4.93 +            p.load(is);
    4.94 +            is.close();
    4.95 +        }
    4.96 +        return p;
    4.97 +    }
    4.98 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/UsersTest.java	Sat Nov 07 15:23:14 2009 +0100
     5.3 @@ -0,0 +1,127 @@
     5.4 +/*
     5.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     5.6 + *
     5.7 + * The contents of this file are subject to the terms of either the GNU
     5.8 + * General Public License Version 2 only ("GPL") or the Common
     5.9 + * Development and Distribution License("CDDL") (collectively, the
    5.10 + * "License"). You may not use this file except in compliance with the
    5.11 + * License. You can obtain a copy of the License at
    5.12 + * http://www.netbeans.org/cddl-gplv2.html
    5.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    5.14 + * specific language governing permissions and limitations under the
    5.15 + * License.  When distributing the software, include this License Header
    5.16 + * Notice in each file and include the License file at
    5.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    5.18 + * particular file as subject to the "Classpath" exception as provided
    5.19 + * by Sun in the GPL Version 2 section of the License file that
    5.20 + * accompanied this code. If applicable, add the following below the
    5.21 + * License Header, with the fields enclosed by brackets [] replaced by
    5.22 + * your own identifying information:
    5.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    5.24 + *
    5.25 + * Contributor(s):
    5.26 + *
    5.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    5.28 + */
    5.29 +
    5.30 +package cz.xelfi.quoridor.webidor;
    5.31 +
    5.32 +import com.sun.jersey.api.client.GenericType;
    5.33 +import com.sun.jersey.test.framework.JerseyTest;
    5.34 +import java.io.File;
    5.35 +import java.io.FileOutputStream;
    5.36 +import java.io.IOException;
    5.37 +import java.util.List;
    5.38 +import java.util.Properties;
    5.39 +import javax.ws.rs.core.MediaType;
    5.40 +import org.junit.Test;
    5.41 +import static org.junit.Assert.*;
    5.42 +
    5.43 +/**
    5.44 + *
    5.45 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.46 + */
    5.47 +public class UsersTest extends JerseyTest {
    5.48 +    private File dir;
    5.49 +
    5.50 +    public UsersTest() throws Exception {
    5.51 +        super("cz.xelfi.quoridor.webidor.resources");
    5.52 +    }
    5.53 +
    5.54 +    @Override
    5.55 +    public void setUp() throws Exception {
    5.56 +        dir = File.createTempFile("quoridor", ".dir");
    5.57 +        dir.delete();
    5.58 +        System.setProperty("quoridor.dir", dir.getPath());
    5.59 +        dir.mkdirs();
    5.60 +        File passwd = new File(dir, "passwd");
    5.61 +        FileOutputStream os = new FileOutputStream(passwd);
    5.62 +        os.write("Jarda=heslo\nJirka=pesko\n".getBytes("UTF-8"));
    5.63 +        os.close();
    5.64 +        super.setUp();
    5.65 +    }
    5.66 +
    5.67 +    @Override
    5.68 +    public void tearDown() throws Exception {
    5.69 +        deleteRec(dir);
    5.70 +    }
    5.71 +
    5.72 +    static void deleteRec(File dir) throws IOException {
    5.73 +        if (dir == null) {
    5.74 +            return;
    5.75 +        }
    5.76 +        File[] arr = dir.listFiles();
    5.77 +        if (arr != null) {
    5.78 +            for (File f : arr) {
    5.79 +                deleteRec(f);
    5.80 +            }
    5.81 +        }
    5.82 +        dir.delete();
    5.83 +    }
    5.84 +
    5.85 +    @Test public void testListUsers() throws Exception {
    5.86 +        File usersDir = new File(dir, "users");
    5.87 +        usersDir.mkdirs();
    5.88 +        File fJarda = new File(usersDir, "Jarda");
    5.89 +        {
    5.90 +            Properties p = new Properties();
    5.91 +            p.setProperty("email", "jar@da.cz");
    5.92 +            p.setProperty("permission.email", "true");
    5.93 +            p.store(new FileOutputStream(fJarda), "");
    5.94 +        }
    5.95 +        File fJirka = new File(usersDir, "Jirka");
    5.96 +        {
    5.97 +            Properties p = new Properties();
    5.98 +            p.setProperty("email", "jir@ka.cz");
    5.99 +            p.store(new FileOutputStream(fJirka), "");
   5.100 +        }
   5.101 +
   5.102 +        String logJarda = webResource.path("login").
   5.103 +            queryParam("name", "Jarda").
   5.104 +            queryParam("password", "heslo").
   5.105 +            accept(MediaType.TEXT_PLAIN).
   5.106 +            put(String.class);
   5.107 +        String logJirka = webResource.path("login").
   5.108 +            queryParam("name", "Jirka").
   5.109 +            queryParam("password", "pesko").
   5.110 +            accept(MediaType.TEXT_PLAIN).
   5.111 +            put(String.class);
   5.112 +
   5.113 +        User uJirka = webResource.path("users/Jirka").accept(MediaType.TEXT_XML).get(User.class);
   5.114 +        assertEquals("Jirka", uJirka.getId());
   5.115 +        assertNull("Cannot get email without login", uJirka.getProperty("email"));
   5.116 +
   5.117 +        uJirka = webResource.path("users/Jirka").queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(User.class);
   5.118 +        assertEquals("Jirka", uJirka.getId());
   5.119 +        assertEquals("Email for ownself is OK", "jir@ka.cz", uJirka.getProperty("email"));
   5.120 +
   5.121 +        uJirka = webResource.path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(User.class);
   5.122 +        assertEquals("Jirka", uJirka.getId());
   5.123 +        assertEquals("Jarda has permission for property email", "jir@ka.cz", uJirka.getProperty("email"));
   5.124 +
   5.125 +        String txt = webResource.path("users/Jirka").queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(String.class);
   5.126 +        if (!txt.contains("<user id=\"Jirka\"><property name=\"email\">jir@ka.cz</property></user>")) {
   5.127 +            fail(txt);
   5.128 +        }
   5.129 +    }
   5.130 +}