Skip to content

Commit

Permalink
Tests for ability to ignore null values
Browse files Browse the repository at this point in the history
  • Loading branch information
spgingras committed Jan 30, 2015
1 parent 7db7120 commit 0f22efb
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 52 deletions.
11 changes: 8 additions & 3 deletions README.md
@@ -1,4 +1,4 @@
##GwtChosen
##GwtChosen
[Chosen](https://github.com/harvesthq/chosen) is a javascript plugin (for jQuery and Prototype) _that makes long, unwieldy select boxes much more user-friendly._ GwtChosen is a port of the jquery version of Chosen for GWT Web Toolkit. It is not a wrapper but a complete rewrite using the GWT standards. It is available as a GwtQuery plugin or as a widget.

##Documentation
Expand Down Expand Up @@ -52,10 +52,15 @@ Find the the available jars in [Maven Central](http://search.maven.org/#search%7
* General IDEA and Eclipse project import instructions can be found [here](http://c.gwt-examples.com/home/maven/ide-import).
* If using Eclipse double check the GwtChosen GPE plugin and see if GWT is enabled.

##Debugging integration tests locally
1. `cd integration-test`
2. `mvn gwt:run -Pintegration-test`
3. Open your browser to `http://127.0.0.1:8080/#{test case token}`

#FAQ

##Credits
* The initial chosen javascript plugin was built by [Harvest](http://www.getharvest.com/).
* Concept and development by [Patrick Filler](http://patrickfiller.com/).
* The initial chosen javascript plugin was built by [Harvest](http://www.getharvest.com/).
* Concept and development by [Patrick Filler](http://patrickfiller.com/).
* Design and CSS by [Matthew Lettini](http://matthewlettini.com/)
* The GWT port of Chosen was built by [Julien Dramaix](https://plus.google.com/u/0/103916508880440628637)
4 changes: 4 additions & 0 deletions integration-test/pom.xml
Expand Up @@ -55,6 +55,10 @@
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>${gwt.version}</version>
<configuration>
<runTarget>index.html</runTarget>
<port>8080</port>
</configuration>
<executions>
<execution>
<configuration>
Expand Down
Expand Up @@ -16,28 +16,45 @@

package com.arcbees.chosen.integrationtest.client;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

import com.arcbees.chosen.client.gwt.ChosenValueListBox;
import com.google.common.base.CaseFormat;
import com.arcbees.chosen.integrationtest.client.testcases.ChooseOption;
import com.arcbees.chosen.integrationtest.client.testcases.HideEmptyValues;
import com.arcbees.chosen.integrationtest.client.testcases.ShowNonEmptyValues;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.text.shared.AbstractRenderer;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.RootPanel;

public class ChosenSampleIntegrationTests implements EntryPoint {
public class ChosenSampleIntegrationTests implements EntryPoint, ValueChangeHandler<String> {
private final Map<String, TestCase> testCaseMap;

public ChosenSampleIntegrationTests() {
testCaseMap = new HashMap<String, TestCase>();
registerTestCase(new ChooseOption());
registerTestCase(new HideEmptyValues());
registerTestCase(new ShowNonEmptyValues());
}

private void registerTestCase(TestCase testCase) {
assert !testCaseMap.containsKey(testCase.getToken());

testCaseMap.put(testCase.getToken(), testCase);
}

@Override
public void onModuleLoad() {
RootPanel rootPanel = RootPanel.get();
ChosenValueListBox<CarBrand> listBox = new ChosenValueListBox<CarBrand>(new AbstractRenderer<CarBrand>() {
@Override
public String render(CarBrand object) {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, object.name());
}
});

listBox.setAcceptableValues(EnumSet.allOf(CarBrand.class));
listBox.setValue(CarBrand.AUDI);

rootPanel.add(listBox);
History.addValueChangeHandler(this);
History.fireCurrentHistoryState();
}

@Override
public void onValueChange(ValueChangeEvent<String> event) {
RootPanel.get().clear();
String token = event.getValue();
TestCase testCase = testCaseMap.get(token);
testCase.run();
}
}
Expand Up @@ -16,22 +16,8 @@

package com.arcbees.chosen.integrationtest.client;

public enum CarBrand {
TOYOTA,
HONDA,
MERCEDES,
FORD,
HYUNDAI,
FERRARI,
BMW,
TESLA,
AUDI,
BENTLEY,
CADILLAC,
CHEVROLET,
CHRYSLER,
DODGE,
MITSUBISHI,
JAGUAR,
JEEP
public abstract class TestCase implements Runnable {
public String getToken() {
return getClass().getSimpleName();
}
}
@@ -0,0 +1,51 @@
/**
* Copyright 2014 ArcBees Inc.
*
* 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.arcbees.chosen.integrationtest.client.domain;

import java.util.HashSet;
import java.util.Set;

import com.google.gwt.text.shared.Renderer;

public enum CarBrand {
TOYOTA,
HONDA,
MERCEDES,
FORD,
HYUNDAI,
FERRARI,
BMW,
TESLA,
AUDI,
BENTLEY,
CADILLAC,
CHEVROLET,
CHRYSLER,
DODGE,
MITSUBISHI,
JAGUAR,
JEEP;

public static Set<String> getAllNames(Renderer<CarBrand> renderer) {
Set<String> brandNames = new HashSet<String>();
for (CarBrand carBrand : CarBrand.values()) {
brandNames.add(renderer.render(carBrand));
}

return brandNames;
}
}
@@ -0,0 +1,49 @@
/**
* Copyright 2014 ArcBees Inc.
*
* 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.arcbees.chosen.integrationtest.client.testcases;

import java.util.EnumSet;

import com.arcbees.chosen.client.gwt.ChosenValueListBox;
import com.arcbees.chosen.integrationtest.client.TestCase;
import com.arcbees.chosen.integrationtest.client.domain.CarBrand;
import com.google.gwt.text.shared.AbstractRenderer;
import com.google.gwt.user.client.ui.RootPanel;

public class ChooseOption extends TestCase {
public static final AbstractRenderer<CarBrand> RENDERER = new AbstractRenderer<CarBrand>() {
@Override
public String render(CarBrand object) {
if (object == null) {
return "";
}
return object.name();
}
};

@Override
public void run() {
RootPanel rootPanel = RootPanel.get();

ChosenValueListBox<CarBrand> listBox = new ChosenValueListBox<CarBrand>(RENDERER);

listBox.setAcceptableValues(EnumSet.allOf(CarBrand.class));
listBox.setValue(CarBrand.AUDI);

rootPanel.add(listBox);
}
}
@@ -0,0 +1,57 @@
/**
* Copyright 2014 ArcBees Inc.
*
* 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.arcbees.chosen.integrationtest.client.testcases;

import java.util.List;

import com.arcbees.chosen.client.gwt.ChosenValueListBox;
import com.arcbees.chosen.integrationtest.client.TestCase;
import com.arcbees.chosen.integrationtest.client.domain.CarBrand;
import com.google.common.collect.Lists;
import com.google.gwt.text.shared.AbstractRenderer;
import com.google.gwt.text.shared.Renderer;
import com.google.gwt.user.client.ui.RootPanel;

/**
* This test makes sure that when null values are rendered as empty string (""),
* then the empty string will not be displayed in the dropdown options.
*/

public class HideEmptyValues extends TestCase {
public static final Renderer<CarBrand> RENDERER = new AbstractRenderer<CarBrand>() {
@Override
public String render(CarBrand object) {
if (object == null) {
return "";
}
return object.name();
}
};

@Override
public void run() {
ChosenValueListBox<CarBrand> listBox = new ChosenValueListBox<CarBrand>(RENDERER);

List<CarBrand> carBrands = Lists.newArrayList(CarBrand.values());
carBrands.add(0, null);
listBox.setAcceptableValues(carBrands);

listBox.setValue(null);

RootPanel.get().add(listBox);
}
}
@@ -0,0 +1,57 @@
/**
* Copyright 2014 ArcBees Inc.
*
* 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.arcbees.chosen.integrationtest.client.testcases;

import java.util.List;

import com.arcbees.chosen.client.gwt.ChosenValueListBox;
import com.arcbees.chosen.integrationtest.client.TestCase;
import com.arcbees.chosen.integrationtest.client.domain.CarBrand;
import com.google.common.collect.Lists;
import com.google.gwt.text.shared.AbstractRenderer;
import com.google.gwt.text.shared.Renderer;
import com.google.gwt.user.client.ui.RootPanel;

/**
* This test makes sure that when null values are rendered as a non-empty string,
* then that exact non-empty string will be displayed in the dropdown options.
*/

public class ShowNonEmptyValues extends TestCase {
public static final Renderer<CarBrand> RENDERER = new AbstractRenderer<CarBrand>() {
@Override
public String render(CarBrand object) {
if (object == null) {
return "Placeholder for null";
}
return object.name();
}
};

@Override
public void run() {
ChosenValueListBox<CarBrand> listBox = new ChosenValueListBox<CarBrand>(RENDERER);

List<CarBrand> carBrands = Lists.newArrayList(CarBrand.values());
carBrands.add(0, null);
listBox.setAcceptableValues(carBrands);

listBox.setValue(null);

RootPanel.get().add(listBox);
}
}

0 comments on commit 0f22efb

Please sign in to comment.