package de.retest.recheck.ui.descriptors;

import static org.assertj.core.api.Assertions.assertThat;

import java.awt.Rectangle;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXB;

import org.junit.Before;
import org.junit.Test;

import de.retest.recheck.ui.descriptors.Attribute;
import de.retest.recheck.ui.descriptors.OutlineAttribute;

public class OutlineAttributeTest {

	OutlineAttribute attribute;
	Rectangle outline;

	@Before
	public void setUp() throws Exception {
		outline = new Rectangle( 1, 2, 3, 4 );
		attribute = OutlineAttribute.create( outline );
	}

	@Test
	public void getValue_should_return_equal_rectangle() throws Exception {
		assertThat( attribute.getValue() ).isEqualTo( outline );
	}

	@Test
	public void getValue_roundtrip_should_return_equal_rectangle() throws Exception {
		final StringWriter writer = new StringWriter();
		JAXB.marshal( attribute, writer );
		final StringReader xml = new StringReader( writer.getBuffer().toString() );
		final OutlineAttribute roundTrip = JAXB.unmarshal( xml, OutlineAttribute.class );

		assertThat( roundTrip.getValue() ).isEqualTo( outline );
	}

	@Test
	public void parse_roundtrip_should_return_equal_rectangle() throws Exception {
		final Rectangle normal = new Rectangle( 1, 10, 100, 0 );
		assertThat( OutlineAttribute.parse( normal.toString() ) ).isEqualTo( normal );

		final Rectangle negative = new Rectangle( -10, 10, -1, -100 );
		assertThat( OutlineAttribute.parse( negative.toString() ) ).isEqualTo( negative );
	}

	@Test
	public void parse_strange_values_should_work() throws Exception {
		// assertThat( OutlineAttribute.parse( "java.awt.Rectangle[x=-10,y=10,width=-1,height=-100]" ) )

		assertThat( OutlineAttribute.parse( null ) ).isNull();
		assertThat( OutlineAttribute.parse( "" ) ).isNull();
	}

	@Test
	public void getWeight_should_not_be_ignored() {
		assertThat( attribute.getWeight() ).isEqualTo( Attribute.NORMAL_WEIGHT );
	}

	@Test
	public void should_not_produce_greater_one_match() {
		final OutlineAttribute attributeOne = OutlineAttribute.create( new Rectangle( 27, 13, 97, 30 ) );
		final OutlineAttribute attributeTwo = OutlineAttribute.create( new Rectangle( -1000000, -999987, 1, 1 ) );
		attributeOne.match( attributeTwo );
	}

	@Test
	public void absoluteOutline_should_be_ignored() {
		final OutlineAttribute attribute = OutlineAttribute.createAbsolute( new Rectangle( 27, 13, 97, 30 ) );
		assertThat( attribute.getWeight() ).isEqualTo( Attribute.IGNORE_WEIGHT );
	}

	@Test
	public void relativeOutline_should_NOT_be_ignored() {
		final OutlineAttribute attribute = OutlineAttribute.create( new Rectangle( 27, 13, 97, 30 ) );
		assertThat( attribute.getWeight() ).isEqualTo( Attribute.NORMAL_WEIGHT );
	}
}

@@ -141,78 +141,66 @@
 .io.
-StringReader;%0Aimport java.io.StringWriter;%0A%0Aimport javax.xml.bind.JAXB
+ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream
 ;%0A%0Ai
@@ -276,90 +276,38 @@
 eck.
-ui.descriptors.Attribute;%0Aimport de.retest.recheck.ui.descriptors.OutlineAttribute
+persistence.xml.XmlTransformer
 ;%0A%0Ap
@@ -781,250 +781,305 @@
 nal 
-StringWriter writer = new StringWriter();%0A%09%09JAXB.marshal( attribute, writer );%0A%09%09final StringReader xml = new StringReader( writer.getBuffer().toString() );%0A%09%09final OutlineAttribute roundTrip = JAXB.unmarshal( xml, OutlineAttribute.class
+ByteArrayOutputStream baos = new ByteArrayOutputStream();%0A%09%09final XmlTransformer xmlTransformer = new XmlTransformer();%0A%09%09xmlTransformer.toXML( attribute, baos );%0A%09%09final OutlineAttribute roundTrip =%0A%09%09%09%09(OutlineAttribute) xmlTransformer.fromXML( new ByteArrayInputStream( baos.toByteArray() )
  );%0A
-%0A
 %09%09as

Fix test