JSONEmitter { public void serialize(EObject resolvable, StringBuilder result, int depth) { writeObject(resolvable, result, depth); } JSONEmitter(PermissionCallback permissionCallback); void serialize(EObject resolvable, StringBuilder result, int depth);  }
@Test public void testSerializePropertyFile() { PropertyFile file = PropertiesFactory.eINSTANCE.createPropertyFile(); Property property = PropertiesFactory.eINSTANCE.createProperty(); property.setComment("comment1"); property.setKey("key1"); property.setValue("value1"); file.getProperties().add(property); Property property2 = PropertiesFactory.eINSTANCE.createProperty(); property2.setComment("comment2"); property2.setKey("key2"); property2.setValue("value2"); file.getProperties().add(property2); StringBuilder result = new StringBuilder(); getFixture().serialize(file, result, 2); String expected = "{\"properties\":[{\"key\":\"key1\",\"value\":\"value1\",\"comment\":\"comment1\"},{\"key\":\"key2\",\"value\":\"value2\",\"comment\":\"comment2\"}]}"; assertEquals(expected, result.toString()); }
@Test public void testSerializePropertyFileDescriptorWithDepth1AndURI() { PropertyFileDescriptor spyDescriptor = mock(PropertyFileDescriptor.class); when(spyDescriptor.eGet(PropertiesPackage.Literals.PROPERTY_FILE_DESCRIPTOR__LOCATION)).thenReturn(URI.createURI("test/uri")); when(spyDescriptor.eIsSet(PropertiesPackage.Literals.PROPERTY_FILE_DESCRIPTOR__LOCATION)).thenReturn(true); when(spyDescriptor.eClass()).thenReturn(PropertiesPackage.Literals.PROPERTY_FILE_DESCRIPTOR); PropertyFile file = PropertiesFactory.eINSTANCE.createPropertyFile(); when(spyDescriptor.loadProperties()).thenReturn(file); Property property = PropertiesFactory.eINSTANCE.createProperty(); property.setComment("comment1"); property.setKey("key1"); property.setValue("value1"); file.getProperties().add(property); Property property2 = PropertiesFactory.eINSTANCE.createProperty(); property2.setComment("comment2"); property2.setKey("key2"); property2.setValue("value2"); file.getProperties().add(property2); StringBuilder result = new StringBuilder(); getFixture().serialize(spyDescriptor, result, 1); String expected = "{\"location\":\"test/uri\"}"; assertEquals(expected, result.toString()); }
@Test public void testSerializePropertyFileDescriptorWithDepth1() { PropertyFileDescriptor spyDescriptor = mock(PropertyFileDescriptor.class); when(spyDescriptor.eClass()).thenReturn(PropertiesPackage.Literals.PROPERTY_FILE_DESCRIPTOR); PropertyFile file = PropertiesFactory.eINSTANCE.createPropertyFile(); when(spyDescriptor.loadProperties()).thenReturn(file); Property property = PropertiesFactory.eINSTANCE.createProperty(); property.setComment("comment1"); property.setKey("key1"); property.setValue("value1"); file.getProperties().add(property); Property property2 = PropertiesFactory.eINSTANCE.createProperty(); property2.setComment("comment2"); property2.setKey("key2"); property2.setValue("value2"); file.getProperties().add(property2); StringBuilder result = new StringBuilder(); getFixture().serialize(spyDescriptor, result, 1); String expected = "{}"; assertEquals(expected, result.toString()); }
@Test public void testSerializeWithEmptyProperties(){ String expected = "{}"; PropertyFileDescriptor spyDescriptor = mock(PropertyFileDescriptor.class); when(spyDescriptor.eClass()).thenReturn(PropertiesPackage.Literals.PROPERTY_FILE_DESCRIPTOR); when(spyDescriptor.loadProperties()).thenReturn(PropertiesFactory.eINSTANCE.createPropertyFile()); StringBuilder result = new StringBuilder(); getFixture().serialize(spyDescriptor, result, 2); assertEquals(expected, result.toString()); }
@Test public void testSerializeWithFilledLocationAndEmptyProperties(){ String expected = "{\"location\":\"test/uri\"}"; PropertyFileDescriptor spyDescriptor = mock(PropertyFileDescriptor.class); when(spyDescriptor.eGet(PropertiesPackage.Literals.PROPERTY_FILE_DESCRIPTOR__LOCATION)).thenReturn(URI.createURI("test/uri")); when(spyDescriptor.eIsSet(PropertiesPackage.Literals.PROPERTY_FILE_DESCRIPTOR__LOCATION)).thenReturn(true); when(spyDescriptor.eClass()).thenReturn(PropertiesPackage.Literals.PROPERTY_FILE_DESCRIPTOR); when(spyDescriptor.loadProperties()).thenReturn(PropertiesFactory.eINSTANCE.createPropertyFile()); StringBuilder result = new StringBuilder(); getFixture().serialize(spyDescriptor, result, 2); assertEquals(expected, result.toString()); }
@Test public void testSerializeRestrictedProject(){ Workspace workspace = PropertiesFactory.eINSTANCE.createWorkspace(); Project project1 = PropertiesFactory.eINSTANCE.createProject(); Project project2 = PropertiesFactory.eINSTANCE.createProject(); Project project3 = PropertiesFactory.eINSTANCE.createProject(); project1.setName("project1"); project2.setName("project2"); project3.setName("project3"); workspace.getChildren().add(project1); workspace.getChildren().add(project2); workspace.getChildren().add(project3); String expected = "{\"children\":[{\"name\":\"project2\"},{\"name\":\"project3\"}],\"name\":\"workspace\"}"; when(callback.isAuthorized(project2)).thenReturn(true); when(callback.isAuthorized(project3)).thenReturn(true); when(callback.isAuthorized(project1)).thenReturn(false); StringBuilder result = new StringBuilder(); getFixture().serialize(workspace, result, 2); assertEquals(expected, result.toString()); }