Skip to content

Commit

Permalink
Fixed issue #113
Browse files Browse the repository at this point in the history
  • Loading branch information
oshoukry committed Mar 9, 2018
1 parent a31e659 commit 33f9f24
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2010-2018 Osman Shoukry
*
* 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.openpojo.random.awt;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import com.openpojo.random.RandomGenerator;
import com.openpojo.reflection.PojoClass;
import com.openpojo.reflection.PojoField;
import com.openpojo.reflection.construct.InstanceFactory;

import static com.openpojo.reflection.impl.PojoClassFactory.getPojoClass;
import static com.openpojo.reflection.java.load.ClassUtil.isClassLoaded;
import static com.openpojo.reflection.java.load.ClassUtil.loadClass;

/**
* @author oshoukry
*/
public class BufferedImageRandomGenerator implements RandomGenerator {
private static final String TYPE = "java.awt.image.BufferedImage";
private static final Random RANDOM = new Random(System.currentTimeMillis());
private static final BufferedImageRandomGenerator INSTANCE = new BufferedImageRandomGenerator();

public static RandomGenerator getInstance() {
return INSTANCE;
}

public Collection<Class<?>> getTypes() {
List<Class<?>> types = new ArrayList<Class<?>>();
if (isClassLoaded(TYPE)) {
types.add(loadClass(TYPE));
}
return types;
}

public Object doGenerate(Class<?> type) {
int width = RANDOM.nextInt(10) + 1;
int height = RANDOM.nextInt(10) + 1;
int imageType = getRandomImageType();
return InstanceFactory.getInstance(getPojoClass(loadClass(TYPE)), width, height, imageType);
}

private int getRandomImageType() {
//public static final int TYPE
List<Integer> availableTypes = new ArrayList<Integer>();

PojoClass bufferedImagePojoClass = getPojoClass(loadClass(TYPE));

for (PojoField field : bufferedImagePojoClass.getPojoFields()) {
if(isPublicStaticFinalIntNamedTypeAndNotTypeCustom(field))
availableTypes.add((Integer) field.get(null));
}
return availableTypes.get(RANDOM.nextInt(availableTypes.size()));
}

private boolean isPublicStaticFinalIntNamedTypeAndNotTypeCustom(PojoField field) {
return field.isPublic()
&& field.isStatic()
&& field.isFinal()
&& field.getType().equals(int.class)
&& field.getName().startsWith("TYPE_")
&& !field.getName().equals("TYPE_CUSTOM");
}

private BufferedImageRandomGenerator() {}
}
4 changes: 4 additions & 0 deletions src/main/java/com/openpojo/registry/ServiceRegistrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.openpojo.registry;

import com.openpojo.business.BusinessIdentity;
import com.openpojo.random.awt.BufferedImageRandomGenerator;
import com.openpojo.random.collection.AbstractCollectionRandomGenerator;
import com.openpojo.random.collection.CollectionRandomGenerator;
import com.openpojo.random.collection.list.*;
Expand Down Expand Up @@ -79,6 +80,9 @@ public void initializeRandomGeneratorService() {
newRandomGeneratorService.registerRandomGenerator(URLRandomGenerator.getInstance());
newRandomGeneratorService.registerRandomGenerator(URIRandomGenerator.getInstance());

// AWT
newRandomGeneratorService.registerRandomGenerator(BufferedImageRandomGenerator.getInstance());

// Time
newRandomGeneratorService.registerRandomGenerator(ZonedDateTimeRandomGenerator.getInstance());
newRandomGeneratorService.registerRandomGenerator(ZoneIdRandomGenerator.getInstance());
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/com/openpojo/issues/issue113/IssueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2010-2018 Osman Shoukry
*
* 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.openpojo.issues.issue113;

import com.openpojo.issues.issue113.sample.AClassWithBufferedImage;
import com.openpojo.validation.Validator;
import com.openpojo.validation.ValidatorBuilder;
import com.openpojo.validation.rule.impl.GetterMustExistRule;
import com.openpojo.validation.rule.impl.SetterMustExistRule;
import com.openpojo.validation.test.impl.GetterTester;
import com.openpojo.validation.test.impl.SetterTester;
import org.junit.Test;

import static com.openpojo.reflection.impl.PojoClassFactory.getPojoClass;

/**
* @author oshoukry
*/
public class IssueTest {

@Test
public void testGetterAndSetter() {
Validator validator = ValidatorBuilder
.create()
.with(new GetterMustExistRule())
.with(new GetterTester())
.with(new SetterMustExistRule())
.with(new SetterTester())
.build();
validator.validate(getPojoClass(AClassWithBufferedImage.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2010-2018 Osman Shoukry
*
* 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.openpojo.issues.issue113.sample;

import java.awt.image.BufferedImage;

/**
* @author oshoukry
*/
public class AClassWithBufferedImage {
public BufferedImage getBufferedImage() {
return bufferedImage;
}

public void setBufferedImage(BufferedImage bufferedImage) {
this.bufferedImage = bufferedImage;
}

private BufferedImage bufferedImage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2010-2018 Osman Shoukry
*
* 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.openpojo.random.awt;

import com.openpojo.random.RandomGenerator;
import com.openpojo.random.generator.AbstractGeneratorTest;
import com.openpojo.reflection.PojoClass;
import com.openpojo.reflection.impl.PojoClassFactory;

/**
* @author oshoukry
*/
public class BufferedImageRandomGeneratorTest extends AbstractGeneratorTest {

protected PojoClass getPojoClass() {
return PojoClassFactory.getPojoClass(BufferedImageRandomGenerator.class);
}

protected String getTypeName() {
return "java.awt.image.BufferedImage";
}

protected RandomGenerator getRandomGenerator() {
return BufferedImageRandomGenerator.getInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public class ServiceRegistrarTest {
private String javaVersion = System.getProperty("java.version");
private final String[] expectedDefaultTypeNames = new String[] {
// @formatter:off
"java.lang.Boolean"
"java.awt.image.BufferedImage"
,"java.lang.Boolean"
,"java.lang.Byte"
,"java.lang.Character"
,"java.lang.Class"
Expand Down

0 comments on commit 33f9f24

Please sign in to comment.