Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 12.08 KB | None | 0 0
  1. /*
  2.  * RHQ Management Platform
  3.  * Copyright (C) 2005-2008 Red Hat, Inc.
  4.  * All rights reserved.
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License, version 2, as
  8.  * published by the Free Software Foundation, and/or the GNU Lesser
  9.  * General Public License, version 2.1, also as published by the Free
  10.  * Software Foundation.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15.  * GNU General Public License and the GNU Lesser General Public License
  16.  * for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * and the GNU Lesser General Public License along with this program;
  20.  * if not, write to the Free Software Foundation, Inc.,
  21.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22.  */
  23.  
  24. package org.rhq.enterprise.client;
  25.  
  26. import static org.testng.Assert.*;
  27.  
  28. import org.rhq.core.domain.measurement.AvailabilityType;
  29. import org.rhq.core.domain.measurement.ResourceAvailability;
  30. import org.rhq.core.domain.resource.Resource;
  31. import org.rhq.core.domain.resource.ResourceCategory;
  32. import org.rhq.core.domain.resource.ResourceType;
  33. import org.testng.annotations.BeforeMethod;
  34. import org.testng.annotations.Test;
  35.  
  36. import org.apache.commons.lang.StringUtils;
  37. import javax.persistence.Entity;
  38. import javax.persistence.Id;
  39. import javax.persistence.OneToOne;
  40. import javax.persistence.OneToMany;
  41. import java.io.PrintWriter;
  42. import java.io.StringWriter;
  43. import java.util.Arrays;
  44. import java.util.List;
  45. import java.util.ArrayList;
  46.  
  47. public class TabularWriterTest {
  48.  
  49.     StringWriter stringWriter;
  50.  
  51.     TabularWriter writer;
  52.  
  53.     @BeforeMethod
  54.     public void initFixture() {
  55.         stringWriter = new StringWriter();
  56.         writer = new TabularWriter(new PrintWriter(stringWriter));
  57.     }
  58.  
  59.     @Test
  60.     public void aByteShouldPrintUnchanged() {
  61.         byte value = 1;
  62.  
  63.         writer.print(value);
  64.  
  65.         String expected = "1\n";
  66.         String actual = stringWriter.toString();
  67.  
  68.         assertEquals(actual, expected, "A byte or Byte should have its value printed as a String");
  69.     }
  70.  
  71.     @Test
  72.     public void anIntegerShouldPrintUnchanged() {
  73.         int value = 1;
  74.  
  75.         writer.print(value);
  76.  
  77.         String expected = "1\n";
  78.         String actual = stringWriter.toString();
  79.  
  80.         assertEquals(actual, expected, "An int or Integer should have its value printed as a String");
  81.     }
  82.  
  83.     @Test
  84.     public void theSimpleClassNameShouldPrintFirstForEntity() {
  85.         User user = new User(1, "rhqadmin", "rhqadmin");
  86.  
  87.         writer.print(user);
  88.  
  89.         assertNumberOfLinesPrintedIs(4);
  90.         assertLineEquals(0, user.getClass().getSimpleName() + ":", "The simple class name should be the first line printed");
  91.     }
  92.  
  93.     @Test
  94.     public void theIdShouldBeTheFirstPropertyPrintedForEntity() {
  95.         User user = new User(1, "rhqadmin", "rhqadmin");
  96.  
  97.         writer.print(user);
  98.  
  99.         int paddingSize = "username".length();
  100.         int idLineNumber = 1;
  101.         String expected = "\t" + StringUtils.leftPad("id", paddingSize) + ": " + user.getId();
  102.  
  103.         assertLineEquals(idLineNumber, expected, "The id property should be the 2nd line printed");
  104.     }
  105.  
  106.     @Test
  107.     public void otherPropertiesShouldPrintAfterIdForEntity() {
  108.         User user = new User(1, "rhqadmin", "rhqadmin");
  109.  
  110.         writer.print(user);
  111.  
  112.         int passwordLineNumber = 2;
  113.         String expectedPasswordLine = "\tpassword: " + user.getPassword();
  114.  
  115.         assertLineEquals(passwordLineNumber, expectedPasswordLine, "The password property should be the 3rd line printed");
  116.  
  117.         int usernameLineNumber = 3;
  118.         String expectedUsernameLine = "\tusername: " + user.getUsername();
  119.  
  120.         assertLineEquals(usernameLineNumber, expectedUsernameLine, "The username property should be the 4th line printed");
  121.     }
  122.  
  123.     @Test
  124.     public void oneToOneAssociationShouldPrintForAnEntity() {
  125.         User mgr = new User(1, "rhqadmin", "rhqadmin");
  126.         Department department = new Department(1, mgr);
  127.  
  128.         writer.print(department);
  129.  
  130.         int lineNumber = 2;
  131.         String expectedLine = "\tmanager: " + mgr;
  132.  
  133.         assertLineEquals(lineNumber, expectedLine, "The manager property should be the 3rd line printed");
  134.     }
  135.  
  136.     @Test(enabled = false) // TODO revisit
  137.     public void oneToManyAssociationShouldPrintForEntity() {
  138.         User employee = new User(1, "rhq", "rhq");
  139.  
  140.         Company company = new Company(1);
  141.         company.addEmployee(employee);
  142.  
  143.         writer.print(company);
  144.  
  145.         int lineNumber = 2;
  146.         String expectedLine = "\temployees: " + company.getEmployees();
  147.  
  148.         assertLineEquals(lineNumber, expectedLine, "The employees property should be the 2nd line printed and the " +
  149.                 "toString() value of the collection should be displayed.");
  150.     }
  151.  
  152.     @Test
  153.     public void idShouldBeFirstResourcePropertyPrinted() {
  154.         Resource resource = createResource();
  155.  
  156.         writer.print(resource);
  157.  
  158.         assertLineEquals(
  159.             1,
  160.             "\t" + padResourceField("id") + ": " + resource.getId(),
  161.             "Expected Resource.id to be the first property printed."
  162.         );
  163.     }
  164.  
  165.     @Test
  166.     public void nameShouldBeSecondResourcePropertyPrinted() {
  167.         Resource resource = createResource();
  168.  
  169.         writer.print(resource);
  170.  
  171.         assertLineEquals(
  172.             2,
  173.             "\t" + padResourceField("name") + ": " + resource.getName(),
  174.             "Expected Resource.name to be second property printed"
  175.         );
  176.     }
  177.  
  178.     @Test
  179.     public void versionShouldBeThirdResourcePropertyPrinted() {
  180.         Resource resource = createResource();
  181.  
  182.         writer.print(resource);
  183.  
  184.         assertLineEquals(
  185.             3,
  186.             "\t" + padResourceField("version") + ": " + resource.getVersion(),
  187.             "Expected Resource.version to be third property printed"
  188.         );
  189.     }
  190.  
  191.     @Test
  192.     public void currentAvailabilityShouldBeFourthResourcePropertyPrinted() {
  193.         Resource resource = createResource();
  194.  
  195.         writer.print(resource);
  196.  
  197.         assertLineEquals(
  198.             4,
  199.             "\t" + padResourceField("currentAvailability") + ": " + resource.getCurrentAvailability().getAvailabilityType(),
  200.             "Expected short version of Resource.currentAvailability to be fourth property printed"
  201.         );
  202.     }
  203.  
  204.     @Test
  205.     public void handleNullCurrentAvailabilityForResource() {
  206.         Resource resource = createUncommittedResource();
  207.  
  208.         writer.print(resource);
  209.  
  210.         assertLineEquals(
  211.             4,
  212.             "\t" + padResourceField("currentAvailability") + ":",
  213.             "Expected to see empty string for Resource.currentAvailability when property is null"
  214.         );
  215.     }
  216.  
  217.     @Test
  218.     public void resourceTypeShouldBeLastResourcePropertyPrinted() {
  219.         Resource resource = createResource();
  220.  
  221.         writer.print(resource);
  222.  
  223.         assertLineEquals(
  224.             5,
  225.             "\t" + padResourceField("resourceType") + ": " + resource.getResourceType().getName(),
  226.             "Expected short version of Resource.resourceType to be the fifth property printed"
  227.         );
  228.     }
  229.  
  230.     private Resource createResource() {
  231.         return new ResourceBuilder().createResource()
  232.             .withId(111)
  233.             .withName("my-test-server")
  234.             .withUuid("12345")
  235.             .withVersion("1.0")
  236.             .withCurrentAvailability(AvailabilityType.UP)
  237.             .withResourceType(new ResourceTypeBuilder().createResourceType()
  238.                 .withName("test-server")
  239.                 .withId(222)
  240.                 .withPlugin("test-plugin")
  241.                 .withCategory(ResourceCategory.SERVER)
  242.                 .build())
  243.             .build();
  244.     }
  245.  
  246.     private Resource createUncommittedResource() {
  247.         return new ResourceBuilder().createResource()
  248.             .withId(111)
  249.             .withName("my-test-server")
  250.             .withUuid("12345")
  251.             .withVersion("1.0")
  252.             .withResourceType(new ResourceTypeBuilder().createResourceType()
  253.                 .withName("test-server")
  254.                 .withId(222)
  255.                 .withPlugin("test-plugin")
  256.                 .withCategory(ResourceCategory.SERVER)
  257.                 .build())
  258.             .build();
  259.     }
  260.  
  261.     @Test
  262.     public void collectionOfUncommittedResourceShouldPrint() {
  263.         ResourceType resourceType = new ResourceType("my-server", "my-plugin", ResourceCategory.SERVER, null);
  264.  
  265.         Resource resource1 = new Resource("resource-key-1", "my-server-1", resourceType);
  266.         resource1.setCurrentAvailability(new ResourceAvailability(resource1, null));
  267.  
  268.         Resource resource2 = new Resource("resource-key-2", "my-server-2", resourceType);
  269.         resource1.setCurrentAvailability(new ResourceAvailability(resource1, null));
  270.  
  271.         List<Resource> resources = Arrays.asList(resource1, resource2);
  272.  
  273.         writer.print(resources);
  274.     }
  275.  
  276.     void assertNumberOfLinesPrintedIs(int expectedNumberOfLines) {
  277.         String lines[] = getLines();
  278.         assertEquals(lines.length, expectedNumberOfLines, "The actual lines printed were\n[\n" +
  279.             stringWriter.toString() + "\n]");
  280.     }
  281.  
  282.     void assertLineEquals(int lineNumber, String expectedLine, String msg) {
  283.         String actualLine = getLines()[lineNumber];
  284.  
  285.         assertEquals(actualLine, expectedLine, msg + " -- The actual output was \n[\n" + stringWriter + "\n].");
  286.     }
  287.  
  288.     String[] getLines() {
  289.         return stringWriter.toString().split("\n");
  290.     }
  291.  
  292.     String padResourceField(String field) {
  293.         return StringUtils.leftPad(field, "currentAvailability".length());
  294.     }
  295.  
  296.     @Entity
  297.     static class User {
  298.         @Id
  299.         private int id;
  300.  
  301.         private String username;
  302.  
  303.         private String password;
  304.  
  305.         public User(int id, String username, String password) {
  306.             this.id = id;
  307.             this.username = username;
  308.             this.password = password;
  309.         }
  310.  
  311.         public int getId() {
  312.             return id;
  313.         }
  314.  
  315.         public void setId(int id) {
  316.             this.id = id;
  317.         }
  318.  
  319.         public String getUsername() {
  320.             return username;
  321.         }
  322.  
  323.         public void setUsername(String username) {
  324.             this.username = username;
  325.         }
  326.  
  327.         public String getPassword() {
  328.             return password;
  329.         }
  330.  
  331.         public void setPassword(String password) {
  332.             this.password = password;
  333.         }
  334.  
  335.         @Override
  336.         public String toString() {
  337.             return User.class.getSimpleName() + "[id=" + id + ", username=" + username + ", password=" + password + "]";
  338.         }
  339.     }
  340.  
  341.     @Entity
  342.     static class Department {
  343.         @Id
  344.         private int id;
  345.  
  346.         @OneToOne
  347.         private User manager;
  348.  
  349.         public Department(int id, User manager) {
  350.             this.id = id;
  351.             this.manager = manager;
  352.         }
  353.  
  354.         public int getId() {
  355.             return id;
  356.         }
  357.  
  358.         public void setId(int id) {
  359.             this.id = id;
  360.         }
  361.  
  362.         public User getManager() {
  363.             return manager;
  364.         }
  365.  
  366.         public void setManager(User manager) {
  367.             this.manager = manager;
  368.         }
  369.     }
  370.  
  371.     @Entity
  372.     static class Company {
  373.         @Id
  374.         private int id;
  375.  
  376.         @OneToMany
  377.         private List<User> employees = new ArrayList<User>();
  378.  
  379.         public Company(int id) {
  380.             this.id = id;
  381.         }
  382.  
  383.         public int getId() {
  384.             return id;
  385.         }
  386.  
  387.         public void setId(int id) {
  388.             this.id = id;
  389.         }
  390.  
  391.         public List<User> getEmployees() {
  392.             return employees;
  393.         }
  394.  
  395.         public void setEmployees(List<User> employees) {
  396.             this.employees = employees;
  397.         }
  398.  
  399.         public void addEmployee(User employee) {
  400.             employees.add(employee);
  401.         }
  402.     }
  403.  
  404. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement